forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
301 lines (258 loc) · 8 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package ssh
import (
"bytes"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"text/template"
"time"
"github.com/wallix/awless/logger"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
"golang.org/x/crypto/ssh/terminal"
)
type Client struct {
*gossh.Client
signer gossh.Signer
Keypath, IP, User string
HostKeyCallback gossh.HostKeyCallback
StrictHostKeyChecking bool
InteractiveTerminalFunc func(*gossh.Client) error
logger *logger.Logger
}
func InitClient(keyname string, keyFolders ...string) (*Client, error) {
privkey, err := resolvePrivateKey(keyname, keyFolders...)
if err != nil {
return nil, err
}
signer, err := resolveSigner(privkey)
if err != nil {
return nil, err
}
cli := &Client{
logger: logger.DiscardLogger,
InteractiveTerminalFunc: func(*gossh.Client) error { return nil },
Keypath: privkey.path,
signer: signer,
StrictHostKeyChecking: true,
}
return cli, nil
}
func (c *Client) SetLogger(l *logger.Logger) {
c.logger = l
}
func (c *Client) SetStrictHostKeyChecking(hostKeyChecking bool) {
c.StrictHostKeyChecking = hostKeyChecking
}
func (c *Client) DialWithUsers(usernames ...string) (*Client, error) {
var failures int
for _, user := range usernames {
c.logger.Verbosef("trying with user %s", user)
client, err := gossh.Dial("tcp", c.IP+":22", c.buildClientConfig(user))
if err == nil {
c.User = user
c.Client = client
break
}
if err != nil && strings.Contains(err.Error(), "unable to authenticate") {
failures++
if len(usernames) == failures {
return c, fmt.Errorf("with users %q: %s", usernames, err)
}
continue
} else if err != nil {
return c, err
}
}
return c, nil
}
func (c *Client) Connect() error {
defer func() {
if c.Client != nil {
c.Client.Close()
}
}()
args, installed := c.localExec()
if installed {
c.logger.Infof("Login as '%s' on '%s', using keypair '%s' with ssh client '%s'\n", c.User, c.IP, c.Keypath, args[0])
return syscall.Exec(args[0], args, os.Environ())
}
c.logger.Infof("No SSH. Fallback on builtin client. Login as '%s' on '%s', using keypair '%s'\n", c.User, c.IP, c.Keypath)
return c.InteractiveTerminalFunc(c.Client)
}
func (c *Client) buildClientConfig(username string) *gossh.ClientConfig {
config := &gossh.ClientConfig{
User: username,
Auth: []gossh.AuthMethod{gossh.PublicKeys(c.signer)},
Timeout: 2 * time.Second,
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}
if c.StrictHostKeyChecking {
config.HostKeyCallback = checkHostKey
}
return config
}
func (c *Client) SSHConfigString(hostname string) string {
var buf bytes.Buffer
params := struct {
IP, User, Keypath, Name string
}{c.IP, c.User, c.Keypath, hostname}
template.Must(template.New("ssh_config").Parse(`
Host {{ .Name }}
Hostname {{ .IP }}
User {{ .User }}
IdentityFile {{ .Keypath }}
`)).Execute(&buf, params)
return buf.String()
}
func (c *Client) ConnectString() string {
args, _ := c.localExec()
return strings.Join(args, " ")
}
func (c *Client) localExec() ([]string, bool) {
exists := true
bin, err := exec.LookPath("ssh")
if err != nil {
exists = false
bin = "ssh"
}
args := []string{bin, "-i", c.Keypath, fmt.Sprintf("%s@%s", c.User, c.IP)}
if !c.StrictHostKeyChecking {
args = append(args, "-o", "StrictHostKeychecking=no")
}
return args, exists
}
func DecryptSSHKey(key []byte, password []byte) (gossh.Signer, error) {
block, _ := pem.Decode(key)
pem, err := x509.DecryptPEMBlock(block, password)
if err != nil {
return nil, err
}
sshkey, err := x509.ParsePKCS1PrivateKey(pem)
if err != nil {
return nil, err
}
return gossh.NewSignerFromKey(sshkey)
}
func resolveSigner(priv privateKey) (gossh.Signer, error) {
signer, err := gossh.ParsePrivateKey(priv.body)
if err != nil && strings.Contains(err.Error(), "cannot decode encrypted private keys") {
fmt.Fprintf(os.Stderr, "This SSH key is encrypted. Please enter passphrase for key '%s':", priv.path)
var passphrase []byte
passphrase, err = terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
fmt.Fprintln(os.Stderr)
signer, err = DecryptSSHKey(priv.body, passphrase)
}
return signer, err
}
type privateKey struct {
path string
body []byte
}
func resolvePrivateKey(keyname string, keyFolders ...string) (priv privateKey, err error) {
keyPaths := []string{
keyname,
}
if !strings.HasPrefix(keyname, ".pem") {
keyPaths = append(keyPaths, fmt.Sprintf("%s.pem", keyname))
}
for _, folder := range keyFolders {
if filepath.IsAbs(keyname) {
break
}
if _, err = os.Stat(folder); err != nil {
continue
}
keyPaths = append(keyPaths, filepath.Join(folder, keyname))
if !strings.HasPrefix(keyname, ".pem") {
keyPaths = append(keyPaths, filepath.Join(folder, fmt.Sprintf("%s.pem", keyname)))
}
}
for _, path := range keyPaths {
priv.body, err = ioutil.ReadFile(path)
if err == nil {
priv.path = path
return
}
if !os.IsNotExist(err) {
return
}
}
err = fmt.Errorf("cannot find SSH key '%s'. Searched at paths '%s'", keyname, strings.Join(keyPaths, "','"))
return
}
func checkHostKey(hostname string, remote net.Addr, key gossh.PublicKey) error {
var knownHostsFiles []string
var fileToAddKnownKey string
opensshFile := filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts")
if _, err := os.Stat(opensshFile); err == nil {
knownHostsFiles = append(knownHostsFiles, opensshFile)
fileToAddKnownKey = opensshFile
}
awlessFile := filepath.Join(os.Getenv("__AWLESS_HOME"), "known_hosts")
if _, err := os.Stat(awlessFile); err == nil {
knownHostsFiles = append(knownHostsFiles, awlessFile)
}
if fileToAddKnownKey == "" {
fileToAddKnownKey = awlessFile
}
checkKnownHostFunc, err := knownhosts.New(knownHostsFiles...)
if err != nil {
return err
}
knownhostsErr := checkKnownHostFunc(hostname, remote, key)
keyError, ok := knownhostsErr.(*knownhosts.KeyError)
if !ok {
return knownhostsErr
}
if len(keyError.Want) == 0 {
if trustKeyFunc(hostname, remote, key, fileToAddKnownKey) {
f, err := os.OpenFile(fileToAddKnownKey, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(knownhosts.Line([]string{hostname}, key) + "\n")
return err
} else {
return errors.New("Host public key verification failed.")
}
}
var knownKeyInfos string
var knownKeyFiles []string
for _, knownKey := range keyError.Want {
knownKeyInfos += fmt.Sprintf("\n-> %s (%s key in %s:%d)", gossh.FingerprintSHA256(knownKey.Key), knownKey.Key.Type(), knownKey.Filename, knownKey.Line)
knownKeyFiles = append(knownKeyFiles, fmt.Sprintf("'%s:%d'", knownKey.Filename, knownKey.Line))
}
return fmt.Errorf(`
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
AWLESS DETECTED THAT THE REMOTE HOST PUBLIC KEY HAS CHANGED
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Host key for '%s' has changed and you did not disable strict host key checking.
Someone may be trying to intercept your connection (man-in-the-middle attack). Otherwise, the host key may have been changed.
The fingerprint for the %s key sent by the remote host is %s.
You persisted:%s
To get rid of this message, update %s`, hostname, key.Type(), gossh.FingerprintSHA256(key), knownKeyInfos, strings.Join(knownKeyFiles, ","))
}
var trustKeyFunc func(hostname string, remote net.Addr, key gossh.PublicKey, keyFileName string) bool = func(hostname string, remote net.Addr, key gossh.PublicKey, keyFileName string) bool {
fmt.Printf("awless could not validate the authenticity of '%s' (unknown host)\n", hostname)
fmt.Printf("%s public key fingerprint is %s.\n", key.Type(), gossh.FingerprintSHA256(key))
fmt.Printf("Do you want to continue connecting and persist this key to '%s' (yes/no)? ", keyFileName)
var yesorno string
_, err := fmt.Scanln(&yesorno)
if err != nil {
return false
}
return strings.ToLower(yesorno) == "yes"
}