@@ -58,7 +58,7 @@ type SSH struct {
58
58
type PasswordCallback func () (secret string , err error )
59
59
60
60
var (
61
- authMethodCache = sync.Map {}
61
+ signerCache = sync.Map {}
62
62
defaultKeypaths = []string {"~/.ssh/id_rsa" , "~/.ssh/identity" , "~/.ssh/id_dsa" , "~/.ssh/id_ecdsa" , "~/.ssh/id_ed25519" }
63
63
dummyhostKeyPaths []string
64
64
globalOnce sync.Once
@@ -407,33 +407,33 @@ func (c *SSH) clientConfig() (*ssh.ClientConfig, error) { //nolint:cyclop
407
407
log .Tracef ("%s: using %d passed-in auth methods" , c , len (c .AuthMethods ))
408
408
config .Auth = c .AuthMethods
409
409
} else if len (signers ) > 0 {
410
- log .Debugf ("%s: using all keys (%d) from ssh agent because a keypath was not explicitly given" , c , len (signers ))
411
- config .Auth = append (config .Auth , ssh .PublicKeys (signers ... ))
410
+ log .Debugf ("%s: using all keys (%d) from ssh agent" , c , len (signers ))
412
411
}
413
412
414
413
for _ , keyPath := range c .keyPaths {
415
- if am , ok := authMethodCache .Load (keyPath ); ok {
414
+ if am , ok := signerCache .Load (keyPath ); ok {
416
415
switch authM := am .(type ) {
417
- case ssh.AuthMethod :
418
- log .Tracef ("%s: using cached auth method for %s" , c , keyPath )
419
- config . Auth = append (config . Auth , authM )
416
+ case ssh.Signer :
417
+ log .Tracef ("%s: using cached signer for %s" , c , keyPath )
418
+ signers = append (signers , authM )
420
419
case error :
421
420
log .Tracef ("%s: already discarded key %s: %v" , c , keyPath , authM )
422
421
default :
423
422
log .Tracef ("%s: unexpected type %T for cached auth method for %s" , c , am , keyPath )
424
423
}
425
424
continue
426
425
}
427
- privateKeyAuth , err := c .pkeySigner (signers , keyPath )
426
+ signer , err := c .pkeySigner (signers , keyPath )
428
427
if err != nil {
429
428
log .Debugf ("%s: failed to obtain a signer for identity %s: %v" , c , keyPath , err )
430
429
// store the error so this key won't be loaded again
431
- authMethodCache .Store (keyPath , err )
430
+ signerCache .Store (keyPath , err )
432
431
} else {
433
- authMethodCache .Store (keyPath , privateKeyAuth )
434
- config . Auth = append (config . Auth , privateKeyAuth )
432
+ signerCache .Store (keyPath , signer )
433
+ signers = append (signers , signer )
435
434
}
436
435
}
436
+ config .Auth = append (config .Auth , ssh .PublicKeys (signers ... ))
437
437
438
438
if len (config .Auth ) == 0 {
439
439
return nil , fmt .Errorf ("%w: no usable authentication method found" , ErrCantConnect )
@@ -489,22 +489,22 @@ func (c *SSH) Connect() error {
489
489
return nil
490
490
}
491
491
492
- func (c * SSH ) pubkeySigner (signers []ssh.Signer , key ssh.PublicKey ) (ssh.AuthMethod , error ) {
492
+ func (c * SSH ) pubkeySigner (signers []ssh.Signer , key ssh.PublicKey ) (ssh.Signer , error ) {
493
493
if len (signers ) == 0 {
494
494
return nil , fmt .Errorf ("%w: signer not found for public key" , ErrCantConnect )
495
495
}
496
496
497
497
for _ , s := range signers {
498
498
if bytes .Equal (key .Marshal (), s .PublicKey ().Marshal ()) {
499
499
log .Debugf ("%s: signer for public key available in ssh agent" , c )
500
- return ssh . PublicKeys ( s ) , nil
500
+ return s , nil
501
501
}
502
502
}
503
503
504
504
return nil , fmt .Errorf ("%w: the provided key is a public key and is not known by agent" , ErrAuthFailed )
505
505
}
506
506
507
- func (c * SSH ) pkeySigner (signers []ssh.Signer , path string ) (ssh.AuthMethod , error ) {
507
+ func (c * SSH ) pkeySigner (signers []ssh.Signer , path string ) (ssh.Signer , error ) {
508
508
log .Tracef ("%s: checking identity file %s" , c , path )
509
509
key , err := os .ReadFile (path )
510
510
if err != nil {
@@ -520,7 +520,7 @@ func (c *SSH) pkeySigner(signers []ssh.Signer, path string) (ssh.AuthMethod, err
520
520
signer , err := ssh .ParsePrivateKey (key )
521
521
if err == nil {
522
522
log .Debugf ("%s: using an unencrypted private key from %s" , c , path )
523
- return ssh . PublicKeys ( signer ) , nil
523
+ return signer , nil
524
524
}
525
525
526
526
var ppErr * ssh.PassphraseMissingError
@@ -543,7 +543,7 @@ func (c *SSH) pkeySigner(signers []ssh.Signer, path string) (ssh.AuthMethod, err
543
543
if err != nil {
544
544
return nil , fmt .Errorf ("%w: protected key %s decoding failed: %w" , ErrCantConnect , path , err )
545
545
}
546
- return ssh . PublicKeys ( signer ) , nil
546
+ return signer , nil
547
547
}
548
548
}
549
549
0 commit comments