github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/internal/adapters/terraform/google/kms/adapt.go (about) 1 package kms 2 3 import ( 4 "strconv" 5 6 "github.com/khulnasoft-lab/defsec/pkg/types" 7 8 "github.com/khulnasoft-lab/defsec/pkg/terraform" 9 10 "github.com/khulnasoft-lab/defsec/pkg/providers/google/kms" 11 ) 12 13 func Adapt(modules terraform.Modules) kms.KMS { 14 return kms.KMS{ 15 KeyRings: adaptKeyRings(modules), 16 } 17 } 18 19 func adaptKeyRings(modules terraform.Modules) []kms.KeyRing { 20 var keyRings []kms.KeyRing 21 for _, module := range modules { 22 for _, resource := range module.GetResourcesByType("google_kms_key_ring") { 23 var keys []kms.Key 24 25 keyBlocks := module.GetReferencingResources(resource, "google_kms_crypto_key", "key_ring") 26 for _, keyBlock := range keyBlocks { 27 keys = append(keys, adaptKey(keyBlock)) 28 } 29 keyRings = append(keyRings, kms.KeyRing{ 30 Metadata: resource.GetMetadata(), 31 Keys: keys, 32 }) 33 } 34 } 35 return keyRings 36 } 37 38 func adaptKey(resource *terraform.Block) kms.Key { 39 40 key := kms.Key{ 41 Metadata: resource.GetMetadata(), 42 RotationPeriodSeconds: types.IntDefault(-1, resource.GetMetadata()), 43 } 44 45 rotationPeriodAttr := resource.GetAttribute("rotation_period") 46 if !rotationPeriodAttr.IsString() { 47 return key 48 } 49 rotationStr := rotationPeriodAttr.Value().AsString() 50 if rotationStr[len(rotationStr)-1:] != "s" { 51 return key 52 } 53 seconds, err := strconv.Atoi(rotationStr[:len(rotationStr)-1]) 54 if err != nil { 55 return key 56 } 57 58 key.RotationPeriodSeconds = types.Int(seconds, rotationPeriodAttr.GetMetadata()) 59 return key 60 }