-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeys.go
102 lines (97 loc) · 2.37 KB
/
keys.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"errors"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
)
// Getting keys:
// > --key flag overrides all
// > If no --key is defined, check for ssh-agent on $SSH_AUTH_SOCK
// > If no ssh-agent defined, look or key in ~/.ssh/id_rsa
// > fail if none of the above work
func getKeyAuths(keyfile ...string) (auths []ssh.AuthMethod, err error) {
auths = []ssh.AuthMethod{}
if len(keyfile) == 0 {
// If no keys are provided see if there's an ssh-agent running
if len(os.Getenv("SSH_AUTH_SOCK")) > 0 {
if auths, err = loadEnvAgent(); err != nil {
return
}
} else {
// Otherwise use ~/.ssh/id_rsa or ~/ssh/id_rsa (for windows, but
// it works on linux too)
if auths, err = loadDefaultKeys(); err != nil {
return
}
}
} else {
// Append each provided key to auths
auths, err = parseKeyFiles(keyfile)
}
if len(auths) == 0 {
err = errors.New("No auths parsed from provided keys")
}
return
}
func parseKeyFiles(paths []string) (auths []ssh.AuthMethod, err error) {
for _, key := range paths {
var (
pemBytes []byte
signer ssh.Signer
)
if !fileExists(key) {
err = errors.New("Specified key does not exist")
return
}
pemBytes, err = ioutil.ReadFile(key)
if err != nil {
return
}
signer, err = ssh.ParsePrivateKey(pemBytes)
if err != nil {
return
}
auths = append(auths, ssh.PublicKeys(signer))
}
return
}
func loadEnvAgent() (auths []ssh.AuthMethod, err error) {
sshAuthSock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return
}
defer sshAuthSock.Close()
ag := agent.NewClient(sshAuthSock)
auths = []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}
return
}
func loadDefaultKeys() (auths []ssh.AuthMethod, err error) {
k := ""
currentUser, err := user.Current()
defaultKeyPathA := filepath.FromSlash(currentUser.HomeDir + "/.ssh/id_rsa")
defaultKeyPathB := filepath.FromSlash(currentUser.HomeDir + "/ssh/id_rsa")
if fileExists(defaultKeyPathA) {
k = defaultKeyPathA
} else if fileExists(defaultKeyPathB) {
k = defaultKeyPathB
}
if len(k) == 0 {
err = errors.New("No key specified")
return
}
pemBytes, err := ioutil.ReadFile(k)
if err != nil {
return
}
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return
}
auths = []ssh.AuthMethod{ssh.PublicKeys(signer)}
return
}