2021-09-29 15:43:56 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-10-01 09:02:46 +00:00
|
|
|
"strings"
|
2021-09-29 15:43:56 +00:00
|
|
|
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
"github.com/snyk/driftctl/enumeration/resource/google"
|
2021-09-29 15:43:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GoogleStorageBucketIAMPolicyTransformer Transforms Bucket IAM policy in bucket iam binding to ease comparison.
|
|
|
|
type GoogleStorageBucketIAMPolicyTransformer struct {
|
|
|
|
resourceFactory resource.ResourceFactory
|
2021-10-01 09:02:46 +00:00
|
|
|
resFieldByType map[string]string // map of the field to add to resource attribute for all supported type
|
2021-09-29 15:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 09:02:46 +00:00
|
|
|
func NewGoogleIAMPolicyTransformer(resourceFactory resource.ResourceFactory) *GoogleStorageBucketIAMPolicyTransformer {
|
|
|
|
return &GoogleStorageBucketIAMPolicyTransformer{
|
|
|
|
resourceFactory,
|
|
|
|
map[string]string{
|
|
|
|
google.GoogleStorageBucketIamPolicyResourceType: "bucket",
|
|
|
|
google.GoogleProjectIamPolicyResourceType: "project",
|
|
|
|
}}
|
2021-09-29 15:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *GoogleStorageBucketIAMPolicyTransformer) Execute(_, resourcesFromState *[]*resource.Resource) error {
|
|
|
|
|
|
|
|
resources := make([]*resource.Resource, 0)
|
|
|
|
|
|
|
|
for _, stateRes := range *resourcesFromState {
|
2021-10-19 16:26:03 +00:00
|
|
|
// Ignore all resources with type not in resFieldByType map
|
2021-10-01 09:02:46 +00:00
|
|
|
resType := stateRes.ResourceType()
|
|
|
|
resField, supported := m.resFieldByType[resType]
|
|
|
|
if !supported {
|
2021-09-29 15:43:56 +00:00
|
|
|
resources = append(resources, stateRes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-01 09:02:46 +00:00
|
|
|
resName := *stateRes.Attrs.GetString(resField)
|
2021-09-29 15:43:56 +00:00
|
|
|
policyJSON := *stateRes.Attrs.GetString("policy_data")
|
|
|
|
|
|
|
|
policies := policyDataType{}
|
|
|
|
err := json.Unmarshal([]byte(policyJSON), &policies)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, policy := range policies.Bindings {
|
2021-10-01 09:02:46 +00:00
|
|
|
roleName := policy["role"].(string)
|
|
|
|
members := policy["members"].([]interface{})
|
2021-10-04 17:03:44 +00:00
|
|
|
for _, member := range members {
|
2021-10-01 09:02:46 +00:00
|
|
|
id := fmt.Sprintf("%s/%s/%s", resName, roleName, member)
|
2021-10-04 17:03:44 +00:00
|
|
|
resources = append(
|
|
|
|
resources,
|
|
|
|
m.resourceFactory.CreateAbstractResource(
|
2021-10-01 09:02:46 +00:00
|
|
|
fmt.Sprintf("%s_member", strings.TrimSuffix(resType, "_policy")),
|
2021-10-04 17:03:44 +00:00
|
|
|
id,
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": id,
|
2021-10-01 09:02:46 +00:00
|
|
|
resField: resName,
|
2021-10-04 17:03:44 +00:00
|
|
|
"role": roleName,
|
2021-10-01 09:02:46 +00:00
|
|
|
"member": member.(string),
|
2021-10-04 17:03:44 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2021-09-29 15:43:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*resourcesFromState = resources
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type policyDataType struct {
|
2021-10-01 09:02:46 +00:00
|
|
|
Bindings []map[string]interface{}
|
2021-09-29 15:43:56 +00:00
|
|
|
}
|