Skip to content

Commit

Permalink
better handling of invalid or empty JSON strings in policy strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Hitchon committed Jun 15, 2018
1 parent 22f26fc commit ab52353
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
12 changes: 8 additions & 4 deletions linter/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ func replaceVariables(templateResource interface{}, variables []Variable) interf
switch v := templateResource.(type) {
case map[string]interface{}:
return replaceVariablesInMap(v, variables)
case string:
return resolveValue(v, variables)
default:
assertion.Debugf("replaceVariables cannot process type %T\n", v)
assertion.Debugf("replaceVariables cannot process type %T: %v\n", v, v)
return templateResource
}
}
Expand Down Expand Up @@ -251,9 +253,11 @@ func parsePolicy(resource interface{}) (interface{}, error) {
if policyAttribute, hasPolicyString := properties[attribute]; hasPolicyString {
if policyString, isString := policyAttribute.(string); isString {
var policy interface{}
err := json.Unmarshal([]byte(policyString), &policy)
if err != nil {
return properties, err
if policyString != "" {
err := json.Unmarshal([]byte(policyString), &policy)
if err != nil {
assertion.Debugf("Unable to parse '%s' as JSON\n", policyString)
}
}
properties[attribute] = policy
}
Expand Down
12 changes: 12 additions & 0 deletions linter/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ func TestTerraformLinterCases(t *testing.T) {
1,
"TEST_POLICY",
},
"PolicyInvalidJSON": {
"./testdata/resources/terraform_policy_invalid_json.tf",
"./testdata/rules/terraform_policy.yml",
0,
"",
},
"PolicyEmpty": {
"./testdata/resources/terraform_policy_empty.tf",
"./testdata/rules/terraform_policy.yml",
0,
"",
},
}
for name, tc := range testCases {
options := Options{
Expand Down
5 changes: 5 additions & 0 deletions linter/testdata/resources/terraform_policy_empty.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_iam_role" "role1" {
name = "role1"
assume_role_policy = <<EOF
EOF
}
10 changes: 10 additions & 0 deletions linter/testdata/resources/terraform_policy_invalid_json.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "aws_iam_role" "role1" {
name = "role1"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": }
}
EOF
}

0 comments on commit ab52353

Please sign in to comment.