Skip to content

Commit

Permalink
Allow subkeys
Browse files Browse the repository at this point in the history
This commit removes some erroneus extra logic that did prevent
us from passing through subkeys.

Fixes gopasspw#1841
Fixes gopasspw#1842

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz committed Mar 21, 2021
1 parent 7dcc964 commit 2beb61d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
8 changes: 4 additions & 4 deletions internal/action/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ func (s *Action) RecipientsAdd(c *cli.Context) error {
// select recipient
recipients := []string(c.Args().Slice())
if len(recipients) < 1 {
debug.Log("no recipients given, asking for selection")
r, err := s.recipientsSelectForAdd(ctx, store)
if err != nil {
return err
}
recipients = r
}

debug.Log("adding recipients: %+v", recipients)
for _, r := range recipients {
keys, err := crypto.FindRecipients(ctx, r)
if err != nil {
Expand All @@ -109,11 +111,9 @@ func (s *Action) RecipientsAdd(c *cli.Context) error {
}

recp := r
if len(keys) > 0 {
recp = crypto.Fingerprint(ctx, keys[0])
}
debug.Log("found recipients for %q: %+v", r, keys)

if !termio.AskForConfirmation(ctx, fmt.Sprintf("Do you want to add %q as a recipient to the store %q?", crypto.FormatKey(ctx, recp, ""), store)) {
if !termio.AskForConfirmation(ctx, fmt.Sprintf("Do you want to add %q (key %q) as a recipient to the store %q?", crypto.FormatKey(ctx, recp, ""), recp, store)) {
continue
}

Expand Down
10 changes: 5 additions & 5 deletions internal/action/recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func TestRecipients(t *testing.T) {
t.Run("print recipients tree", func(t *testing.T) {
defer buf.Reset()
assert.NoError(t, act.RecipientsPrint(gptest.CliCtx(ctx, t)))
want := `Hint: run 'gopass sync' to import any missing public keys
gopass
└── 0xDEADBEEF

`
hint := `Hint: run 'gopass sync' to import any missing public keys`
want := `gopass
└── 0xDEADBEEF`

assert.Equal(t, want, buf.String())
assert.Contains(t, buf.String(), hint)
assert.Contains(t, buf.String(), want)
})

t.Run("complete recipients", func(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions internal/backend/crypto/gpg/cli/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ func (g *GPG) FindRecipients(ctx context.Context, search ...string) ([]string, e
if err != nil || kl == nil {
return nil, err
}

recp := kl.UseableKeys(gpg.IsAlwaysTrust(ctx)).Recipients()
if gpg.IsAlwaysTrust(ctx) {
return kl.Recipients(), nil
recp = kl.Recipients()
}
return kl.UseableKeys(gpg.IsAlwaysTrust(ctx)).Recipients(), nil

debug.Log("found useable keys for %+v: %+v (all: %+v)", search, recp, kl.Recipients())
return recp, nil
}

// ListIdentities returns a parsed list of GPG secret keys
Expand Down
3 changes: 3 additions & 0 deletions internal/backend/crypto/gpg/key_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func (kl KeyList) Recipients() []string {
sort.Sort(kl)
for _, k := range kl {
l = append(l, k.ID())
for sid := range k.SubKeys {
l = append(l, sid)
}
}
return l
}
Expand Down
11 changes: 9 additions & 2 deletions internal/backend/crypto/gpg/key_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ func TestKeyList(t *testing.T) {
assert.Equal(t, []string{
"0x62AF4031C82E0019",
"0x62AF4031C82E2019",
"0xDEADBEEF",
"0x62AF4031C82E0039",
}, kl.Recipients())
assert.Equal(t, []string{"0x62AF4031C82E0019", "0x62AF4031C82E0039"}, kl.UseableKeys(false).Recipients())
assert.Equal(t, []string{"0x62AF4031C82E2019"}, kl.UnusableKeys(false).Recipients())
assert.Equal(t, []string{
"0x62AF4031C82E0019",
"0x62AF4031C82E0039",
}, kl.UseableKeys(false).Recipients())
assert.Equal(t, []string{
"0x62AF4031C82E2019",
"0xDEADBEEF",
}, kl.UnusableKeys(false).Recipients())

// search by email
k, err := kl.FindKey("jim.doe@example.org")
Expand Down
15 changes: 4 additions & 11 deletions internal/store/leaf/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (s *Store) AddRecipient(ctx context.Context, id string) error {
return fmt.Errorf("failed to read recipient list: %w", err)
}

debug.Log("new recipient: %q - existing: %+v", id, rs)
for _, k := range rs {
if k == id {
return fmt.Errorf("recipient already in store")
Expand Down Expand Up @@ -174,17 +175,9 @@ func (s *Store) getRecipients(ctx context.Context, idf string) ([]string, error)
return nil, fmt.Errorf("failed to get recipients from %q: %w", idf, err)
}

rawRecps := recipients.Unmarshal(buf)
finalRecps := make([]string, 0, len(rawRecps))
for _, r := range rawRecps {
fp := s.crypto.Fingerprint(ctx, r)
if fp == "" {
fp = r
}
finalRecps = append(finalRecps, fp)
}
sort.Strings(finalRecps)
return finalRecps, nil
recps := recipients.Unmarshal(buf)
sort.Strings(recps)
return recps, nil
}

type keyExporter interface {
Expand Down

0 comments on commit 2beb61d

Please sign in to comment.