Skip to content

Commit c391b07

Browse files
[bugfix] Do not remove unused keys on import by default (#2657)
The cleanup during import is currently buggy on some scenarios so as a workaround we'll disable auto-cleanup by default and introduce `recipients.remove-extra-keys` to allow users to turn it back on. See GH-2620 Signed-off-by: Dominik Schulz <[email protected]>
1 parent b0c5ce7 commit c391b07

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

docs/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ This is a list of available options:
115115
| `mounts.path` | `string` | Path to the root store. | `$XDG_DATA_HOME/gopass/stores/root` |
116116
| `recipients.check` | `bool` | Check recipients hash. | `false` |
117117
| `recipients.hash` | `string` | SHA256 hash of the recipients file. Used to notify the user when the recipients files change. | `` |
118+
| `recipients.remove-extra-keys` | `bool` | Remove extra recipients during key import. | `false` |
118119
| `show.post-hook` | `string` | This hook is run right after displaying a secret with `gopass show` | `None` |
119120
| `updater.check` | `bool` | Check for updates when running `gopass version` | `true` |
120121
| `output.internal-pager` | `bool` | Use the internal pager `ov` | `false` |

internal/store/leaf/recipients.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,42 @@ func (s *Store) UpdateExportedPublicKeys(ctx context.Context, rs []string) (bool
327327
}
328328

329329
// add any missing keys
330+
failed, exported := s.addMissingKeys(ctx, exp, recipients)
331+
332+
// remove any extra key files
333+
// TODO(GH-2620): Temporarily disabled by default until we fix the
334+
// key cleanup.
335+
if config.Bool(ctx, "recipients.remove-extra-keys") {
336+
f, e := s.removeExtraKeys(ctx, recipients)
337+
failed = failed || f
338+
exported = exported || e
339+
}
340+
341+
if exported && ctxutil.IsGitCommit(ctx) {
342+
if err := s.storage.Commit(ctx, "Updated exported Public Keys"); err != nil {
343+
switch {
344+
case errors.Is(err, store.ErrGitNothingToCommit):
345+
debug.Log("nothing to commit: %s", err)
346+
case errors.Is(err, store.ErrGitNotInit):
347+
debug.Log("git not initialized: %s", err)
348+
default:
349+
failed = true
350+
351+
out.Errorf(ctx, "Failed to git commit: %s", err)
352+
}
353+
}
354+
}
355+
356+
if failed {
357+
return exported, fmt.Errorf("some keys failed")
358+
}
359+
360+
return exported, nil
361+
}
362+
363+
func (s *Store) addMissingKeys(ctx context.Context, exp keyExporter, recipients map[string]bool) (bool, bool) {
330364
var failed, exported bool
365+
331366
for r := range recipients {
332367
if r == "" {
333368
continue
@@ -358,7 +393,12 @@ func (s *Store) UpdateExportedPublicKeys(ctx context.Context, rs []string) (bool
358393
}
359394
}
360395

361-
// remove any extra key files
396+
return failed, exported
397+
}
398+
399+
func (s *Store) removeExtraKeys(ctx context.Context, recipients map[string]bool) (bool, bool) {
400+
var failed, exported bool
401+
362402
keys, err := s.storage.List(ctx, keyDir)
363403
if err != nil {
364404
failed = true
@@ -397,26 +437,7 @@ func (s *Store) UpdateExportedPublicKeys(ctx context.Context, rs []string) (bool
397437
debug.Log("Removed extra key %s", key)
398438
}
399439

400-
if exported && ctxutil.IsGitCommit(ctx) {
401-
if err := s.storage.Commit(ctx, "Updated exported Public Keys"); err != nil {
402-
switch {
403-
case errors.Is(err, store.ErrGitNothingToCommit):
404-
debug.Log("nothing to commit: %s", err)
405-
case errors.Is(err, store.ErrGitNotInit):
406-
debug.Log("git not initialized: %s", err)
407-
default:
408-
failed = true
409-
410-
out.Errorf(ctx, "Failed to git commit: %s", err)
411-
}
412-
}
413-
}
414-
415-
if failed {
416-
return exported, fmt.Errorf("some keys failed")
417-
}
418-
419-
return exported, nil
440+
return failed, exported
420441
}
421442

422443
type recipientMarshaler interface {

0 commit comments

Comments
 (0)