forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_secret.go
47 lines (39 loc) · 1.13 KB
/
ssh_secret.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
package util
import (
cryptoRand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"golang.org/x/crypto/ssh"
)
func GenerateRsaKey() (map[string]string, error) {
bitSize := 1024
privateKey, err := rsa.GenerateKey(cryptoRand.Reader, bitSize)
if err != nil {
//klog.Errorf("rsa generateKey err: %v", err)
return nil, err
}
// id_rsa
privBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
privateKeyBytes := pem.EncodeToMemory(&privBlock)
// id_rsa.pub
publicRsaKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
//klog.Errorf("ssh newPublicKey err: %v", err)
return nil, err
}
publicKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
data := make(map[string]string)
data["privateKey"] = base64.StdEncoding.EncodeToString(privateKeyBytes)
data["publicKey"] = base64.StdEncoding.EncodeToString(publicKeyBytes)
data["config"] = generateSSHConfig()
return data, nil
}
func generateSSHConfig() string {
config := "StrictHostKeyChecking no\nUserKnownHostsFile /dev/null\n"
return base64.StdEncoding.EncodeToString([]byte(config))
}