2020-12-09 15:31:34 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Split Policy attachment when there is multiple user and groups and generate a repeatable id
|
2021-05-12 09:35:41 +00:00
|
|
|
type IamPolicyAttachmentExpander struct {
|
2021-05-07 15:47:53 +00:00
|
|
|
resourceFactory resource.ResourceFactory
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-05-12 09:35:41 +00:00
|
|
|
func NewIamPolicyAttachmentExpander(resourceFactory resource.ResourceFactory) IamPolicyAttachmentExpander {
|
|
|
|
return IamPolicyAttachmentExpander{
|
2021-05-07 15:47:53 +00:00
|
|
|
resourceFactory,
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 09:35:41 +00:00
|
|
|
func (m IamPolicyAttachmentExpander) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
|
2020-12-09 15:31:34 +00:00
|
|
|
var newStateResources = make([]resource.Resource, 0)
|
|
|
|
|
|
|
|
for _, stateResource := range *resourcesFromState {
|
|
|
|
// Ignore all resources other than policy attachment
|
|
|
|
if stateResource.TerraformType() != resourceaws.AwsIamPolicyAttachmentResourceType {
|
|
|
|
newStateResources = append(newStateResources, stateResource)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:47:53 +00:00
|
|
|
policyAttachment := stateResource.(*resource.AbstractResource)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-05-12 09:35:41 +00:00
|
|
|
newStateResources = append(newStateResources, m.expand(policyAttachment)...)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var newRemoteResources = make([]resource.Resource, 0)
|
|
|
|
|
2021-05-14 14:57:16 +00:00
|
|
|
for _, remoteResource := range *remoteResources {
|
2020-12-09 15:31:34 +00:00
|
|
|
// Ignore all resources other than policy attachment
|
2021-05-14 14:57:16 +00:00
|
|
|
if remoteResource.TerraformType() != resourceaws.AwsIamPolicyAttachmentResourceType {
|
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
2020-12-09 15:31:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-14 14:57:16 +00:00
|
|
|
policyAttachment := remoteResource.(*resource.AbstractResource)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-05-12 09:35:41 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, m.expand(policyAttachment)...)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*resourcesFromState = newStateResources
|
|
|
|
*remoteResources = newRemoteResources
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:35:41 +00:00
|
|
|
func (m IamPolicyAttachmentExpander) expand(policyAttachment *resource.AbstractResource) []resource.Resource {
|
2021-05-21 14:09:45 +00:00
|
|
|
var newResources []resource.Resource
|
|
|
|
users := policyAttachment.Attrs.GetSlice("users")
|
|
|
|
// we create one attachment per user
|
|
|
|
for _, user := range users {
|
2021-05-14 14:57:16 +00:00
|
|
|
user := user.(string)
|
|
|
|
newAttachment := m.resourceFactory.CreateAbstractResource(
|
|
|
|
resourceaws.AwsIamPolicyAttachmentResourceType,
|
2021-05-21 14:09:45 +00:00
|
|
|
fmt.Sprintf("%s-%s", user, (*policyAttachment.Attrs)["policy_arn"]),
|
2021-05-14 14:57:16 +00:00
|
|
|
map[string]interface{}{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": *policyAttachment.Attrs.GetString("policy_arn"),
|
|
|
|
"users": []interface{}{user},
|
2021-05-14 14:57:16 +00:00
|
|
|
},
|
|
|
|
)
|
2021-05-21 14:09:45 +00:00
|
|
|
newResources = append(newResources, newAttachment)
|
2021-05-14 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
roles := policyAttachment.Attrs.GetSlice("roles")
|
|
|
|
// we create one attachment per role
|
|
|
|
for _, role := range roles {
|
2021-05-14 14:57:16 +00:00
|
|
|
role := role.(string)
|
|
|
|
newAttachment := m.resourceFactory.CreateAbstractResource(
|
|
|
|
resourceaws.AwsIamPolicyAttachmentResourceType,
|
2021-05-21 14:09:45 +00:00
|
|
|
fmt.Sprintf("%s-%s", role, (*policyAttachment.Attrs)["policy_arn"]),
|
2021-05-14 14:57:16 +00:00
|
|
|
map[string]interface{}{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": *policyAttachment.Attrs.GetString("policy_arn"),
|
|
|
|
"roles": []interface{}{role},
|
2021-05-14 14:57:16 +00:00
|
|
|
},
|
|
|
|
)
|
2021-05-21 14:09:45 +00:00
|
|
|
newResources = append(newResources, newAttachment)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
return newResources
|
|
|
|
}
|