Skip to content

Commit 790be38

Browse files
Improve error message for encrypted SSH keys without password
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 #802 Signed-off-by: Ogulcan Aydogan <ogulcanaydogan@hotmail.com>
1 parent a72cc39 commit 790be38

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

internal/controller/gitrepository_controller.go

Lines changed: 16 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,21 @@ 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+
_, err := ssh.ParseRawPrivateKey(opts.Identity)
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+
654670
// Configure provider authentication if specified.
655671
var getCreds func() (*authutils.GitCredentials, error)
656672
switch provider := obj.GetProvider(); provider {

0 commit comments

Comments
 (0)