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 {
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() != aws.AwsIamUserPolicyAttachmentResourceType &&
|
|
|
|
res.ResourceType() != aws.AwsIamRolePolicyAttachmentResourceType {
|
2021-05-21 14:09:45 +00:00
|
|
|
newResources = append(newResources, res)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() == aws.AwsIamUserPolicyAttachmentResourceType {
|
2021-05-21 14:09:45 +00:00
|
|
|
attrs := *res.Attributes()
|
|
|
|
policyAttachmentData := resource.Attributes{
|
2021-08-18 13:58:28 +00:00
|
|
|
"id": res.ResourceId(),
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": attrs["policy_arn"],
|
|
|
|
"users": []interface{}{attrs["user"]},
|
|
|
|
"groups": []interface{}{},
|
|
|
|
"roles": []interface{}{},
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:58:28 +00:00
|
|
|
policyAttachment := m.resourceFactory.CreateAbstractResource(aws.AwsIamPolicyAttachmentResourceType, res.ResourceId(), policyAttachmentData)
|
2021-05-21 14:09:45 +00:00
|
|
|
|
|
|
|
newResources = append(newResources, policyAttachment)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() == aws.AwsIamRolePolicyAttachmentResourceType {
|
2021-05-21 14:09:45 +00:00
|
|
|
attrs := *res.Attributes()
|
|
|
|
policyAttachmentData := resource.Attributes{
|
2021-08-18 13:58:28 +00:00
|
|
|
"id": res.ResourceId(),
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": attrs["policy_arn"],
|
|
|
|
"users": []interface{}{},
|
|
|
|
"groups": []interface{}{},
|
|
|
|
"roles": []interface{}{attrs["role"]},
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:58:28 +00:00
|
|
|
policyAttachment := m.resourceFactory.CreateAbstractResource(aws.AwsIamPolicyAttachmentResourceType, res.ResourceId(), policyAttachmentData)
|
2021-05-21 14:09:45 +00:00
|
|
|
|
|
|
|
newResources = append(newResources, policyAttachment)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newResources
|
|
|
|
}
|