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 minified_json to aws_iam_policy_document data source #35677

Merged
merged 4 commits into from
May 9, 2024
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/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.
Loading