Skip to content

Commit

Permalink
imitate ssh.Dial func for simplicity
Browse files Browse the repository at this point in the history
previously ScanHostKey ignored any SSH/network errors
in case it managed to get host keys

to make it more obvious we imitate `ssh.Dial` with `sshDial` func

Signed-off-by: Artem Nistratov <[email protected]>
  • Loading branch information
adone committed Nov 27, 2024
1 parent 63c6588 commit 8c10c5e
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions ssh/host_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,31 @@ func ScanHostKey(host string, timeout time.Duration, clientHostKeyAlgos []string
config.HostKeyAlgorithms = clientHostKeyAlgos
}

ctx, cancel := context.WithTimeout(context.Background(), timeout)
err := sshDial(host, config)

if len(col.knownKeys) > 0 {
return col.knownKeys, nil
}

return col.knownKeys, err
}

func sshDial(host string, config *ssh.ClientConfig) error {
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
defer cancel()
// support for ALL_PROXY ENV varaible
// this reads the ALL_PROXY environment varaible
conn, err := proxy.Dial(ctx, "tcp", host)
if err != nil {
return nil, err
return err
}
c, chans, reqs, err := ssh.NewClientConn(conn, host, config)
if err != nil {
return nil, err
return err
}
client := ssh.NewClient(c, chans, reqs)
defer client.Close()

if len(col.knownKeys) > 0 {
return col.knownKeys, nil
}
return col.knownKeys, err
return nil
}

// HostKeyCollector offers a StoreKey method which provides an
Expand Down

0 comments on commit 8c10c5e

Please sign in to comment.