From ada4b8d6a2446e34f6d2047c2d3c136c44058cad Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Wed, 16 Mar 2022 21:14:14 +0100 Subject: [PATCH] Do not display (missing public key) for age identities They are fully defined by the public key. RELEASE_NOTES=[BUGFIX] Do not print missing public key for age. Signed-off-by: Dominik Schulz --- internal/backend/crypto/gpg/cli/identities.go | 8 +++---- internal/backend/crypto/gpg/cli/keyring.go | 22 +++++++++++++++---- internal/store/root/recipients.go | 3 +++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/internal/backend/crypto/gpg/cli/identities.go b/internal/backend/crypto/gpg/cli/identities.go index 15988f10f7..7389b51f8b 100644 --- a/internal/backend/crypto/gpg/cli/identities.go +++ b/internal/backend/crypto/gpg/cli/identities.go @@ -33,16 +33,16 @@ func (g *GPG) FindIdentities(ctx context.Context, search ...string) ([]string, e return kl.UseableKeys(gpg.IsAlwaysTrust(ctx)).Recipients(), nil } -func (g *GPG) findKey(ctx context.Context, id string) gpg.Key { +func (g *GPG) findKey(ctx context.Context, id string) (gpg.Key, bool) { kl, _ := g.listKeys(ctx, "secret", id) if len(kl) >= 1 { - return kl[0] + return kl[0], true } kl, _ = g.listKeys(ctx, "public", id) if len(kl) >= 1 { - return kl[0] + return kl[0], true } return gpg.Key{ Fingerprint: id, - } + }, false } diff --git a/internal/backend/crypto/gpg/cli/keyring.go b/internal/backend/crypto/gpg/cli/keyring.go index f716f6fa37..29e1810d39 100644 --- a/internal/backend/crypto/gpg/cli/keyring.go +++ b/internal/backend/crypto/gpg/cli/keyring.go @@ -27,7 +27,7 @@ func (g *GPG) listKeys(ctx context.Context, typ string, search ...string) (gpg.K } } cmd := exec.CommandContext(ctx, g.binary, args...) - var errBuf = bytes.Buffer{} + errBuf := bytes.Buffer{} cmd.Stderr = &errBuf debug.Log("%s %+v\n", cmd.Path, cmd.Args) @@ -46,7 +46,11 @@ func (g *GPG) listKeys(ctx context.Context, typ string, search ...string) (gpg.K // Fingerprint returns the fingerprint. func (g *GPG) Fingerprint(ctx context.Context, id string) string { - return g.findKey(ctx, id).Fingerprint + k, found := g.findKey(ctx, id) + if !found { + return "" + } + return k.Fingerprint } // FormatKey formats the details of a key id @@ -55,7 +59,11 @@ func (g *GPG) Fingerprint(ctx context.Context, id string) string { // - EmailFromKey: {{ .Email }}. func (g *GPG) FormatKey(ctx context.Context, id, tpl string) string { if tpl == "" { - return g.findKey(ctx, id).OneLine() + k, found := g.findKey(ctx, id) + if !found { + return "" + } + return k.OneLine() } tmpl, err := template.New(tpl).Parse(tpl) @@ -63,8 +71,14 @@ func (g *GPG) FormatKey(ctx context.Context, id, tpl string) string { return "" } + var gid gpg.Identity + k, found := g.findKey(ctx, id) + if found { + gid = k.Identity() + } + buf := &bytes.Buffer{} - if err := tmpl.Execute(buf, g.findKey(ctx, id).Identity()); err != nil { + if err := tmpl.Execute(buf, gid); err != nil { debug.Log("Failed to render template %q: %s", tpl, err) return "" } diff --git a/internal/store/root/recipients.go b/internal/store/root/recipients.go index 352959f971..cdb38e6ccb 100644 --- a/internal/store/root/recipients.go +++ b/internal/store/root/recipients.go @@ -34,6 +34,9 @@ func (r *Store) RemoveRecipient(ctx context.Context, store, rec string) error { func (r *Store) addRecipient(ctx context.Context, prefix string, root *tree.Root, recp string, pretty bool) error { sub, _ := r.getStore(prefix) key := fmt.Sprintf("%s (missing public key)", recp) + if v := sub.Crypto().FormatKey(ctx, recp, ""); v != "" { + key = v + } kl, err := sub.Crypto().FindRecipients(ctx, recp) if err == nil { if len(kl) > 0 {