Skip to content

fix: SSH does not try multiple authentication methods of the same type#264

Closed
cgroschupp wants to merge 1 commit intok0sproject:mainfrom
cgroschupp:fix/ssh-doesnt-tries-multiple-authmethods-from-the-same-type-v2
Closed

fix: SSH does not try multiple authentication methods of the same type#264
cgroschupp wants to merge 1 commit intok0sproject:mainfrom
cgroschupp:fix/ssh-doesnt-tries-multiple-authmethods-from-the-same-type-v2

Conversation

@cgroschupp
Copy link
Copy Markdown

@cgroschupp cgroschupp commented May 9, 2025

The ssh does not try multiple authentication methods of the same type.

Fixes #237, k0sproject/k0sctl#674

Signed-off-by: Christian Groschupp <christian@groschupp.org>
@cgroschupp cgroschupp force-pushed the fix/ssh-doesnt-tries-multiple-authmethods-from-the-same-type-v2 branch from 836b675 to 764cbee Compare May 14, 2025 10:43
@cgroschupp
Copy link
Copy Markdown
Author

@kke can you please trigger again the workflows?

@cgroschupp
Copy link
Copy Markdown
Author

@kke can you please review this PR?

Copy link
Copy Markdown

@apreiml apreiml left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works as intended.

@cgroschupp
Copy link
Copy Markdown
Author

@twz123 Could you please review this PR?

@kke
Copy link
Copy Markdown
Contributor

kke commented Mar 11, 2026

This seems to target the main branch which isn't currently used anywhere. The release-0.x branch is what k0sctl uses.

@kke kke changed the base branch from main to release-0.x March 11, 2026 09:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes SSH authentication behavior where multiple publickey auth methods were configured separately, causing golang.org/x/crypto/ssh to try only the first one and skip later key-based methods (e.g., an explicitly configured key file when agent keys exist).

Changes:

  • Replace caching of ssh.AuthMethod per keyPath with caching of ssh.Signer.
  • Build a single ssh.PublicKeys(signers...) auth method from agent signers + keyPath-derived signers to ensure multiple keys of the same method type are attempted.
  • Adjust helper functions to return ssh.Signer instead of ssh.AuthMethod.
Comments suppressed due to low confidence (1)

protocol/ssh/connection.go:345

  • config.Auth is appended with ssh.PublicKeys(signers...) unconditionally. When signers is empty (no agent + no usable keyPaths), this still adds an AuthMethod, making the len(config.Auth) == 0 check ineffective and shifting the failure to a later/less clear auth error. Suggest only appending the PublicKeys auth method when len(signers) > 0, so the existing "no usable authentication method found" error is preserved.
	config.Auth = append(config.Auth, ssh.PublicKeys(signers...))

	if len(config.Auth) == 0 {
		return nil, fmt.Errorf("%w: no usable authentication method found", protocol.ErrAbort)
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +306 to +311
if len(c.AuthMethods) > 0 {
log.Trace(context.Background(), "using passed-in auth methods", "count", len(c.AuthMethods))
config.Auth = c.AuthMethods
} else if len(signers) > 0 {
c.Log().Debug("using all keys from ssh agent", "count", len(signers))
}
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signers is populated from the SSH agent before keyPath signers are appended. This means explicitly configured identity files may be tried only after potentially many agent keys, which can still fail in practice due to server-side MaxAuthTries before the keyPath key is attempted. Consider collecting keyPath-derived signers first (and/or prioritizing Config.KeyPath), then appending agent signers after, and pass only the agent signers into pkeySigner when resolving .pub keys against the agent.

Copilot uses AI. Check for mistakes.
Comment on lines +321 to +327
case ssh.Signer:
log.Trace(context.Background(), "using cached auth method", log.FileAttr(keyPath))
signers = append(signers, signerCacheItem)
case error:
log.Trace(context.Background(), "already discarded key", log.FileAttr(keyPath), log.ErrorAttr(signerCacheItem))
default:
log.Trace(context.Background(), fmt.Sprintf("unexpected type %T for cached auth method for %s", am, keyPath))
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trace messages in this cache branch still refer to a cached "auth method", but the cache now stores ssh.Signer values. Updating these log strings (and the "unexpected type" message) to refer to cached signers would avoid confusion during debugging.

Copilot uses AI. Check for mistakes.
@kke kke changed the base branch from release-0.x to main March 11, 2026 10:12
@kke
Copy link
Copy Markdown
Contributor

kke commented Mar 11, 2026

Recreated in #297 because this fell too far behind

@kke kke closed this Mar 11, 2026
@cgroschupp
Copy link
Copy Markdown
Author

This seems to target the main branch which isn't currently used anywhere. The release-0.x branch is what k0sctl uses.

See here: #265

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

key file is not used if other keys exist in ssh agent

4 participants