hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.82k stars 9.17k forks source link

[Bug]: override_policy_documents not working for data.aws_iam_policy_document #29498

Open dustyhorizon opened 1 year ago

dustyhorizon commented 1 year ago

Terraform Core Version

1.3.9

AWS Provider Version

4.55.0

Affected Resource(s)

aws_iam_policy_document

Expected Behavior

Per the documentations here I expected that resources with statements of a specific SID will override (merge) with the policies specific in override_policy_documents.

Actual Behavior

Specifying the SID in the statement of the resource that is supposed to override one of the policy in override_policy_documents with the same SID does not does not seem to produce a "merged" policy.

In fact, the documentations here also seem to suggest that "not merging" is the correct behavior ... unless the documentation is wrong too.

i.e. in the "combined" resource, I expected that the statement with SID OverridePlaceHolderTwo to result in policy with effect = "Deny" and actions = ["*"] for that SID instead of the values from pre-merge / override.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

data "aws_iam_policy_document" "policy_one" {
  statement {
    sid    = "OverridePlaceHolderOne"
    effect = "Allow"

    actions   = ["s3:*"]
    resources = ["*"]
  }
}

data "aws_iam_policy_document" "policy_two" {
  statement {
    effect    = "Allow"
    actions   = ["ec2:*"]
    resources = ["*"]
  }

  statement {
    sid    = "OverridePlaceHolderTwo"
    effect = "Allow"

    actions   = ["iam:*"]
    resources = ["*"]
  }
}

data "aws_iam_policy_document" "policy_three" {
  statement {
    sid    = "OverridePlaceHolderOne"
    effect = "Deny"

    actions   = ["logs:*"]
    resources = ["*"]
  }
}

data "aws_iam_policy_document" "combined" {
  override_policy_documents = [
    data.aws_iam_policy_document.policy_one.json,
    data.aws_iam_policy_document.policy_two.json,
    data.aws_iam_policy_document.policy_three.json
  ]

  statement {
    sid    = "OverridePlaceHolderTwo"
    effect = "Deny"

    actions   = ["*"]
    resources = ["*"]
  }
}

Steps to Reproduce

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

justinretzolk commented 1 year ago

Hey @dustyhorizon 👋 Thank you for taking the time to raise this! Can you give me a better idea of what you believe the resulting JSON would look like? I'm not quite following, and want to make sure I'm able to answer your question appropriately.

edentsai commented 1 year ago

I have a similar confuse on this, for example:

data "aws_iam_policy_document" "override" {
  statement {
    sid = "SidToOverride"

    actions   = ["s3:*"]
    resources = ["*"]
  }
}

data "aws_iam_policy_document" "override_policy_document_example" {
  override_policy_documents = [data.aws_iam_policy_document.override.json]

  # NOTE: I want to override the principals and resources in the statement `SidToOverride`
  statement {
    sid = "SidToOverride"

    principals {
      type        = "AWS"
      identifiers = ["arn:aws:iam::123456789:role/developer"]
    }

    resources = [
      "arn:aws:s3:::somebucket",
      "arn:aws:s3:::somebucket/*",
    ]
  }
}

data.override.aws_iam_policy_document.policy_document_example.json actually generate the result without overrided principals and resources in the statement SidToOverride:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "SidToOverride",
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": "*"
    }
  ]
}

here is the result what I expected:

  {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Sid": "SidToOverride",
        "Effect": "Allow",
        "Action": "s3:*",
-       "Resource": "*"
+       "Principal": {
+         "AWS": "arn:aws:iam::123456789:role/developer",
+       ],
+       "Resource": [
+         "arn:aws:s3:::somebucket",
+         "arn:aws:s3:::somebucket/*",
+       ]
      }
    ]
  }
edentsai commented 1 year ago

maybe I misunderstand the usage, override_policy_documents will override the entire statement by matching same sid, not just replace the partial fields. 🤔

luqasz commented 9 months ago

I have exactly same problem. I'd like to provide ready made actions sets in module and let end user fill in resources list. Even add additional actions if needed. NOT replacing whole list.