2016-04-26 00:14:16 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-04-27 20:59:06 +00:00
|
|
|
"testing"
|
2016-04-26 00:14:16 +00:00
|
|
|
)
|
|
|
|
|
2017-02-08 01:44:26 +00:00
|
|
|
var (
|
|
|
|
TestCoreUnsealedWithConfigs = testCoreUnsealedWithConfigs
|
|
|
|
TestSealDefConfigs = testSealDefConfigs
|
|
|
|
)
|
|
|
|
|
2016-04-26 00:14:16 +00:00
|
|
|
type TestSeal struct {
|
2017-01-17 20:43:10 +00:00
|
|
|
defseal *DefaultSeal
|
|
|
|
barrierKeys [][]byte
|
|
|
|
recoveryKey []byte
|
|
|
|
recoveryConfig *SealConfig
|
|
|
|
storedKeysDisabled bool
|
|
|
|
recoveryKeysDisabled bool
|
2016-04-26 00:14:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 17:28:12 +00:00
|
|
|
func newTestSeal(t *testing.T) Seal {
|
|
|
|
return &TestSeal{}
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:14:16 +00:00
|
|
|
func (d *TestSeal) checkCore() error {
|
|
|
|
if d.defseal.core == nil {
|
|
|
|
return fmt.Errorf("seal does not have a core set")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) SetCore(core *Core) {
|
|
|
|
d.defseal = &DefaultSeal{}
|
|
|
|
d.defseal.core = core
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) Init() error {
|
|
|
|
d.barrierKeys = [][]byte{}
|
|
|
|
return d.defseal.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) Finalize() error {
|
|
|
|
return d.defseal.Finalize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) BarrierType() string {
|
|
|
|
return "shamir"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) StoredKeysSupported() bool {
|
2017-01-17 20:43:10 +00:00
|
|
|
return !d.storedKeysDisabled
|
2016-04-26 00:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) RecoveryKeySupported() bool {
|
2017-01-17 20:43:10 +00:00
|
|
|
return !d.recoveryKeysDisabled
|
2016-04-26 00:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) SetStoredKeys(keys [][]byte) error {
|
|
|
|
d.barrierKeys = keys
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) GetStoredKeys() ([][]byte, error) {
|
|
|
|
return d.barrierKeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) BarrierConfig() (*SealConfig, error) {
|
|
|
|
return d.defseal.BarrierConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) SetBarrierConfig(config *SealConfig) error {
|
|
|
|
return d.defseal.SetBarrierConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) RecoveryType() string {
|
|
|
|
return "shamir"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) RecoveryConfig() (*SealConfig, error) {
|
|
|
|
return d.recoveryConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) SetRecoveryConfig(config *SealConfig) error {
|
2017-03-01 22:10:06 +00:00
|
|
|
if config == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:14:16 +00:00
|
|
|
d.recoveryConfig = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) VerifyRecoveryKey(key []byte) error {
|
|
|
|
if bytes.Equal(d.recoveryKey, key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("not equivalent")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TestSeal) SetRecoveryKey(key []byte) error {
|
|
|
|
newbuf := bytes.NewBuffer(nil)
|
|
|
|
newbuf.Write(key)
|
|
|
|
d.recoveryKey = newbuf.Bytes()
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-27 20:59:06 +00:00
|
|
|
|
2017-02-08 01:44:26 +00:00
|
|
|
func testCoreUnsealedWithConfigs(t *testing.T, barrierConf, recoveryConf *SealConfig) (*Core, [][]byte, [][]byte, string) {
|
2016-04-27 20:59:06 +00:00
|
|
|
seal := &TestSeal{}
|
2017-09-15 04:21:35 +00:00
|
|
|
core := TestCoreWithSeal(t, seal, false)
|
2016-09-13 22:42:24 +00:00
|
|
|
result, err := core.Initialize(&InitParams{
|
|
|
|
BarrierConfig: barrierConf,
|
|
|
|
RecoveryConfig: recoveryConf,
|
|
|
|
})
|
2016-04-27 20:59:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
err = core.UnsealWithStoredKeys()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if sealed, _ := core.Sealed(); sealed {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed, err = core.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err checking seal status: %s", err)
|
|
|
|
}
|
|
|
|
if sealed {
|
|
|
|
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{
|
|
|
|
SecretShares: 5,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
StoredShares: 2,
|
|
|
|
}, &SealConfig{
|
|
|
|
SecretShares: 5,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}
|
|
|
|
}
|