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

data/aws_iam_policy_document - additional json validation #27055

Merged
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/27055.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_iam_policy_document: Better handling when invalid JSON passed to `override_policy_documents`
```
22 changes: 15 additions & 7 deletions internal/service/iam/policy_document_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,28 @@ func DataSourcePolicyDocument() *schema.Resource {
Computed: true,
},
"override_json": {
Type: schema.TypeString,
Optional: true,
Deprecated: "Use the attribute \"override_policy_documents\" instead.",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
Deprecated: "Use the attribute \"override_policy_documents\" instead.",
},
"override_policy_documents": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringIsJSON,
},
},
"policy_id": {
Type: schema.TypeString,
Optional: true,
},
"source_json": {
Type: schema.TypeString,
Optional: true,
Deprecated: "Use the attribute \"source_policy_documents\" instead.",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
Deprecated: "Use the attribute \"source_policy_documents\" instead.",
},
"source_policy_documents": {
Type: schema.TypeList,
Expand Down Expand Up @@ -256,6 +261,9 @@ func dataSourcePolicyDocumentRead(d *schema.ResourceData, meta interface{}) erro
// merge override_policy_documents policies into mergedDoc in order specified
if v, ok := d.GetOk("override_policy_documents"); ok && len(v.([]interface{})) > 0 {
for _, overrideJSON := range v.([]interface{}) {
if overrideJSON == nil {
continue
}
overrideDoc := &IAMPolicyDoc{}
if err := json.Unmarshal([]byte(overrideJSON.(string)), overrideDoc); err != nil {
return err
Expand Down
102 changes: 102 additions & 0 deletions internal/service/iam/policy_document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,72 @@ func TestAccIAMPolicyDocumentDataSource_sourcePolicyValidJSON(t *testing.T) {
})
}

func TestAccIAMPolicyDocumentDataSource_overridePolicyDocumentValidJSON(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_overridePolicyDocument_invalidJSON,
ExpectError: regexp.MustCompile(`"override_policy_documents.0" contains an invalid JSON: unexpected end of JSON input`),
},
{
Config: testAccPolicyDocumentDataSourceConfig_overridePolicyDocument_emptyString,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "json",
testAccPolicyDocumentExpectedJSONNoStatement,
),
),
},
},
})
}

func TestAccIAMPolicyDocumentDataSource_overrideJSONValidJSON(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_overrideJSON_invalidJSON,
ExpectError: regexp.MustCompile(`"override_json" contains an invalid JSON: unexpected end of JSON input`),
},
{
Config: testAccPolicyDocumentDataSourceConfig_overrideJSON_emptyString,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "json",
testAccPolicyDocumentExpectedJSONNoStatement,
),
),
},
},
})
}

func TestAccIAMPolicyDocumentDataSource_sourceJSONValidJSON(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_sourceJSON_invalidJSON,
ExpectError: regexp.MustCompile(`"source_json" contains an invalid JSON: unexpected end of JSON input`),
},
{
Config: testAccPolicyDocumentDataSourceConfig_sourceJSON_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 @@ -1441,3 +1507,39 @@ func testAccPolicyDocumentExpectedJSONStatementPrincipalIdentifiersMultiplePrinc
]
}`, acctest.Partition())
}

var testAccPolicyDocumentDataSourceConfig_overridePolicyDocument_emptyString = `
data "aws_iam_policy_document" "test" {
override_policy_documents = [""]
}
`

var testAccPolicyDocumentDataSourceConfig_overridePolicyDocument_invalidJSON = `
data "aws_iam_policy_document" "test" {
override_policy_documents = ["{"]
}
`

var testAccPolicyDocumentDataSourceConfig_overrideJSON_emptyString = `
data "aws_iam_policy_document" "test" {
override_json = ""
}
`

var testAccPolicyDocumentDataSourceConfig_overrideJSON_invalidJSON = `
data "aws_iam_policy_document" "test" {
override_json = "{"
}
`

var testAccPolicyDocumentDataSourceConfig_sourceJSON_emptyString = `
data "aws_iam_policy_document" "test" {
source_json = ""
}
`

var testAccPolicyDocumentDataSourceConfig_sourceJSON_invalidJSON = `
data "aws_iam_policy_document" "test" {
source_json = "{"
}
`