Make pgpkeys helper implement our flags interface

This commit is contained in:
Seth Vargo 2017-09-04 23:57:55 -04:00
parent 06580ebd61
commit 844fe0a507
No known key found for this signature in database
GPG Key ID: C921994F9C27E0FF
1 changed files with 60 additions and 18 deletions

View File

@ -11,48 +11,90 @@ import (
"github.com/keybase/go-crypto/openpgp" "github.com/keybase/go-crypto/openpgp"
) )
// PGPPubKeyFiles implements the flag.Value interface and allows // PubKeyFileFlag implements flag.Value and command.Example to receive exactly
// parsing and reading a list of pgp public key files // one PGP or keybase key via a flag.
type PubKeyFileFlag string
func (p *PubKeyFileFlag) String() string { return string(*p) }
func (p *PubKeyFileFlag) Set(val string) error {
if p != nil && *p != "" {
return errors.New("can only be specified once")
}
keys, err := ParsePGPKeys(strings.Split(val, ","))
if err != nil {
return err
}
if len(keys) > 1 {
return errors.New("can only specify one pgp key")
}
*p = PubKeyFileFlag(keys[0])
return nil
}
func (p *PubKeyFileFlag) Example() string { return "keybase:user" }
// PGPPubKeyFiles implements the flag.Value interface and allows parsing and
// reading a list of PGP public key files.
type PubKeyFilesFlag []string type PubKeyFilesFlag []string
func (p *PubKeyFilesFlag) String() string { func (p *PubKeyFilesFlag) String() string {
return fmt.Sprint(*p) return fmt.Sprint(*p)
} }
func (p *PubKeyFilesFlag) Set(value string) error { func (p *PubKeyFilesFlag) Set(val string) error {
if len(*p) > 0 { if len(*p) > 0 {
return errors.New("pgp-keys can only be specified once") return errors.New("can only be specified once")
} }
splitValues := strings.Split(value, ",") keys, err := ParsePGPKeys(strings.Split(val, ","))
keybaseMap, err := FetchKeybasePubkeys(splitValues)
if err != nil { if err != nil {
return err return err
} }
// Now go through the actual flag, and substitute in resolved keybase *p = PubKeyFilesFlag(keys)
// entries where appropriate return nil
for _, keyfile := range splitValues { }
func (p *PubKeyFilesFlag) Example() string { return "keybase:user1, keybase:user2, ..." }
// ParsePGPKeys takes a list of PGP keys and parses them either using keybase
// or reading them from disk and returns the "expanded" list of pgp keys in
// the same order.
func ParsePGPKeys(keyfiles []string) ([]string, error) {
keys := make([]string, len(keyfiles))
keybaseMap, err := FetchKeybasePubkeys(keyfiles)
if err != nil {
return nil, err
}
for i, keyfile := range keyfiles {
keyfile = strings.TrimSpace(keyfile)
if strings.HasPrefix(keyfile, kbPrefix) { if strings.HasPrefix(keyfile, kbPrefix) {
key := keybaseMap[keyfile] key, ok := keybaseMap[keyfile]
if key == "" { if !ok || key == "" {
return fmt.Errorf("key for keybase user %s was not found in the map", strings.TrimPrefix(keyfile, kbPrefix)) return nil, fmt.Errorf("keybase user %q not found", strings.TrimPrefix(keyfile, kbPrefix))
} }
*p = append(*p, key) keys[i] = key
continue continue
} }
pgpStr, err := ReadPGPFile(keyfile) pgpStr, err := ReadPGPFile(keyfile)
if err != nil { if err != nil {
return err return nil, err
} }
keys[i] = pgpStr
*p = append(*p, pgpStr)
} }
return nil
return keys, nil
} }
// ReadPGPFile reads the given PGP file from disk.
func ReadPGPFile(path string) (string, error) { func ReadPGPFile(path string) (string, error) {
if path[0] == '@' { if path[0] == '@' {
path = path[1:] path = path[1:]