-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh_config.go
66 lines (57 loc) · 1.63 KB
/
ssh_config.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
package sshkeymanager
import (
"io/ioutil"
"log"
"os"
"golang.org/x/crypto/ssh"
kh "golang.org/x/crypto/ssh/knownhosts"
)
func DefaultConfig() *ssh.ClientConfig {
var keys []string
if os.Getenv("SSH_KEY") != "" {
keys = []string{os.Getenv("SSH_KEY")}
} else {
keys = []string{os.Getenv("HOME") + "/.ssh/id_rsa", os.Getenv("HOME") + "/.ssh/id_dsa"}
}
return MakeConfig(keys)
}
func MakeConfig(keys []string) *ssh.ClientConfig {
config := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{},
}
if os.Getenv("SSH_HOST_KEY") != "" {
key, err := ssh.ParsePublicKey([]byte(os.Getenv("SSH_HOST_KEY")))
if err != nil {
log.Fatal("failed to parse public key: ", os.Getenv("SSH_HOST_KEY"))
}
config.HostKeyCallback = ssh.FixedHostKey(key)
} else if os.Getenv("INSECURE_IGNORE_HOST_KEY") == "YES" {
if os.Getenv("INSECURE_IGNORE_HOST_KEY") != "YES" {
log.Fatal("INSECURE_IGNORE_HOST_KEY: only possible value is YES in all caps")
}
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
} else {
hostKeyCallback, err := kh.New(os.Getenv("HOME") + "/.ssh/known_hosts")
if err != nil {
log.Fatal("could not create hostkeycallback function: ", err)
}
config.HostKeyCallback = hostKeyCallback
}
for _, keyname := range keys {
key, err := ioutil.ReadFile(keyname)
if err == nil {
var signer ssh.Signer
if os.Getenv("KEY_PASS") != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(os.Getenv("KEY_PASS")))
} else {
signer, err = ssh.ParsePrivateKey(key)
}
if err != nil {
panic(err)
}
config.Auth = append(config.Auth, ssh.PublicKeys(signer))
}
}
return config
}