Skip to content

Commit

Permalink
Merge pull request #26640 from mtt88/validate-invalid-json
Browse files Browse the repository at this point in the history
Add validation to on aws_iam_policy_document source_policy_documents
  • Loading branch information
ewbankkit authored Sep 7, 2022
2 parents 1aad3f4 + df075ee commit 4d44ff5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/26640.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_iam_policy_document: Prevent crash when `source_policy_documents` contains empty or invalid JSON documents
```
9 changes: 8 additions & 1 deletion internal/service/iam/policy_document_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func DataSourcePolicyDocument() *schema.Resource {
"source_policy_documents": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringIsJSON,
},
},
"statement": {
Type: schema.TypeList,
Expand Down Expand Up @@ -136,6 +139,10 @@ func dataSourcePolicyDocumentRead(d *schema.ResourceData, meta interface{}) erro

// merge sourceDocs in order specified
for sourceJSONIndex, sourceJSON := range v.([]interface{}) {
if sourceJSON == nil {
continue
}

sourceDoc := &IAMPolicyDoc{}
if err := json.Unmarshal([]byte(sourceJSON.(string)), sourceDoc); err != nil {
return err
Expand Down
38 changes: 38 additions & 0 deletions internal/service/iam/policy_document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ func TestAccIAMPolicyDocumentDataSource_duplicateSid(t *testing.T) {
})
}

func TestAccIAMPolicyDocumentDataSource_sourcePolicyValidJSON(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, iam.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccPolicyDocumentDataSourceConfig_invalidJSON,
ExpectError: regexp.MustCompile(`"source_policy_documents.0" contains an invalid JSON: unexpected end of JSON input`),
},
{
Config: testAccPolicyDocumentDataSourceConfig_emptyString,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "json",
testAccPolicyDocumentExpectedJSONNoStatement,
),
),
},
},
})
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/10777
func TestAccIAMPolicyDocumentDataSource_StatementPrincipalIdentifiers_stringAndSlice(t *testing.T) {
dataSourceName := "data.aws_iam_policy_document.test"
Expand Down Expand Up @@ -1360,6 +1382,22 @@ data "aws_iam_policy_document" "test" {
}
`

var testAccPolicyDocumentDataSourceConfig_emptyString = `
data "aws_iam_policy_document" "test" {
source_policy_documents = [""]
}
`

var testAccPolicyDocumentDataSourceConfig_invalidJSON = `
data "aws_iam_policy_document" "test" {
source_policy_documents = ["{"]
}
`

var testAccPolicyDocumentExpectedJSONNoStatement = `{
"Version": "2012-10-17"
}`

func testAccPolicyDocumentExpectedJSONStatementPrincipalIdentifiersMultiplePrincipals() string {
return fmt.Sprintf(`{
"Version": "2012-10-17",
Expand Down
2 changes: 1 addition & 1 deletion internal/service/iam/policy_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
type IAMPolicyDoc struct {
Version string `json:",omitempty"`
Id string `json:",omitempty"`
Statements []*IAMPolicyStatement `json:"Statement"`
Statements []*IAMPolicyStatement `json:"Statement,omitempty"`
}

type IAMPolicyStatement struct {
Expand Down

0 comments on commit 4d44ff5

Please sign in to comment.