Skip to content

Commit

Permalink
Show public key identities before importing (#427)
Browse files Browse the repository at this point in the history
Fixes #425
  • Loading branch information
dominikschulz authored Oct 26, 2017
1 parent b61bee4 commit 3d639a5
Show file tree
Hide file tree
Showing 58 changed files with 14,160 additions and 5 deletions.
4 changes: 2 additions & 2 deletions action/clihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ func (s *Action) askForPassword(ctx context.Context, name string, askFn func(con
}

// AskForKeyImport asks for permissions to import the named key
func (s *Action) AskForKeyImport(ctx context.Context, key string) bool {
func (s *Action) AskForKeyImport(ctx context.Context, key string, names []string) bool {
if ctxutil.IsAlwaysYes(ctx) {
return true
}
if !ctxutil.IsInteractive(ctx) {
return false
}

ok, err := s.askForBool(ctx, fmt.Sprintf("Do you want to import the public key '%s' into your keyring?", key), false)
ok, err := s.askForBool(ctx, fmt.Sprintf("Do you want to import the public key '%s' (Names: %+v) into your keyring?", key, names), false)
if err != nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type RecipientCallback func(context.Context, string, []string) ([]string, error)

// ImportCallback is a callback to ask the user if he wants to import
// a certain recipients public key into his keystore
type ImportCallback func(context.Context, string) bool
type ImportCallback func(context.Context, string, []string) bool

// FsckCallback is a callback to ask the user to confirm certain fsck
// corrective actions
Expand Down
2 changes: 1 addition & 1 deletion store/sub/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func HasImportFunc(ctx context.Context) bool {
func GetImportFunc(ctx context.Context) store.ImportCallback {
imf, ok := ctx.Value(ctxKeyImportFunc).(store.ImportCallback)
if !ok || imf == nil {
return func(context.Context, string) bool {
return func(context.Context, string, []string) bool {
return true
}
}
Expand Down
39 changes: 38 additions & 1 deletion store/sub/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package sub

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/blang/semver"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
"golang.org/x/crypto/openpgp"
)

// GPGVersion returns parsed GPG version information
Expand Down Expand Up @@ -38,10 +40,17 @@ func (s *Store) ImportMissingPublicKeys(ctx context.Context) error {
continue
}

// get info about this public key
names, err := s.decodePublicKey(ctx, r)
if err != nil {
out.Red(ctx, "[%s] Failed to decode public key %s: %s", s.alias, r, err)
continue
}

// we need to ask the user before importing
// any key material into his keyring!
if imf := GetImportFunc(ctx); imf != nil {
if !imf(ctx, r) {
if !imf(ctx, r, names) {
continue
}
}
Expand Down Expand Up @@ -90,6 +99,34 @@ func (s *Store) exportPublicKey(ctx context.Context, r string) (string, error) {
return filename, nil
}

func (s *Store) decodePublicKey(ctx context.Context, r string) ([]string, error) {
filename := filepath.Join(s.path, keyDir, r)
if !fsutil.IsFile(filename) {
return nil, errors.Errorf("Public Key %s not found at %s", r, filename)
}

fh, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func() {
_ = fh.Close()
}()

el, err := openpgp.ReadArmoredKeyRing(fh)
if err != nil {
return nil, err
}
if len(el) != 1 {
return nil, fmt.Errorf("Public Key must contain exactly one Entity")
}
names := make([]string, 0, len(el[0].Identities))
for _, v := range el[0].Identities {
names = append(names, v.Name)
}
return names, nil
}

// import an public key into the default keyring
func (s *Store) importPublicKey(ctx context.Context, r string) error {
filename := filepath.Join(s.path, keyDir, r)
Expand Down
Loading

0 comments on commit 3d639a5

Please sign in to comment.