Skip to content

Commit a393899

Browse files
authored
fix: ignore statement action order when comparing policy documents (#12)
Implements #2
1 parent 1ed8a50 commit a393899

6 files changed

Lines changed: 62 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
### Fixed
9+
10+
- objsto_bucket_policy: when comparing policy documents, ignore statement action order because Minio returns actions in inconsistent order.
11+
812
## [0.1.0]
913

1014
### Added

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test:
2828
go test -v -cover -timeout=120s -parallel=10 ./...
2929

3030
testacc:
31-
TF_ACC=1 go test -v -cover -timeout 120m ./...
31+
TF_ACC=1 go test -v -count 1 -cover -timeout 120m ./...
3232

3333
release-notes: CHANGELOG_HEADER = ^\#\# \[
3434
release-notes: CHANGELOG_VERSION = $(subst v,,$(VERSION))

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-go v0.25.0
1313
github.com/hashicorp/terraform-plugin-log v0.9.0
1414
github.com/hashicorp/terraform-plugin-testing v1.10.0
15+
github.com/stretchr/testify v1.10.0
1516
)
1617

1718
require (
@@ -27,6 +28,7 @@ require (
2728
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.5 // indirect
2829
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.3 // indirect
2930
github.com/cloudflare/circl v1.3.7 // indirect
31+
github.com/davecgh/go-spew v1.1.1 // indirect
3032
github.com/fatih/color v1.16.0 // indirect
3133
github.com/golang/protobuf v1.5.4 // indirect
3234
github.com/google/go-cmp v0.6.0 // indirect
@@ -58,8 +60,8 @@ require (
5860
github.com/mitchellh/mapstructure v1.5.0 // indirect
5961
github.com/mitchellh/reflectwalk v1.0.2 // indirect
6062
github.com/oklog/run v1.0.0 // indirect
63+
github.com/pmezard/go-difflib v1.0.0 // indirect
6164
github.com/rogpeppe/go-internal v1.12.0 // indirect
62-
github.com/stretchr/testify v1.9.0 // indirect
6365
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
6466
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
6567
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
@@ -75,4 +77,5 @@ require (
7577
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
7678
google.golang.org/grpc v1.67.1 // indirect
7779
google.golang.org/protobuf v1.35.1 // indirect
80+
gopkg.in/yaml.v3 v3.0.1 // indirect
7881
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L
165165
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
166166
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
167167
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
168-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
169-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
168+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
169+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
170170
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
171171
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
172172
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=

internal/provider/bucket_policy_resource.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"sort"
89

910
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
1011
"github.com/aws/aws-sdk-go-v2/service/s3"
@@ -196,6 +197,24 @@ func normalizePolicyDocument(document string) (string, diag.Diagnostics) {
196197
delete(unmarshaled, "ID")
197198
}
198199

200+
// Sort statement actions (Minio)
201+
if statements, ok := unmarshaled["Statement"].([]interface{}); ok {
202+
for _, statement := range statements {
203+
if statement, ok := statement.(map[string]interface{}); ok {
204+
if actions, ok := statement["Action"].([]interface{}); ok {
205+
sort.Slice(actions, func(i, j int) bool {
206+
a, aOk := actions[i].(string)
207+
b, bOk := actions[j].(string)
208+
if !aOk || !bOk {
209+
return false
210+
}
211+
return a < b
212+
})
213+
}
214+
}
215+
}
216+
}
217+
199218
// Remove "null" Id (UpCloud Managed Object Storage)
200219
if id := unmarshaled["Id"]; id == "null" {
201220
delete(unmarshaled, "Id")

internal/provider/bucket_policy_resource_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/config"
99
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-testing/terraform"
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
func checkGetUrl(name, key string, expectedStatus int) resource.TestCheckFunc {
@@ -31,6 +32,37 @@ func checkGetUrl(name, key string, expectedStatus int) resource.TestCheckFunc {
3132
}
3233
}
3334

35+
func TestNormalizePolicyDocument(t *testing.T) {
36+
tests := []struct {
37+
name string
38+
input string
39+
expected string
40+
}{
41+
{
42+
name: "Change ID to Id",
43+
input: `{"ID":"PublicRead"}`,
44+
expected: `{"Id":"PublicRead"}`,
45+
},
46+
{
47+
name: "Removes null Id",
48+
input: `{"Id":"null"}`,
49+
expected: `{}`,
50+
},
51+
{
52+
name: "Sorts statement actions",
53+
input: `{"Statement":[{"Action":["s3:ListBucket","s3:GetBucketLocation"]}]}`,
54+
expected: `{"Statement":[{"Action":["s3:GetBucketLocation","s3:ListBucket"]}]}`,
55+
},
56+
}
57+
for _, test := range tests {
58+
t.Run(test.name, func(t *testing.T) {
59+
actual, diags := normalizePolicyDocument(test.input)
60+
assert.False(t, diags.HasError())
61+
assert.Equal(t, test.expected, actual)
62+
})
63+
}
64+
}
65+
3466
func TestAccBucketPolicyResource(t *testing.T) {
3567
bucket_name := withSuffix("bucket-policy")
3668
variables := func(public_read_access bool) map[string]config.Variable {

0 commit comments

Comments
 (0)