driftctl/pkg/middlewares/aws_default_sqs_queue_polic...

62 lines
1.9 KiB
Go
Raw Normal View History

2021-01-29 11:35:11 +00:00
package middlewares
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/sirupsen/logrus"
)
// SQS queues from AWS have a weird behaviour when we fetch them.
// By default they have a Policy attached with only an ID
// "arn:aws:sqs:eu-west-3:XXXXXXXXXXXX:foobar/SQSDefaultPolicy" but on fetch
// the SDK return an empty policy (e.g. policy = "").
// We need to ignore those policy from unmanaged resources if they are not managed
// by IaC.
type AwsDefaultSQSQueuePolicy struct{}
2021-01-29 11:35:11 +00:00
func NewAwsDefaultSQSQueuePolicy() AwsDefaultSQSQueuePolicy {
return AwsDefaultSQSQueuePolicy{}
2021-01-29 11:35:11 +00:00
}
2021-08-09 14:03:04 +00:00
func (m AwsDefaultSQSQueuePolicy) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
newRemoteResources := make([]*resource.Resource, 0)
2021-01-29 11:35:11 +00:00
for _, res := range *remoteResources {
// Ignore all resources other than sqs_queue_policy
if res.TerraformType() != aws.AwsSqsQueuePolicyResourceType {
newRemoteResources = append(newRemoteResources, res)
continue
}
// Ignore all non-default queue policy
2021-08-09 14:03:04 +00:00
pol, exists := res.Attrs.Get("policy")
policy := pol.(string)
if exists && policy != "" {
2021-08-09 14:03:04 +00:00
newRemoteResources = append(newRemoteResources, res)
2021-01-29 11:35:11 +00:00
continue
}
// Check if queue policy is managed by IaC
existInState := false
for _, stateResource := range *resourcesFromState {
2021-08-09 14:03:04 +00:00
if res.Equal(stateResource) {
2021-01-29 11:35:11 +00:00
existInState = true
break
}
}
// Include resource if it's managed in IaC
if existInState {
newRemoteResources = append(newRemoteResources, res)
continue
}
// Else, resource is not added to newRemoteResources slice so it will be ignored
logrus.WithFields(logrus.Fields{
"id": res.TerraformId(),
"type": res.TerraformType(),
}).Debug("Ignoring default queue policy as it is not managed by IaC")
}
*remoteResources = newRemoteResources
return nil
}