driftctl/pkg/middlewares/google_legacy_bucket_iam_me...

61 lines
1.8 KiB
Go
Raw Normal View History

2021-09-15 18:36:04 +00:00
package middlewares
import (
"strings"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/enumeration/resource"
"github.com/snyk/driftctl/enumeration/resource/google"
2021-09-15 18:36:04 +00:00
)
// Creating buckets add legacy role bindings, this middleware will filter them unless they are managed.
2021-10-06 15:03:47 +00:00
type GoogleLegacyBucketIAMMember struct{}
2021-09-15 18:36:04 +00:00
2021-10-06 15:03:47 +00:00
func NewGoogleLegacyBucketIAMMember() *GoogleLegacyBucketIAMMember {
return &GoogleLegacyBucketIAMMember{}
2021-09-15 18:36:04 +00:00
}
2021-10-06 15:03:47 +00:00
func (m *GoogleLegacyBucketIAMMember) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
2021-09-15 18:36:04 +00:00
newRemoteResources := make([]*resource.Resource, 0)
for _, remoteResource := range *remoteResources {
// Ignore all resources other than BucketIamBinding
if remoteResource.ResourceType() != google.GoogleStorageBucketIamMemberResourceType {
2021-09-15 18:36:04 +00:00
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
// Ignore all non-legacy member
2021-09-15 18:36:04 +00:00
if roleName := remoteResource.Attrs.GetString("role"); roleName != nil && !strings.Contains(*roleName, "legacy") {
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
// Check if member is managed by IaC
2021-09-15 18:36:04 +00:00
existInState := false
for _, stateResource := range *resourcesFromState {
if remoteResource.Equal(stateResource) {
existInState = true
break
}
}
// Include resource if it's managed in IaC
if existInState {
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
// Else, resource is not added to newRemoteResources slice, so it will be ignored
logrus.WithFields(logrus.Fields{
"id": remoteResource.ResourceId(),
"type": remoteResource.ResourceType(),
}).Debug("Ignoring legacy bucket member as it is not managed by IaC")
2021-09-15 18:36:04 +00:00
}
*remoteResources = newRemoteResources
return nil
}