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.
|
2021-07-08 14:13:51 +00:00
|
|
|
type AwsDefaultSQSQueuePolicy struct{}
|
2021-01-29 11:35:11 +00:00
|
|
|
|
2021-07-08 14:13:51 +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
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() != aws.AwsSqsQueuePolicyResourceType {
|
2021-01-29 11:35:11 +00:00
|
|
|
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")
|
2021-04-30 17:19:26 +00:00
|
|
|
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{
|
2021-08-18 13:58:28 +00:00
|
|
|
"id": res.ResourceId(),
|
|
|
|
"type": res.ResourceType(),
|
2021-01-29 11:35:11 +00:00
|
|
|
}).Debug("Ignoring default queue policy as it is not managed by IaC")
|
|
|
|
}
|
|
|
|
*remoteResources = newRemoteResources
|
|
|
|
return nil
|
|
|
|
}
|