Skip to content

Commit

Permalink
Invalidate cache after generating new GPG key
Browse files Browse the repository at this point in the history
Fixes gopasspw#1693

RELEASE_NOTES=[BUGFIX] Invalidate GPG key list after generation

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz committed Jan 12, 2021
1 parent 8d227c9 commit a821478
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
20 changes: 11 additions & 9 deletions internal/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (s *Action) InitOnboarding(c *cli.Context) error {
return nil
}

ctx = out.AddPrefix(ctx, "[init] ")
debug.Log("Starting Onboarding Wizard - remote: %s - team: %s - create: %t - name: %s - email: %s", remote, team, create, ctxutil.GetUsername(ctx), ctxutil.GetEmail(ctx))

crypto := s.getCryptoFor(ctx, team)
Expand Down Expand Up @@ -285,7 +284,7 @@ https://github.com/gopasspw/gopass/blob/master/docs/entropy.md`)
out.Green(ctx, "-> OK")
out.Print(ctx, color.MagentaString("Passphrase: ")+color.HiGreenString(passphrase))

kl, err := crypto.ListIdentities(ctx)
kl, err := crypto.ListIdentities(gpg.WithUseCache(ctx, false))
if err != nil {
return errors.Wrapf(err, "failed to list private keys")
}
Expand All @@ -297,14 +296,17 @@ https://github.com/gopasspw/gopass/blob/master/docs/entropy.md`)
debug.Log("Private Keys: %+v", kl)
return errors.New("failed to create a useable key pair")
}
key := kl[0]
fn := key + ".pub.key"
pk, err := crypto.ExportPublicKey(ctx, key)
if err != nil {
return errors.Wrapf(err, "failed to export public key")

if want, err := termio.AskForBool(ctx, "Do you want to export your public key?", false); err == nil && want {
key := kl[0]
fn := key + ".pub.key"
pk, err := crypto.ExportPublicKey(ctx, key)
if err != nil {
return errors.Wrapf(err, "failed to export public key")
}
_ = ioutil.WriteFile(fn, pk, 06444)
out.Cyan(ctx, "Public key exported to '%s'", fn)
}
_ = ioutil.WriteFile(fn, pk, 06444)
out.Cyan(ctx, "Public key exported to '%s'", fn)
out.Green(ctx, "Done")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/crypto/gpg/cli/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func (g *GPG) listKeys(ctx context.Context, typ string, search ...string) (gpg.KeyList, error) {
args := []string{"--with-colons", "--with-fingerprint", "--fixed-list-mode", "--list-" + typ + "-keys"}
args = append(args, search...)
if e, found := g.listCache.Get(strings.Join(args, ",")); found {
if e, found := g.listCache.Get(strings.Join(args, ",")); found && gpg.UseCache(ctx) {
if ev, ok := e.(gpg.KeyList); ok {
return ev, nil
}
Expand Down
15 changes: 15 additions & 0 deletions internal/backend/crypto/gpg/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type contextKey int

const (
ctxKeyAlwaysTrust contextKey = iota
ctxKeyUseCache
)

// WithAlwaysTrust will return a context with the flag for always trust set
Expand All @@ -22,3 +23,17 @@ func IsAlwaysTrust(ctx context.Context) bool {
}
return bv
}

// WithUseCache returns a context with the value of NoCache set
func WithUseCache(ctx context.Context, nc bool) context.Context {
return context.WithValue(ctx, ctxKeyUseCache, nc)
}

// UseCache returns true if this request should ignore the cache
func UseCache(ctx context.Context) bool {
nc, ok := ctx.Value(ctxKeyUseCache).(bool)
if !ok {
return false
}
return nc
}

0 comments on commit a821478

Please sign in to comment.