2016-01-01 01:43:24 +00:00
|
|
|
package pgpkeys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2016-08-11 12:31:43 +00:00
|
|
|
"github.com/keybase/go-crypto/openpgp"
|
2016-01-01 01:43:24 +00:00
|
|
|
)
|
|
|
|
|
2016-01-04 16:18:04 +00:00
|
|
|
const (
|
|
|
|
kbPrefix = "keybase:"
|
|
|
|
)
|
|
|
|
|
2016-01-01 01:43:24 +00:00
|
|
|
// FetchKeybasePubkeys fetches public keys from Keybase given a set of
|
2016-01-04 16:18:04 +00:00
|
|
|
// usernames, which are derived from correctly formatted input entries. It
|
|
|
|
// doesn't use their client code due to both the API and the fact that it is
|
|
|
|
// considered alpha and probably best not to rely on it. The keys are returned
|
|
|
|
// as base64-encoded strings.
|
|
|
|
func FetchKeybasePubkeys(input []string) (map[string]string, error) {
|
2016-01-01 01:43:24 +00:00
|
|
|
client := cleanhttp.DefaultClient()
|
|
|
|
if client == nil {
|
|
|
|
return nil, fmt.Errorf("unable to create an http client")
|
|
|
|
}
|
|
|
|
|
2016-01-04 16:18:04 +00:00
|
|
|
if len(input) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
usernames := make([]string, 0, len(input))
|
|
|
|
for _, v := range input {
|
|
|
|
if strings.HasPrefix(v, kbPrefix) {
|
|
|
|
usernames = append(usernames, strings.TrimPrefix(v, kbPrefix))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:43:24 +00:00
|
|
|
if len(usernames) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-01-04 16:18:04 +00:00
|
|
|
ret := make(map[string]string, len(usernames))
|
|
|
|
url := fmt.Sprintf("https://keybase.io/_/api/1.0/user/lookup.json?usernames=%s&fields=public_keys", strings.Join(usernames, ","))
|
|
|
|
resp, err := client.Get(url)
|
2016-01-01 01:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-02-23 19:47:25 +00:00
|
|
|
type PublicKeys struct {
|
2016-01-01 01:43:24 +00:00
|
|
|
Primary struct {
|
|
|
|
Bundle string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:47:25 +00:00
|
|
|
type LThem struct {
|
|
|
|
PublicKeys `json:"public_keys"`
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 19:47:25 +00:00
|
|
|
type KbResp struct {
|
2016-01-01 01:43:24 +00:00
|
|
|
Status struct {
|
|
|
|
Name string
|
|
|
|
}
|
2018-02-23 19:47:25 +00:00
|
|
|
Them []LThem
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 19:47:25 +00:00
|
|
|
out := &KbResp{
|
|
|
|
Them: []LThem{},
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 16:25:40 +00:00
|
|
|
if err := jsonutil.DecodeJSONFromReader(resp.Body, out); err != nil {
|
2016-01-01 01:43:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Status.Name != "OK" {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("got non-OK response: %q", out.Status.Name)
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 18:56:45 +00:00
|
|
|
missingNames := make([]string, 0, len(usernames))
|
2016-01-01 01:43:24 +00:00
|
|
|
var keyReader *bytes.Reader
|
|
|
|
serializedEntity := bytes.NewBuffer(nil)
|
|
|
|
for i, themVal := range out.Them {
|
2016-01-04 18:56:45 +00:00
|
|
|
if themVal.Primary.Bundle == "" {
|
|
|
|
missingNames = append(missingNames, usernames[i])
|
|
|
|
continue
|
|
|
|
}
|
2016-01-04 16:18:04 +00:00
|
|
|
keyReader = bytes.NewReader([]byte(themVal.Primary.Bundle))
|
2016-01-01 01:43:24 +00:00
|
|
|
entityList, err := openpgp.ReadArmoredKeyRing(keyReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(entityList) != 1 {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("primary key could not be parsed for user %q", usernames[i])
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
if entityList[0] == nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("primary key was nil for user %q", usernames[i])
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializedEntity.Reset()
|
|
|
|
err = entityList[0].Serialize(serializedEntity)
|
|
|
|
if err != nil {
|
2021-05-07 12:53:20 +00:00
|
|
|
return nil, fmt.Errorf("error serializing entity for user %q: %w", usernames[i], err)
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
2016-01-04 16:18:04 +00:00
|
|
|
|
|
|
|
// The API returns values in the same ordering requested, so this should properly match
|
|
|
|
ret[kbPrefix+usernames[i]] = base64.StdEncoding.EncodeToString(serializedEntity.Bytes())
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 18:56:45 +00:00
|
|
|
if len(missingNames) > 0 {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("unable to fetch keys for user(s) %q from keybase", strings.Join(missingNames, ","))
|
2016-01-04 18:56:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 01:43:24 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|