-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
s3 bucket policy with not_principal always changes due to ordering #13144
Comments
I can confirm this issue is still occurring in AWS hashicorp/aws version v3.18.0. Even adding a sort on the principals directly does not change this diff behavior. principals {
type = "AWS"
identifiers = sort([for role in data.aws_iam_role.decrypt_allowed_roles : role.arn])
} While the end result makes no difference, at a much larger scale this puts an undo burden on the reviewer to manually parse the output. I personally have had to review about 9 prinicple changes for someone simply adding 1 new role to a list.
|
@craiggenner I can confirm what @roccomuscaritolo-okta sees. It happens in the normal principal field as well. The crazy thing is that if you test an expression such as It looks like the bucket policy resource is jumbling them up at some point. |
The Principals and NotPrincipals elements of IAM policy statements appear to be handled in exactly the same way, so that would explain why the behavior is observed in both places. terraform-provider-aws/aws/iam_policy_model.go Lines 22 to 23 in 3b05635
The problem appears to occur in the unmarshalling of the policy document. That's a guess because it only happens when the result of encodejson has been set on the bucket policy resource. I'm unsure if a change is required in UnmarshalJSON. It looks like it just iterates over whatever its given without reodering anything. terraform-provider-aws/aws/iam_policy_model.go Lines 127 to 159 in e9a98cf
The schema for the principal policy element says that identifiers are a set, and so unordered. As it turns out, the order matters for diffs. Instead of using schema.TypeSet, should it instead be using schema.TypeList? terraform-provider-aws/aws/data_source_aws_iam_policy_document.go Lines 344 to 364 in ac06ced
A similar change was applied to preserve the order of condition values. The type of the values in the condition schema was changes from TypeSet to TypeList. @YakDriver you were here recently (it was your PR that fixed the condition ordering!) Do you think changing TypeSet to TypeList here would be enough to fix this? |
See below for a small Terraform configuration that reproduces the issue. Change to a temporary directory and copy the configuration to a file in the directory called main.tf. Before running the Terraform commands, first set your environment with credentials and a region. export AWS_PROFILE=...
export AWS_DEFAULT_REGION=... Output after first apply shows that the principal elements in the bucket policy are ordered according to the configuration. terraform apply
terraform output --raw test | jq {
"Statement": [
{
"Action": "*",
"Effect": "Deny",
"Principal": {
"AWS": [
"arn:aws:iam::111111111111:role/role-0",
"arn:aws:iam::111111111111:role/role-1",
"arn:aws:iam::111111111111:role/role-2",
"arn:aws:iam::111111111111:role/role-3",
"arn:aws:iam::111111111111:role/role-4",
"arn:aws:iam::111111111111:role/role-5",
"arn:aws:iam::111111111111:role/role-6",
"arn:aws:iam::111111111111:role/role-7",
"arn:aws:iam::111111111111:role/role-8",
"arn:aws:iam::111111111111:role/role-9"
]
},
"Resource": "arn:aws:s3:::tftest20210525081545966100000001"
}
],
"Version": "2012-10-17"
} Second apply shows a change to the output. Without changing the effective permissions of the bucket policy, the principals are reordered.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Principal": {
"AWS": [
"arn:aws:iam::111111111111:role/role-4",
"arn:aws:iam::111111111111:role/role-5",
"arn:aws:iam::111111111111:role/role-2",
"arn:aws:iam::111111111111:role/role-3",
"arn:aws:iam::111111111111:role/role-0",
"arn:aws:iam::111111111111:role/role-1",
"arn:aws:iam::111111111111:role/role-8",
"arn:aws:iam::111111111111:role/role-9",
"arn:aws:iam::111111111111:role/role-6",
"arn:aws:iam::111111111111:role/role-7"
]
},
"Action": "*",
"Resource": "arn:aws:s3:::tftest20210525081545966100000001"
}
]
} The order of the principals changes arbitrarily with each successive apply. The Terraform configuration: output "test" {
value = aws_s3_bucket_policy.test.policy
}
resource "aws_s3_bucket" "test" {
bucket_prefix = "tftest"
}
resource "aws_s3_bucket_policy" "test" {
bucket = aws_s3_bucket.test.id
policy = jsonencode({
Statement = [
{
Principal = {
AWS = [for r in aws_iam_role.test: r.arn]
}
Resource = "${aws_s3_bucket.test.arn}"
Action = "*"
Effect = "Deny"
}
]
Version = "2012-10-17"
})
}
resource "aws_iam_role" "test" {
count = 10
name = "role-${count.index}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Deny"
Action = "sts:AssumeRole"
Principal = {
AWS = "*"
}
}
]
})
} |
Potentially related: jen20/awspolicyequivalence#10. |
I can confirm I'm having this same problem for both |
Also observing this issue with |
This functionality has been released in v3.68.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Community Note
Terraform Version
$ terraform -v
Terraform v0.12.24
Affected Resource(s)
Terraform Configuration Files
Debug Output
https://gist.github.com/craiggenner/b315384e6ca2cead486b14665840807e
Panic Output
None
Expected Behavior
Terraform shouldn't expect to make any changes
Actual Behavior
Terraform always wants to replace the policy of the s3 bucket
Steps to Reproduce
Create terraform code as above
terraform apply
terraform apply
Important Factoids
Running this against eu-west-1 region.
The issue appears to be related to the use of the sts assumed-role in the
not_principals
, a regular iam role entry doesn't produce this. With multiple entries the order of thenot_principals
block is moved around within the json policyReferences
The text was updated successfully, but these errors were encountered: