@@ -54,7 +54,7 @@ func (a *KeyboardInteractive) String() string {
5454}
5555
5656func (a * KeyboardInteractive ) ClientConfig () (* ssh.ClientConfig , error ) {
57- return a .SetHostKeyCallback (& ssh.ClientConfig {
57+ return a .SetHostKeyCallbackAndAlgorithms (& ssh.ClientConfig {
5858 User : a .User ,
5959 Auth : []ssh.AuthMethod {
6060 a .Challenge ,
@@ -78,7 +78,7 @@ func (a *Password) String() string {
7878}
7979
8080func (a * Password ) ClientConfig () (* ssh.ClientConfig , error ) {
81- return a .SetHostKeyCallback (& ssh.ClientConfig {
81+ return a .SetHostKeyCallbackAndAlgorithms (& ssh.ClientConfig {
8282 User : a .User ,
8383 Auth : []ssh.AuthMethod {ssh .Password (a .Password )},
8484 })
@@ -101,7 +101,7 @@ func (a *PasswordCallback) String() string {
101101}
102102
103103func (a * PasswordCallback ) ClientConfig () (* ssh.ClientConfig , error ) {
104- return a .SetHostKeyCallback (& ssh.ClientConfig {
104+ return a .SetHostKeyCallbackAndAlgorithms (& ssh.ClientConfig {
105105 User : a .User ,
106106 Auth : []ssh.AuthMethod {ssh .PasswordCallback (a .Callback )},
107107 })
@@ -150,7 +150,7 @@ func (a *PublicKeys) String() string {
150150}
151151
152152func (a * PublicKeys ) ClientConfig () (* ssh.ClientConfig , error ) {
153- return a .SetHostKeyCallback (& ssh.ClientConfig {
153+ return a .SetHostKeyCallbackAndAlgorithms (& ssh.ClientConfig {
154154 User : a .User ,
155155 Auth : []ssh.AuthMethod {ssh .PublicKeys (a .Signer )},
156156 })
@@ -211,7 +211,7 @@ func (a *PublicKeysCallback) String() string {
211211}
212212
213213func (a * PublicKeysCallback ) ClientConfig () (* ssh.ClientConfig , error ) {
214- return a .SetHostKeyCallback (& ssh.ClientConfig {
214+ return a .SetHostKeyCallbackAndAlgorithms (& ssh.ClientConfig {
215215 User : a .User ,
216216 Auth : []ssh.AuthMethod {ssh .PublicKeysCallback (a .Callback )},
217217 })
@@ -230,11 +230,23 @@ func (a *PublicKeysCallback) ClientConfig() (*ssh.ClientConfig, error) {
230230// ~/.ssh/known_hosts
231231// /etc/ssh/ssh_known_hosts
232232func NewKnownHostsCallback (files ... string ) (ssh.HostKeyCallback , error ) {
233- kh , err := newKnownHosts (files ... )
234- return ssh .HostKeyCallback (kh ), err
233+ kh , err := NewKnownHostsDb (files ... )
234+ return kh .HostKeyCallback (), err
235235}
236236
237- func newKnownHosts (files ... string ) (knownhosts.HostKeyCallback , error ) {
237+ // NewKnownHostsDb returns knownhosts.HostKeyDB based on a file based on a
238+ // known_hosts file. http://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT
239+ //
240+ // If list of files is empty, then it will be read from the SSH_KNOWN_HOSTS
241+ // environment variable, example:
242+ //
243+ // /home/foo/custom_known_hosts_file:/etc/custom_known/hosts_file
244+ //
245+ // If SSH_KNOWN_HOSTS is not set the following file locations will be used:
246+ //
247+ // ~/.ssh/known_hosts
248+ // /etc/ssh/ssh_known_hosts
249+ func NewKnownHostsDb (files ... string ) (* knownhosts.HostKeyDB , error ) {
238250 var err error
239251
240252 if len (files ) == 0 {
@@ -247,7 +259,7 @@ func newKnownHosts(files ...string) (knownhosts.HostKeyCallback, error) {
247259 return nil , err
248260 }
249261
250- return knownhosts .New (files ... )
262+ return knownhosts .NewDB (files ... )
251263}
252264
253265func getDefaultKnownHostsFiles () ([]string , error ) {
@@ -289,25 +301,23 @@ func filterKnownHostsFiles(files ...string) ([]string, error) {
289301}
290302
291303// HostKeyCallbackHelper is a helper that provides common functionality to
292- // configure HostKeyCallback into a ssh.ClientConfig.
304+ // configure HostKeyCallback and HostKeyAlgorithms into a ssh.ClientConfig.
293305type HostKeyCallbackHelper struct {
294306 // HostKeyCallback is the function type used for verifying server keys.
295- // If nil default callback will be create using NewKnownHostsCallback
307+ // If nil, a default callback will be created using NewKnownHostsDb
296308 // without argument.
297309 HostKeyCallback ssh.HostKeyCallback
298- }
299310
300- // SetHostKeyCallback sets the field HostKeyCallback in the given cfg. If
301- // HostKeyCallback is empty a default callback is created using
302- // NewKnownHostsCallback.
303- func (m * HostKeyCallbackHelper ) SetHostKeyCallback (cfg * ssh.ClientConfig ) (* ssh.ClientConfig , error ) {
304- var err error
305- if m .HostKeyCallback == nil {
306- if m .HostKeyCallback , err = NewKnownHostsCallback (); err != nil {
307- return cfg , err
308- }
309- }
311+ // HostKeyAlgorithms is a list of supported host key algorithms that will
312+ // be used for host key verification.
313+ HostKeyAlgorithms []string
314+ }
310315
316+ // SetHostKeyCallbackAndAlgorithms sets the field HostKeyCallback and HostKeyAlgorithms in the given cfg.
317+ // If the host key callback or algorithms is empty it is left empty. It will be handled by the dial method,
318+ // falling back to knownhosts.
319+ func (m * HostKeyCallbackHelper ) SetHostKeyCallbackAndAlgorithms (cfg * ssh.ClientConfig ) (* ssh.ClientConfig , error ) {
311320 cfg .HostKeyCallback = m .HostKeyCallback
321+ cfg .HostKeyAlgorithms = m .HostKeyAlgorithms
312322 return cfg , nil
313323}
0 commit comments