2016-04-04 14:44:22 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-04-04 14:44:22 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical/inmem"
|
2016-04-04 14:44:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCore_NewTestCore(t *testing.T, seal Seal) (*Core, *CoreConfig) {
|
2018-09-18 03:03:00 +00:00
|
|
|
return testCore_NewTestCoreLicensing(t, seal, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCore_NewTestCoreLicensing(t *testing.T, seal Seal, licensingConfig *LicensingConfig) (*Core, *CoreConfig) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
inm, err := inmem.NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
conf := &CoreConfig{
|
|
|
|
Physical: inm,
|
|
|
|
DisableMlock: true,
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
2017-09-15 13:02:29 +00:00
|
|
|
"kv": LeasedPassthroughBackendFactory,
|
2016-04-04 14:44:22 +00:00
|
|
|
},
|
2018-09-18 03:03:00 +00:00
|
|
|
Seal: seal,
|
|
|
|
LicensingConfig: licensingConfig,
|
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) {
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err := c.Initialized(context.Background())
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if init {
|
|
|
|
t.Fatalf("should not be init")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the seal configuration
|
2018-01-19 06:44:44 +00:00
|
|
|
outConf, err := c.seal.BarrierConfig(context.Background())
|
2016-04-04 14:44:22 +00:00
|
|
|
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 {
|
2018-01-19 06:44:44 +00:00
|
|
|
outConf, err := c.seal.RecoveryConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
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
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
res, err := c.Initialize(context.Background(), &InitParams{
|
2016-09-13 22:42:24 +00:00
|
|
|
BarrierConfig: barrierConf,
|
|
|
|
RecoveryConfig: 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
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
_, err = c.Initialize(context.Background(), &InitParams{
|
2016-09-13 22:42:24 +00:00
|
|
|
BarrierConfig: barrierConf,
|
|
|
|
RecoveryConfig: recoveryConf,
|
|
|
|
})
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != ErrAlreadyInit {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err = c.Initialized(context.Background())
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !init {
|
|
|
|
t.Fatalf("should be init")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the seal configuration
|
2018-01-19 06:44:44 +00:00
|
|
|
outConf, err = c.seal.BarrierConfig(context.Background())
|
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 !reflect.DeepEqual(outConf, barrierConf) {
|
|
|
|
t.Fatalf("bad: %v expect: %v", outConf, barrierConf)
|
|
|
|
}
|
|
|
|
if recoveryConf != nil {
|
2018-01-19 06:44:44 +00:00
|
|
|
outConf, err = c.seal.RecoveryConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
_, err = c2.Initialize(context.Background(), &InitParams{
|
2016-09-13 22:42:24 +00:00
|
|
|
BarrierConfig: barrierConf,
|
|
|
|
RecoveryConfig: recoveryConf,
|
|
|
|
})
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != ErrAlreadyInit {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err = c2.Initialized(context.Background())
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !init {
|
|
|
|
t.Fatalf("should be init")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the seal configuration
|
2018-01-19 06:44:44 +00:00
|
|
|
outConf, err = c2.seal.BarrierConfig(context.Background())
|
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 !reflect.DeepEqual(outConf, barrierConf) {
|
|
|
|
t.Fatalf("bad: %v expect: %v", outConf, barrierConf)
|
|
|
|
}
|
|
|
|
if recoveryConf != nil {
|
2018-01-19 06:44:44 +00:00
|
|
|
outConf, err = c2.seal.RecoveryConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|