Skip to content

Commit 725815b

Browse files
authored
Merge pull request #2018 from fluxcd/backport-2013-to-release/v1.8.x
[release/v1.8.x] Improve error message for encrypted SSH keys without password
2 parents 5add99f + 956faff commit 725815b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

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)