2015-03-05 21:27:35 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2019-10-17 17:33:00 +00:00
|
|
|
"crypto/rand"
|
2015-03-05 21:27:35 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-05-28 00:25:36 +00:00
|
|
|
"time"
|
2019-01-31 14:25:18 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-03-05 21:27:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testBarrier(t *testing.T, b SecurityBarrier) {
|
|
|
|
// Should not be initialized
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err := b.Initialized(context.Background())
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
t.Fatalf("should not be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should start sealed
|
|
|
|
sealed, err := b.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !sealed {
|
|
|
|
t.Fatalf("should be sealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sealing should be a no-op
|
|
|
|
if err := b.Seal(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// All operations should fail
|
2019-01-31 14:25:18 +00:00
|
|
|
e := &logical.StorageEntry{Key: "test", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), e); err != ErrBarrierSealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if _, err := b.Get(context.Background(), "test"); err != ErrBarrierSealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Delete(context.Background(), "test"); err != ErrBarrierSealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if _, err := b.List(context.Background(), ""); err != ErrBarrierSealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new key
|
2019-10-17 17:33:00 +00:00
|
|
|
key, err := b.GenerateKey(rand.Reader)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-12 18:20:27 +00:00
|
|
|
// Validate minimum key length
|
|
|
|
min, max := b.KeyLength()
|
|
|
|
if min < 16 {
|
|
|
|
t.Fatalf("minimum key size too small: %d", min)
|
|
|
|
}
|
|
|
|
if max < min {
|
|
|
|
t.Fatalf("maximum key size smaller than min")
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Unseal should not work
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Unseal(context.Background(), key); err != ErrBarrierNotInit {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the vault
|
2019-10-18 18:46:00 +00:00
|
|
|
if err := b.Initialize(context.Background(), key, nil, rand.Reader); err != nil {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double Initialize should fail
|
2019-10-18 18:46:00 +00:00
|
|
|
if err := b.Initialize(context.Background(), key, nil, rand.Reader); err != ErrBarrierAlreadyInit {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be initialized
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err = b.Initialized(context.Background())
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !init {
|
|
|
|
t.Fatalf("should be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should still be sealed
|
|
|
|
sealed, err = b.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !sealed {
|
|
|
|
t.Fatalf("should sealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unseal should work
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Unseal(context.Background(), key); err != nil {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:29:23 +00:00
|
|
|
// Unseal should no-op when done twice
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Unseal(context.Background(), key); err != nil {
|
2015-03-05 21:29:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Should no longer be sealed
|
|
|
|
sealed, err = b.Sealed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if sealed {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:28:33 +00:00
|
|
|
// Verify the master key
|
|
|
|
if err := b.VerifyMaster(key); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Operations should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.Get(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// List should have only "core/"
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := b.List(context.Background(), "")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-05-27 23:01:25 +00:00
|
|
|
if len(keys) != 1 || keys[0] != "core/" {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to write
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), e); err != nil {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be equal
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, e) {
|
|
|
|
t.Fatalf("bad: %v exp: %v", out, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List should show the items
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 2 {
|
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
2015-05-27 23:01:25 +00:00
|
|
|
if keys[0] != "core/" || keys[1] != "test" {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete should clear
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double Delete is fine
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be nil
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List should have nothing
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-05-27 23:01:25 +00:00
|
|
|
if len(keys) != 1 || keys[0] != "core/" {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("bad: %v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the item back
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), e); err != nil {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reseal should prevent any updates
|
|
|
|
if err := b.Seal(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No access allowed
|
2018-01-19 06:44:44 +00:00
|
|
|
if _, err := b.Get(context.Background(), "test"); err != ErrBarrierSealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unseal should work
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Unseal(context.Background(), key); err != nil {
|
2015-03-05 21:27:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be equal
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, e) {
|
|
|
|
t.Fatalf("bad: %v exp: %v", out, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final cleanup
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "test")
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-03-12 18:27:32 +00:00
|
|
|
|
|
|
|
// Reseal should prevent any updates
|
|
|
|
if err := b.Seal(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify the key
|
|
|
|
key[0]++
|
|
|
|
|
|
|
|
// Unseal should fail
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Unseal(context.Background(), key); err != ErrBarrierInvalidKey {
|
2015-03-12 18:27:32 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
2015-05-28 00:10:08 +00:00
|
|
|
|
|
|
|
func testBarrier_Rotate(t *testing.T, b SecurityBarrier) {
|
|
|
|
// Initialize the barrier
|
2019-10-17 17:33:00 +00:00
|
|
|
key, _ := b.GenerateKey(rand.Reader)
|
2019-10-18 18:46:00 +00:00
|
|
|
b.Initialize(context.Background(), key, nil, rand.Reader)
|
2018-01-19 06:44:44 +00:00
|
|
|
err := b.Unseal(context.Background(), key)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:25:36 +00:00
|
|
|
// Check the key info
|
|
|
|
info, err := b.ActiveKeyInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if info.Term != 1 {
|
|
|
|
t.Fatalf("Bad term: %d", info.Term)
|
|
|
|
}
|
|
|
|
if time.Since(info.InstallTime) > time.Second {
|
|
|
|
t.Fatalf("Bad install: %v", info.InstallTime)
|
|
|
|
}
|
|
|
|
first := info.InstallTime
|
|
|
|
|
2015-05-28 00:10:08 +00:00
|
|
|
// Write a key
|
2019-01-31 14:25:18 +00:00
|
|
|
e1 := &logical.StorageEntry{Key: "test", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), e1); err != nil {
|
2015-05-28 00:10:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate the encryption key
|
2019-10-17 17:33:00 +00:00
|
|
|
newTerm, err := b.Rotate(context.Background(), rand.Reader)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-05-28 23:42:32 +00:00
|
|
|
if newTerm != 2 {
|
|
|
|
t.Fatalf("bad: %v", newTerm)
|
|
|
|
}
|
2015-05-28 00:10:08 +00:00
|
|
|
|
2015-05-28 00:25:36 +00:00
|
|
|
// Check the key info
|
|
|
|
info, err = b.ActiveKeyInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if info.Term != 2 {
|
|
|
|
t.Fatalf("Bad term: %d", info.Term)
|
|
|
|
}
|
|
|
|
if !info.InstallTime.After(first) {
|
|
|
|
t.Fatalf("Bad install: %v", info.InstallTime)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:10:08 +00:00
|
|
|
// Write another key
|
2019-01-31 14:25:18 +00:00
|
|
|
e2 := &logical.StorageEntry{Key: "foo", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), e2); err != nil {
|
2015-05-28 00:10:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading both should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.Get(context.Background(), e1.Key)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), e2.Key)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seal and unseal
|
|
|
|
err = b.Seal()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Unseal(context.Background(), key)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading both should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), e1.Key)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), e2.Key)
|
2015-05-28 00:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2015-05-28 22:52:26 +00:00
|
|
|
|
2015-05-28 23:09:15 +00:00
|
|
|
// Should be fine to reload keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.ReloadKeyring(context.Background())
|
2015-05-28 23:09:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-05-28 00:10:08 +00:00
|
|
|
}
|
2015-05-28 00:17:03 +00:00
|
|
|
|
|
|
|
func testBarrier_Rekey(t *testing.T, b SecurityBarrier) {
|
|
|
|
// Initialize the barrier
|
2019-10-17 17:33:00 +00:00
|
|
|
key, _ := b.GenerateKey(rand.Reader)
|
2019-10-18 18:46:00 +00:00
|
|
|
b.Initialize(context.Background(), key, nil, rand.Reader)
|
2018-01-19 06:44:44 +00:00
|
|
|
err := b.Unseal(context.Background(), key)
|
2015-05-28 00:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a key
|
2019-01-31 14:25:18 +00:00
|
|
|
e1 := &logical.StorageEntry{Key: "test", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), e1); err != nil {
|
2015-05-28 00:17:03 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:28:33 +00:00
|
|
|
// Verify the master key
|
|
|
|
if err := b.VerifyMaster(key); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:17:03 +00:00
|
|
|
// Rekey to a new key
|
2019-10-17 17:33:00 +00:00
|
|
|
newKey, _ := b.GenerateKey(rand.Reader)
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Rekey(context.Background(), newKey)
|
2015-05-28 00:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:28:33 +00:00
|
|
|
// Verify the old master key
|
|
|
|
if err := b.VerifyMaster(key); err != ErrBarrierInvalidKey {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the new master key
|
|
|
|
if err := b.VerifyMaster(newKey); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:17:03 +00:00
|
|
|
// Reading should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.Get(context.Background(), e1.Key)
|
2015-05-28 00:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seal
|
|
|
|
err = b.Seal()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unseal with old key should fail
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Unseal(context.Background(), key)
|
2015-05-28 00:17:03 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("unseal should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unseal with new keys should work
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Unseal(context.Background(), newKey)
|
2015-05-28 00:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), e1.Key)
|
2015-05-28 00:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2015-05-28 23:09:15 +00:00
|
|
|
|
|
|
|
// Should be fine to reload keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.ReloadKeyring(context.Background())
|
2015-05-28 23:09:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-05-28 00:17:03 +00:00
|
|
|
}
|
2015-05-28 23:42:32 +00:00
|
|
|
|
|
|
|
func testBarrier_Upgrade(t *testing.T, b1, b2 SecurityBarrier) {
|
|
|
|
// Initialize the barrier
|
2019-10-17 17:33:00 +00:00
|
|
|
key, _ := b1.GenerateKey(rand.Reader)
|
2019-10-18 18:46:00 +00:00
|
|
|
b1.Initialize(context.Background(), key, nil, rand.Reader)
|
2018-01-19 06:44:44 +00:00
|
|
|
err := b1.Unseal(context.Background(), key)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b2.Unseal(context.Background(), key)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate the encryption key
|
2019-10-17 17:33:00 +00:00
|
|
|
newTerm, err := b1.Rotate(context.Background(), rand.Reader)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create upgrade path
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b1.CreateUpgrade(context.Background(), newTerm)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for an upgrade
|
2018-01-19 06:44:44 +00:00
|
|
|
did, updated, err := b2.CheckUpgrade(context.Background())
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !did || updated != newTerm {
|
|
|
|
t.Fatalf("failed to upgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have no upgrades pending
|
2018-01-19 06:44:44 +00:00
|
|
|
did, updated, err = b2.CheckUpgrade(context.Background())
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if did {
|
|
|
|
t.Fatalf("should not have upgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate the encryption key
|
2019-10-17 17:33:00 +00:00
|
|
|
newTerm, err = b1.Rotate(context.Background(), rand.Reader)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create upgrade path
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b1.CreateUpgrade(context.Background(), newTerm)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy upgrade path
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b1.DestroyUpgrade(context.Background(), newTerm)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should have no upgrades pending
|
2018-01-19 06:44:44 +00:00
|
|
|
did, updated, err = b2.CheckUpgrade(context.Background())
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if did {
|
|
|
|
t.Fatalf("should not have upgrade")
|
|
|
|
}
|
|
|
|
}
|
2015-05-29 21:29:55 +00:00
|
|
|
|
|
|
|
func testBarrier_Upgrade_Rekey(t *testing.T, b1, b2 SecurityBarrier) {
|
|
|
|
// Initialize the barrier
|
2019-10-17 17:33:00 +00:00
|
|
|
key, _ := b1.GenerateKey(rand.Reader)
|
2019-10-18 18:46:00 +00:00
|
|
|
b1.Initialize(context.Background(), key, nil, rand.Reader)
|
2018-01-19 06:44:44 +00:00
|
|
|
err := b1.Unseal(context.Background(), key)
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b2.Unseal(context.Background(), key)
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rekey to a new key
|
2019-10-17 17:33:00 +00:00
|
|
|
newKey, _ := b1.GenerateKey(rand.Reader)
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b1.Rekey(context.Background(), newKey)
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload the master key
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b2.ReloadMasterKey(context.Background())
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload the keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b2.ReloadKeyring(context.Background())
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|