forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_key_getter.go
52 lines (43 loc) · 1.01 KB
/
host_key_getter.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
package proxy
import (
"net"
"golang.org/x/crypto/ssh"
)
type HostKeyGetter struct {
publicKeyChannel chan ssh.PublicKey
dialErrorChannel chan error
}
func NewHostKeyGetter() HostKeyGetter {
return HostKeyGetter{
publicKeyChannel: make(chan ssh.PublicKey),
dialErrorChannel: make(chan error),
}
}
func (h HostKeyGetter) Get(key, serverURL string) (ssh.PublicKey, error) {
signer, err := ssh.ParsePrivateKey([]byte(key))
if err != nil {
return nil, err
}
clientConfig := &ssh.ClientConfig{
User: "jumpbox",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: h.keyScanCallback,
}
go func() {
conn, err := ssh.Dial("tcp", serverURL, clientConfig)
if err != nil {
h.publicKeyChannel <- nil
h.dialErrorChannel <- err
return
}
defer conn.Close()
h.dialErrorChannel <- nil
}()
return <-h.publicKeyChannel, <-h.dialErrorChannel
}
func (h HostKeyGetter) keyScanCallback(hostname string, remote net.Addr, key ssh.PublicKey) error {
h.publicKeyChannel <- key
return nil
}