2016-04-04 14:44:22 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2016-04-26 03:10:32 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2016-04-04 14:44:22 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/physical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCore_Init(t *testing.T) {
|
2016-04-25 19:39:04 +00:00
|
|
|
c, conf := testCore_NewTestCore(t, nil)
|
|
|
|
testCore_Init_Common(t, c, conf, &SealConfig{SecretShares: 5, SecretThreshold: 3}, nil)
|
|
|
|
|
|
|
|
c, conf = testCore_NewTestCore(t, &TestSeal{})
|
|
|
|
bc, rc := TestSealDefConfigs()
|
|
|
|
rc.SecretShares = 4
|
|
|
|
rc.SecretThreshold = 2
|
|
|
|
testCore_Init_Common(t, c, conf, bc, rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCore_NewTestCore(t *testing.T, seal Seal) (*Core, *CoreConfig) {
|
2016-04-26 03:10:32 +00:00
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
inm := physical.NewInmem(logger)
|
2016-04-04 14:44:22 +00:00
|
|
|
conf := &CoreConfig{
|
|
|
|
Physical: inm,
|
|
|
|
DisableMlock: true,
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"generic": LeasedPassthroughBackendFactory,
|
|
|
|
},
|
2016-04-25 19:39:04 +00:00
|
|
|
Seal: seal,
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
c, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
return c, conf
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
func testCore_Init_Common(t *testing.T, c *Core, conf *CoreConfig, barrierConf, recoveryConf *SealConfig) {
|
2016-04-04 14:44:22 +00:00
|
|
|
init, err := c.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if init {
|
|
|
|
t.Fatalf("should not be init")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the seal configuration
|
|
|
|
outConf, err := c.seal.BarrierConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if outConf != nil {
|
|
|
|
t.Fatalf("bad: %v", outConf)
|
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
if recoveryConf != nil {
|
|
|
|
outConf, err := c.seal.RecoveryConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if outConf != nil {
|
|
|
|
t.Fatalf("bad: %v", outConf)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
|
|
|
|
res, err := c.Initialize(barrierConf, recoveryConf)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
if len(res.SecretShares) != (barrierConf.SecretShares - barrierConf.StoredShares) {
|
|
|
|
t.Fatalf("Bad: got\n%#v\nexpected conf matching\n%#v\n", *res, *barrierConf)
|
|
|
|
}
|
|
|
|
if recoveryConf != nil {
|
|
|
|
if len(res.RecoveryShares) != recoveryConf.SecretShares {
|
|
|
|
t.Fatalf("Bad: got\n%#v\nexpected conf matching\n%#v\n", *res, *recoveryConf)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
if res.RootToken == "" {
|
2016-04-25 19:39:04 +00:00
|
|
|
t.Fatalf("Bad: %#v", res)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
_, err = c.Initialize(barrierConf, recoveryConf)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != ErrAlreadyInit {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
init, err = c.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !init {
|
|
|
|
t.Fatalf("should be init")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the seal configuration
|
|
|
|
outConf, err = c.seal.BarrierConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
if !reflect.DeepEqual(outConf, barrierConf) {
|
|
|
|
t.Fatalf("bad: %v expect: %v", outConf, barrierConf)
|
|
|
|
}
|
|
|
|
if recoveryConf != nil {
|
|
|
|
outConf, err = c.seal.RecoveryConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(outConf, recoveryConf) {
|
|
|
|
t.Fatalf("bad: %v expect: %v", outConf, recoveryConf)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New Core, same backend
|
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
_, err = c2.Initialize(barrierConf, recoveryConf)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != ErrAlreadyInit {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
init, err = c2.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !init {
|
|
|
|
t.Fatalf("should be init")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the seal configuration
|
|
|
|
outConf, err = c2.seal.BarrierConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
if !reflect.DeepEqual(outConf, barrierConf) {
|
|
|
|
t.Fatalf("bad: %v expect: %v", outConf, barrierConf)
|
|
|
|
}
|
|
|
|
if recoveryConf != nil {
|
|
|
|
outConf, err = c2.seal.RecoveryConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(outConf, recoveryConf) {
|
|
|
|
t.Fatalf("bad: %v expect: %v", outConf, recoveryConf)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
}
|