|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + hcl "github.com/hashicorp/hcl/v2" |
| 7 | + "github.com/terraform-linters/tflint-plugin-sdk/helper" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_AwsIAMRoleDeprecatedPolicyAttributes(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + Name string |
| 13 | + Content string |
| 14 | + Expected helper.Issues |
| 15 | + }{ |
| 16 | + { |
| 17 | + Name: "inline_policies", |
| 18 | + Content: ` |
| 19 | +resource "aws_iam_role" "example" { |
| 20 | + name = "yak_role" |
| 21 | + assume_role_policy = data.aws_iam_policy_document.instance_assume_role_policy.json # (not shown) |
| 22 | +
|
| 23 | + inline_policy { |
| 24 | + name = "my_inline_policy" |
| 25 | +
|
| 26 | + policy = jsonencode({ |
| 27 | + Version = "2012-10-17" |
| 28 | + Statement = [ |
| 29 | + { |
| 30 | + Action = ["ec2:Describe*"] |
| 31 | + Effect = "Allow" |
| 32 | + Resource = "*" |
| 33 | + }, |
| 34 | + ] |
| 35 | + }) |
| 36 | + } |
| 37 | +
|
| 38 | + inline_policy { |
| 39 | + name = "policy-8675309" |
| 40 | + policy = data.aws_iam_policy_document.inline_policy.json |
| 41 | + } |
| 42 | +} |
| 43 | +
|
| 44 | +data "aws_iam_policy_document" "inline_policy" { |
| 45 | + statement { |
| 46 | + actions = ["ec2:DescribeAccountAttributes"] |
| 47 | + resources = ["*"] |
| 48 | + } |
| 49 | +} |
| 50 | +`, |
| 51 | + Expected: helper.Issues{ |
| 52 | + { |
| 53 | + Rule: NewAwsIAMRoleDeprecatedPolicyAttributesRule(), |
| 54 | + Message: "The inline_policy argument is deprecated. Use the aws_iam_role_policy resource instead. If Terraform should exclusively manage all inline policy associations (the current behavior of this argument), use the aws_iam_role_policies_exclusive resource as well.", |
| 55 | + Range: hcl.Range{ |
| 56 | + Filename: "resource.tf", |
| 57 | + Start: hcl.Pos{Line: 6, Column: 3}, |
| 58 | + End: hcl.Pos{Line: 6, Column: 16}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + Rule: NewAwsIAMRoleDeprecatedPolicyAttributesRule(), |
| 63 | + Message: "The inline_policy argument is deprecated. Use the aws_iam_role_policy resource instead. If Terraform should exclusively manage all inline policy associations (the current behavior of this argument), use the aws_iam_role_policies_exclusive resource as well.", |
| 64 | + Range: hcl.Range{ |
| 65 | + Filename: "resource.tf", |
| 66 | + Start: hcl.Pos{Line: 21, Column: 3}, |
| 67 | + End: hcl.Pos{Line: 21, Column: 16}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + Name: "managed_policy_arns", |
| 74 | + Content: ` |
| 75 | +resource "aws_iam_role" "example" { |
| 76 | + name = "yak_role" |
| 77 | + assume_role_policy = data.aws_iam_policy_document.instance_assume_role_policy.json # (not shown) |
| 78 | + managed_policy_arns = [] |
| 79 | +} |
| 80 | +`, |
| 81 | + Expected: helper.Issues{ |
| 82 | + { |
| 83 | + Rule: NewAwsIAMRoleDeprecatedPolicyAttributesRule(), |
| 84 | + Message: "The managed_policy_arns argument is deprecated. Use the aws_iam_role_policy_attachment resource instead. If Terraform should exclusively manage all managed policy attachments (the current behavior of this argument), use the aws_iam_role_policy_attachments_exclusive resource as well.", |
| 85 | + Range: hcl.Range{ |
| 86 | + Filename: "resource.tf", |
| 87 | + Start: hcl.Pos{Line: 5, Column: 3}, |
| 88 | + End: hcl.Pos{Line: 5, Column: 27}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + rule := NewAwsIAMRoleDeprecatedPolicyAttributesRule() |
| 96 | + |
| 97 | + for _, tc := range cases { |
| 98 | + runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) |
| 99 | + |
| 100 | + if err := rule.Check(runner); err != nil { |
| 101 | + t.Fatalf("Unexpected error occurred: %s", err) |
| 102 | + } |
| 103 | + |
| 104 | + helper.AssertIssues(t, tc.Expected, runner.Issues) |
| 105 | + } |
| 106 | +} |
0 commit comments