2020-12-09 15:31:34 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Remove grant field on remote resources when acl field != private in state
|
|
|
|
type S3BucketAcl struct{}
|
|
|
|
|
|
|
|
func NewS3BucketAcl() S3BucketAcl {
|
|
|
|
return S3BucketAcl{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m S3BucketAcl) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
|
|
|
|
|
|
|
|
for _, iacResource := range *resourcesFromState {
|
|
|
|
// Ignore all resources other than s3 buckets
|
|
|
|
if iacResource.TerraformType() != aws.AwsS3BucketResourceType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:36:05 +00:00
|
|
|
decodedIacResource, _ := iacResource.(*resource.AbstractResource)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
for _, remoteResource := range *remoteResources {
|
|
|
|
if resource.IsSameResource(remoteResource, decodedIacResource) {
|
2021-04-29 14:36:05 +00:00
|
|
|
decodedRemoteResource, _ := remoteResource.(*resource.AbstractResource)
|
|
|
|
aclAttr, exist := decodedIacResource.Attrs.Get("acl")
|
|
|
|
if !exist || aclAttr == nil || aclAttr == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if aclAttr != "private" {
|
2020-12-09 15:31:34 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"type": decodedRemoteResource.TerraformType(),
|
|
|
|
"id": decodedRemoteResource.TerraformId(),
|
|
|
|
}).Debug("Found a resource to update")
|
2021-04-29 14:36:05 +00:00
|
|
|
decodedRemoteResource.Attrs.SafeDelete([]string{"grant"})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 14:36:05 +00:00
|
|
|
|
|
|
|
decodedIacResource.Attrs.SafeDelete([]string{"acl"})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|