driftctl/pkg/middlewares/aws_iam_policy_attachement_...

73 lines
2.4 KiB
Go
Raw Normal View History

2021-05-21 14:09:45 +00:00
package middlewares
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
/**
When listing policy attachment from aws we retrieve only user_policy_attachment or role_policy_attachment thus making it
impossible to compare with policy_attachment that could exist in terraform.
We decided to transform all attachments to policy_attachment so we can find which attachments are managed.
*/
type IamPolicyAttachmentTransformer struct {
resourceFactory resource.ResourceFactory
}
func NewIamPolicyAttachmentTransformer(resourceFactory resource.ResourceFactory) IamPolicyAttachmentTransformer {
return IamPolicyAttachmentTransformer{
resourceFactory,
}
}
2021-08-09 14:03:04 +00:00
func (m IamPolicyAttachmentTransformer) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
2021-05-21 14:09:45 +00:00
*remoteResources = m.transform(remoteResources)
*resourcesFromState = m.transform(resourcesFromState)
return nil
}
2021-08-09 14:03:04 +00:00
func (m IamPolicyAttachmentTransformer) transform(resources *[]*resource.Resource) []*resource.Resource {
var newResources []*resource.Resource
2021-05-21 14:09:45 +00:00
for _, res := range *resources {
if res.ResourceType() != aws.AwsIamUserPolicyAttachmentResourceType &&
res.ResourceType() != aws.AwsIamRolePolicyAttachmentResourceType {
2021-05-21 14:09:45 +00:00
newResources = append(newResources, res)
continue
}
if res.ResourceType() == aws.AwsIamUserPolicyAttachmentResourceType {
2021-05-21 14:09:45 +00:00
attrs := *res.Attributes()
policyAttachmentData := resource.Attributes{
"id": res.ResourceId(),
2021-05-21 14:09:45 +00:00
"policy_arn": attrs["policy_arn"],
"users": []interface{}{attrs["user"]},
"groups": []interface{}{},
"roles": []interface{}{},
}
policyAttachment := m.resourceFactory.CreateAbstractResource(aws.AwsIamPolicyAttachmentResourceType, res.ResourceId(), policyAttachmentData)
2021-05-21 14:09:45 +00:00
newResources = append(newResources, policyAttachment)
continue
}
if res.ResourceType() == aws.AwsIamRolePolicyAttachmentResourceType {
2021-05-21 14:09:45 +00:00
attrs := *res.Attributes()
policyAttachmentData := resource.Attributes{
"id": res.ResourceId(),
2021-05-21 14:09:45 +00:00
"policy_arn": attrs["policy_arn"],
"users": []interface{}{},
"groups": []interface{}{},
"roles": []interface{}{attrs["role"]},
}
policyAttachment := m.resourceFactory.CreateAbstractResource(aws.AwsIamPolicyAttachmentResourceType, res.ResourceId(), policyAttachmentData)
2021-05-21 14:09:45 +00:00
newResources = append(newResources, policyAttachment)
continue
}
}
return newResources
}