Skip to content

Commit 470b6ff

Browse files
fix: Explicitly assume with condition matching role arn (#283)
Co-authored-by: Anton Babenko <[email protected]>
1 parent 99c69ad commit 470b6ff

File tree

17 files changed

+229
-47
lines changed

17 files changed

+229
-47
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.75.0
3+
rev: v1.76.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

modules/iam-account/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.iam_account.aws_iam_account_alias.this: Import complete!
1919
module.iam_account.aws_iam_account_alias.this: Refreshing state... (ID: this)
2020
2121
Import successful!
22-
```
22+
```
2323

2424
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2525
## Requirements

modules/iam-assumable-role-with-oidc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Creates single IAM role which can be assumed by trusted resources using OpenID C
44

55
[Creating IAM OIDC Identity Providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html)
66

7-
This module supports IAM Roles for kubernetes service accounts as described in the [EKS documentation](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html).
7+
This module supports IAM Roles for kubernetes service accounts as described in the [EKS documentation](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html).
88

99
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1010
## Requirements

modules/iam-assumable-role-with-oidc/main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ locals {
66
replace(url, "https://", "")
77
]
88
number_of_role_policy_arns = coalesce(var.number_of_role_policy_arns, length(var.role_policy_arns))
9+
role_name_condition = var.role_name != null ? var.role_name : "${var.role_name_prefix}*"
910
}
1011

1112
data "aws_caller_identity" "current" {}
@@ -25,7 +26,13 @@ data "aws_iam_policy_document" "assume_role_with_oidc" {
2526

2627
principals {
2728
type = "AWS"
28-
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role${var.role_path}${var.role_name}"]
29+
identifiers = ["*"]
30+
}
31+
32+
condition {
33+
test = "ArnLike"
34+
variable = "aws:PrincipalArn"
35+
values = ["arn:${local.partition}:iam::${local.account_id}:role${var.role_path}${local.role_name_condition}"]
2936
}
3037
}
3138
}

modules/iam-assumable-role-with-saml/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
Creates single IAM role which can be assumed by trusted resources using SAML Federated Users.
44

5-
[Creating IAM SAML Identity Providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_saml.html)
6-
[Enabling SAML 2.0 Federated Users to Access the AWS Management Console](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-saml.html)
5+
[Creating IAM SAML Identity Providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_saml.html)
6+
[Enabling SAML 2.0 Federated Users to Access the AWS Management Console](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-saml.html)
77

88
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
99
## Requirements
@@ -29,9 +29,7 @@ No modules.
2929
|------|------|
3030
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
3131
| [aws_iam_role_policy_attachment.custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
32-
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
3332
| [aws_iam_policy_document.assume_role_with_saml](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
34-
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
3533

3634
## Inputs
3735

modules/iam-assumable-role-with-saml/main.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
data "aws_caller_identity" "current" {}
2-
data "aws_partition" "current" {}
3-
41
locals {
52
identifiers = compact(distinct(concat(var.provider_ids, [var.provider_id])))
63
number_of_role_policy_arns = coalesce(var.number_of_role_policy_arns, length(var.role_policy_arns))
4+
role_name_condition = var.role_name != null ? var.role_name : "${var.role_name_prefix}*"
75
}
86

97
data "aws_iam_policy_document" "assume_role_with_saml" {
@@ -18,7 +16,13 @@ data "aws_iam_policy_document" "assume_role_with_saml" {
1816

1917
principals {
2018
type = "AWS"
21-
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role${var.role_path}${var.role_name}"]
19+
identifiers = ["*"]
20+
}
21+
22+
condition {
23+
test = "ArnLike"
24+
variable = "aws:PrincipalArn"
25+
values = ["arn:${local.partition}:iam::${local.account_id}:role${var.role_path}${local.role_name_condition}"]
2226
}
2327
}
2428
}

modules/iam-assumable-role/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ No modules.
3232
| [aws_iam_role_policy_attachment.custom](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
3333
| [aws_iam_role_policy_attachment.poweruser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
3434
| [aws_iam_role_policy_attachment.readonly](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
35-
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
3635
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
3736
| [aws_iam_policy_document.assume_role_with_mfa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
38-
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
3937

4038
## Inputs
4139

@@ -58,6 +56,7 @@ No modules.
5856
| <a name="input_readonly_role_policy_arn"></a> [readonly\_role\_policy\_arn](#input\_readonly\_role\_policy\_arn) | Policy ARN to use for readonly role | `string` | `"arn:aws:iam::aws:policy/ReadOnlyAccess"` | no |
5957
| <a name="input_role_description"></a> [role\_description](#input\_role\_description) | IAM Role description | `string` | `""` | no |
6058
| <a name="input_role_name"></a> [role\_name](#input\_role\_name) | IAM role name | `string` | `""` | no |
59+
| <a name="input_role_name_prefix"></a> [role\_name\_prefix](#input\_role\_name\_prefix) | IAM role name prefix | `string` | `null` | no |
6160
| <a name="input_role_path"></a> [role\_path](#input\_role\_path) | Path of IAM role | `string` | `"/"` | no |
6261
| <a name="input_role_permissions_boundary_arn"></a> [role\_permissions\_boundary\_arn](#input\_role\_permissions\_boundary\_arn) | Permissions boundary ARN to use for IAM role | `string` | `""` | no |
6362
| <a name="input_role_requires_mfa"></a> [role\_requires\_mfa](#input\_role\_requires\_mfa) | Whether role requires MFA | `bool` | `true` | no |

modules/iam-assumable-role/main.tf

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
data "aws_caller_identity" "current" {}
2-
data "aws_partition" "current" {}
3-
41
locals {
52
role_sts_externalid = flatten([var.role_sts_externalid])
3+
role_name_condition = var.role_name != null ? var.role_name : "${var.role_name_prefix}*"
64
}
75

86
data "aws_iam_policy_document" "assume_role" {
@@ -19,7 +17,13 @@ data "aws_iam_policy_document" "assume_role" {
1917

2018
principals {
2119
type = "AWS"
22-
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role${var.role_path}${var.role_name}"]
20+
identifiers = ["*"]
21+
}
22+
23+
condition {
24+
test = "ArnLike"
25+
variable = "aws:PrincipalArn"
26+
values = ["arn:${local.partition}:iam::${local.account_id}:role${var.role_path}${local.role_name_condition}"]
2327
}
2428
}
2529
}
@@ -63,7 +67,13 @@ data "aws_iam_policy_document" "assume_role_with_mfa" {
6367

6468
principals {
6569
type = "AWS"
66-
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role${var.role_path}${var.role_name}"]
70+
identifiers = ["*"]
71+
}
72+
73+
condition {
74+
test = "ArnLike"
75+
variable = "aws:PrincipalArn"
76+
values = ["arn:${local.partition}:iam::${local.account_id}:role${var.role_path}${local.role_name_condition}"]
6777
}
6878
}
6979
}
@@ -109,6 +119,7 @@ resource "aws_iam_role" "this" {
109119
count = var.create_role ? 1 : 0
110120

111121
name = var.role_name
122+
name_prefix = var.role_name_prefix
112123
path = var.role_path
113124
max_session_duration = var.max_session_duration
114125
description = var.role_description

modules/iam-assumable-role/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ variable "role_name" {
4646
default = ""
4747
}
4848

49+
variable "role_name_prefix" {
50+
description = "IAM role name prefix"
51+
type = string
52+
default = null
53+
}
54+
4955
variable "role_path" {
5056
description = "Path of IAM role"
5157
type = string

modules/iam-assumable-roles-with-saml/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Creates predefined IAM roles (admin, poweruser and readonly) which can be assumed by trusted resources using SAML Federated Users.
44

55

6-
[Creating IAM SAML Identity Providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_saml.html)
7-
[Enabling SAML 2.0 Federated Users to Access the AWS Management Console](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-saml.html)
6+
[Creating IAM SAML Identity Providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_saml.html)
7+
[Enabling SAML 2.0 Federated Users to Access the AWS Management Console](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-saml.html)
88

99
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1010
## Requirements
@@ -34,9 +34,7 @@ No modules.
3434
| [aws_iam_role_policy_attachment.admin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
3535
| [aws_iam_role_policy_attachment.poweruser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
3636
| [aws_iam_role_policy_attachment.readonly](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
37-
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
3837
| [aws_iam_policy_document.assume_role_with_saml](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
39-
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
4038

4139
## Inputs
4240

0 commit comments

Comments
 (0)