Skip to content

Commit 6c252cf

Browse files
authored
feat: Add a flag to enable/disable role creation (#68)
The module previously created a role automatically, and allowed managed policies and inline policies to be attached to the role, this works well for simple setups but makes it difficult to do multiple account/multiple role OIDC configurations.
1 parent 208ca71 commit 6c252cf

File tree

5 files changed

+60
-38
lines changed

5 files changed

+60
-38
lines changed

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,25 @@ applied, the JWT will contain an updated `iss` claim.
8383

8484
## Inputs
8585

86-
| Name | Description | Type | Default | Required |
87-
| ------------------------------- | ----------------------------------------------------------------------------- | -------------- | ----------------- | :------: |
88-
| additional_audiences | List of additional OIDC audiences allowed to assume the role. | `list(string)` | `null` | no |
89-
| additional_thumbprints | A list of additional thumbprints for the OIDC provider. | `list(string)` | `[]` | no |
90-
| attach_read_only_policy | Flag to enable/disable the attachment of the ReadOnly policy. | `bool` | `false` | no |
91-
| create_oidc_provider | Flag to enable/disable the creation of the GitHub OIDC provider. | `bool` | `true` | no |
92-
| dangerously_attach_admin_policy | Flag to enable/disable the attachment of the AdministratorAccess policy. | `bool` | `false` | no |
93-
| enterprise_slug | Enterprise slug for GitHub Enterprise Cloud customers. | `string` | `""` | no |
94-
| force_detach_policies | Flag to force detachment of policies attached to the IAM role. | `bool` | `false` | no |
95-
| github_repositories | A list of GitHub organization/repository names authorized to assume the role. | `list(string)` | n/a | yes |
96-
| iam_role_inline_policies | Inline policies map with policy name as key and json as value. | `map(string)` | `{}` | no |
97-
| iam_role_name | The name of the IAM role to be created and made assumable by GitHub Actions. | `string` | `"GitHubActions"` | no |
98-
| iam_role_path | The path under which to create IAM role. | `string` | `"/"` | no |
99-
| iam_role_permissions_boundary | The ARN of the permissions boundary to be used by the IAM role. | `string` | `""` | no |
100-
| iam_role_policy_arns | A list of IAM policy ARNs to attach to the IAM role. | `list(string)` | `[]` | no |
101-
| max_session_duration | The maximum session duration in seconds. | `number` | `3600` | no |
102-
| tags | A map of tags to be applied to all applicable resources. | `map(string)` | `{}` | no |
86+
| Name | Description | Type | Default | Required |
87+
| ------------------------------- | ---------------------------------------------------------------------------- | -------------- | ----------------- | :------: |
88+
| additional_audiences | Additional OIDC audiences allowed to assume the role. | `list(string)` | `null` | no |
89+
| additional_thumbprints | Additional thumbprints for the OIDC provider. | `list(string)` | `[]` | no |
90+
| attach_read_only_policy | Enable/disable the attachment of the ReadOnly policy. | `bool` | `false` | no |
91+
| create_iam_role | Enable/disable creation of the IAM role. | `bool` | `true` | no |
92+
| create_oidc_provider | Enable/disable the creation of the GitHub OIDC provider. | `bool` | `true` | no |
93+
| dangerously_attach_admin_policy | Enable/disable the attachment of the AdministratorAccess policy. | `bool` | `false` | no |
94+
| enabled | Enable/disable the creation of resources. | `bool` | `true` | no |
95+
| enterprise_slug | Enterprise slug for GitHub Enterprise Cloud customers. | `string` | `""` | no |
96+
| force_detach_policies | Force detachment of policies attached to the IAM role. | `bool` | `false` | no |
97+
| github_repositories | GitHub organization/repository names authorized to assume the role. | `list(string)` | n/a | yes |
98+
| iam_role_inline_policies | Inline policies map with policy name as key and json as value. | `map(string)` | `{}` | no |
99+
| iam_role_name | The name of the IAM role to be created and made assumable by GitHub Actions. | `string` | `"GitHubActions"` | no |
100+
| iam_role_path | The path under which to create IAM role. | `string` | `"/"` | no |
101+
| iam_role_permissions_boundary | The ARN of the permissions boundary to be used by the IAM role. | `string` | `""` | no |
102+
| iam_role_policy_arns | IAM policy ARNs to attach to the IAM role. | `list(string)` | `[]` | no |
103+
| max_session_duration | The maximum session duration in seconds. | `number` | `3600` | no |
104+
| tags | Tags to be applied to all applicable resources. | `map(string)` | `{}` | no |
103105

104106
## Outputs
105107

@@ -108,6 +110,7 @@ applied, the JWT will contain an updated `iss` claim.
108110
| iam_role_arn | The ARN of the IAM role. |
109111
| iam_role_name | The name of the IAM role. |
110112
| oidc_provider_arn | The ARN of the OIDC provider. |
113+
| oidc_provider_url | The URL of the OIDC provider. |
111114

112115
<!-- END_TF_DOCS -->
113116

data.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
data "aws_partition" "this" {}
55

66
data "aws_iam_policy_document" "assume_role" {
7+
count = var.enabled && var.create_oidc_provider ? 1 : 0
8+
79
statement {
810
actions = ["sts:AssumeRoleWithWebIdentity"]
911
effect = "Allow"

main.tf

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ locals {
1212
}
1313

1414
resource "aws_iam_role" "github" {
15-
assume_role_policy = data.aws_iam_policy_document.assume_role.json
16-
description = "Role assumed by the GitHub OIDC provider."
15+
count = var.enabled && var.create_iam_role ? 1 : 0
16+
17+
assume_role_policy = data.aws_iam_policy_document.assume_role[0].json
18+
description = "Assumed by the GitHub OIDC provider."
1719
force_detach_policies = var.force_detach_policies
1820
max_session_duration = var.max_session_duration
1921
name = var.iam_role_name
@@ -27,28 +29,31 @@ resource "aws_iam_role_policy" "inline_policies" {
2729

2830
name = each.key
2931
policy = each.value
30-
role = aws_iam_role.github.id
32+
role = aws_iam_role.github[0].id
3133
}
3234

3335
resource "aws_iam_role_policy_attachment" "admin" {
34-
count = var.dangerously_attach_admin_policy ? 1 : 0
36+
count = var.enabled && var.create_iam_role && var.dangerously_attach_admin_policy ? 1 : 0
3537

3638
policy_arn = "arn:${local.partition}:iam::aws:policy/AdministratorAccess"
37-
role = aws_iam_role.github.id
39+
role = aws_iam_role.github[0].id
3840
}
3941

4042
resource "aws_iam_role_policy_attachment" "read_only" {
41-
count = var.attach_read_only_policy ? 1 : 0
43+
count = var.enabled && var.create_iam_role && var.attach_read_only_policy ? 1 : 0
4244

4345
policy_arn = "arn:${local.partition}:iam::aws:policy/ReadOnlyAccess"
44-
role = aws_iam_role.github.id
46+
role = aws_iam_role.github[0].id
4547
}
4648

4749
resource "aws_iam_role_policy_attachment" "custom" {
48-
count = length(var.iam_role_policy_arns)
50+
count = var.enabled && var.create_iam_role ? length(var.iam_role_policy_arns) : 0
4951

50-
policy_arn = var.iam_role_policy_arns[count.index]
51-
role = aws_iam_role.github.id
52+
role = aws_iam_role.github[0].id
53+
policy_arn = format(
54+
"arn:%v:iam::aws:policy/AdministratorAccess",
55+
local.partition,
56+
)
5257
}
5358

5459
resource "aws_iam_openid_connect_provider" "github" {

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
output "iam_role_arn" {
55
description = "The ARN of the IAM role."
6-
value = aws_iam_role.github.arn
6+
value = var.enabled && var.create_iam_role ? aws_iam_role.github[0].arn : ""
77
}
88

99
output "iam_role_name" {
1010
description = "The name of the IAM role."
11-
value = aws_iam_role.github.name
11+
value = var.enabled && var.create_iam_role ? aws_iam_role.github[0].name : ""
1212
}
1313

1414
output "oidc_provider_arn" {

variables.tf

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
variable "additional_audiences" {
55
default = null
6-
description = "List of additional OIDC audiences allowed to assume the role."
6+
description = "Additional OIDC audiences allowed to assume the role."
77
type = list(string)
88
}
99

1010
variable "additional_thumbprints" {
1111
default = []
12-
description = "A list of additional thumbprints for the OIDC provider."
12+
description = "Additional thumbprints for the OIDC provider."
1313
type = list(string)
1414

1515
validation {
@@ -20,19 +20,31 @@ variable "additional_thumbprints" {
2020

2121
variable "attach_read_only_policy" {
2222
default = false
23-
description = "Flag to enable/disable the attachment of the ReadOnly policy."
23+
description = "Enable/disable the attachment of the ReadOnly policy."
2424
type = bool
2525
}
2626

2727
variable "create_oidc_provider" {
2828
default = true
29-
description = "Flag to enable/disable the creation of the GitHub OIDC provider."
29+
description = "Enable/disable the creation of the GitHub OIDC provider."
30+
type = bool
31+
}
32+
33+
variable "create_iam_role" {
34+
default = true
35+
description = "Enable/disable creation of the IAM role."
3036
type = bool
3137
}
3238

3339
variable "dangerously_attach_admin_policy" {
3440
default = false
35-
description = "Flag to enable/disable the attachment of the AdministratorAccess policy."
41+
description = "Enable/disable the attachment of the AdministratorAccess policy."
42+
type = bool
43+
}
44+
45+
variable "enabled" {
46+
default = true
47+
description = "Enable/disable the creation of resources."
3648
type = bool
3749
}
3850

@@ -44,12 +56,12 @@ variable "enterprise_slug" {
4456

4557
variable "force_detach_policies" {
4658
default = false
47-
description = "Flag to force detachment of policies attached to the IAM role."
59+
description = "Force detachment of policies attached to the IAM role."
4860
type = bool
4961
}
5062

5163
variable "github_repositories" {
52-
description = "A list of GitHub organization/repository names authorized to assume the role."
64+
description = "GitHub organization/repository names authorized to assume the role."
5365
type = list(string)
5466

5567
validation {
@@ -83,7 +95,7 @@ variable "iam_role_permissions_boundary" {
8395

8496
variable "iam_role_policy_arns" {
8597
default = []
86-
description = "A list of IAM policy ARNs to attach to the IAM role."
98+
description = "IAM policy ARNs to attach to the IAM role."
8799
type = list(string)
88100
}
89101

@@ -106,6 +118,6 @@ variable "max_session_duration" {
106118

107119
variable "tags" {
108120
default = {}
109-
description = "A map of tags to be applied to all applicable resources."
121+
description = "Tags to be applied to all applicable resources."
110122
type = map(string)
111123
}

0 commit comments

Comments
 (0)