driftctl/enumeration/remote/aws/sqs_queue_policy_enumerator.go

70 lines
1.9 KiB
Go
Raw Normal View History

package aws
import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
"strings"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/enumeration/resource"
"github.com/snyk/driftctl/enumeration/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
}
2021-08-09 14:03:04 +00:00
func (e *SQSQueuePolicyEnumerator) Enumerate() ([]*resource.Resource, error) {
queues, err := e.repository.ListAllQueues()
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsSqsQueueResourceType)
}
2021-08-09 14:03:04 +00:00
results := make([]*resource.Resource, 0, len(queues))
for _, queue := range queues {
2021-07-23 08:40:42 +00:00
attrs := map[string]interface{}{
"policy": "",
}
attributes, err := e.repository.GetQueueAttributes(*queue)
if err != nil {
if strings.Contains(err.Error(), "NonExistentQueue") {
logrus.WithFields(logrus.Fields{
"queue": *queue,
"type": aws.AwsSqsQueueResourceType,
}).Debugf("Ignoring queue that seems to be already deleted: %+v", err)
continue
}
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
2021-07-23 08:40:42 +00:00
if attributes.Attributes != nil {
attrs["policy"] = *attributes.Attributes[sqs.QueueAttributeNamePolicy]
}
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
awssdk.StringValue(queue),
2021-07-23 08:40:42 +00:00
attrs,
),
)
}
return results, err
}