github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/adapters/terraform/digitalocean/spaces/adapt.go (about) 1 package spaces 2 3 import ( 4 "github.com/google/uuid" 5 "github.com/khulnasoft-lab/defsec/pkg/providers/digitalocean/spaces" 6 "github.com/khulnasoft-lab/defsec/pkg/terraform" 7 defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types" 8 ) 9 10 func Adapt(modules terraform.Modules) spaces.Spaces { 11 return spaces.Spaces{ 12 Buckets: adaptBuckets(modules), 13 } 14 } 15 16 func adaptBuckets(modules terraform.Modules) []spaces.Bucket { 17 bucketMap := make(map[string]spaces.Bucket) 18 for _, module := range modules { 19 20 for _, block := range module.GetResourcesByType("digitalocean_spaces_bucket") { 21 22 bucket := spaces.Bucket{ 23 Metadata: block.GetMetadata(), 24 Name: block.GetAttribute("name").AsStringValueOrDefault("", block), 25 Objects: nil, 26 ACL: block.GetAttribute("acl").AsStringValueOrDefault("public-read", block), 27 ForceDestroy: block.GetAttribute("force_destroy").AsBoolValueOrDefault(false, block), 28 Versioning: spaces.Versioning{ 29 Metadata: block.GetMetadata(), 30 Enabled: defsecTypes.BoolDefault(false, block.GetMetadata()), 31 }, 32 } 33 34 if versioning := block.GetBlock("versioning"); versioning.IsNotNil() { 35 bucket.Versioning = spaces.Versioning{ 36 Metadata: versioning.GetMetadata(), 37 Enabled: versioning.GetAttribute("enabled").AsBoolValueOrDefault(false, versioning), 38 } 39 } 40 bucketMap[block.ID()] = bucket 41 } 42 for _, block := range module.GetResourcesByType("digitalocean_spaces_bucket_object") { 43 object := spaces.Object{ 44 Metadata: block.GetMetadata(), 45 ACL: block.GetAttribute("acl").AsStringValueOrDefault("private", block), 46 } 47 bucketName := block.GetAttribute("bucket") 48 var found bool 49 if bucketName.IsString() { 50 for i, bucket := range bucketMap { 51 if bucket.Name.Value() == bucketName.Value().AsString() { 52 bucket.Objects = append(bucket.Objects, object) 53 bucketMap[i] = bucket 54 found = true 55 break 56 } 57 } 58 if found { 59 continue 60 } 61 } else if bucketName.IsNotNil() { 62 if referencedBlock, err := module.GetReferencedBlock(bucketName, block); err == nil { 63 if bucket, ok := bucketMap[referencedBlock.ID()]; ok { 64 bucket.Objects = append(bucket.Objects, object) 65 bucketMap[referencedBlock.ID()] = bucket 66 continue 67 } 68 } 69 } 70 bucketMap[uuid.NewString()] = spaces.Bucket{ 71 Metadata: defsecTypes.NewUnmanagedMetadata(), 72 Name: defsecTypes.StringDefault("", defsecTypes.NewUnmanagedMetadata()), 73 Objects: []spaces.Object{ 74 object, 75 }, 76 ACL: defsecTypes.StringDefault("private", defsecTypes.NewUnmanagedMetadata()), 77 ForceDestroy: defsecTypes.BoolDefault(false, defsecTypes.NewUnmanagedMetadata()), 78 Versioning: spaces.Versioning{ 79 Metadata: block.GetMetadata(), 80 Enabled: defsecTypes.BoolDefault(false, block.GetMetadata()), 81 }, 82 } 83 } 84 } 85 86 var buckets []spaces.Bucket 87 for _, bucket := range bucketMap { 88 buckets = append(buckets, bucket) 89 } 90 return buckets 91 }