Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not display (missing public key) for age identities #2166

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/backend/crypto/gpg/cli/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 18 additions & 4 deletions internal/backend/crypto/gpg/cli/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -55,16 +59,26 @@ 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)
if err != nil {
return ""
}

var gid gpg.Identity
k, found := g.findKey(ctx, id)
if found {
dominikschulz marked this conversation as resolved.
Show resolved Hide resolved
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 ""
}
Expand Down
3 changes: 3 additions & 0 deletions internal/store/root/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down