2016-04-26 00:14:16 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-01-09 00:48:57 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
2016-04-26 00:14:16 +00:00
|
|
|
)
|
|
|
|
|
2017-02-08 01:44:26 +00:00
|
|
|
var (
|
|
|
|
TestCoreUnsealedWithConfigs = testCoreUnsealedWithConfigs
|
|
|
|
TestSealDefConfigs = testSealDefConfigs
|
|
|
|
)
|
|
|
|
|
2017-10-23 17:42:04 +00:00
|
|
|
type TestSealOpts struct {
|
|
|
|
StoredKeysDisabled bool
|
|
|
|
RecoveryKeysDisabled bool
|
2019-02-01 19:29:55 +00:00
|
|
|
Secret []byte
|
2019-06-20 19:14:58 +00:00
|
|
|
Logger log.Logger
|
2016-04-26 00:14:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 17:42:04 +00:00
|
|
|
func testCoreUnsealedWithConfigs(t testing.T, barrierConf, recoveryConf *SealConfig) (*Core, [][]byte, [][]byte, string) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Helper()
|
|
|
|
var opts *TestSealOpts
|
|
|
|
if recoveryConf == nil {
|
|
|
|
opts = &TestSealOpts{
|
|
|
|
StoredKeysDisabled: true,
|
|
|
|
RecoveryKeysDisabled: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seal := NewTestSeal(t, opts)
|
2017-09-15 04:21:35 +00:00
|
|
|
core := TestCoreWithSeal(t, seal, false)
|
2018-01-19 06:44:44 +00:00
|
|
|
result, err := core.Initialize(context.Background(), &InitParams{
|
2016-09-13 22:42:24 +00:00
|
|
|
BarrierConfig: barrierConf,
|
|
|
|
RecoveryConfig: recoveryConf,
|
|
|
|
})
|
2016-04-27 20:59:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = core.UnsealWithStoredKeys(context.Background())
|
2019-02-01 19:29:55 +00:00
|
|
|
if err != nil && IsFatalError(err) {
|
2016-04-27 20:59:06 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-07-24 20:57:25 +00:00
|
|
|
if core.Sealed() {
|
2016-04-27 20:59:06 +00:00
|
|
|
for _, key := range result.SecretShares {
|
2017-01-17 20:43:10 +00:00
|
|
|
if _, err := core.Unseal(TestKeyCopy(key)); err != nil {
|
2016-04-27 20:59:06 +00:00
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 20:57:25 +00:00
|
|
|
if core.Sealed() {
|
2016-04-27 20:59:06 +00:00
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return core, result.SecretShares, result.RecoveryShares, result.RootToken
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:44:26 +00:00
|
|
|
func testSealDefConfigs() (*SealConfig, *SealConfig) {
|
2016-04-27 20:59:06 +00:00
|
|
|
return &SealConfig{
|
2017-10-23 17:42:04 +00:00
|
|
|
SecretShares: 5,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoreUnsealedWithConfigSealOpts(t testing.T, barrierConf, recoveryConf *SealConfig, sealOpts *TestSealOpts) (*Core, [][]byte, [][]byte, string) {
|
|
|
|
seal := NewTestSeal(t, sealOpts)
|
|
|
|
core := TestCoreWithSeal(t, seal, false)
|
2018-01-19 06:44:44 +00:00
|
|
|
result, err := core.Initialize(context.Background(), &InitParams{
|
2017-10-23 17:42:04 +00:00
|
|
|
BarrierConfig: barrierConf,
|
|
|
|
RecoveryConfig: recoveryConf,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
err = core.UnsealWithStoredKeys(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2017-10-23 17:42:04 +00:00
|
|
|
}
|
2018-07-24 20:57:25 +00:00
|
|
|
if core.Sealed() {
|
2018-09-18 03:03:00 +00:00
|
|
|
for _, key := range result.SecretShares {
|
|
|
|
if _, err := core.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if core.Sealed() {
|
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
2017-10-23 17:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return core, result.SecretShares, result.RecoveryShares, result.RootToken
|
2016-04-27 20:59:06 +00:00
|
|
|
}
|