2021-09-15 18:36:04 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/resource"
|
|
|
|
"github.com/snyk/driftctl/pkg/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
|
2021-10-04 17:03:44 +00:00
|
|
|
if remoteResource.ResourceType() != google.GoogleStorageBucketIamMemberResourceType {
|
2021-09-15 18:36:04 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:03:44 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:03:44 +00:00
|
|
|
// 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(),
|
2021-10-04 17:03:44 +00:00
|
|
|
}).Debug("Ignoring legacy bucket member as it is not managed by IaC")
|
2021-09-15 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*remoteResources = newRemoteResources
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|