forked from radondb/radondb-mysql-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
264 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
# ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no sample-mysql-0.sample-mysql.default | ||
kubectl create secret generic sample-ssh-key --from-file=id_ecdsa=/root/.ssh/id_ecdsa --from-file=authorized_keys=/root/.ssh/id_ecdsa.pub | ||
|
||
/usr/sbin/sshd -D -e -f /etc/ssh/sshd_config | ||
mkdir -p /root/.ssh | ||
chmod 700 /root/.ssh | ||
cp /etc/secret-ssh/* /root/.ssh | ||
chmod 600 /root/.ssh/authorized_keys | ||
|
||
|
||
cat <<EOF >>sshd_config | ||
PermitRootLogin yes | ||
PasswordAuthentication no | ||
PermitEmptyPasswords no | ||
ChallengeResponseAuthentication no | ||
GSSAPIAuthentication no | ||
UseDNS no | ||
UsePAM yes | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2021 RadonDB. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package syncer | ||
|
||
import ( | ||
"github.com/presslabs/controller-util/pkg/syncer" | ||
"github.com/radondb/radondb-mysql-kubernetes/mysqlcluster" | ||
"github.com/radondb/radondb-mysql-kubernetes/utils" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// NewSecretSyncer returns secret syncer. | ||
func NewSShKeySyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.Interface { | ||
secret := &corev1.Secret{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Secret", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: c.GetNameForResource(utils.SShKey), | ||
Namespace: c.Namespace, | ||
}, | ||
} | ||
|
||
return syncer.NewObjectSyncer("Secret", c.Unwrap(), secret, cli, func() error { | ||
|
||
if secret.Data == nil { | ||
secret.Data = make(map[string][]byte) | ||
} | ||
|
||
if len(secret.Data["id_ecdsa"]) == 0 { | ||
pub, priv, err := GenSSHKey() | ||
if err != nil { | ||
return err | ||
} | ||
secret.Data["id_ecdsa"] = priv | ||
secret.Data["authorized_keys"] = pub | ||
|
||
} | ||
|
||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
Copyright 2021 RadonDB. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package syncer | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"log" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// ssh -o UserKnownHostsFile=/dev/null | ||
func GenSSHKey() (pubkey, privekey []byte, err error) { | ||
|
||
bitSize := 4096 | ||
|
||
privateKey, err := generatePrivateKey(bitSize) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
privateKeyBytes := encodePrivateKeyToPEM(privateKey) | ||
|
||
return publicKeyBytes, privateKeyBytes, err | ||
|
||
} | ||
|
||
// generatePrivateKey creates a RSA Private Key of specified byte size | ||
func generatePrivateKey(bitSize int) (*ecdsa.PrivateKey, error) { | ||
// Private Key generation | ||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Validate Private Key | ||
// err = privateKey.Validate() | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
log.Println("Private Key generated") | ||
return privateKey, nil | ||
} | ||
|
||
// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format | ||
func encodePrivateKeyToPEM(privateKey *ecdsa.PrivateKey) []byte { | ||
// Get ASN.1 DER format | ||
privDER, err := x509.MarshalECPrivateKey(privateKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// pem.Block | ||
privBlock := pem.Block{ | ||
Type: "EC PRIVATE KEY", | ||
Headers: nil, | ||
Bytes: privDER, | ||
} | ||
|
||
// Private key in PEM format | ||
privatePEM := pem.EncodeToMemory(&privBlock) | ||
|
||
return privatePEM | ||
} | ||
|
||
// generatePublicKey take a rsa.PublicKey and return bytes suitable for writing to .pub file | ||
// returns in the format "ssh-rsa ..." | ||
func generatePublicKey(privatekey *ecdsa.PublicKey) ([]byte, error) { | ||
publicKey, err := ssh.NewPublicKey(privatekey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pubKeyBytes := ssh.MarshalAuthorizedKey(publicKey) | ||
|
||
log.Println("Public key generated") | ||
return pubKeyBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.