Skip to content

Commit

Permalink
d/aws_iam_policy_document: Don't crash if 'source_policy_documents' c…
Browse files Browse the repository at this point in the history
…ontains an empty string.
  • Loading branch information
ewbankkit committed Sep 7, 2022
1 parent 3083a3f commit df075ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion internal/service/iam/policy_document_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func DataSourcePolicyDocument() *schema.Resource {
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.All(validation.StringIsNotEmpty, validation.StringIsJSON),
ValidateFunc: validation.StringIsJSON,
},
},
"statement": {
Expand Down Expand Up @@ -139,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
22 changes: 15 additions & 7 deletions internal/service/iam/policy_document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,23 @@ func TestAccIAMPolicyDocumentDataSource_duplicateSid(t *testing.T) {
})
}

func TestAccIAMPolicyDocumentDataSource_sourcePolicyValidJson(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,
Config: testAccPolicyDocumentDataSourceConfig_invalidJSON,
ExpectError: regexp.MustCompile(`"source_policy_documents.0" contains an invalid JSON: unexpected end of JSON input`),
},
{
Config: testAccPolicyDocumentDataSourceConfig_emptyString,
ExpectError: regexp.MustCompile(`expected "source_policy_documents.0" to not be an empty string, got`),
Config: testAccPolicyDocumentDataSourceConfig_emptyString,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "json",
testAccPolicyDocumentExpectedJSONNoStatement,
),
),
},
},
})
Expand Down Expand Up @@ -1297,16 +1301,20 @@ data "aws_iam_policy_document" "test" {

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

var testAccPolicyDocumentDataSourceConfig_invalidJson = `
var testAccPolicyDocumentDataSourceConfig_invalidJSON = `
data "aws_iam_policy_document" "test" {
source_policy_documents = ["{"]
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

0 comments on commit df075ee

Please sign in to comment.