Skip to content

Commit 481095e

Browse files
egarbibryantbiggs
andauthored
feat: Add create_custom_role_trust_policy to control when a custom_role_trust_policy should be used (#321)
Co-authored-by: Bryant Biggs <[email protected]>
1 parent cbc6dfa commit 481095e

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.82.0
3+
rev: v1.83.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

examples/iam-assumable-role/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ module "iam_assumable_role_custom_trust_policy" {
104104

105105
role_name = "iam_assumable_role_custom_trust_policy"
106106

107-
custom_role_trust_policy = data.aws_iam_policy_document.custom_trust_policy.json
108-
custom_role_policy_arns = ["arn:aws:iam::aws:policy/AmazonCognitoReadOnly"]
107+
create_custom_role_trust_policy = true
108+
custom_role_trust_policy = data.aws_iam_policy_document.custom_trust_policy.json
109+
custom_role_policy_arns = ["arn:aws:iam::aws:policy/AmazonCognitoReadOnly"]
109110
}
110111

111112
data "aws_iam_policy_document" "custom_trust_policy" {

modules/iam-assumable-role/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ No modules.
4646
| <a name="input_attach_admin_policy"></a> [attach\_admin\_policy](#input\_attach\_admin\_policy) | Whether to attach an admin policy to a role | `bool` | `false` | no |
4747
| <a name="input_attach_poweruser_policy"></a> [attach\_poweruser\_policy](#input\_attach\_poweruser\_policy) | Whether to attach a poweruser policy to a role | `bool` | `false` | no |
4848
| <a name="input_attach_readonly_policy"></a> [attach\_readonly\_policy](#input\_attach\_readonly\_policy) | Whether to attach a readonly policy to a role | `bool` | `false` | no |
49+
| <a name="input_create_custom_role_trust_policy"></a> [create\_custom\_role\_trust\_policy](#input\_create\_custom\_role\_trust\_policy) | Whether to create a custom\_role\_trust\_policy. Prevent errors with count, when custom\_role\_trust\_policy is computed | `bool` | `false` | no |
4950
| <a name="input_create_instance_profile"></a> [create\_instance\_profile](#input\_create\_instance\_profile) | Whether to create an instance profile | `bool` | `false` | no |
5051
| <a name="input_create_role"></a> [create\_role](#input\_create\_role) | Whether to create a role | `bool` | `false` | no |
5152
| <a name="input_custom_role_policy_arns"></a> [custom\_role\_policy\_arns](#input\_custom\_role\_policy\_arns) | List of ARNs of IAM policies to attach to IAM role | `list(string)` | `[]` | no |
52-
| <a name="input_custom_role_trust_policy"></a> [custom\_role\_trust\_policy](#input\_custom\_role\_trust\_policy) | A custom role trust policy | `string` | `""` | no |
53+
| <a name="input_custom_role_trust_policy"></a> [custom\_role\_trust\_policy](#input\_custom\_role\_trust\_policy) | A custom role trust policy. (Only valid if create\_custom\_role\_trust\_policy = true) | `string` | `""` | no |
5354
| <a name="input_force_detach_policies"></a> [force\_detach\_policies](#input\_force\_detach\_policies) | Whether policies should be detached from this role when destroying | `bool` | `false` | no |
5455
| <a name="input_max_session_duration"></a> [max\_session\_duration](#input\_max\_session\_duration) | Maximum CLI/API session duration in seconds between 3600 and 43200 | `number` | `3600` | no |
5556
| <a name="input_mfa_age"></a> [mfa\_age](#input\_mfa\_age) | Max age of valid MFA (in seconds) for roles which require MFA | `number` | `86400` | no |

modules/iam-assumable-role/main.tf

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ data "aws_caller_identity" "current" {}
22
data "aws_partition" "current" {}
33

44
locals {
5-
account_id = data.aws_caller_identity.current.account_id
6-
partition = data.aws_partition.current.partition
7-
role_sts_externalid = flatten([var.role_sts_externalid])
8-
role_name_condition = var.role_name != null ? var.role_name : "${var.role_name_prefix}*"
5+
account_id = data.aws_caller_identity.current.account_id
6+
partition = data.aws_partition.current.partition
7+
role_sts_externalid = flatten([var.role_sts_externalid])
8+
role_name_condition = var.role_name != null ? var.role_name : "${var.role_name_prefix}*"
9+
custom_role_trust_policy_condition = var.create_custom_role_trust_policy ? var.custom_role_trust_policy : ""
910
}
1011

1112
data "aws_iam_policy_document" "assume_role" {
12-
count = var.custom_role_trust_policy == "" && var.role_requires_mfa ? 0 : 1
13+
count = !var.create_custom_role_trust_policy && var.role_requires_mfa ? 0 : 1
1314

1415
dynamic "statement" {
1516
# https://aws.amazon.com/blogs/security/announcing-an-update-to-iam-role-trust-policy-behavior/
@@ -68,7 +69,7 @@ data "aws_iam_policy_document" "assume_role" {
6869
}
6970

7071
data "aws_iam_policy_document" "assume_role_with_mfa" {
71-
count = var.custom_role_trust_policy == "" && var.role_requires_mfa ? 1 : 0
72+
count = !var.create_custom_role_trust_policy && var.role_requires_mfa ? 1 : 0
7273

7374
dynamic "statement" {
7475
# https://aws.amazon.com/blogs/security/announcing-an-update-to-iam-role-trust-policy-behavior/
@@ -151,7 +152,7 @@ resource "aws_iam_role" "this" {
151152
permissions_boundary = var.role_permissions_boundary_arn
152153

153154
assume_role_policy = coalesce(
154-
var.custom_role_trust_policy,
155+
local.custom_role_trust_policy_condition,
155156
try(data.aws_iam_policy_document.assume_role_with_mfa[0].json,
156157
data.aws_iam_policy_document.assume_role[0].json
157158
)

modules/iam-assumable-role/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ variable "custom_role_policy_arns" {
8383
}
8484

8585
variable "custom_role_trust_policy" {
86-
description = "A custom role trust policy"
86+
description = "A custom role trust policy. (Only valid if create_custom_role_trust_policy = true)"
8787
type = string
8888
default = ""
8989
}
9090

91+
variable "create_custom_role_trust_policy" {
92+
description = "Whether to create a custom_role_trust_policy. Prevent errors with count, when custom_role_trust_policy is computed"
93+
type = bool
94+
default = false
95+
}
96+
9197
variable "number_of_custom_role_policy_arns" {
9298
description = "Number of IAM policies to attach to IAM role"
9399
type = number

0 commit comments

Comments
 (0)