open-vault/command/operator_rekey_test.go

518 lines
11 KiB
Go
Raw Normal View History

//go:build !race
2015-05-28 22:22:42 +00:00
package command
import (
2017-09-05 04:03:44 +00:00
"io"
"reflect"
"regexp"
2015-05-28 22:22:42 +00:00
"strings"
"testing"
2017-09-05 04:03:44 +00:00
"github.com/hashicorp/vault/api"
2015-05-28 22:22:42 +00:00
"github.com/mitchellh/cli"
)
2017-09-08 02:03:12 +00:00
func testOperatorRekeyCommand(tb testing.TB) (*cli.MockUi, *OperatorRekeyCommand) {
2017-09-05 04:03:44 +00:00
tb.Helper()
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
ui := cli.NewMockUi()
2017-09-08 02:03:12 +00:00
return ui, &OperatorRekeyCommand{
2017-09-05 04:03:44 +00:00
BaseCommand: &BaseCommand{
UI: ui,
},
}
}
2015-05-28 22:22:42 +00:00
2017-09-08 02:03:12 +00:00
func TestOperatorRekeyCommand_Run(t *testing.T) {
2017-09-05 04:03:44 +00:00
t.Parallel()
cases := []struct {
name string
args []string
out string
code int
}{
{
"pgp_keys_multi",
[]string{
"-init",
"-pgp-keys", "keybase:hashicorp",
"-pgp-keys", "keybase:jefferai",
},
"can only be specified once",
1,
},
{
"key_shares_pgp_less",
[]string{
"-init",
"-key-shares", "10",
"-pgp-keys", "keybase:jefferai,keybase:sethvargo",
2017-01-17 20:43:10 +00:00
},
2017-09-05 04:03:44 +00:00
"incorrect number",
2,
},
{
"key_shares_pgp_more",
[]string{
"-init",
"-key-shares", "1",
"-pgp-keys", "keybase:jefferai,keybase:sethvargo",
},
"incorrect number",
2,
},
}
t.Run("validations", func(t *testing.T) {
t.Parallel()
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code := cmd.Run(tc.args)
if code != tc.code {
t.Errorf("expected %d to be %d", code, tc.code)
}
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, tc.out) {
t.Errorf("expected %q to contain %q", combined, tc.out)
}
})
2017-01-17 20:43:10 +00:00
}
2017-09-05 04:03:44 +00:00
})
2017-01-17 20:43:10 +00:00
2017-09-05 04:03:44 +00:00
t.Run("status", func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
// Verify the non-init response
code := cmd.Run([]string{
"-status",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
2017-01-17 20:43:10 +00:00
}
2017-09-05 04:03:44 +00:00
expected := "Nonce"
combined := ui.OutputWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
2017-01-17 20:43:10 +00:00
}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
// Now init to verify the init response
if _, err := client.Sys().RekeyInit(&api.RekeyInitRequest{
2017-09-05 04:03:44 +00:00
SecretShares: 1,
SecretThreshold: 1,
}); err != nil {
t.Fatal(err)
}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
// Verify the init response
2017-09-08 02:03:12 +00:00
ui, cmd = testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code = cmd.Run([]string{
"-status",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
expected = "Progress"
combined = ui.OutputWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
})
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
t.Run("cancel", func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
// Initialize a rekey
if _, err := client.Sys().RekeyInit(&api.RekeyInitRequest{
2017-09-05 04:03:44 +00:00
SecretShares: 1,
SecretThreshold: 1,
}); err != nil {
t.Fatal(err)
2017-01-17 20:43:10 +00:00
}
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code := cmd.Run([]string{
"-cancel",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d", code, exp)
2017-01-17 20:43:10 +00:00
}
2017-09-05 04:03:44 +00:00
expected := "Success! Canceled rekeying"
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
2017-01-17 20:43:10 +00:00
}
2015-05-28 22:22:42 +00:00
status, err := client.Sys().GenerateRootStatus()
2017-09-05 04:03:44 +00:00
if err != nil {
t.Fatal(err)
}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
if status.Started {
t.Errorf("expected status to be canceled: %#v", status)
}
})
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
t.Run("init", func(t *testing.T) {
t.Parallel()
2017-01-17 20:43:10 +00:00
2017-09-05 04:03:44 +00:00
client, closer := testVaultServer(t)
defer closer()
2015-05-28 22:22:42 +00:00
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
code := cmd.Run([]string{
"-init",
"-key-shares", "1",
"-key-threshold", "1",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
expected := "Nonce"
combined := ui.OutputWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
2015-05-28 22:22:42 +00:00
status, err := client.Sys().RekeyStatus()
2017-09-05 04:03:44 +00:00
if err != nil {
t.Fatal(err)
}
if !status.Started {
t.Errorf("expected status to be started: %#v", status)
}
})
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
t.Run("init_pgp", func(t *testing.T) {
t.Parallel()
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
pgpKey := "keybase:hashicorp"
pgpFingerprints := []string{"c874011f0ab405110d02105534365d9472d7468f"}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
client, closer := testVaultServer(t)
defer closer()
2015-05-28 22:22:42 +00:00
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
code := cmd.Run([]string{
"-init",
"-key-shares", "1",
"-key-threshold", "1",
"-pgp-keys", pgpKey,
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
expected := "Nonce"
combined := ui.OutputWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
2015-05-28 22:22:42 +00:00
status, err := client.Sys().RekeyStatus()
2017-09-05 04:03:44 +00:00
if err != nil {
t.Fatal(err)
}
if !status.Started {
t.Errorf("expected status to be started: %#v", status)
}
if !reflect.DeepEqual(status.PGPFingerprints, pgpFingerprints) {
t.Errorf("expected %#v to be %#v", status.PGPFingerprints, pgpFingerprints)
}
})
2015-05-28 22:22:42 +00:00
2017-09-05 04:03:44 +00:00
t.Run("provide_arg", func(t *testing.T) {
t.Parallel()
2017-09-05 04:03:44 +00:00
client, keys, closer := testVaultServerUnseal(t)
defer closer()
2017-09-05 04:03:44 +00:00
// Initialize a rekey
status, err := client.Sys().RekeyInit(&api.RekeyInitRequest{
2017-09-05 04:03:44 +00:00
SecretShares: 1,
SecretThreshold: 1,
})
if err != nil {
t.Fatal(err)
}
nonce := status.Nonce
// Supply the first n-1 unseal keys
for _, key := range keys[:len(keys)-1] {
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code := cmd.Run([]string{
"-nonce", nonce,
key,
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
}
2015-12-16 21:56:15 +00:00
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
2017-09-05 04:03:44 +00:00
code := cmd.Run([]string{
"-nonce", nonce,
keys[len(keys)-1], // the last unseal key
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
2017-09-05 04:03:44 +00:00
re := regexp.MustCompile(`Key 1: (.+)`)
output := ui.OutputWriter.String()
match := re.FindAllStringSubmatch(output, -1)
if len(match) < 1 || len(match[0]) < 2 {
t.Fatalf("bad match: %#v", match)
}
2017-09-05 04:03:44 +00:00
// Grab the unseal key and try to unseal
unsealKey := match[0][1]
if err := client.Sys().Seal(); err != nil {
t.Fatal(err)
}
sealStatus, err := client.Sys().Unseal(unsealKey)
2017-09-05 04:03:44 +00:00
if err != nil {
t.Fatal(err)
}
if sealStatus.Sealed {
t.Errorf("expected vault to be unsealed: %#v", sealStatus)
}
})
2017-09-05 04:03:44 +00:00
t.Run("provide_stdin", func(t *testing.T) {
t.Parallel()
client, keys, closer := testVaultServerUnseal(t)
defer closer()
// Initialize a rekey
status, err := client.Sys().RekeyInit(&api.RekeyInitRequest{
2017-09-05 04:03:44 +00:00
SecretShares: 1,
SecretThreshold: 1,
})
if err != nil {
t.Fatal(err)
}
nonce := status.Nonce
// Supply the first n-1 unseal keys
for _, key := range keys[:len(keys)-1] {
stdinR, stdinW := io.Pipe()
go func() {
stdinW.Write([]byte(key))
stdinW.Close()
}()
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
cmd.testStdin = stdinR
code := cmd.Run([]string{
"-nonce", nonce,
"-",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
2017-01-17 20:43:10 +00:00
}
2015-12-16 21:56:15 +00:00
2017-09-05 04:03:44 +00:00
stdinR, stdinW := io.Pipe()
go func() {
stdinW.Write([]byte(keys[len(keys)-1])) // the last unseal key
stdinW.Close()
}()
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
cmd.testStdin = stdinR
code := cmd.Run([]string{
"-nonce", nonce,
"-",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
2017-01-17 20:43:10 +00:00
2017-09-05 04:03:44 +00:00
re := regexp.MustCompile(`Key 1: (.+)`)
output := ui.OutputWriter.String()
match := re.FindAllStringSubmatch(output, -1)
if len(match) < 1 || len(match[0]) < 2 {
t.Fatalf("bad match: %#v", match)
2017-01-17 20:43:10 +00:00
}
2017-09-05 04:03:44 +00:00
// Grab the unseal key and try to unseal
unsealKey := match[0][1]
if err := client.Sys().Seal(); err != nil {
t.Fatal(err)
2017-01-17 20:43:10 +00:00
}
sealStatus, err := client.Sys().Unseal(unsealKey)
2017-09-05 04:03:44 +00:00
if err != nil {
t.Fatal(err)
}
if sealStatus.Sealed {
t.Errorf("expected vault to be unsealed: %#v", sealStatus)
}
})
2017-09-05 04:03:44 +00:00
t.Run("backup", func(t *testing.T) {
t.Parallel()
2015-12-16 21:56:15 +00:00
2017-09-05 04:03:44 +00:00
pgpKey := "keybase:hashicorp"
// pgpFingerprints := []string{"c874011f0ab405110d02105534365d9472d7468f"}
2015-12-16 21:56:15 +00:00
2017-09-05 04:03:44 +00:00
client, keys, closer := testVaultServerUnseal(t)
defer closer()
2015-12-16 21:56:15 +00:00
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
2015-12-16 21:56:15 +00:00
2017-09-05 04:03:44 +00:00
code := cmd.Run([]string{
"-init",
"-key-shares", "1",
"-key-threshold", "1",
"-pgp-keys", pgpKey,
"-backup",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
// Get the status for the nonce
status, err := client.Sys().RekeyStatus()
2017-09-05 04:03:44 +00:00
if err != nil {
t.Fatal(err)
}
nonce := status.Nonce
var combined string
// Supply the unseal keys
for _, key := range keys {
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code := cmd.Run([]string{
"-nonce", nonce,
key,
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
// Append to our output string
combined += ui.OutputWriter.String()
}
re := regexp.MustCompile(`Key 1 fingerprint: (.+); value: (.+)`)
match := re.FindAllStringSubmatch(combined, -1)
if len(match) < 1 || len(match[0]) < 3 {
t.Fatalf("bad match: %#v", match)
}
// Grab the output fingerprint and encrypted key
fingerprint, encryptedKey := match[0][1], match[0][2]
// Get the backup
2017-09-08 02:03:12 +00:00
ui, cmd = testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code = cmd.Run([]string{
"-backup-retrieve",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, fingerprint) {
t.Errorf("expected %q to contain %q", output, fingerprint)
}
if !strings.Contains(output, encryptedKey) {
t.Errorf("expected %q to contain %q", output, encryptedKey)
}
// Delete the backup
2017-09-08 02:03:12 +00:00
ui, cmd = testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code = cmd.Run([]string{
"-backup-delete",
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
}
secret, err := client.Sys().RekeyRetrieveBackup()
2017-09-05 04:03:44 +00:00
if err == nil {
t.Errorf("expected error: %#v", secret)
}
})
t.Run("communication_failure", func(t *testing.T) {
t.Parallel()
client, closer := testVaultServerBad(t)
defer closer()
2017-09-08 02:03:12 +00:00
ui, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
cmd.client = client
code := cmd.Run([]string{
"secret/foo",
})
if exp := 2; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
expected := "Error getting rekey status: "
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
})
t.Run("no_tabs", func(t *testing.T) {
t.Parallel()
2017-09-08 02:03:12 +00:00
_, cmd := testOperatorRekeyCommand(t)
2017-09-05 04:03:44 +00:00
assertNoTabs(t, cmd)
})
}