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

minio_iam_policy order keeps shifting #117

Closed
tv42 opened this issue Aug 3, 2021 · 2 comments · Fixed by #180
Closed

minio_iam_policy order keeps shifting #117

tv42 opened this issue Aug 3, 2021 · 2 comments · Fixed by #180

Comments

@tv42
Copy link

tv42 commented Aug 3, 2021

I don't know if this problem originates from this code, or if minio itself keeps shuffling this (and the provider should just somehow cope with it), but I keep getting output like this from terraform plan:

  # module.a.minio_iam_policy.b has been changed
  ~ resource "minio_iam_policy" "b" {
        id     = "b"
        name   = "b"
      ~ policy = jsonencode(
          ~ {
              ~ Statement = [
                  ~ {
                      ~ Resource = [
                          - "arn:aws:s3:::x",
                            "arn:aws:s3:::y",
                            "arn:aws:s3:::z",
                          + "arn:aws:s3:::x",
                        ]
                        # (2 unchanged elements hidden)
                    },
                    {
                        Action   = [
                            "s3:GetObject",
                            "s3:PutObject",
                        ]
                        Effect   = "Allow"
                        Resource = [
                            "arn:aws:s3:::x/*",
                            "arn:aws:s3:::y/*",
                            "arn:aws:s3:::z/*",
                        ]
                    },
                ]
                # (1 unchanged element hidden)
            }
        )
    }
[...]
No changes. Your infrastructure matches the configuration.

Updating the *.tf to match the order here doesn't really matter; in a couple of runs, it'll show a different order.

All the lists seem to be getting shuffled, observing Resource, Action.

And in the end, terraform thinks that there are no changes to apply, so something seems to be doing an order-independent comparison, but the diffs are distractingly long.

@tv42
Copy link
Author

tv42 commented Aug 3, 2021

This is caused by these being stored as map[string]struct{}, and map iteration order is random.

Actions set.StringSet `json:"Action"`
Conditions ConditionMap `json:"Condition,omitempty"`
Effect string `json:",omitempty"`
Principal string `json:"Principal,omitempty"`
Resources set.StringSet `json:"Resource"`

https://pkg.go.dev/github.com/minio/minio-go/v7/pkg/set#StringSet

encoding/json would sort the keys of maps, but it's being overridden to get the list-like marshaling:

https://github.com/minio/minio-go/blob/0823af6c707c3af8ada4a4cd45cf1ff7378ebc2b/pkg/set/stringset.go#L147

https://github.com/minio/minio-go/blob/0823af6c707c3af8ada4a4cd45cf1ff7378ebc2b/pkg/set/stringset.go#L33

If terraform can't be taught a smarter comparison mechanism, then ToSlice should alphabetize the list, on both sides (data fetched from minio, data created from *.tf).

@pjsier
Copy link
Collaborator

pjsier commented Mar 12, 2022

It looks like the most straightforward place to address this might be the suppressEquivalentAwsPolicyDiffs function since it's already being used as the DiffSuppressFunc in multiple places.

func suppressEquivalentAwsPolicyDiffs(k, old, new string, d *schema.ResourceData) bool {
equivalent, err := awspolicy.PoliciesAreEquivalent(old, new)
if err != nil {
return false
}
return equivalent
}

It looks like this is a wrapper around jen20/awspolicyequivalence which seems to require the order to be the same. It looks like there's a hashicorp fork here hashicorp/awspolicyequivalence that fixes this issue. I'll put in a PR replacing this to see if it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants