github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/internal/adapters/terraform/aws/provider/adapt_test.go (about) 1 package provider 2 3 import ( 4 "testing" 5 6 "github.com/aquasecurity/defsec/pkg/providers/aws" 7 "github.com/aquasecurity/defsec/pkg/types" 8 9 "github.com/aquasecurity/trivy-iac/internal/adapters/terraform/tftestutil" 10 "github.com/aquasecurity/trivy-iac/test/testutil" 11 ) 12 13 func TestAdapt(t *testing.T) { 14 tests := []struct { 15 name string 16 source string 17 expected []aws.TerraformProvider 18 }{ 19 { 20 name: "happy", 21 source: ` 22 variable "s3_use_path_style" { 23 default = true 24 } 25 26 provider "aws" { 27 version = "~> 5.0" 28 region = "us-east-1" 29 profile = "localstack" 30 31 access_key = "fake" 32 secret_key = "fake" 33 skip_credentials_validation = true 34 skip_metadata_api_check = true 35 skip_requesting_account_id = true 36 s3_use_path_style = var.s3_use_path_style 37 38 endpoints { 39 dynamodb = "http://localhost:4566" 40 s3 = "http://localhost:4566" 41 } 42 43 default_tags { 44 tags = { 45 Environment = "Local" 46 Name = "LocalStack" 47 } 48 } 49 }`, 50 expected: []aws.TerraformProvider{ 51 { 52 Version: types.String("~> 5.0", types.NewTestMetadata()), 53 Region: types.String("us-east-1", types.NewTestMetadata()), 54 DefaultTags: aws.DefaultTags{ 55 Metadata: types.NewTestMetadata(), 56 Tags: types.Map(map[string]string{ 57 "Environment": "Local", 58 "Name": "LocalStack", 59 }, types.NewTestMetadata()), 60 }, 61 Endpoints: types.Map(map[string]string{ 62 "dynamodb": "http://localhost:4566", 63 "s3": "http://localhost:4566", 64 }, types.NewTestMetadata()), 65 Profile: types.String("localstack", types.NewTestMetadata()), 66 AccessKey: types.String("fake", types.NewTestMetadata()), 67 SecretKey: types.String("fake", types.NewTestMetadata()), 68 SkipCredentialsValidation: types.Bool(true, types.NewTestMetadata()), 69 SkipMetadataAPICheck: types.Bool(true, types.NewTestMetadata()), 70 SkipRequestingAccountID: types.Bool(true, types.NewTestMetadata()), 71 S3UsePathStyle: types.Bool(true, types.NewTestMetadata()), 72 MaxRetries: types.IntDefault(defaultMaxRetires, types.NewTestMetadata()), 73 SharedConfigFiles: types.StringValueList{ 74 types.StringDefault(defaultSharedConfigFile, types.NewTestMetadata()), 75 }, 76 SharedCredentialsFiles: types.StringValueList{ 77 types.StringDefault(defaultSharedCredentialsFile, types.NewTestMetadata()), 78 }, 79 }, 80 }, 81 }, 82 { 83 name: "multiply provider configurations", 84 source: ` 85 86 provider "aws" { 87 region = "us-east-1" 88 } 89 90 provider "aws" { 91 alias = "west" 92 region = "us-west-2" 93 } 94 `, 95 expected: []aws.TerraformProvider{ 96 { 97 Region: types.String("us-east-1", types.NewTestMetadata()), 98 Endpoints: types.Map(make(map[string]string), types.NewTestMetadata()), 99 MaxRetries: types.IntDefault(defaultMaxRetires, types.NewTestMetadata()), 100 SharedConfigFiles: types.StringValueList{ 101 types.StringDefault(defaultSharedConfigFile, types.NewTestMetadata()), 102 }, 103 SharedCredentialsFiles: types.StringValueList{ 104 types.StringDefault(defaultSharedCredentialsFile, types.NewTestMetadata()), 105 }, 106 }, 107 { 108 Alias: types.String("west", types.NewTestMetadata()), 109 Region: types.String("us-west-2", types.NewTestMetadata()), 110 Endpoints: types.Map(make(map[string]string), types.NewTestMetadata()), 111 MaxRetries: types.IntDefault(defaultMaxRetires, types.NewTestMetadata()), 112 SharedConfigFiles: types.StringValueList{ 113 types.StringDefault(defaultSharedConfigFile, types.NewTestMetadata()), 114 }, 115 SharedCredentialsFiles: types.StringValueList{ 116 types.StringDefault(defaultSharedCredentialsFile, types.NewTestMetadata()), 117 }, 118 }, 119 }, 120 }, 121 } 122 123 for _, test := range tests { 124 t.Run(test.name, func(t *testing.T) { 125 modules := tftestutil.CreateModulesFromSource(t, test.source, ".tf") 126 testutil.AssertDefsecEqual(t, test.expected, Adapt(modules)) 127 }) 128 } 129 }