Skip to content

Commit 766cff0

Browse files
authored
Fix false positive for IAM policy document without Sid (#176)
1 parent 24e58a4 commit 766cff0

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

rules/aws_iam_policy_sid_invalid_characters.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package rules
33
import (
44
"encoding/json"
55
"fmt"
6+
"regexp"
7+
68
hcl "github.com/hashicorp/hcl/v2"
79
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
810
"github.com/terraform-linters/tflint-ruleset-aws/project"
9-
"regexp"
1011
)
1112

1213
type AwsIAMPolicySidInvalidCharactersStatementStruct struct {
@@ -65,7 +66,11 @@ func (r *AwsIAMPolicySidInvalidCharactersRule) Check(runner tflint.Runner) error
6566

6667
return runner.EnsureNoError(err, func() error {
6768
for _, statement := range statements {
68-
if r.validCharacters.MatchString(statement.Sid) == false {
69+
if statement.Sid == "" {
70+
continue
71+
}
72+
73+
if !r.validCharacters.MatchString(statement.Sid) {
6974
runner.EmitIssueOnExpr(
7075
r,
7176
fmt.Sprintf("The policy's sid (\"%s\") does not match \"%s\".", statement.Sid, r.validCharacters.String()),
@@ -75,6 +80,5 @@ func (r *AwsIAMPolicySidInvalidCharactersRule) Check(runner tflint.Runner) error
7580
}
7681
return nil
7782
})
78-
return nil
7983
})
8084
}

rules/aws_iam_policy_sid_invalid_characters_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@ EOF
9191
},
9292
},
9393
},
94+
{
95+
Name: "No Sid",
96+
Content: `
97+
resource "aws_iam_policy" "policy" {
98+
name = "test_policy"
99+
role = "test_role"
100+
policy = <<-EOF
101+
{
102+
"Version": "2012-10-17",
103+
"Statement": [
104+
{
105+
"Action": [
106+
"ec2:Describe*"
107+
],
108+
"Effect": "Allow",
109+
"Resource": "arn:aws:s3:::<bucketname>/*"
110+
}
111+
]
112+
}
113+
EOF
114+
}
115+
`,
116+
Expected: helper.Issues{},
117+
},
94118
}
95119

96120
rule := NewAwsIAMPolicySidInvalidCharactersRule()

0 commit comments

Comments
 (0)