From 25283d357ee19708e9b4d5a8e5f0532f2d543905 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Fri, 8 Oct 2021 17:51:27 +0100 Subject: [PATCH] Add check for empty path and better error messaging Signed-off-by: Somtochi Onyekwere --- internal/bootstrap/git/commit_options.go | 5 +++++ internal/bootstrap/git/gogit/gogit.go | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/bootstrap/git/commit_options.go b/internal/bootstrap/git/commit_options.go index e1a9712c..bda92c2a 100644 --- a/internal/bootstrap/git/commit_options.go +++ b/internal/bootstrap/git/commit_options.go @@ -27,6 +27,11 @@ func (w GpgSigningOption) ApplyToCommit(in *CommitOptions) { } func WithGpgSigningOption(path, passphrase, keyID string) Option { + // return nil info if no path is set + if path == "" { + return GpgSigningOption{} + } + return GpgSigningOption{ GPGSigningInfo: &GPGSigningInfo{ PrivateKeyPath: path, diff --git a/internal/bootstrap/git/gogit/gogit.go b/internal/bootstrap/git/gogit/gogit.go index 11f5b508..d96971c8 100644 --- a/internal/bootstrap/git/gogit/gogit.go +++ b/internal/bootstrap/git/gogit/gogit.go @@ -194,7 +194,7 @@ func (g *GoGit) Commit(message git.Commit, opts ...git.Option) (string, error) { }, } - if options.GPGSigningInfo != nil && options.GPGSigningInfo.PrivateKeyPath != "" { + if options.GPGSigningInfo != nil { entity, err := getOpenPgpEntity(*options.GPGSigningInfo) if err != nil { return "", err @@ -260,7 +260,7 @@ func isRemoteBranchNotFoundErr(err error, ref string) bool { func getOpenPgpEntity(info git.GPGSigningInfo) (*openpgp.Entity, error) { r, err := os.Open(info.PrivateKeyPath) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to open gpg private key %s", err) } entityList, err := openpgp.ReadKeyRing(r) @@ -269,7 +269,7 @@ func getOpenPgpEntity(info git.GPGSigningInfo) (*openpgp.Entity, error) { } if len(entityList) == 0 { - return nil, fmt.Errorf("no entity formed") + return nil, fmt.Errorf("no GPP entity formed") } var entity *openpgp.Entity @@ -281,7 +281,7 @@ func getOpenPgpEntity(info git.GPGSigningInfo) (*openpgp.Entity, error) { } if entity == nil { - return nil, fmt.Errorf("no key matching the key id was found") + return nil, fmt.Errorf("no gpg private key matching the key id was found") } } else { entity = entityList[0] @@ -289,7 +289,7 @@ func getOpenPgpEntity(info git.GPGSigningInfo) (*openpgp.Entity, error) { err = entity.PrivateKey.Decrypt([]byte(info.Passphrase)) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to decrypt private key: %s", err) } return entity, nil