Prevent spurious diffs in IAM policies #13813
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
IAM policies have several fields that are treated as arrays by the AWS API and Terraform, but are actually sets with no meaningful order (examples: actions, principals, resources). As a result, the order of the results returned by the AWS API can vary from API call to API call. This provider makes liberal use of
diff_suppress_funcs.suppressEquivalentAwsPolicyDiffs
which can suppress such a diff if the policy isn't changing for any other reason. However, if the policy is changing, then we will (correctly) get a plan, but it will include spurious diffs due to the arbitrary ordering of these sets.One solution, as in #11463, is to normalize the policy document as it's read from AWS, and normalize it as it's serialized to Terraform state. This PR does that for several resources (the ones where I've noted the need, though this is probably not exhaustive).
Here's an example:
Apply this, and then remove the middle principal and plan again. Without this patch, the plan has a random element to it, but it can look like:
With this patch, the plan is:
It would be ideal to centralize this logic, but I don't see a clear way to do that, and I'd appreciate suggestions.
Also, this PR actually doesn't touch actions and resources in the policy because they're not given any structure in
iam_policy_model.go
(i.e. just left asinterface{}
). With some guidance on how this should look, I'd be happy to make that change, as well.Community Note
Release note for CHANGELOG:
Output from acceptance testing:
Unfortunately, as in #11463, this doesn't fit the mold of most changes as far as testing goes. I think the most meaningful test would be one that inspects the contents of the plan, and I don't know how to do that with the testing framework.