Skip to content

Commit 5817e4e

Browse files
Improve error message for encrypted SSH keys without password
When a password-protected SSH private key is provided without the 'password' field in the Secret, the error message was misleading: "SSH agent requested but SSH_AUTH_SOCK not-specified" This change detects encrypted SSH keys early by attempting to parse the identity with ssh.ParseRawPrivateKey and checking for PassphraseMissingError. When detected, a clear error is returned pointing the user to add the 'password' field to their Secret. Fixes #802 Signed-off-by: Ogulcan Aydogan <ogulcanaydogan@hotmail.com>
1 parent 5376a7e commit 5817e4e

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

internal/controller/gitrepository_controller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/fluxcd/pkg/runtime/logger"
3434
"github.com/fluxcd/pkg/runtime/secrets"
3535
"github.com/go-git/go-git/v5/plumbing/transport"
36+
ssh "golang.org/x/crypto/ssh"
3637
corev1 "k8s.io/api/core/v1"
3738
"k8s.io/apimachinery/pkg/runtime"
3839
"k8s.io/apimachinery/pkg/types"
@@ -651,6 +652,22 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
651652
return nil, e
652653
}
653654

655+
// Check if SSH identity key is encrypted but no password was provided.
656+
if opts.Transport == git.SSH && len(opts.Identity) > 0 && opts.Password == "" {
657+
if _, err := ssh.ParseRawPrivateKey(opts.Identity); err != nil {
658+
var missingErr *ssh.PassphraseMissingError
659+
if errors.As(err, &missingErr) {
660+
e := serror.NewGeneric(
661+
fmt.Errorf("SSH identity key is encrypted but no 'password' field was provided in the secret '%s/%s'",
662+
obj.GetNamespace(), obj.Spec.SecretRef.Name),
663+
sourcev1.AuthenticationFailedReason,
664+
)
665+
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, "%s", e)
666+
return nil, e
667+
}
668+
}
669+
}
670+
654671
// Configure provider authentication if specified.
655672
var getCreds func() (*authutils.GitCredentials, error)
656673
switch provider := obj.GetProvider(); provider {

0 commit comments

Comments
 (0)