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

feat: Implementation of the create_custom_role_trust_policy flag to control custom_role_trust_policy creation #360

Closed
wants to merge 14 commits into from

Conversation

schniber
Copy link

@schniber schniber commented Mar 31, 2023

Description

Hello,

This PR objective is to fix #320 (that got closed because of being stale) and an attempt to solve PR #321 as initially raised by @egarbi

Relying on the var.custom_role_trust_policy = "" in the iam_assume_role module under specific circumstances makes terraform plan error our as below:

╷
│ Error: Invalid count argument
│ 
│   on ../../modules/iam-assumable-role/main.tf line 12, in data "aws_iam_policy_document" "assume_role":
│   12:   count = var.custom_role_trust_policy == "" && var.role_requires_mfa ? 0 : 1
│ 
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the
│ -target argument to first apply only the resources that the count depends on.
╵
╷
│ Error: Invalid count argument
│ 
│   on ../../modules/iam-assumable-role/main.tf line 62, in data "aws_iam_policy_document" "assume_role_with_mfa":
│   62:   count = var.custom_role_trust_policy == "" && var.role_requires_mfa ? 1 : 0
│ 
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the
│ -target argument to first apply only the resources that the count depends on.

An example of code which makes the module error out is as below:

# #########################################
# # IAM assumable role with custom trust policy 2
# #########################################

resource "aws_iam_saml_provider" "idp_saml" {
  name                   = "idp_saml"
  saml_metadata_document = file("saml-metadata.xml")
}

data "aws_iam_policy_document" "custom_trust_policy_2" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRoleWithSAML"]

    principals {
      type        = "Federated"
      identifiers = [
        aws_iam_saml_provider.idp_saml.id
      ]
    }
    condition {
      test     = "StringEquals"
      variable = "SAML:aud"
      values   = [
        "https://signin.aws.amazon.com/saml"
      ]
    }
  }
  statement {
    effect  = "Allow"
    actions = ["sts:TagSession"]

    principals {
      type        = "Federated"
      identifiers = [
        aws_iam_saml_provider.idp_saml.id
      ]
    }
    condition {
      test     = "StringLike"
      variable = "aws:RequestTag/groups"
      values   = ["*"]
    }
  }
}

module "iam_assumable_role_custom_trust_policy_2" {
  source = "../../modules/iam-assumable-role"

  create_role = true

  role_name = "iam_assumable_role_custom_trust_policy_2"

  custom_role_trust_policy = data.aws_iam_policy_document.custom_trust_policy_2.json
  custom_role_policy_arns  = ["arn:aws:iam::aws:policy/AmazonCognitoReadOnly"]
}

Motivation and Context

As of today, the implementation does not allow the use of custom_role_trust_policies in all cases.

Breaking Changes

Nope this feature is not breaking changes since the new bool flag <create_custom_role_trust_policy> is defaulting to false.

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@schniber
Copy link
Author

Hello @antonbabenko and @bryantbiggs,
Would it be possible to provide me with a peer review ?

Thanks in advance.

Bests !

@schniber
Copy link
Author

Update fork to keep it in sync with upstream.
Thanks a lot for your valuable support.

@schniber
Copy link
Author

schniber commented May 5, 2023

Update fork to keep it in sync with upstream.
Thanks a lot for your valuable support @bryantbiggs @antonbabenko

@schniber
Copy link
Author

Synced Forked with Upstream repository.

@davepattie
Copy link

Thanks for your efforts on this PR. I am also hitting this same problem and would benefit from seeing this get merged, cheers

@schniber
Copy link
Author

schniber commented Jul 6, 2023

hello @bryantbiggs,

Any luck for the review of this PR ?

Thanks a lot for your valuable support.

Bests.

@github-actions
Copy link

github-actions bot commented Aug 6, 2023

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Aug 6, 2023
@schniber
Copy link
Author

schniber commented Aug 6, 2023

Hello @antonbabenko
Any luck with the peer review of this PR ? It's solving an issue we face witg the current implementation.

Thanks a lot for your valuable help.

@github-actions github-actions bot removed the stale label Aug 7, 2023
@egarbi
Copy link
Contributor

egarbi commented Aug 31, 2023

@schniber my original PR was merged and this is no longer needed. Please check #321

@schniber
Copy link
Author

schniber commented Sep 1, 2023

@egarbi : Yes I saw that, but it looks like your PR only fixes this for the submodule iam assumable role.
in this PR, we apply the same logic to other modules where IAM Roles are created to allow users to maintain their own trust policies if need be.

Issue is that I see that some of the changes that @bryantbiggs recommended are conflicting with the merge on #321 as I deleted the variable "create_custom_trust_policy" in this iteration in favor of the variable "assume_role_policy".

Hopefully this helps converge to a single solution for all.

Bests.

@schniber schniber requested a review from bryantbiggs September 1, 2023 14:02
@@ -82,16 +82,10 @@ variable "custom_role_policy_arns" {
default = []
}

variable "custom_role_trust_policy" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change so we won't be making that here. a lot of these changes and nuanced differences will be resolved at the next breaking change (ref https://github.com/clowdhaus/terraform-aws-iam)

Copy link
Author

@schniber schniber Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I reverted back my changes to remove the breaking chages.
Thanks.

@schniber schniber requested a review from bryantbiggs September 1, 2023 16:56
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Oct 2, 2023
@schniber
Copy link
Author

schniber commented Oct 2, 2023

Commenting this to make sure it does not become stale!

Thanks.

@github-actions github-actions bot removed the stale label Oct 3, 2023
Copy link

github-actions bot commented Nov 3, 2023

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Nov 3, 2023
Copy link

This PR was automatically closed because of stale in 10 days

@github-actions github-actions bot closed this Nov 13, 2023
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Module "iam-assumable-role" Invalid count value when policy is computed
4 participants