2015-03-13 18:18:42 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-08-25 16:32:45 +00:00
|
|
|
"os"
|
2015-03-13 18:18:42 +00:00
|
|
|
"reflect"
|
2015-08-25 21:24:19 +00:00
|
|
|
"regexp"
|
2015-03-13 18:18:42 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/http"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
core := vault.TestCore(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
init, err := core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
t.Fatal("should not be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-address", addr}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
init, err = core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !init {
|
|
|
|
t.Fatal("should be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
sealConf, err := core.SealConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
expected := &vault.SealConfig{
|
|
|
|
SecretShares: 5,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, sealConf) {
|
|
|
|
t.Fatalf("bad: %#v", sealConf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit_custom(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
core := vault.TestCore(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
init, err := core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
t.Fatal("should not be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"-key-shares", "7",
|
|
|
|
"-key-threshold", "3",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
init, err = core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !init {
|
|
|
|
t.Fatal("should be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
sealConf, err := core.SealConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
expected := &vault.SealConfig{
|
|
|
|
SecretShares: 7,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, sealConf) {
|
|
|
|
t.Fatalf("bad: %#v", sealConf)
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 16:32:45 +00:00
|
|
|
|
|
|
|
func TestInit_PGP(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
core := vault.TestCore(t)
|
|
|
|
ln, addr := http.TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
init, err := core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
t.Fatal("should not be initialized")
|
|
|
|
}
|
|
|
|
|
2015-08-25 21:24:19 +00:00
|
|
|
tempDir, pubFiles, err := getPubKeyFiles(t)
|
2015-08-25 16:32:45 +00:00
|
|
|
if err != nil {
|
2015-08-25 21:24:19 +00:00
|
|
|
t.Fatal(err)
|
2015-08-25 16:32:45 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address", addr,
|
|
|
|
"-key-shares", "2",
|
2015-08-25 21:24:19 +00:00
|
|
|
"-pgp-keys", pubFiles[0] + ",@" + pubFiles[1] + "," + pubFiles[2],
|
2015-08-25 16:32:45 +00:00
|
|
|
"-key-threshold", "2",
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should fail, as key-shares does not match pgp-keys size
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad (command should have failed): %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
args = []string{
|
|
|
|
"-address", addr,
|
2015-08-25 22:33:58 +00:00
|
|
|
"-key-shares", "3",
|
2015-08-25 21:24:19 +00:00
|
|
|
"-pgp-keys", pubFiles[0] + ",@" + pubFiles[1] + "," + pubFiles[2],
|
2015-08-25 16:32:45 +00:00
|
|
|
"-key-threshold", "2",
|
|
|
|
}
|
|
|
|
|
2015-08-25 21:24:19 +00:00
|
|
|
ui.OutputWriter.Reset()
|
2015-08-25 16:32:45 +00:00
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
init, err = core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !init {
|
|
|
|
t.Fatal("should be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
sealConf, err := core.SealConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &vault.SealConfig{
|
|
|
|
SecretShares: 3,
|
|
|
|
SecretThreshold: 2,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, sealConf) {
|
|
|
|
t.Fatalf("bad:\nexpected: %#v\ngot: %#v", expected, sealConf)
|
|
|
|
}
|
2015-08-25 22:33:58 +00:00
|
|
|
|
2015-08-25 21:24:19 +00:00
|
|
|
re, err := regexp.Compile("\\s+Initial Root Token:\\s+(.*)")
|
2015-08-25 16:32:45 +00:00
|
|
|
if err != nil {
|
2015-08-25 21:24:19 +00:00
|
|
|
t.Fatalf("Error compiling regex: %s", err)
|
2015-08-25 16:32:45 +00:00
|
|
|
}
|
2015-08-25 21:24:19 +00:00
|
|
|
matches := re.FindAllStringSubmatch(ui.OutputWriter.String(), -1)
|
|
|
|
if len(matches) != 1 {
|
|
|
|
t.Fatalf("Unexpected number of tokens found, got %d", len(matches))
|
2015-08-25 16:32:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 21:24:19 +00:00
|
|
|
rootToken := matches[0][1]
|
2015-08-25 16:32:45 +00:00
|
|
|
|
2015-08-25 21:24:19 +00:00
|
|
|
parseDecryptAndTestUnsealKeys(t, ui.OutputWriter.String(), rootToken, core)
|
2015-08-25 16:32:45 +00:00
|
|
|
}
|