-
Notifications
You must be signed in to change notification settings - Fork 460
/
ssh.go
164 lines (140 loc) · 3.85 KB
/
ssh.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package uri
import (
"fmt"
"log"
"net"
"os"
"os/user"
"strings"
"github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"
)
const (
defaultSSHPort = "22"
defaultSSHKeyPath = "${HOME}/.ssh/id_rsa"
defaultSSHKnownHostsPath = "${HOME}/.ssh/known_hosts"
defaultSSHConfigFile = "${HOME}/.ssh/config"
defaultSSHAuthMethods = "agent,privkey"
)
func (u *ConnectionURI) parseAuthMethods() []ssh.AuthMethod {
q := u.Query()
authMethods := q.Get("sshauth")
if authMethods == "" {
authMethods = defaultSSHAuthMethods
}
sshKeyPath := q.Get("keyfile")
if sshKeyPath == "" {
sshKeyPath = defaultSSHKeyPath
}
auths := strings.Split(authMethods, ",")
result := make([]ssh.AuthMethod, 0)
for _, v := range auths {
switch v {
case "agent":
socket := os.Getenv("SSH_AUTH_SOCK")
if socket == "" {
continue
}
conn, err := net.Dial("unix", socket)
// Ignore error, we just fall back to another auth method
if err != nil {
log.Printf("[ERROR] Unable to connect to SSH agent: %v", err)
continue
}
agentClient := agent.NewClient(conn)
result = append(result, ssh.PublicKeysCallback(agentClient.Signers))
case "privkey":
sshKey, err := os.ReadFile(os.ExpandEnv(sshKeyPath))
if err != nil {
log.Printf("[ERROR] Failed to read ssh key: %v", err)
continue
}
signer, err := ssh.ParsePrivateKey(sshKey)
if err != nil {
log.Printf("[ERROR] Failed to parse ssh key: %v", err)
}
result = append(result, ssh.PublicKeys(signer))
case "ssh-password":
if sshPassword, ok := u.User.Password(); ok {
result = append(result, ssh.Password(sshPassword))
} else {
log.Printf("[ERROR] Missing password in userinfo of URI authority section")
}
default:
// For future compatibility it's better to just warn and not error
log.Printf("[WARN] Unsupported auth method: %s", v)
}
}
return result
}
func (u *ConnectionURI) dialSSH() (net.Conn, error) {
sshConfigFile, err := os.Open(os.ExpandEnv(defaultSSHConfigFile))
if err != nil {
log.Printf("[WARN] Failed to open ssh config file: %v", err)
}
sshcfg, err := ssh_config.Decode(sshConfigFile)
if err != nil {
log.Printf("[WARN] Failed to parse ssh config file: %v", err)
}
authMethods := u.parseAuthMethods()
if len(authMethods) < 1 {
return nil, fmt.Errorf("could not configure SSH authentication methods")
}
q := u.Query()
knownHostsPath := q.Get("knownhosts")
knownHostsVerify := q.Get("known_hosts_verify")
doVerify := q.Get("no_verify") == ""
if knownHostsVerify == "ignore" {
doVerify = false
}
if knownHostsPath == "" {
knownHostsPath = defaultSSHKnownHostsPath
}
hostKeyCallback := ssh.InsecureIgnoreHostKey()
if doVerify {
cb, err := knownhosts.New(os.ExpandEnv(knownHostsPath))
if err != nil {
return nil, fmt.Errorf("failed to read ssh known hosts: %w", err)
}
hostKeyCallback = cb
}
username := u.User.Username()
if username == "" {
sshu, err := sshcfg.Get(u.Host, "User")
log.Printf("[DEBUG] SSH User: %v", sshu)
if err != nil {
log.Printf("[DEBUG] ssh user: system username")
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("unable to get username: %w", err)
}
sshu = u.Username
}
username = sshu
}
cfg := ssh.ClientConfig{
User: username,
HostKeyCallback: hostKeyCallback,
Auth: authMethods,
Timeout: dialTimeout,
}
port := u.Port()
if port == "" {
port = defaultSSHPort
}
sshClient, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", u.Hostname(), port), &cfg)
if err != nil {
return nil, err
}
address := q.Get("socket")
if address == "" {
address = defaultUnixSock
}
c, err := sshClient.Dial("unix", address)
if err != nil {
return nil, fmt.Errorf("failed to connect to libvirt on the remote host: %w", err)
}
return c, nil
}