Skip to content

Commit

Permalink
Merge pull request #35677 from alexwilcox9/scp-min-json
Browse files Browse the repository at this point in the history
Add `minified_json` to `aws_iam_policy_document` data source
  • Loading branch information
jar-b authored May 9, 2024
2 parents bd1e00b + 0b2ccf5 commit 5204802
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/35677.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_iam_policy_document: Add `minified_json` attribute
```
14 changes: 14 additions & 0 deletions internal/service/iam/policy_document_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func dataSourcePolicyDocument() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"minified_json": {
Type: schema.TypeString,
Computed: true,
},
// https://github.com/hashicorp/terraform-provider-aws/issues/31637.
"override_json": {
Type: schema.TypeString,
Expand Down Expand Up @@ -309,6 +313,16 @@ func dataSourcePolicyDocumentRead(ctx context.Context, d *schema.ResourceData, m
jsonString := string(jsonDoc)

d.Set("json", jsonString)

jsonMinDoc, err := json.Marshal(mergedDoc)
if err != nil {
// should never happen if the above code is correct
return sdkdiag.AppendErrorf(diags, "writing IAM Policy Document: formatting JSON: %s", err)
}
jsonMinString := string(jsonMinDoc)

d.Set("minified_json", jsonMinString)

d.SetId(strconv.Itoa(create.StringHashcode(jsonString)))

return diags
Expand Down
7 changes: 7 additions & 0 deletions internal/service/iam/policy_document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func TestAccIAMPolicyDocumentDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "json",
testAccPolicyDocumentExpectedJSON(),
),
resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "minified_json",
testAccPolicyDocumentExpectedJSONMinified(),
),
),
},
},
Expand Down Expand Up @@ -589,6 +592,10 @@ func testAccPolicyDocumentExpectedJSON() string {
}`, acctest.Partition())
}

func testAccPolicyDocumentExpectedJSONMinified() string {
return fmt.Sprintf(`{"Version":"2012-10-17","Id":"policy_id","Statement":[{"Sid":"1","Effect":"Allow","Action":["s3:ListAllMyBuckets","s3:GetBucketLocation"],"Resource":"arn:%[1]s:s3:::*"},{"Effect":"Allow","Action":"s3:ListBucket","Resource":"arn:%[1]s:s3:::foo","NotPrincipal":{"AWS":"arn:blahblah:example"},"Condition":{"StringLike":{"s3:prefix":["home/","","home/${aws:username}/"]}}},{"Effect":"Allow","Action":"s3:*","Resource":["arn:%[1]s:s3:::foo/home/${aws:username}/*","arn:%[1]s:s3:::foo/home/${aws:username}"],"Principal":{"AWS":"arn:blahblah:example"}},{"Effect":"Deny","NotAction":"s3:*","NotResource":"arn:%[1]s:s3:::*"},{"Effect":"Allow","Action":"kinesis:*","Principal":{"AWS":"*"}},{"Effect":"Allow","Action":"firehose:*","Principal":"*"}]}`, acctest.Partition())
}

const testAccPolicyDocumentDataSourceConfig_singleConditionValue = `
data "aws_iam_policy_document" "test" {
statement {
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/iam_policy_document.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,4 @@ The following arguments are required:
This data source exports the following attributes in addition to the arguments above:

* `json` - Standard JSON policy document rendered based on the arguments above.
* `minified_json` - Minified JSON policy document rendered based on the arguments above.

0 comments on commit 5204802

Please sign in to comment.