driftctl/pkg/remote/sns_scanner_test.go

349 lines
13 KiB
Go
Raw Normal View History

2021-07-06 09:31:01 +00:00
package remote
import (
"testing"
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/sns"
"github.com/cloudskiff/driftctl/mocks"
2021-07-26 09:55:59 +00:00
"github.com/cloudskiff/driftctl/pkg/filter"
2021-08-03 13:01:20 +00:00
"github.com/cloudskiff/driftctl/pkg/remote/alerts"
2021-07-06 09:31:01 +00:00
"github.com/cloudskiff/driftctl/pkg/remote/aws"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/cloudskiff/driftctl/pkg/remote/common"
remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error"
2021-07-06 09:31:01 +00:00
testresource "github.com/cloudskiff/driftctl/test/resource"
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
"github.com/pkg/errors"
2021-07-06 09:31:01 +00:00
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
"github.com/stretchr/testify/assert"
)
func TestScanSNSTopic(t *testing.T) {
cases := []struct {
test string
dirName string
2021-08-20 13:15:51 +00:00
mocks func(*repository.MockSNSRepository, *mocks.AlerterInterface)
2021-07-06 09:31:01 +00:00
err error
}{
{
test: "no SNS Topic",
dirName: "sns_topic_empty",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-06 09:31:01 +00:00
client.On("ListAllTopics").Return([]*sns.Topic{}, nil)
},
err: nil,
},
{
test: "Multiple SNSTopic",
dirName: "sns_topic_multiple",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-06 09:31:01 +00:00
client.On("ListAllTopics").Return([]*sns.Topic{
{TopicArn: awssdk.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic")},
{TopicArn: awssdk.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic2")},
{TopicArn: awssdk.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic3")},
}, nil)
},
err: nil,
},
{
test: "cannot list SNSTopic",
dirName: "sns_topic_empty",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
client.On("ListAllTopics").Return(nil, awsError)
2021-08-20 13:15:51 +00:00
alerter.On("SendAlert", resourceaws.AwsSnsTopicResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsSnsTopicResourceType, resourceaws.AwsSnsTopicResourceType), alerts.EnumerationPhase)).Return()
2021-07-06 09:31:01 +00:00
},
err: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range cases {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
2021-08-20 13:15:51 +00:00
sess := session.Must(session.NewSessionWithOptions(session.Options{
2021-07-06 09:31:01 +00:00
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
2021-08-20 13:15:51 +00:00
alerter := &mocks.AlerterInterface{}
2021-07-06 09:31:01 +00:00
fakeRepo := &repository.MockSNSRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
2021-07-06 09:31:01 +00:00
var repo repository.SNSRepository = fakeRepo
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()
2021-08-20 13:15:51 +00:00
repo = repository.NewSNSRepository(sess, cache.New(0))
2021-07-06 09:31:01 +00:00
}
remoteLibrary.AddEnumerator(aws.NewSNSTopicEnumerator(repo, factory))
2021-08-09 15:15:35 +00:00
remoteLibrary.AddDetailsFetcher(resourceaws.AwsSnsTopicResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsSnsTopicResourceType, provider, deserializer))
2021-07-06 09:31:01 +00:00
2021-07-26 09:55:59 +00:00
testFilter := &filter.MockFilter{}
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
2021-07-06 09:31:01 +00:00
got, err := s.Resources()
assert.Equal(tt, c.err, err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsSnsTopicResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-08-20 13:15:51 +00:00
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
2021-07-06 09:31:01 +00:00
})
}
}
2021-07-06 16:46:38 +00:00
func TestSNSTopicPolicyScan(t *testing.T) {
cases := []struct {
test string
dirName string
2021-08-20 13:15:51 +00:00
mocks func(*repository.MockSNSRepository, *mocks.AlerterInterface)
2021-07-06 16:46:38 +00:00
err error
}{
{
test: "no SNS Topic policy",
dirName: "sns_topic_policy_empty",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-06 16:46:38 +00:00
client.On("ListAllTopics").Return([]*sns.Topic{}, nil)
},
err: nil,
},
{
test: "Multiple SNSTopicPolicy",
dirName: "sns_topic_policy_multiple",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-06 16:46:38 +00:00
client.On("ListAllTopics").Return([]*sns.Topic{
{TopicArn: awssdk.String("arn:aws:sns:us-east-1:526954929923:my-topic-with-policy")},
{TopicArn: awssdk.String("arn:aws:sns:us-east-1:526954929923:my-topic-with-policy2")},
}, nil)
},
err: nil,
},
{
test: "cannot list SNSTopic",
dirName: "sns_topic_policy_topic_list",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
client.On("ListAllTopics").Return(nil, awsError)
2021-08-20 13:15:51 +00:00
alerter.On("SendAlert", resourceaws.AwsSnsTopicPolicyResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsSnsTopicPolicyResourceType, resourceaws.AwsSnsTopicResourceType), alerts.EnumerationPhase)).Return()
2021-07-06 16:46:38 +00:00
},
err: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range cases {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
2021-08-20 13:15:51 +00:00
sess := session.Must(session.NewSessionWithOptions(session.Options{
2021-07-06 16:46:38 +00:00
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
2021-08-20 13:15:51 +00:00
alerter := &mocks.AlerterInterface{}
2021-07-06 16:46:38 +00:00
fakeRepo := &repository.MockSNSRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
2021-07-06 16:46:38 +00:00
var repo repository.SNSRepository = fakeRepo
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()
2021-08-20 13:15:51 +00:00
repo = repository.NewSNSRepository(sess, cache.New(0))
2021-07-06 16:46:38 +00:00
}
remoteLibrary.AddEnumerator(aws.NewSNSTopicPolicyEnumerator(repo, factory))
2021-08-09 15:15:35 +00:00
remoteLibrary.AddDetailsFetcher(resourceaws.AwsSnsTopicPolicyResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsSnsTopicPolicyResourceType, provider, deserializer))
2021-07-06 16:46:38 +00:00
2021-07-26 09:55:59 +00:00
testFilter := &filter.MockFilter{}
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
2021-07-06 16:46:38 +00:00
got, err := s.Resources()
assert.Equal(tt, c.err, err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsSnsTopicPolicyResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-08-20 13:15:51 +00:00
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
2021-07-06 16:46:38 +00:00
})
}
}
2021-07-07 17:23:43 +00:00
func TestSNSTopicSubscriptionScan(t *testing.T) {
cases := []struct {
test string
dirName string
2021-08-20 13:15:51 +00:00
mocks func(*repository.MockSNSRepository, *mocks.AlerterInterface)
2021-07-07 17:23:43 +00:00
err error
}{
{
test: "no SNS Topic Subscription",
dirName: "sns_topic_subscription_empty",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-07 17:23:43 +00:00
client.On("ListAllSubscriptions").Return([]*sns.Subscription{}, nil)
},
2021-08-20 13:15:51 +00:00
err: nil,
2021-07-07 17:23:43 +00:00
},
{
test: "Multiple SNSTopic Subscription",
dirName: "sns_topic_subscription_multiple",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-07 17:23:43 +00:00
client.On("ListAllSubscriptions").Return([]*sns.Subscription{
{SubscriptionArn: awssdk.String("arn:aws:sns:us-east-1:526954929923:user-updates-topic2:c0f794c5-a009-4db4-9147-4c55959787fa")},
{SubscriptionArn: awssdk.String("arn:aws:sns:us-east-1:526954929923:user-updates-topic:b6e66147-2b31-4486-8d4b-2a2272264c8e")},
}, nil)
},
2021-08-20 13:15:51 +00:00
err: nil,
2021-07-07 17:23:43 +00:00
},
{
test: "Multiple SNSTopic Subscription with one pending and one incorrect",
dirName: "sns_topic_subscription_multiple",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
2021-07-07 17:23:43 +00:00
client.On("ListAllSubscriptions").Return([]*sns.Subscription{
{SubscriptionArn: awssdk.String("PendingConfirmation"), Endpoint: awssdk.String("TEST")},
{SubscriptionArn: awssdk.String("Incorrect"), Endpoint: awssdk.String("INCORRECT")},
{SubscriptionArn: awssdk.String("arn:aws:sns:us-east-1:526954929923:user-updates-topic2:c0f794c5-a009-4db4-9147-4c55959787fa")},
{SubscriptionArn: awssdk.String("arn:aws:sns:us-east-1:526954929923:user-updates-topic:b6e66147-2b31-4486-8d4b-2a2272264c8e")},
}, nil)
2021-08-20 13:15:51 +00:00
alerter.On("SendAlert", "aws_sns_topic_subscription.PendingConfirmation", aws.NewWrongArnTopicAlert("PendingConfirmation", awssdk.String("TEST"))).Return()
alerter.On("SendAlert", "aws_sns_topic_subscription.Incorrect", aws.NewWrongArnTopicAlert("Incorrect", awssdk.String("INCORRECT"))).Return()
2021-07-07 17:23:43 +00:00
},
err: nil,
},
{
test: "cannot list SNSTopic subscription",
dirName: "sns_topic_subscription_list",
2021-08-20 13:15:51 +00:00
mocks: func(client *repository.MockSNSRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
client.On("ListAllSubscriptions").Return(nil, awsError)
2021-08-20 13:15:51 +00:00
alerter.On("SendAlert", resourceaws.AwsSnsTopicSubscriptionResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsSnsTopicSubscriptionResourceType, resourceaws.AwsSnsTopicSubscriptionResourceType), alerts.EnumerationPhase)).Return()
2021-07-07 17:23:43 +00:00
},
err: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range cases {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
2021-08-20 13:15:51 +00:00
sess := session.Must(session.NewSessionWithOptions(session.Options{
2021-07-07 17:23:43 +00:00
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
2021-08-20 13:15:51 +00:00
alerter := &mocks.AlerterInterface{}
2021-07-07 17:23:43 +00:00
fakeRepo := &repository.MockSNSRepository{}
2021-08-20 13:15:51 +00:00
c.mocks(fakeRepo, alerter)
2021-07-07 17:23:43 +00:00
var repo repository.SNSRepository = fakeRepo
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()
2021-08-20 13:15:51 +00:00
repo = repository.NewSNSRepository(sess, cache.New(0))
2021-07-07 17:23:43 +00:00
}
remoteLibrary.AddEnumerator(aws.NewSNSTopicSubscriptionEnumerator(repo, factory, alerter))
2021-08-09 15:15:35 +00:00
remoteLibrary.AddDetailsFetcher(resourceaws.AwsSnsTopicSubscriptionResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsSnsTopicSubscriptionResourceType, provider, deserializer))
2021-07-07 17:23:43 +00:00
2021-07-26 09:55:59 +00:00
testFilter := &filter.MockFilter{}
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
2021-07-07 17:23:43 +00:00
got, err := s.Resources()
assert.Equal(tt, err, c.err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsSnsTopicSubscriptionResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-08-20 13:15:51 +00:00
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
2021-07-07 17:23:43 +00:00
})
}
}