-
Notifications
You must be signed in to change notification settings - Fork 39
/
ssh_key_pair.go
67 lines (55 loc) · 1.88 KB
/
ssh_key_pair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package utils
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"strings"
"golang.org/x/crypto/ssh"
"github.com/berops/claudie/proto/pb"
)
// sshKeyPair is a struct containing private and public SSH keys as a string.
// These SSH key-pairs are required to SSH into the VMs in the cluster and execute commands.
type sshKeyPair struct {
public string
private string
}
// createSSHKeyPair will create a RSA key-pair and save it into the clusterInfo provided
// if clusterInfo doesn't already have an RSA key-pair related to it.
// returns error if the key-pair creation fails.
func createSSHKeyPair(desiredInfo *pb.ClusterInfo) error {
// If the cluster doesn't have an SSH keypair associated with it, then generate a new SSH keypair and associate
// with the cluster
if desiredInfo.PublicKey == "" {
keys, err := generateSSHKeyPair()
if err != nil {
return fmt.Errorf("error while creating SSH key pair for %s : %w", desiredInfo.Name, err)
}
desiredInfo.PrivateKey = keys.private
desiredInfo.PublicKey = keys.public
}
return nil
}
// generateSSHKeyPair function generates SSH key pair
// returns the keypair if successful, nil otherwise
func generateSSHKeyPair() (sshKeyPair, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return sshKeyPair{}, err
}
// Generate and write private key as PEM
var privKeyBuf strings.Builder
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
return sshKeyPair{}, err
}
// Generate and write public key
pubKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return sshKeyPair{}, err
}
var pubKeyBuf strings.Builder
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pubKey))
return sshKeyPair{public: pubKeyBuf.String(), private: privKeyBuf.String()}, nil
}