diff --git a/pkg/ssh/host_scan.go b/pkg/ssh/host_key.go similarity index 71% rename from pkg/ssh/host_scan.go rename to pkg/ssh/host_key.go index a7a1a20d..a02bcaab 100644 --- a/pkg/ssh/host_scan.go +++ b/pkg/ssh/host_key.go @@ -14,10 +14,10 @@ import ( // Any errors (e.g. authentication failures) are ignored, except if // no key could be collected from the host. func ScanHostKey(host string, timeout time.Duration) ([]byte, error) { - col := &collector{} + col := &HostKeyCollector{} config := &ssh.ClientConfig{ - HostKeyCallback: col.StoreKey(), - Timeout: timeout, + HostKeyCallback: col.StoreKey(), + Timeout: timeout, } client, err := ssh.Dial("tcp", host, config) if err == nil { @@ -29,7 +29,9 @@ func ScanHostKey(host string, timeout time.Duration) ([]byte, error) { return col.knownKeys, err } -type collector struct { +// HostKeyCollector offers a StoreKey method which provides an +// HostKeyCallBack to collect public keys from an SSH server. +type HostKeyCollector struct { knownKeys []byte } @@ -37,7 +39,7 @@ type collector struct { // To collect multiple public key types from the host, multiple // SSH dials need with the ClientConfig HostKeyAlgorithms set to // the algorithm you want to collect. -func (c *collector) StoreKey() ssh.HostKeyCallback { +func (c *HostKeyCollector) StoreKey() ssh.HostKeyCallback { return func(hostname string, remote net.Addr, key ssh.PublicKey) error { c.knownKeys = append( c.knownKeys, @@ -46,3 +48,8 @@ func (c *collector) StoreKey() ssh.HostKeyCallback { return nil } } + +// GetKnownKeys returns the collected public keys in bytes. +func (c *HostKeyCollector) GetKnownKeys() []byte { + return c.knownKeys +}