2015-08-25 21:24:19 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
2015-12-16 21:56:15 +00:00
|
|
|
"reflect"
|
2015-08-25 21:24:19 +00:00
|
|
|
"regexp"
|
2016-01-09 03:21:41 +00:00
|
|
|
"sort"
|
2015-08-25 21:24:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-01-09 02:21:02 +00:00
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
2015-08-25 21:24:19 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
|
2016-08-11 12:31:43 +00:00
|
|
|
"github.com/keybase/go-crypto/openpgp"
|
|
|
|
"github.com/keybase/go-crypto/openpgp/packet"
|
2015-08-25 21:24:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getPubKeyFiles(t *testing.T) (string, []string, error) {
|
|
|
|
tempDir, err := ioutil.TempDir("", "vault-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating temporary directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pubFiles := []string{
|
|
|
|
tempDir + "/pubkey1",
|
|
|
|
tempDir + "/pubkey2",
|
|
|
|
tempDir + "/pubkey3",
|
2016-01-18 22:01:52 +00:00
|
|
|
tempDir + "/aapubkey1",
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
|
|
|
decoder := base64.StdEncoding
|
2016-01-09 02:21:02 +00:00
|
|
|
pub1Bytes, err := decoder.DecodeString(pgpkeys.TestPubKey1)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding bytes for public key 1: %s", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(pubFiles[0], pub1Bytes, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error writing pub key 1 to temp file: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
pub2Bytes, err := decoder.DecodeString(pgpkeys.TestPubKey2)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding bytes for public key 2: %s", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(pubFiles[1], pub2Bytes, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error writing pub key 2 to temp file: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
pub3Bytes, err := decoder.DecodeString(pgpkeys.TestPubKey3)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding bytes for public key 3: %s", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(pubFiles[2], pub3Bytes, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error writing pub key 3 to temp file: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
err = ioutil.WriteFile(pubFiles[3], []byte(pgpkeys.TestAAPubKey1), 0755)
|
2016-01-18 22:01:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error writing aa pub key 1 to temp file: %s", err)
|
|
|
|
}
|
2015-08-25 21:24:19 +00:00
|
|
|
|
|
|
|
return tempDir, pubFiles, nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 21:56:15 +00:00
|
|
|
func parseDecryptAndTestUnsealKeys(t *testing.T,
|
|
|
|
input, rootToken string,
|
|
|
|
fingerprints bool,
|
2016-01-09 03:21:41 +00:00
|
|
|
backupKeys map[string][]string,
|
2016-08-15 20:01:15 +00:00
|
|
|
backupKeysB64 map[string][]string,
|
2015-12-16 21:56:15 +00:00
|
|
|
core *vault.Core) {
|
2016-08-15 20:01:15 +00:00
|
|
|
|
2015-08-25 21:24:19 +00:00
|
|
|
decoder := base64.StdEncoding
|
2016-01-09 02:21:02 +00:00
|
|
|
priv1Bytes, err := decoder.DecodeString(pgpkeys.TestPrivKey1)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding bytes for private key 1: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
priv2Bytes, err := decoder.DecodeString(pgpkeys.TestPrivKey2)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding bytes for private key 2: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
priv3Bytes, err := decoder.DecodeString(pgpkeys.TestPrivKey3)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding bytes for private key 3: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
privBytes := [][]byte{
|
|
|
|
priv1Bytes,
|
|
|
|
priv2Bytes,
|
|
|
|
priv3Bytes,
|
|
|
|
}
|
|
|
|
|
2016-09-01 16:59:15 +00:00
|
|
|
testFunc := func(bkeys map[string][]string) {
|
2016-08-15 20:01:15 +00:00
|
|
|
var re *regexp.Regexp
|
2015-12-16 21:56:15 +00:00
|
|
|
if fingerprints {
|
2016-09-01 16:59:15 +00:00
|
|
|
re, err = regexp.Compile("\\s*Key\\s+\\d+\\s+fingerprint:\\s+([0-9a-fA-F]+);\\s+value:\\s+(.*)")
|
2015-12-16 21:56:15 +00:00
|
|
|
} else {
|
2016-09-01 16:59:15 +00:00
|
|
|
re, err = regexp.Compile("\\s*Key\\s+\\d+:\\s+(.*)")
|
2015-12-16 21:56:15 +00:00
|
|
|
}
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
2016-08-15 20:01:15 +00:00
|
|
|
t.Fatalf("Error compiling regex: %s", err)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2016-08-15 20:01:15 +00:00
|
|
|
matches := re.FindAllStringSubmatch(input, -1)
|
|
|
|
if len(matches) != 4 {
|
|
|
|
t.Fatalf("Unexpected number of keys returned, got %d, matches was \n\n%#v\n\n, input was \n\n%s\n\n", len(matches), matches, input)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2016-08-15 20:01:15 +00:00
|
|
|
|
|
|
|
encodedKeys := []string{}
|
|
|
|
matchedFingerprints := []string{}
|
|
|
|
for _, tuple := range matches {
|
|
|
|
if fingerprints {
|
|
|
|
if len(tuple) != 3 {
|
|
|
|
t.Fatalf("Key not found: %#v", tuple)
|
|
|
|
}
|
|
|
|
matchedFingerprints = append(matchedFingerprints, tuple[1])
|
|
|
|
encodedKeys = append(encodedKeys, tuple[2])
|
|
|
|
} else {
|
|
|
|
if len(tuple) != 2 {
|
|
|
|
t.Fatalf("Key not found: %#v", tuple)
|
|
|
|
}
|
|
|
|
encodedKeys = append(encodedKeys, tuple[1])
|
|
|
|
}
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:01:15 +00:00
|
|
|
if bkeys != nil && len(matchedFingerprints) != 0 {
|
|
|
|
testMap := map[string][]string{}
|
|
|
|
for i, v := range matchedFingerprints {
|
|
|
|
testMap[v] = append(testMap[v], encodedKeys[i])
|
|
|
|
sort.Strings(testMap[v])
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(testMap, bkeys) {
|
|
|
|
t.Fatalf("test map and backup map do not match, test map is\n%#v\nbackup map is\n%#v", testMap, bkeys)
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 21:24:19 +00:00
|
|
|
|
2016-08-15 20:01:15 +00:00
|
|
|
unsealKeys := []string{}
|
|
|
|
ptBuf := bytes.NewBuffer(nil)
|
|
|
|
for i, privKeyBytes := range privBytes {
|
|
|
|
if i > 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ptBuf.Reset()
|
|
|
|
entity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(privKeyBytes)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error parsing private key %d: %s", i, err)
|
|
|
|
}
|
|
|
|
var keyBytes []byte
|
2016-09-01 16:59:15 +00:00
|
|
|
keyBytes, err = base64.StdEncoding.DecodeString(encodedKeys[i])
|
2016-08-15 20:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decoding key %d: %s", i, err)
|
|
|
|
}
|
|
|
|
entityList := &openpgp.EntityList{entity}
|
|
|
|
md, err := openpgp.ReadMessage(bytes.NewBuffer(keyBytes), entityList, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error decrypting with key %d (%s): %s", i, encodedKeys[i], err)
|
|
|
|
}
|
|
|
|
ptBuf.ReadFrom(md.UnverifiedBody)
|
|
|
|
unsealKeys = append(unsealKeys, ptBuf.String())
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2016-08-15 20:01:15 +00:00
|
|
|
|
|
|
|
err = core.Seal(rootToken)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
2016-08-15 20:01:15 +00:00
|
|
|
t.Fatalf("Error sealing vault with provided root token: %s", err)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2016-08-15 20:01:15 +00:00
|
|
|
|
|
|
|
for i, unsealKey := range unsealKeys {
|
|
|
|
unsealBytes, err := hex.DecodeString(unsealKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error hex decoding unseal key %s: %s", unsealKey, err)
|
|
|
|
}
|
|
|
|
unsealed, err := core.Unseal(unsealBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error using unseal key %s: %s", unsealKey, err)
|
|
|
|
}
|
|
|
|
if i >= 2 && !unsealed {
|
|
|
|
t.Fatalf("Error: Provided two unseal keys but core is not unsealed")
|
|
|
|
}
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 16:59:15 +00:00
|
|
|
testFunc(backupKeysB64)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|