forked from stephane-martin/golang-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostkey.go
44 lines (41 loc) · 1.05 KB
/
hostkey.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
package ssh
import (
"bytes"
"fmt"
"net"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
func MakeHostKeyCallback(insecure bool, l Logger) (ssh.HostKeyCallback, error) {
if insecure {
hkcb := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
l.Debugw(
"host key",
"hostname", hostname,
"remote", remote.String(),
"key", string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key))),
)
return nil
}
return hkcb, nil
}
kh, err := homedir.Expand("~/.ssh/known_hosts")
if err != nil {
return nil, fmt.Errorf("failed to expand known_hosts path: %s", err)
}
callback, err := knownhosts.New(kh)
if err != nil {
return nil, fmt.Errorf("failed to open known_hosts file: %s", err)
}
hkcb := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
l.Debugw(
"host key",
"hostname", hostname,
"remote", remote.String(),
"key", string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key))),
)
return callback(hostname, remote, key)
}
return hkcb, nil
}