driftctl/enumeration/remote/aws_kms_scanner_test.go

227 lines
8.0 KiB
Go
Raw Normal View History

2021-07-01 14:45:46 +00:00
package remote
import (
"testing"
"github.com/snyk/driftctl/enumeration"
"github.com/snyk/driftctl/enumeration/remote/alerts"
aws2 "github.com/snyk/driftctl/enumeration/remote/aws"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
2022-07-06 08:44:42 +00:00
"github.com/snyk/driftctl/enumeration/remote/common"
remoteerr "github.com/snyk/driftctl/enumeration/remote/error"
terraform3 "github.com/snyk/driftctl/enumeration/terraform"
2021-07-01 14:45:46 +00:00
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/pkg/errors"
"github.com/snyk/driftctl/enumeration/resource"
resourceaws "github.com/snyk/driftctl/enumeration/resource/aws"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/mocks"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/goldenfile"
testresource "github.com/snyk/driftctl/test/resource"
terraform2 "github.com/snyk/driftctl/test/terraform"
2021-07-01 14:45:46 +00:00
"github.com/stretchr/testify/assert"
2021-07-06 10:12:55 +00:00
"github.com/stretchr/testify/mock"
2021-07-01 14:45:46 +00:00
)
func TestKMSKey(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(*repository.MockKMSRepository, *mocks.AlerterInterface)
2021-07-01 14:45:46 +00:00
wantErr error
}{
{
test: "no keys",
dirName: "aws_kms_key_empty",
mocks: func(repository *repository.MockKMSRepository, alerter *mocks.AlerterInterface) {
2021-07-01 14:45:46 +00:00
repository.On("ListAllKeys").Return([]*kms.KeyListEntry{}, nil)
},
},
{
test: "multiple keys",
dirName: "aws_kms_key_multiple",
mocks: func(repository *repository.MockKMSRepository, alerter *mocks.AlerterInterface) {
2021-07-01 14:45:46 +00:00
repository.On("ListAllKeys").Return([]*kms.KeyListEntry{
{KeyId: awssdk.String("8ee21d91-c000-428c-8032-235aac55da36")},
{KeyId: awssdk.String("5d765f32-bfdc-4610-b6ab-f82db5d0601b")},
{KeyId: awssdk.String("89d2c023-ea53-40a5-b20a-d84905c622d7")},
}, nil)
},
},
{
test: "cannot list keys",
dirName: "aws_kms_key_list",
mocks: func(repository *repository.MockKMSRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
repository.On("ListAllKeys").Return(nil, awsError)
2021-08-20 13:15:51 +00:00
2022-07-06 08:44:42 +00:00
alerter.On("SendAlert", resourceaws.AwsKmsKeyResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsKmsKeyResourceType, resourceaws.AwsKmsKeyResourceType), alerts.EnumerationPhase)).Return()
2021-07-01 14:45:46 +00:00
},
2021-07-06 10:12:55 +00:00
wantErr: nil,
2021-07-01 14:45:46 +00:00
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform3.NewTerraformResourceFactory(schemaRepository)
2021-07-01 14:45:46 +00:00
deserializer := resource.NewDeserializer(factory)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform3.NewProviderLibrary()
2022-07-06 08:44:42 +00:00
remoteLibrary := common.NewRemoteLibrary()
2021-07-01 14:45:46 +00:00
// Initialize mocks
2021-07-06 10:12:55 +00:00
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockKMSRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
var repo repository.KMSRepository = fakeRepo
2021-07-01 14:45:46 +00:00
providerVersion := "3.19.0"
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
if err != nil {
t.Fatal(err)
}
provider := terraform2.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
repo = repository.NewKMSRepository(sess, cache.New(0))
2021-07-01 14:45:46 +00:00
}
remoteLibrary.AddEnumerator(aws2.NewKMSKeyEnumerator(repo, factory))
2022-07-06 08:44:42 +00:00
remoteLibrary.AddDetailsFetcher(resourceaws.AwsKmsKeyResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsKmsKeyResourceType, provider, deserializer))
2021-07-01 14:45:46 +00:00
testFilter := &enumeration.MockFilter{}
2021-07-26 09:55:59 +00:00
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
2021-07-01 14:45:46 +00:00
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsKmsKeyResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-08-20 13:15:51 +00:00
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
2021-07-01 14:45:46 +00:00
})
}
}
2021-07-01 15:03:40 +00:00
func TestKMSAlias(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(*repository.MockKMSRepository, *mocks.AlerterInterface)
2021-07-01 15:03:40 +00:00
wantErr error
}{
{
test: "no aliases",
dirName: "aws_kms_alias_empty",
mocks: func(repository *repository.MockKMSRepository, alerter *mocks.AlerterInterface) {
2021-07-01 15:03:40 +00:00
repository.On("ListAllAliases").Return([]*kms.AliasListEntry{}, nil)
},
},
{
test: "multiple aliases",
dirName: "aws_kms_alias_multiple",
mocks: func(repository *repository.MockKMSRepository, alerter *mocks.AlerterInterface) {
2021-07-01 15:03:40 +00:00
repository.On("ListAllAliases").Return([]*kms.AliasListEntry{
{AliasName: awssdk.String("alias/foo")},
{AliasName: awssdk.String("alias/bar")},
{AliasName: awssdk.String("alias/baz20210225124429210500000001")},
}, nil)
},
},
{
test: "cannot list aliases",
dirName: "aws_kms_alias_list",
mocks: func(repository *repository.MockKMSRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
repository.On("ListAllAliases").Return(nil, awsError)
2021-08-20 13:15:51 +00:00
2022-07-06 08:44:42 +00:00
alerter.On("SendAlert", resourceaws.AwsKmsAliasResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsKmsAliasResourceType, resourceaws.AwsKmsAliasResourceType), alerts.EnumerationPhase)).Return()
2021-07-01 15:03:40 +00:00
},
2021-07-06 10:12:55 +00:00
wantErr: nil,
2021-07-01 15:03:40 +00:00
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform3.NewTerraformResourceFactory(schemaRepository)
2021-07-01 15:03:40 +00:00
deserializer := resource.NewDeserializer(factory)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform3.NewProviderLibrary()
2022-07-06 08:44:42 +00:00
remoteLibrary := common.NewRemoteLibrary()
2021-07-01 15:03:40 +00:00
// Initialize mocks
2021-07-06 10:12:55 +00:00
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockKMSRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
var repo repository.KMSRepository = fakeRepo
2021-07-01 15:03:40 +00:00
providerVersion := "3.19.0"
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
if err != nil {
t.Fatal(err)
}
provider := terraform2.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
repo = repository.NewKMSRepository(sess, cache.New(0))
2021-07-01 15:03:40 +00:00
}
remoteLibrary.AddEnumerator(aws2.NewKMSAliasEnumerator(repo, factory))
2022-07-06 08:44:42 +00:00
remoteLibrary.AddDetailsFetcher(resourceaws.AwsKmsAliasResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsKmsAliasResourceType, provider, deserializer))
2021-07-01 15:03:40 +00:00
testFilter := &enumeration.MockFilter{}
2021-07-26 09:55:59 +00:00
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
2021-07-01 15:03:40 +00:00
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsKmsAliasResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-08-20 13:15:51 +00:00
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
2021-07-01 15:03:40 +00:00
})
}
}