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{}
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (m S3BucketAcl) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
for _, iacResource := range *resourcesFromState {
|
|
|
|
// Ignore all resources other than s3 buckets
|
|
|
|
if iacResource.TerraformType() != aws.AwsS3BucketResourceType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, remoteResource := range *remoteResources {
|
2021-08-09 14:03:04 +00:00
|
|
|
if remoteResource.Equal(iacResource) {
|
|
|
|
aclAttr, exist := iacResource.Attrs.Get("acl")
|
2021-04-29 14:36:05 +00:00
|
|
|
if !exist || aclAttr == nil || aclAttr == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if aclAttr != "private" {
|
2020-12-09 15:31:34 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-08-09 14:03:04 +00:00
|
|
|
"type": remoteResource.TerraformType(),
|
|
|
|
"id": remoteResource.TerraformId(),
|
2020-12-09 15:31:34 +00:00
|
|
|
}).Debug("Found a resource to update")
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResource.Attrs.SafeDelete([]string{"grant"})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 14:36:05 +00:00
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
iacResource.Attrs.SafeDelete([]string{"acl"})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|