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

ecr: Preserve/ignore order in JSON/policy #22004

Merged
merged 10 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
11 changes: 11 additions & 0 deletions .changelog/22004.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
resource/aws_ecr_repository_policy: Fix order-related diffs in `policy`
```

```release-note:bug
resource/aws_ecr_registry_policy: Fix order-related diffs in `policy`
```

```release-note:bug
resource/aws_iam_role: Prevent `arn` attribute from ever containing a unique ID immediately after role creation
```
20 changes: 20 additions & 0 deletions internal/service/ecr/errorcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ecr_test

import (
"testing"

"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func init() {
acctest.RegisterServiceErrorCheckFunc(ecr.EndpointsID, testAccErrorCheckSkip)
}

// testAccErrorCheckSkip skips tests that have error messages indicating unsupported features
func testAccErrorCheckSkip(t *testing.T) resource.ErrorCheckFunc {
return acctest.ErrorCheckSkipMessagesContaining(t,
"This feature is disabled",
)
}
24 changes: 22 additions & 2 deletions internal/service/ecr/registry_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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/verify"
Expand Down Expand Up @@ -41,8 +42,14 @@ func ResourceRegistryPolicy() *schema.Resource {
func resourceRegistryPolicyPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ECRConn

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

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

input := ecr.PutRegistryPolicyInput{
PolicyText: aws.String(d.Get("policy").(string)),
PolicyText: aws.String(policy),
}

out, err := conn.PutRegistryPolicy(&input)
Expand Down Expand Up @@ -72,7 +79,20 @@ func resourceRegistryPolicyRead(d *schema.ResourceData, meta interface{}) error
}

d.Set("registry_id", out.RegistryId)
d.Set("policy", out.PolicyText)

policyToSet, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), aws.StringValue(out.PolicyText))

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

policyToSet, err = structure.NormalizeJsonString(policyToSet)

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

d.Set("policy", policyToSet)

return nil
}
Expand Down
8 changes: 2 additions & 6 deletions internal/service/ecr/registry_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,8 @@ resource "aws_ecr_registry_policy" "test" {
"Principal" : {
"AWS" : "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action" : [
"ecr:ReplicateImage"
],
"Resource" : [
"arn:${data.aws_partition.current.partition}:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/*"
]
"Action" : "ecr:ReplicateImage",
"Resource" : "arn:${data.aws_partition.current.partition}:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/*",
}
]
})
Expand Down
25 changes: 22 additions & 3 deletions internal/service/ecr/repository_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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"
tfiam "github.com/hashicorp/terraform-provider-aws/internal/service/iam"
Expand Down Expand Up @@ -49,15 +50,20 @@ func ResourceRepositoryPolicy() *schema.Resource {
func resourceRepositoryPolicyPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ECRConn

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

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

input := ecr.SetRepositoryPolicyInput{
RepositoryName: aws.String(d.Get("repository").(string)),
PolicyText: aws.String(d.Get("policy").(string)),
PolicyText: aws.String(policy),
}

log.Printf("[DEBUG] Creating ECR repository policy: %#v", input)

// Retry due to IAM eventual consistency
var err error
var out *ecr.SetRepositoryPolicyOutput
err = resource.Retry(tfiam.PropagationTimeout, func() *resource.RetryError {
out, err = conn.SetRepositoryPolicy(&input)
Expand Down Expand Up @@ -141,7 +147,20 @@ func resourceRepositoryPolicyRead(d *schema.ResourceData, meta interface{}) erro

d.Set("repository", out.RepositoryName)
d.Set("registry_id", out.RegistryId)
d.Set("policy", out.PolicyText)

policyToSet, err := verify.SecondJSONUnlessEquivalent(d.Get("policy").(string), aws.StringValue(out.PolicyText))

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

policyToSet, err = structure.NormalizeJsonString(policyToSet)

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

d.Set("policy", policyToSet)

return nil
}
Expand Down
Loading