driftctl/pkg/remote/aws/repository/kms_repository_test.go

250 lines
7.6 KiB
Go
Raw Normal View History

2021-02-24 17:49:02 +00:00
package repository
import (
"strings"
2021-07-06 15:08:47 +00:00
"sync"
2021-02-24 17:49:02 +00:00
"testing"
"github.com/aws/aws-sdk-go/aws"
2021-02-24 17:49:02 +00:00
"github.com/aws/aws-sdk-go/service/kms"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
2021-05-04 16:37:45 +00:00
awstest "github.com/cloudskiff/driftctl/test/aws"
2021-02-24 17:49:02 +00:00
"github.com/stretchr/testify/mock"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_KMSRepository_ListAllKeys(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeKMS)
2021-02-24 17:49:02 +00:00
want []*kms.KeyListEntry
wantErr error
}{
2021-07-06 15:08:47 +00:00
{
name: "List only enabled keys",
mocks: func(client *awstest.MockFakeKMS) {
client.On("ListKeysPages",
&kms.ListKeysInput{},
mock.MatchedBy(func(callback func(res *kms.ListKeysOutput, lastPage bool) bool) bool {
callback(&kms.ListKeysOutput{
Keys: []*kms.KeyListEntry{
{KeyId: aws.String("1")},
{KeyId: aws.String("2")},
},
}, true)
return true
})).Return(nil).Once()
client.On("DescribeKey",
&kms.DescribeKeyInput{
KeyId: aws.String("1"),
}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyId: aws.String("1"),
KeyManager: aws.String("CUSTOMER"),
KeyState: aws.String(kms.KeyStateEnabled),
},
}, nil).Once()
client.On("DescribeKey",
&kms.DescribeKeyInput{
KeyId: aws.String("2"),
}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyId: aws.String("2"),
KeyManager: aws.String("CUSTOMER"),
KeyState: aws.String(kms.KeyStatePendingDeletion),
},
}, nil).Once()
},
want: []*kms.KeyListEntry{
{KeyId: aws.String("1")},
},
},
2021-02-24 17:49:02 +00:00
{
name: "List only customer keys",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeKMS) {
2021-02-24 17:49:02 +00:00
client.On("ListKeysPages",
&kms.ListKeysInput{},
mock.MatchedBy(func(callback func(res *kms.ListKeysOutput, lastPage bool) bool) bool {
callback(&kms.ListKeysOutput{
Keys: []*kms.KeyListEntry{
{KeyId: aws.String("1")},
{KeyId: aws.String("2")},
{KeyId: aws.String("3")},
},
}, true)
return true
})).Return(nil).Once()
2021-02-24 17:49:02 +00:00
client.On("DescribeKey",
&kms.DescribeKeyInput{
KeyId: aws.String("1"),
}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyId: aws.String("1"),
KeyManager: aws.String("CUSTOMER"),
2021-07-06 15:08:47 +00:00
KeyState: aws.String(kms.KeyStateEnabled),
2021-02-24 17:49:02 +00:00
},
}, nil).Once()
2021-02-24 17:49:02 +00:00
client.On("DescribeKey",
&kms.DescribeKeyInput{
KeyId: aws.String("2"),
}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyId: aws.String("2"),
KeyManager: aws.String("AWS"),
2021-07-06 15:08:47 +00:00
KeyState: aws.String(kms.KeyStateEnabled),
2021-02-24 17:49:02 +00:00
},
}, nil).Once()
2021-02-24 17:49:02 +00:00
client.On("DescribeKey",
&kms.DescribeKeyInput{
KeyId: aws.String("3"),
}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyId: aws.String("3"),
KeyManager: aws.String("AWS"),
2021-07-06 15:08:47 +00:00
KeyState: aws.String(kms.KeyStateEnabled),
2021-02-24 17:49:02 +00:00
},
}, nil).Once()
2021-02-24 17:49:02 +00:00
},
want: []*kms.KeyListEntry{
{KeyId: aws.String("1")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeKMS{}
tt.mocks(&client)
2021-02-24 17:49:02 +00:00
r := &kmsRepository{
2021-07-06 15:08:47 +00:00
client: &client,
cache: store,
describeKeyLock: &sync.Mutex{},
2021-02-24 17:49:02 +00:00
}
got, err := r.ListAllKeys()
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.ListAllKeys()
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, []*kms.KeyListEntry{}, store.Get("kmsListAllKeys"))
}
2021-02-24 17:49:02 +00:00
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %v -> %v", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}
2021-02-25 13:19:49 +00:00
func Test_KMSRepository_ListAllAliases(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeKMS)
2021-02-25 13:19:49 +00:00
want []*kms.AliasListEntry
wantErr error
}{
2021-07-06 15:08:47 +00:00
{
name: "List only aliases for enabled keys",
mocks: func(client *awstest.MockFakeKMS) {
client.On("ListAliasesPages",
&kms.ListAliasesInput{},
mock.MatchedBy(func(callback func(res *kms.ListAliasesOutput, lastPage bool) bool) bool {
callback(&kms.ListAliasesOutput{
Aliases: []*kms.AliasListEntry{
{AliasName: aws.String("alias/1"), TargetKeyId: aws.String("key-id-1")},
{AliasName: aws.String("alias/2"), TargetKeyId: aws.String("key-id-2")},
},
}, true)
return true
})).Return(nil).Once()
client.On("DescribeKey", &kms.DescribeKeyInput{KeyId: aws.String("key-id-1")}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyState: aws.String(kms.KeyStatePendingDeletion),
},
}, nil)
client.On("DescribeKey", &kms.DescribeKeyInput{KeyId: aws.String("key-id-2")}).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyState: aws.String(kms.KeyStateEnabled),
},
}, nil)
},
want: []*kms.AliasListEntry{
{AliasName: aws.String("alias/2"), TargetKeyId: aws.String("key-id-2")},
},
},
2021-02-25 13:19:49 +00:00
{
name: "List only customer aliases",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeKMS) {
2021-02-25 13:19:49 +00:00
client.On("ListAliasesPages",
&kms.ListAliasesInput{},
mock.MatchedBy(func(callback func(res *kms.ListAliasesOutput, lastPage bool) bool) bool {
callback(&kms.ListAliasesOutput{
Aliases: []*kms.AliasListEntry{
2021-07-06 15:08:47 +00:00
{AliasName: aws.String("alias/1"), TargetKeyId: aws.String("key-id-1")},
{AliasName: aws.String("alias/foo/2"), TargetKeyId: aws.String("key-id-2")},
{AliasName: aws.String("alias/aw/3"), TargetKeyId: aws.String("key-id-3")},
{AliasName: aws.String("alias/aws/4"), TargetKeyId: aws.String("key-id-4")},
{AliasName: aws.String("alias/aws/5"), TargetKeyId: aws.String("key-id-5")},
{AliasName: aws.String("alias/awss/6"), TargetKeyId: aws.String("key-id-6")},
{AliasName: aws.String("alias/aws7"), TargetKeyId: aws.String("key-id-7")},
2021-02-25 13:19:49 +00:00
},
}, true)
return true
})).Return(nil).Once()
2021-07-06 15:08:47 +00:00
client.On("DescribeKey", mock.Anything).Return(&kms.DescribeKeyOutput{
KeyMetadata: &kms.KeyMetadata{
KeyState: aws.String(kms.KeyStateEnabled),
},
}, nil)
2021-02-25 13:19:49 +00:00
},
want: []*kms.AliasListEntry{
2021-07-06 15:08:47 +00:00
{AliasName: aws.String("alias/1"), TargetKeyId: aws.String("key-id-1")},
{AliasName: aws.String("alias/foo/2"), TargetKeyId: aws.String("key-id-2")},
{AliasName: aws.String("alias/aw/3"), TargetKeyId: aws.String("key-id-3")},
{AliasName: aws.String("alias/awss/6"), TargetKeyId: aws.String("key-id-6")},
{AliasName: aws.String("alias/aws7"), TargetKeyId: aws.String("key-id-7")},
2021-02-25 13:19:49 +00:00
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeKMS{}
tt.mocks(&client)
2021-02-25 13:19:49 +00:00
r := &kmsRepository{
2021-07-06 15:08:47 +00:00
client: &client,
cache: store,
describeKeyLock: &sync.Mutex{},
2021-02-25 13:19:49 +00:00
}
got, err := r.ListAllAliases()
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.ListAllAliases()
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, []*kms.AliasListEntry{}, store.Get("kmsListAllAliases"))
}
2021-02-25 13:19:49 +00:00
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %v -> %v", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}