2016-01-26 17:23:42 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
"github.com/hashicorp/vault/helper/keysutil"
|
2016-01-26 17:23:42 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-10-26 23:52:31 +00:00
|
|
|
keysArchive []keysutil.KeyEntry
|
2016-01-26 17:23:42 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 17:02:32 +00:00
|
|
|
func resetKeysArchive() {
|
2016-10-26 23:52:31 +00:00
|
|
|
keysArchive = []keysutil.KeyEntry{keysutil.KeyEntry{}}
|
2016-01-27 17:02:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 01:21:58 +00:00
|
|
|
func Test_KeyUpgrade(t *testing.T) {
|
2016-10-26 23:52:31 +00:00
|
|
|
testKeyUpgradeCommon(t, keysutil.NewLockManager(false))
|
|
|
|
testKeyUpgradeCommon(t, keysutil.NewLockManager(true))
|
2016-04-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func testKeyUpgradeCommon(t *testing.T, lm *keysutil.LockManager) {
|
2016-01-27 01:21:58 +00:00
|
|
|
storage := &logical.InmemStorage{}
|
2016-10-26 23:52:31 +00:00
|
|
|
p, lock, upserted, err := lm.GetPolicyUpsert(keysutil.PolicyRequest{
|
|
|
|
Storage: storage,
|
|
|
|
KeyType: keysutil.KeyType_AES256_GCM96,
|
|
|
|
Name: "test",
|
2016-09-21 14:29:42 +00:00
|
|
|
})
|
2016-05-03 04:19:18 +00:00
|
|
|
if lock != nil {
|
|
|
|
defer lock.RUnlock()
|
|
|
|
}
|
2016-01-27 01:21:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
if p == nil {
|
|
|
|
t.Fatal("nil policy")
|
2016-01-27 01:21:58 +00:00
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
if !upserted {
|
|
|
|
t.Fatal("expected an upsert")
|
|
|
|
}
|
2016-01-27 21:24:11 +00:00
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
testBytes := make([]byte, len(p.Keys[1].AESKey))
|
|
|
|
copy(testBytes, p.Keys[1].AESKey)
|
2016-01-27 01:21:58 +00:00
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
p.Key = p.Keys[1].AESKey
|
2016-04-26 15:39:19 +00:00
|
|
|
p.Keys = nil
|
2016-10-26 23:52:31 +00:00
|
|
|
p.MigrateKeyToKeysMap()
|
2016-04-26 15:39:19 +00:00
|
|
|
if p.Key != nil {
|
2016-01-27 01:21:58 +00:00
|
|
|
t.Fatal("policy.Key is not nil")
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
if len(p.Keys) != 1 {
|
2016-01-27 01:21:58 +00:00
|
|
|
t.Fatal("policy.Keys is the wrong size")
|
|
|
|
}
|
2016-09-21 14:29:42 +00:00
|
|
|
if !reflect.DeepEqual(testBytes, p.Keys[1].AESKey) {
|
2016-01-27 01:21:58 +00:00
|
|
|
t.Fatal("key mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 17:02:32 +00:00
|
|
|
func Test_ArchivingUpgrade(t *testing.T) {
|
2016-10-26 23:52:31 +00:00
|
|
|
testArchivingUpgradeCommon(t, keysutil.NewLockManager(false))
|
|
|
|
testArchivingUpgradeCommon(t, keysutil.NewLockManager(true))
|
2016-04-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func testArchivingUpgradeCommon(t *testing.T, lm *keysutil.LockManager) {
|
2016-01-27 17:02:32 +00:00
|
|
|
resetKeysArchive()
|
|
|
|
|
|
|
|
// First, we generate a policy and rotate it a number of times. Each time
|
|
|
|
// we'll ensure that we have the expected number of keys in the archive and
|
|
|
|
// the main keys object, which without changing the min version should be
|
|
|
|
// zero and latest, respectively
|
|
|
|
|
|
|
|
storage := &logical.InmemStorage{}
|
2016-10-26 23:52:31 +00:00
|
|
|
p, lock, _, err := lm.GetPolicyUpsert(keysutil.PolicyRequest{
|
|
|
|
Storage: storage,
|
|
|
|
KeyType: keysutil.KeyType_AES256_GCM96,
|
|
|
|
Name: "test",
|
2016-09-21 14:29:42 +00:00
|
|
|
})
|
2016-01-27 17:02:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-03 04:19:18 +00:00
|
|
|
if p == nil || lock == nil {
|
|
|
|
t.Fatal("nil policy or lock")
|
2016-04-21 20:32:06 +00:00
|
|
|
}
|
2016-05-03 04:19:18 +00:00
|
|
|
lock.RUnlock()
|
2016-01-27 21:24:11 +00:00
|
|
|
|
2016-01-27 17:02:32 +00:00
|
|
|
// Store the initial key in the archive
|
2016-04-26 15:39:19 +00:00
|
|
|
keysArchive = append(keysArchive, p.Keys[1])
|
|
|
|
checkKeys(t, p, storage, "initial", 1, 1, 1)
|
2016-01-27 17:02:32 +00:00
|
|
|
|
|
|
|
for i := 2; i <= 10; i++ {
|
2016-10-26 23:52:31 +00:00
|
|
|
err = p.Rotate(storage)
|
2016-01-27 17:02:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
keysArchive = append(keysArchive, p.Keys[i])
|
|
|
|
checkKeys(t, p, storage, "rotate", i, i, i)
|
2016-01-27 17:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now, wipe the archive and set the archive version to zero
|
|
|
|
err = storage.Delete("archive/test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
p.ArchiveVersion = 0
|
2016-01-27 17:02:32 +00:00
|
|
|
|
|
|
|
// Store it, but without calling persist, so we don't trigger
|
|
|
|
// handleArchiving()
|
2016-04-26 15:39:19 +00:00
|
|
|
buf, err := p.Serialize()
|
2016-01-27 17:02:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the policy into storage
|
|
|
|
err = storage.Put(&logical.StorageEntry{
|
2016-04-26 15:39:19 +00:00
|
|
|
Key: "policy/" + p.Name,
|
2016-01-27 17:02:32 +00:00
|
|
|
Value: buf,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:39:19 +00:00
|
|
|
// If we're caching, expire from the cache since we modified it
|
2016-04-21 20:32:06 +00:00
|
|
|
// under-the-hood
|
2016-04-26 15:39:19 +00:00
|
|
|
if lm.CacheActive() {
|
2016-10-26 23:52:31 +00:00
|
|
|
lm.CacheDelete("test")
|
2016-04-21 20:32:06 +00:00
|
|
|
}
|
2016-01-27 21:24:11 +00:00
|
|
|
|
2016-01-27 17:02:32 +00:00
|
|
|
// Now get the policy again; the upgrade should happen automatically
|
2016-05-03 04:19:18 +00:00
|
|
|
p, lock, err = lm.GetPolicyShared(storage, "test")
|
2016-01-27 17:02:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-03 04:19:18 +00:00
|
|
|
if p == nil || lock == nil {
|
|
|
|
t.Fatal("nil policy or lock")
|
2016-01-27 17:02:32 +00:00
|
|
|
}
|
2016-05-03 04:19:18 +00:00
|
|
|
lock.RUnlock()
|
2016-01-27 17:02:32 +00:00
|
|
|
|
2016-04-26 15:39:19 +00:00
|
|
|
checkKeys(t, p, storage, "upgrade", 10, 10, 10)
|
2016-05-03 04:19:18 +00:00
|
|
|
|
|
|
|
// Let's check some deletion logic while we're at it
|
|
|
|
|
|
|
|
// The policy should be in there
|
2016-10-26 23:52:31 +00:00
|
|
|
if lm.CacheActive() && lm.Cache("test") == nil {
|
2016-05-03 04:19:18 +00:00
|
|
|
t.Fatal("nil policy in cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
// First we'll do this wrong, by not setting the deletion flag
|
|
|
|
err = lm.DeletePolicy(storage, "test")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("got nil error, but should not have been able to delete since we didn't set the deletion flag on the policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The policy should still be in there
|
2016-10-26 23:52:31 +00:00
|
|
|
if lm.CacheActive() && lm.Cache("test") == nil {
|
2016-05-03 04:19:18 +00:00
|
|
|
t.Fatal("nil policy in cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
p, lock, err = lm.GetPolicyShared(storage, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if p == nil || lock == nil {
|
|
|
|
t.Fatal("policy or lock nil after bad delete")
|
|
|
|
}
|
|
|
|
lock.RUnlock()
|
|
|
|
|
|
|
|
// Now do it properly
|
|
|
|
p.DeletionAllowed = true
|
|
|
|
err = p.Persist(storage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = lm.DeletePolicy(storage, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The policy should *not* be in there
|
2016-10-26 23:52:31 +00:00
|
|
|
if lm.CacheActive() && lm.Cache("test") != nil {
|
2016-05-03 04:19:18 +00:00
|
|
|
t.Fatal("non-nil policy in cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
p, lock, err = lm.GetPolicyShared(storage, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if p != nil || lock != nil {
|
|
|
|
t.Fatal("policy or lock not nil after delete")
|
|
|
|
}
|
2016-01-27 17:02:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 17:23:42 +00:00
|
|
|
func Test_Archiving(t *testing.T) {
|
2016-10-26 23:52:31 +00:00
|
|
|
testArchivingCommon(t, keysutil.NewLockManager(false))
|
|
|
|
testArchivingCommon(t, keysutil.NewLockManager(true))
|
2016-04-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func testArchivingCommon(t *testing.T, lm *keysutil.LockManager) {
|
2016-01-27 17:02:32 +00:00
|
|
|
resetKeysArchive()
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
// First, we generate a policy and rotate it a number of times. Each time // we'll ensure that we have the expected number of keys in the archive and
|
2016-01-26 17:23:42 +00:00
|
|
|
// the main keys object, which without changing the min version should be
|
|
|
|
// zero and latest, respectively
|
|
|
|
|
|
|
|
storage := &logical.InmemStorage{}
|
2016-10-26 23:52:31 +00:00
|
|
|
p, lock, _, err := lm.GetPolicyUpsert(keysutil.PolicyRequest{
|
|
|
|
Storage: storage,
|
|
|
|
KeyType: keysutil.KeyType_AES256_GCM96,
|
|
|
|
Name: "test",
|
2016-09-21 14:29:42 +00:00
|
|
|
})
|
2016-05-03 04:19:18 +00:00
|
|
|
if lock != nil {
|
|
|
|
defer lock.RUnlock()
|
|
|
|
}
|
2016-01-26 17:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
if p == nil {
|
|
|
|
t.Fatal("nil policy")
|
2016-04-21 20:32:06 +00:00
|
|
|
}
|
2016-01-27 21:24:11 +00:00
|
|
|
|
2016-01-26 17:23:42 +00:00
|
|
|
// Store the initial key in the archive
|
2016-04-26 15:39:19 +00:00
|
|
|
keysArchive = append(keysArchive, p.Keys[1])
|
|
|
|
checkKeys(t, p, storage, "initial", 1, 1, 1)
|
2016-01-26 17:23:42 +00:00
|
|
|
|
|
|
|
for i := 2; i <= 10; i++ {
|
2016-10-26 23:52:31 +00:00
|
|
|
err = p.Rotate(storage)
|
2016-01-26 17:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
keysArchive = append(keysArchive, p.Keys[i])
|
|
|
|
checkKeys(t, p, storage, "rotate", i, i, i)
|
2016-01-26 17:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move the min decryption version up
|
|
|
|
for i := 1; i <= 10; i++ {
|
2016-04-26 15:39:19 +00:00
|
|
|
p.MinDecryptionVersion = i
|
2016-01-26 17:23:42 +00:00
|
|
|
|
2016-04-26 15:39:19 +00:00
|
|
|
err = p.Persist(storage)
|
2016-01-26 17:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// We expect to find:
|
2016-01-26 22:14:21 +00:00
|
|
|
// * The keys in archive are the same as the latest version
|
2016-01-26 17:23:42 +00:00
|
|
|
// * The latest version is constant
|
|
|
|
// * The number of keys in the policy itself is from the min
|
|
|
|
// decryption version up to the latest version, so for e.g. 7 and
|
|
|
|
// 10, you'd need 7, 8, 9, and 10 -- IOW, latest version - min
|
|
|
|
// decryption version plus 1 (the min decryption version key
|
|
|
|
// itself)
|
2016-04-26 15:39:19 +00:00
|
|
|
checkKeys(t, p, storage, "minadd", 10, 10, p.LatestVersion-p.MinDecryptionVersion+1)
|
2016-01-26 17:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move the min decryption version down
|
|
|
|
for i := 10; i >= 1; i-- {
|
2016-04-26 15:39:19 +00:00
|
|
|
p.MinDecryptionVersion = i
|
2016-01-26 17:23:42 +00:00
|
|
|
|
2016-04-26 15:39:19 +00:00
|
|
|
err = p.Persist(storage)
|
2016-01-26 17:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// We expect to find:
|
2016-01-26 22:14:21 +00:00
|
|
|
// * The keys in archive are never removed so same as the latest version
|
2016-01-26 17:23:42 +00:00
|
|
|
// * The latest version is constant
|
|
|
|
// * The number of keys in the policy itself is from the min
|
|
|
|
// decryption version up to the latest version, so for e.g. 7 and
|
|
|
|
// 10, you'd need 7, 8, 9, and 10 -- IOW, latest version - min
|
|
|
|
// decryption version plus 1 (the min decryption version key
|
|
|
|
// itself)
|
2016-04-26 15:39:19 +00:00
|
|
|
checkKeys(t, p, storage, "minsub", 10, 10, p.LatestVersion-p.MinDecryptionVersion+1)
|
2016-01-26 17:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkKeys(t *testing.T,
|
2016-10-26 23:52:31 +00:00
|
|
|
p *keysutil.Policy,
|
2016-01-26 17:23:42 +00:00
|
|
|
storage logical.Storage,
|
2016-01-26 22:14:21 +00:00
|
|
|
action string,
|
2016-01-26 17:23:42 +00:00
|
|
|
archiveVer, latestVer, keysSize int) {
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
if len(keysArchive) != latestVer+1 {
|
|
|
|
t.Fatalf("latest expected key version is %d, expected test keys archive size is %d, "+
|
|
|
|
"but keys archive is of size %d", latestVer, latestVer+1, len(keysArchive))
|
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
archive, err := p.LoadArchive(storage)
|
2016-01-26 17:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
badArchiveVer := false
|
|
|
|
if archiveVer == 0 {
|
2016-09-01 15:57:28 +00:00
|
|
|
if len(archive.Keys) != 0 || p.ArchiveVersion != 0 {
|
2016-01-26 17:23:42 +00:00
|
|
|
badArchiveVer = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We need to subtract one because we have the indexes match key
|
|
|
|
// versions, which start at 1. So for an archive version of 1, we
|
|
|
|
// actually have two entries -- a blank 0 entry, and the key at spot 1
|
2016-09-01 15:57:28 +00:00
|
|
|
if archiveVer != len(archive.Keys)-1 || archiveVer != p.ArchiveVersion {
|
2016-01-26 17:23:42 +00:00
|
|
|
badArchiveVer = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if badArchiveVer {
|
|
|
|
t.Fatalf(
|
|
|
|
"expected archive version %d, found length of archive keys %d and policy archive version %d",
|
2016-09-01 15:57:28 +00:00
|
|
|
archiveVer, len(archive.Keys), p.ArchiveVersion,
|
2016-01-26 17:23:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-01 15:57:28 +00:00
|
|
|
if latestVer != p.LatestVersion {
|
2016-01-26 17:23:42 +00:00
|
|
|
t.Fatalf(
|
|
|
|
"expected latest version %d, found %d",
|
2016-09-01 15:57:28 +00:00
|
|
|
latestVer, p.LatestVersion,
|
2016-01-26 17:23:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-01 15:57:28 +00:00
|
|
|
if keysSize != len(p.Keys) {
|
2016-01-26 17:23:42 +00:00
|
|
|
t.Fatalf(
|
2016-01-26 22:14:21 +00:00
|
|
|
"expected keys size %d, found %d, action is %s, policy is \n%#v\n",
|
2016-09-01 15:57:28 +00:00
|
|
|
keysSize, len(p.Keys), action, p,
|
2016-01-26 17:23:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-01 15:57:28 +00:00
|
|
|
for i := p.MinDecryptionVersion; i <= p.LatestVersion; i++ {
|
|
|
|
if _, ok := p.Keys[i]; !ok {
|
2016-01-26 17:23:42 +00:00
|
|
|
t.Fatalf(
|
|
|
|
"expected key %d, did not find it in policy keys", i,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 15:57:28 +00:00
|
|
|
for i := p.MinDecryptionVersion; i <= p.LatestVersion; i++ {
|
|
|
|
if !reflect.DeepEqual(p.Keys[i], keysArchive[i]) {
|
2016-01-26 17:23:42 +00:00
|
|
|
t.Fatalf("key %d not equivalent between policy keys and test keys archive", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(archive.Keys); i++ {
|
2016-09-21 14:29:42 +00:00
|
|
|
if !reflect.DeepEqual(archive.Keys[i].AESKey, keysArchive[i].AESKey) {
|
2016-01-26 17:23:42 +00:00
|
|
|
t.Fatalf("key %d not equivalent between policy archive and test keys archive", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|