|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/terraform-linters/tflint-plugin-sdk/helper" |
| 7 | +) |
| 8 | + |
| 9 | +func Test_AwsARNHardcodedRule(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + Name string |
| 12 | + Content string |
| 13 | + ExpectedCount int |
| 14 | + }{ |
| 15 | + { |
| 16 | + Name: "iam role assume_role_policy with hardcoded ARN", |
| 17 | + Content: ` |
| 18 | +resource "aws_iam_role" "test" { |
| 19 | + assume_role_policy = jsonencode({ |
| 20 | + Statement = [{ |
| 21 | + Action = "sts:AssumeRole" |
| 22 | + Effect = "Allow" |
| 23 | + Principal = { |
| 24 | + AWS = "arn:aws:iam:us-east-1:123456789012:root" |
| 25 | + } |
| 26 | + }] |
| 27 | + }) |
| 28 | +}`, |
| 29 | + ExpectedCount: 4, // 2x because WalkExpressions visits nested expressions |
| 30 | + }, |
| 31 | + { |
| 32 | + Name: "lambda permission with hardcoded source_arn", |
| 33 | + Content: ` |
| 34 | +resource "aws_lambda_permission" "test" { |
| 35 | + source_arn = "arn:aws:s3:eu-west-1:123456789012:bucket/my-bucket" |
| 36 | +}`, |
| 37 | + ExpectedCount: 4, |
| 38 | + }, |
| 39 | + { |
| 40 | + Name: "lambda event source mapping with hardcoded event_source_arn", |
| 41 | + Content: ` |
| 42 | +resource "aws_lambda_event_source_mapping" "test" { |
| 43 | + event_source_arn = "arn:aws:dynamodb:us-east-1:123456789012:table/my-table" |
| 44 | +}`, |
| 45 | + ExpectedCount: 4, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "sns subscription with hardcoded topic_arn", |
| 49 | + Content: ` |
| 50 | +resource "aws_sns_topic_subscription" "test" { |
| 51 | + topic_arn = "arn:aws:sns:us-west-2:123456789012:my-topic" |
| 52 | +}`, |
| 53 | + ExpectedCount: 4, |
| 54 | + }, |
| 55 | + { |
| 56 | + Name: "cloudwatch event target with hardcoded arn", |
| 57 | + Content: ` |
| 58 | +resource "aws_cloudwatch_event_target" "test" { |
| 59 | + arn = "arn:aws:lambda:us-west-2:123456789012:function:my-function" |
| 60 | +}`, |
| 61 | + ExpectedCount: 4, |
| 62 | + }, |
| 63 | + { |
| 64 | + Name: "cloudwatch log subscription filter with hardcoded destination_arn", |
| 65 | + Content: ` |
| 66 | +resource "aws_cloudwatch_log_subscription_filter" "test" { |
| 67 | + destination_arn = "arn:aws:lambda:eu-west-1:123456789012:function:my-function" |
| 68 | +}`, |
| 69 | + ExpectedCount: 4, |
| 70 | + }, |
| 71 | + { |
| 72 | + Name: "api gateway integration with hardcoded uri", |
| 73 | + Content: ` |
| 74 | +resource "aws_api_gateway_integration" "test" { |
| 75 | + uri = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:my-function/invocations" |
| 76 | +}`, |
| 77 | + ExpectedCount: 4, // Only the lambda ARN is detected, not the apigateway ARN format |
| 78 | + }, |
| 79 | + { |
| 80 | + Name: "kms grant with hardcoded key_id", |
| 81 | + Content: ` |
| 82 | +resource "aws_kms_grant" "test" { |
| 83 | + key_id = "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012" |
| 84 | +}`, |
| 85 | + ExpectedCount: 4, |
| 86 | + }, |
| 87 | + { |
| 88 | + Name: "kms alias with hardcoded target_key_id", |
| 89 | + Content: ` |
| 90 | +resource "aws_kms_alias" "test" { |
| 91 | + target_key_id = "arn:aws:kms:eu-west-1:123456789012:key/12345678-1234-1234-1234-123456789012" |
| 92 | +}`, |
| 93 | + ExpectedCount: 4, |
| 94 | + }, |
| 95 | + { |
| 96 | + Name: "secretsmanager rotation with hardcoded rotation_lambda_arn", |
| 97 | + Content: ` |
| 98 | +resource "aws_secretsmanager_secret_rotation" "test" { |
| 99 | + rotation_lambda_arn = "arn:aws:lambda:us-east-1:123456789012:function:my-rotation-function" |
| 100 | +}`, |
| 101 | + ExpectedCount: 4, |
| 102 | + }, |
| 103 | + { |
| 104 | + Name: "db instance with hardcoded replicate_source_db", |
| 105 | + Content: ` |
| 106 | +resource "aws_db_instance" "test" { |
| 107 | + replicate_source_db = "arn:aws:rds:us-east-1:123456789012:db:my-source-db" |
| 108 | +}`, |
| 109 | + ExpectedCount: 4, |
| 110 | + }, |
| 111 | + { |
| 112 | + Name: "db event subscription with hardcoded sns_topic", |
| 113 | + Content: ` |
| 114 | +resource "aws_db_event_subscription" "test" { |
| 115 | + sns_topic = "arn:aws:sns:eu-west-1:123456789012:my-topic" |
| 116 | +}`, |
| 117 | + ExpectedCount: 4, |
| 118 | + }, |
| 119 | + { |
| 120 | + Name: "multiple resources with different partitions", |
| 121 | + Content: ` |
| 122 | +resource "aws_lambda_permission" "test1" { |
| 123 | + source_arn = "arn:aws:s3:us-east-1:123456789012:bucket/my-bucket" |
| 124 | +} |
| 125 | +
|
| 126 | +resource "aws_sns_topic_subscription" "test2" { |
| 127 | + topic_arn = "arn:aws-cn:sns:cn-north-1:123456789012:my-topic" |
| 128 | +}`, |
| 129 | + ExpectedCount: 8, |
| 130 | + }, |
| 131 | + { |
| 132 | + Name: "resource with dynamic ARN using data sources", |
| 133 | + Content: ` |
| 134 | +data "aws_region" "current" {} |
| 135 | +data "aws_partition" "current" {} |
| 136 | +
|
| 137 | +resource "aws_lambda_permission" "test" { |
| 138 | + source_arn = "arn:${data.aws_partition.current.partition}:s3:${data.aws_region.current.name}:123456789012:bucket/my-bucket" |
| 139 | +}`, |
| 140 | + ExpectedCount: 0, |
| 141 | + }, |
| 142 | + |
| 143 | + { |
| 144 | + Name: "non-ARN string values", |
| 145 | + Content: ` |
| 146 | +resource "aws_s3_bucket" "test" { |
| 147 | + bucket = "my-bucket-name" |
| 148 | +}`, |
| 149 | + ExpectedCount: 0, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + rule := NewAwsARNHardcodedRule() |
| 154 | + |
| 155 | + for _, test := range tests { |
| 156 | + t.Run(test.Name, func(t *testing.T) { |
| 157 | + runner := helper.TestRunner(t, map[string]string{"main.tf": test.Content}) |
| 158 | + |
| 159 | + if err := rule.Check(runner); err != nil { |
| 160 | + t.Fatalf("Unexpected error occurred: %s", err) |
| 161 | + } |
| 162 | + |
| 163 | + if len(runner.Issues) != test.ExpectedCount { |
| 164 | + t.Errorf("Expected %d issues, got %d", test.ExpectedCount, len(runner.Issues)) |
| 165 | + for i, issue := range runner.Issues { |
| 166 | + t.Logf("Issue %d: %s", i+1, issue.Message) |
| 167 | + } |
| 168 | + } |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
0 commit comments