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

transfer: Fix erroneous diffs with user and access policies #22193

Merged
merged 3 commits into from
Dec 14, 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/22193.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_transfer_access: Fix erroneous diffs in `policy` when no changes made or policies are equivalent
```

```release-note:bug
resource/aws_transfer_user: Fix erroneous diffs in `policy` when no changes made or policies are equivalent
```
32 changes: 29 additions & 3 deletions internal/service/transfer/access.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/transfer"
"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/tfresource"
Expand Down Expand Up @@ -70,6 +71,10 @@ func ResourceAccess() *schema.Resource {
Optional: true,
ValidateFunc: verify.ValidIAMPolicyJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},

"posix_profile": {
Expand Down Expand Up @@ -137,7 +142,13 @@ func resourceAccessCreate(d *schema.ResourceData, meta interface{}) error {
}

if v, ok := d.GetOk("policy"); ok {
input.Policy = aws.String(v.(string))
policy, err := structure.NormalizeJsonString(v.(string))

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

input.Policy = aws.String(policy)
}

if v, ok := d.GetOk("posix_profile"); ok {
Expand Down Expand Up @@ -187,12 +198,21 @@ func resourceAccessRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting home_directory_mappings: %w", err)
}
d.Set("home_directory_type", access.HomeDirectoryType)
d.Set("policy", access.Policy)

if err := d.Set("posix_profile", flattenTransferUserPosixUser(access.PosixProfile)); err != nil {
return fmt.Errorf("error setting posix_profile: %w", err)
}
// Role is currently not returned via the API.
// d.Set("role", access.Role)

policyToSet, err := verify.PolicyToSet(d.Get("policy").(string), aws.StringValue(access.Policy))

if err != nil {
return err
}

d.Set("policy", policyToSet)

d.Set("server_id", serverID)

return nil
Expand Down Expand Up @@ -225,7 +245,13 @@ func resourceAccessUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("policy") {
input.Policy = aws.String(d.Get("policy").(string))
policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

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

input.Policy = aws.String(policy)
}

if d.HasChange("posix_profile") {
Expand Down
31 changes: 28 additions & 3 deletions internal/service/transfer/user.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/transfer"
"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/flex"
Expand Down Expand Up @@ -71,6 +72,10 @@ func ResourceUser() *schema.Resource {
Optional: true,
ValidateFunc: verify.ValidIAMPolicyJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},

"posix_profile": {
Expand Down Expand Up @@ -151,7 +156,13 @@ func resourceUserCreate(d *schema.ResourceData, meta interface{}) error {
}

if v, ok := d.GetOk("policy"); ok {
input.Policy = aws.String(v.(string))
policy, err := structure.NormalizeJsonString(v.(string))

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

input.Policy = aws.String(policy)
}

if v, ok := d.GetOk("posix_profile"); ok {
Expand Down Expand Up @@ -203,7 +214,15 @@ func resourceUserRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting home_directory_mappings: %w", err)
}
d.Set("home_directory_type", user.HomeDirectoryType)
d.Set("policy", user.Policy)

policyToSet, err := verify.PolicyToSet(d.Get("policy").(string), aws.StringValue(user.Policy))

if err != nil {
return err
}

d.Set("policy", policyToSet)

if err := d.Set("posix_profile", flattenTransferUserPosixUser(user.PosixProfile)); err != nil {
return fmt.Errorf("error setting posix_profile: %w", err)
}
Expand Down Expand Up @@ -252,7 +271,13 @@ func resourceUserUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("policy") {
input.Policy = aws.String(d.Get("policy").(string))
policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

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

input.Policy = aws.String(policy)
}

if d.HasChange("posix_profile") {
Expand Down