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

s3: Preserve order in JSON/policy arrays #21997

Merged
merged 8 commits into from
Dec 2, 2021
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
7 changes: 7 additions & 0 deletions .changelog/21997.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_s3_bucket: Fix order-related diffs in `policy`
```

```release-note:bug
resource/aws_s3_bucket_policy: Fix order-related diffs in `policy`
```
21 changes: 17 additions & 4 deletions internal/service/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,11 +911,19 @@ func resourceBucketRead(d *schema.ResourceData, meta interface{}) error {
return err
}
} else {
policy, err := structure.NormalizeJsonString(aws.StringValue(v))
policyToSet, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), aws.StringValue(v))

if err != nil {
return fmt.Errorf("while setting policy (%s), encountered: %w", aws.StringValue(v), err)
}

policyToSet, err = structure.NormalizeJsonString(policyToSet)

if err != nil {
return fmt.Errorf("policy contains an invalid JSON: %s", err)
return fmt.Errorf("policy (%s) contains invalid JSON: %w", d.Get("policy").(string), err)
}
d.Set("policy", policy)

d.Set("policy", policyToSet)
}
}
}
Expand Down Expand Up @@ -1444,7 +1452,12 @@ func resourceBucketDelete(d *schema.ResourceData, meta interface{}) error {

func resourceBucketPolicyUpdate(conn *s3.S3, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
policy := d.Get("policy").(string)

policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

if err != nil {
return fmt.Errorf("policy (%s) is an invalid JSON: %w", policy, err)
}

if policy != "" {
log.Printf("[DEBUG] S3 bucket: %s, put policy: %s", bucket, policy)
Expand Down
28 changes: 24 additions & 4 deletions internal/service/s3/bucket_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -48,7 +49,12 @@ func resourceBucketPolicyPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).S3Conn

bucket := d.Get("bucket").(string)
policy := d.Get("policy").(string)

policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

if err != nil {
return fmt.Errorf("policy (%s) is an invalid JSON: %w", policy, err)
}

log.Printf("[DEBUG] S3 bucket: %s, put policy: %s", bucket, policy)

Expand All @@ -57,7 +63,7 @@ func resourceBucketPolicyPut(d *schema.ResourceData, meta interface{}) error {
Policy: aws.String(policy),
}

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.PutBucketPolicy(params)
if tfawserr.ErrMessageContains(err, "MalformedPolicy", "") {
return resource.RetryableError(err)
Expand Down Expand Up @@ -89,11 +95,25 @@ func resourceBucketPolicyRead(d *schema.ResourceData, meta interface{}) error {

v := ""
if err == nil && pol.Policy != nil {
v = *pol.Policy
v = aws.StringValue(pol.Policy)
}

policyToSet, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), v)

if err != nil {
return fmt.Errorf("while setting policy (%s), encountered: %w", policyToSet, err)
}
if err := d.Set("policy", v); err != nil {

policyToSet, err = structure.NormalizeJsonString(policyToSet)

if err != nil {
return fmt.Errorf("policy (%s) is an invalid JSON: %w", policyToSet, err)
}

if err := d.Set("policy", policyToSet); err != nil {
return err
}

if err := d.Set("bucket", d.Id()); err != nil {
return err
}
Expand Down
Loading