Merge pull request #817 from cloudskiff/issue_749_sqs_policy_split

Split sqs queue policy supplier
main
William BEUIL 2021-07-08 14:53:01 +02:00 committed by GitHub
commit 296ba1191a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 144 additions and 343679 deletions

View File

@ -120,6 +120,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewSqsQueueEnumerator(sqsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsSqsQueueResourceType, common.NewGenericDetailsFetcher(aws.AwsSqsQueueResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewSQSQueuePolicyEnumerator(sqsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsSqsQueuePolicyResourceType, common.NewGenericDetailsFetcher(aws.AwsSqsQueuePolicyResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
@ -136,7 +138,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, sqsRepository))
supplierLibrary.AddSupplier(NewSNSTopicSupplier(provider, deserializer, snsRepository))
supplierLibrary.AddSupplier(NewSNSTopicPolicySupplier(provider, deserializer, snsRepository))
supplierLibrary.AddSupplier(NewSNSTopicSubscriptionSupplier(provider, alerter, deserializer, snsRepository))

View File

@ -0,0 +1,48 @@
package aws
import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
awssdk "github.com/aws/aws-sdk-go/aws"
)
type SQSQueuePolicyEnumerator struct {
repository repository.SQSRepository
factory resource.ResourceFactory
}
func NewSQSQueuePolicyEnumerator(repo repository.SQSRepository, factory resource.ResourceFactory) *SQSQueuePolicyEnumerator {
return &SQSQueuePolicyEnumerator{
repository: repo,
factory: factory,
}
}
func (e *SQSQueuePolicyEnumerator) SupportedType() resource.ResourceType {
return aws.AwsSqsQueuePolicyResourceType
}
func (e *SQSQueuePolicyEnumerator) Enumerate() ([]resource.Resource, error) {
queues, err := e.repository.ListAllQueues()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, string(e.SupportedType()), aws.AwsSqsQueueResourceType)
}
results := make([]resource.Resource, len(queues))
for _, queue := range queues {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
awssdk.StringValue(queue),
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -1,64 +0,0 @@
package aws
import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type SqsQueuePolicySupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.SQSRepository
runner *terraform.ParallelResourceReader
}
func NewSqsQueuePolicySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.SQSRepository) *SqsQueuePolicySupplier {
return &SqsQueuePolicySupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *SqsQueuePolicySupplier) Resources() ([]resource.Resource, error) {
queues, err := s.repository.ListAllQueues()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, aws.AwsSqsQueuePolicyResourceType, aws.AwsSqsQueueResourceType)
}
for _, queue := range queues {
q := *queue
s.runner.Run(func() (cty.Value, error) {
return s.readSqsQueuePolicy(q)
})
}
resources, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(aws.AwsSqsQueuePolicyResourceType, resources)
}
func (s *SqsQueuePolicySupplier) readSqsQueuePolicy(queueURL string) (cty.Value, error) {
var Ty resource.ResourceType = aws.AwsSqsQueuePolicyResourceType
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: Ty,
ID: queueURL,
})
if err != nil {
logrus.WithFields(logrus.Fields{
"type": Ty,
}).Error(err)
return cty.NilVal, err
}
return *val, nil
}

View File

@ -1,100 +0,0 @@
package aws
import (
"context"
"testing"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
testresource "github.com/cloudskiff/driftctl/test/resource"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestSqsQueuePolicySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockSQSRepository)
err error
}{
{
// sqs queue with no policy case is not possible
// as a default SQSDefaultPolicy (e.g. policy="") will always be present in each queue
test: "no sqs queue policies",
dirName: "sqs_queue_policy_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{}, nil)
},
err: nil,
},
{
test: "multiple sqs queue policies (default or not)",
dirName: "sqs_queue_policy_multiple",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/bar.fifo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/foo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/baz"),
}, nil)
},
err: nil,
},
{
test: "cannot list sqs queues, thus sqs queue policies",
dirName: "sqs_queue_policy_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsSqsQueuePolicyResourceType, resourceaws.AwsSqsQueueResourceType),
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(repo)
factory := terraform.NewTerraformResourceFactory(repo)
deserializer := resource.NewDeserializer(factory)
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, repository.NewSQSRepository(provider.session, cache.New(0))))
}
t.Run(c.test, func(tt *testing.T) {
fakeSQS := repository.MockSQSRepository{}
c.mocks(&fakeSQS)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &SqsQueuePolicySupplier{
provider,
deserializer,
&fakeSQS,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -112,3 +112,97 @@ func TestSqsQueue(t *testing.T) {
})
}
}
func TestSQSQueuePolicy(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockSQSRepository)
wantErr error
}{
{
// sqs queue with no policy case is not possible
// as a default SQSDefaultPolicy (e.g. policy="") will always be present in each queue
test: "no sqs queue policies",
dirName: "sqs_queue_policy_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{}, nil)
},
wantErr: nil,
},
{
test: "multiple sqs queue policies (default or not)",
dirName: "sqs_queue_policy_multiple",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/bar.fifo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/foo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/baz"),
}, nil)
},
wantErr: nil,
},
{
test: "cannot list sqs queues, thus sqs queue policies",
dirName: "sqs_queue_policy_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: 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
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
alerter.On("SendAlert", mock.Anything, mock.Anything).Maybe().Return()
fakeRepo := &repository.MockSQSRepository{}
c.mocks(fakeRepo)
var repo repository.SQSRepository = 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()
repo = repository.NewSQSRepository(sess, cache.New(0))
}
remoteLibrary.AddEnumerator(aws.NewSQSQueuePolicyEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsSqsQueuePolicyResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsSqsQueuePolicyResourceType, provider, deserializer))
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsSqsQueuePolicyResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}