Skip to content

Improve error message for encrypted SSH keys without password#2013

Merged
matheuscscp merged 1 commit intofluxcd:mainfrom
ogulcanaydogan:fix/802-ssh-password-error-message
Apr 1, 2026
Merged

Improve error message for encrypted SSH keys without password#2013
matheuscscp merged 1 commit intofluxcd:mainfrom
ogulcanaydogan:fix/802-ssh-password-error-message

Conversation

@ogulcanaydogan
Copy link
Copy Markdown
Contributor

Description

When users provide a password-protected SSH private key in their Secret but forget the password field, the current error is misleading:

error: error creating SSH agent: "SSH agent requested but SSH_AUTH_SOCK not-specified"

This doesn't hint at the actual problem. The fix detects encrypted keys early by parsing the identity with ssh.ParseRawPrivateKey and checking for ssh.PassphraseMissingError. When detected, the error now reads:

SSH identity key is encrypted but no 'password' field was provided in the secret 'namespace/secret-name'

Changes

  • internal/controller/gitrepository_controller.go: Added encrypted key check in getAuthOpts() after SSH auth options are constructed. Uses golang.org/x/crypto/ssh.ParseRawPrivateKey + PassphraseMissingError detection.

How it works

  1. After git.NewAuthOptions() returns with SSH transport
  2. If Identity is present but Password is empty
  3. Try parsing the raw private key — if it returns PassphraseMissingError, the key is encrypted
  4. Return a clear error with the secret name so users know exactly what to fix

No behavior change for unencrypted keys or when password is provided.

Fixes #802

@stefanprodan stefanprodan added the area/git Git related issues and pull requests label Mar 28, 2026
Copy link
Copy Markdown
Member

@matheuscscp matheuscscp left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

I tested this PR and it's working.


// Check if SSH identity key is encrypted but no password was provided.
if opts.Transport == git.SSH && len(opts.Identity) > 0 && opts.Password == "" {
if _, err := ssh.ParseRawPrivateKey(opts.Identity); err != nil {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the err != nil check here is redundant? Because if the error is nil, it wouldn't match the typed error?

@ogulcanaydogan
Copy link
Copy Markdown
Contributor Author

Thanks @hiddeco — good catch! Removed the redundant err != nil check since errors.As already handles nil errors gracefully.

@matheuscscp
Copy link
Copy Markdown
Member

@ogulcanaydogan Can you pls squash and force-push? This PR should have a single commit. I will retest after that 🙏

Detect encrypted SSH identity keys early in getAuthOpts() by parsing
with ssh.ParseRawPrivateKey and checking for PassphraseMissingError.
When detected, return a clear error pointing the user to add the
'password' field to their Secret instead of the misleading
"SSH agent requested but SSH_AUTH_SOCK not-specified" message.

Fixes fluxcd#802

Signed-off-by: Ogulcan Aydogan <ogulcanaydogan@hotmail.com>
@ogulcanaydogan ogulcanaydogan force-pushed the fix/802-ssh-password-error-message branch from 47cc4c9 to 790be38 Compare April 1, 2026 06:44
@ogulcanaydogan
Copy link
Copy Markdown
Contributor Author

Squashed into a single commit and rebased on latest main. Ready for retest.

@stefanprodan stefanprodan added the backport:release/v1.8.x To be backported to release/v1.8.x label Apr 1, 2026
@matheuscscp
Copy link
Copy Markdown
Member

Retested and still works. Thanks!

@matheuscscp matheuscscp merged commit 7a113ec into fluxcd:main Apr 1, 2026
7 checks passed
@fluxcdbot
Copy link
Copy Markdown
Member

Successfully created backport PR for release/v1.8.x:

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

Labels

area/git Git related issues and pull requests backport:release/v1.8.x To be backported to release/v1.8.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve error message when users don't configure the password for password-protected SSH keys

5 participants