Skip to content

Commit

Permalink
normalize JSON of policies
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Oct 31, 2024
1 parent 570ce45 commit 552374d
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions pkg/resources/sos_bucket_policy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package sos_bucket_policy

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/aws/aws-sdk-go-v2/service/s3"
Expand Down Expand Up @@ -189,7 +191,20 @@ func (r *ResourceSOSBucketPolicy) Read(ctx context.Context, req resource.ReadReq
return
}

state.Policy = types.StringPointerValue(policy.Policy)
if policy == nil {
resp.Diagnostics.AddError(
"bucket policy is nil",
"",
)
return
}

pol, err := normalizeJSON(*policy.Policy)
if err != nil {
return
}

state.Policy = types.StringValue(pol)

// Save updated state into Terraform state.
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand All @@ -199,6 +214,35 @@ func (r *ResourceSOSBucketPolicy) Read(ctx context.Context, req resource.ReadReq
})
}

func normalizeJSON(j string) (string, error) {
var m map[string]interface{}

if err := json.Unmarshal([]byte(j), m); err != nil {

Check failure on line 220 in pkg/resources/sos_bucket_policy/resource.go

View workflow job for this annotation

GitHub Actions / test

json.Unmarshal expects to unmarshal into a pointer, but the provided value is not a pointer (SA1014)
return "", err
}

normalized, err := json.Marshal(m)
if err != nil {
return "", err
}

return string(normalized), nil
}

func isJSONEqual(j1, j2 string) (bool, error) {
var map1, map2 map[string]interface{}

if err := json.Unmarshal([]byte(j1), map1); err != nil {

Check failure on line 235 in pkg/resources/sos_bucket_policy/resource.go

View workflow job for this annotation

GitHub Actions / test

json.Unmarshal expects to unmarshal into a pointer, but the provided value is not a pointer (SA1014)
return false, err
}

if err := json.Unmarshal([]byte(j2), map2); err != nil {

Check failure on line 239 in pkg/resources/sos_bucket_policy/resource.go

View workflow job for this annotation

GitHub Actions / test

json.Unmarshal expects to unmarshal into a pointer, but the provided value is not a pointer (SA1014)
return false, err
}

return reflect.DeepEqual(map1, map2), nil
}

// Update resources in-place by receiving Terraform prior state, configuration, and plan data, performing update logic, and saving updated Terraform state data.
func (r *ResourceSOSBucketPolicy) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var state, plan ResourceSOSBucketPolicyModel
Expand Down Expand Up @@ -229,7 +273,16 @@ func (r *ResourceSOSBucketPolicy) Update(ctx context.Context, req resource.Updat
return
}

if !plan.Policy.Equal(state.Policy) {
jsonIsEqual, err := isJSONEqual(plan.Policy.ValueString(), state.Policy.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"failed to test JSON equality of policy",
err.Error(),
)
return
}

if !jsonIsEqual {
_, err = sosClient.PutBucketPolicy(ctx, &s3.PutBucketPolicyInput{
Bucket: plan.Bucket.ValueStringPointer(),
Policy: plan.Policy.ValueStringPointer(),
Expand Down

0 comments on commit 552374d

Please sign in to comment.