Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation to on aws_iam_policy_document source_policy_documents #26640

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -223,6 +223,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 @@ -1277,6 +1299,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 @@ -13,7 +13,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