Skip to content

Commit

Permalink
Fix false positive for IAM policy document without Sid (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Sep 2, 2021
1 parent 24e58a4 commit 766cff0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions rules/aws_iam_policy_sid_invalid_characters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package rules
import (
"encoding/json"
"fmt"
"regexp"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-aws/project"
"regexp"
)

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

return runner.EnsureNoError(err, func() error {
for _, statement := range statements {
if r.validCharacters.MatchString(statement.Sid) == false {
if statement.Sid == "" {
continue
}

if !r.validCharacters.MatchString(statement.Sid) {
runner.EmitIssueOnExpr(
r,
fmt.Sprintf("The policy's sid (\"%s\") does not match \"%s\".", statement.Sid, r.validCharacters.String()),
Expand All @@ -75,6 +80,5 @@ func (r *AwsIAMPolicySidInvalidCharactersRule) Check(runner tflint.Runner) error
}
return nil
})
return nil
})
}
24 changes: 24 additions & 0 deletions rules/aws_iam_policy_sid_invalid_characters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ EOF
},
},
},
{
Name: "No Sid",
Content: `
resource "aws_iam_policy" "policy" {
name = "test_policy"
role = "test_role"
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:Describe*"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<bucketname>/*"
}
]
}
EOF
}
`,
Expected: helper.Issues{},
},
}

rule := NewAwsIAMPolicySidInvalidCharactersRule()
Expand Down

0 comments on commit 766cff0

Please sign in to comment.