Skip to content

Commit 03185e5

Browse files
authored
chore!: Remove the enabled variable (#76)
This is not really necessary, the count feature in Terraform provides similar functionality, it's recommended to use that instead.
1 parent 54470d2 commit 03185e5

File tree

7 files changed

+45
-51
lines changed

7 files changed

+45
-51
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trim_trailing_whitespace = true
1414
trim_trailing_whitespace = false
1515

1616
[*.tf]
17-
indent_size = 120
17+
max_line_length = 120
1818

1919
[Makefile]
2020
indent_style = tab

.github/workflows/pr_label.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ jobs:
2323
with:
2424
script: |
2525
const labels = []
26-
if (context.payload.pull_request.title.startsWith('fix:')) {
26+
if (context.payload.pull_request.title.startsWith('fix')) {
2727
labels.push('bug 🐛')
2828
}
29-
if (context.payload.pull_request.title.startsWith('chore:')) {
29+
if (context.payload.pull_request.title.startsWith('chore')) {
3030
labels.push('chore 🧹')
3131
}
32-
if (context.payload.pull_request.title.startsWith('feat:')) {
32+
if (context.payload.pull_request.title.startsWith('feat')) {
3333
labels.push('feature 💡')
3434
}
3535
if (labels.length > 0) {

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ applied, the JWT will contain an updated `iss` claim.
7777
| [aws_iam_role_policy_attachment.read_only](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
7878
| [aws_iam_openid_connect_provider.github](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_openid_connect_provider) | data source |
7979
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
80-
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
80+
| [aws_partition.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
8181
| [tls_certificate.github](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/data-sources/certificate) | data source |
8282

8383
## Inputs
@@ -89,7 +89,6 @@ applied, the JWT will contain an updated `iss` claim.
8989
| attach_read_only_policy | Flag to enable/disable the attachment of the ReadOnly policy. | `bool` | `false` | no |
9090
| create_oidc_provider | Flag to enable/disable the creation of the GitHub OIDC provider. | `bool` | `true` | no |
9191
| dangerously_attach_admin_policy | Flag to enable/disable the attachment of the AdministratorAccess policy. | `bool` | `false` | no |
92-
| enabled | Flag to enable/disable the creation of resources. | `bool` | `true` | no |
9392
| enterprise_slug | Enterprise slug for GitHub Enterprise Cloud customers. | `string` | `""` | no |
9493
| force_detach_policies | Flag to force detachment of policies attached to the IAM role. | `bool` | `false` | no |
9594
| github_repositories | List of GitHub organization/repository names authorized to assume the role. | `list(string)` | n/a | yes |
@@ -103,11 +102,11 @@ applied, the JWT will contain an updated `iss` claim.
103102

104103
## Outputs
105104

106-
| Name | Description |
107-
| ----------------- | ------------------------- |
108-
| iam_role_arn | ARN of the IAM role. |
109-
| iam_role_name | Name of the IAM role. |
110-
| oidc_provider_arn | ARN of the OIDC provider. |
105+
| Name | Description |
106+
| ----------------- | ----------------------------- |
107+
| iam_role_arn | The ARN of the IAM role. |
108+
| iam_role_name | The name of the IAM role. |
109+
| oidc_provider_arn | The ARN of the OIDC provider. |
111110

112111
<!-- END_TF_DOCS -->
113112

data.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// SPDX-FileCopyrightText: 2024 Daniel Morris <[email protected]>
22
// SPDX-License-Identifier: MIT
33

4-
data "aws_partition" "current" {}
4+
data "aws_partition" "this" {}
55

66
data "aws_iam_policy_document" "assume_role" {
7-
count = var.enabled ? 1 : 0
8-
97
statement {
108
actions = ["sts:AssumeRoleWithWebIdentity"]
119
effect = "Allow"
@@ -38,9 +36,12 @@ data "aws_iam_policy_document" "assume_role" {
3836
}
3937

4038
data "aws_iam_openid_connect_provider" "github" {
41-
count = var.enabled && !var.create_oidc_provider ? 1 : 0
39+
count = !var.create_oidc_provider ? 1 : 0
4240

43-
url = "https://token.actions.githubusercontent.com%{if var.enterprise_slug != ""}/${var.enterprise_slug}%{endif}"
41+
url = format(
42+
"https://token.actions.githubusercontent.com%v",
43+
var.enterprise_slug != "" ? "/${var.enterprise_slug}" : "",
44+
)
4445
}
4546

4647
data "tls_certificate" "github" {

main.tf

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,70 @@ locals {
66
github_organizations = toset([
77
for repo in var.github_repositories : split("/", repo)[0]
88
])
9-
dns_suffix = data.aws_partition.current.dns_suffix
10-
oidc_provider_arn = var.enabled ? (var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : data.aws_iam_openid_connect_provider.github[0].arn) : ""
11-
partition = data.aws_partition.current.partition
9+
dns_suffix = data.aws_partition.this.dns_suffix
10+
oidc_provider_arn = var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : data.aws_iam_openid_connect_provider.github[0].arn
11+
partition = data.aws_partition.this.partition
1212
}
1313

1414
resource "aws_iam_role" "github" {
15-
count = var.enabled ? 1 : 0
16-
17-
assume_role_policy = data.aws_iam_policy_document.assume_role[0].json
15+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
1816
description = "Role assumed by the GitHub OIDC provider."
1917
force_detach_policies = var.force_detach_policies
2018
max_session_duration = var.max_session_duration
2119
name = var.iam_role_name
2220
path = var.iam_role_path
2321
permissions_boundary = var.iam_role_permissions_boundary
2422
tags = var.tags
25-
2623
}
2724

2825
resource "aws_iam_role_policy" "inline_policies" {
29-
for_each = { for k, v in var.iam_role_inline_policies : k => v if var.enabled }
30-
name = each.key
31-
policy = each.value
32-
role = aws_iam_role.github[0].id
26+
for_each = { for k, v in var.iam_role_inline_policies : k => v }
27+
28+
name = each.key
29+
policy = each.value
30+
role = aws_iam_role.github.id
3331
}
3432

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

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

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

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

4947
resource "aws_iam_role_policy_attachment" "custom" {
50-
count = var.enabled ? length(var.iam_role_policy_arns) : 0
48+
count = length(var.iam_role_policy_arns)
5149

5250
policy_arn = var.iam_role_policy_arns[count.index]
53-
role = aws_iam_role.github[0].id
51+
role = aws_iam_role.github.id
5452
}
5553

5654
resource "aws_iam_openid_connect_provider" "github" {
57-
count = var.enabled && var.create_oidc_provider ? 1 : 0
55+
count = var.create_oidc_provider ? 1 : 0
5856

5957
client_id_list = concat(
60-
[for org in local.github_organizations : "https://github.com/${org}"],
58+
[for org in local.github_organizations : format("https://github.com/%v", org)],
6159
[local.audience],
6260
)
6361

6462
tags = var.tags
65-
url = "https://token.actions.githubusercontent.com%{if var.enterprise_slug != ""}/${var.enterprise_slug}%{endif}"
63+
6664
thumbprint_list = toset(
6765
concat(
6866
[data.tls_certificate.github.certificates[0].sha1_fingerprint],
6967
var.additional_thumbprints,
7068
)
7169
)
70+
71+
url = format(
72+
"https://token.actions.githubusercontent.com%v",
73+
var.enterprise_slug != "" ? "/${var.enterprise_slug}" : "",
74+
)
7275
}

outputs.tf

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
// SPDX-License-Identifier: MIT
33

44
output "iam_role_arn" {
5-
depends_on = [aws_iam_role.github]
6-
description = "ARN of the IAM role."
7-
value = var.enabled ? aws_iam_role.github[0].arn : ""
5+
description = "The ARN of the IAM role."
6+
value = aws_iam_role.github.arn
87
}
98

109
output "iam_role_name" {
11-
depends_on = [aws_iam_role.github]
12-
description = "Name of the IAM role."
13-
value = var.enabled ? aws_iam_role.github[0].name : ""
10+
description = "The name of the IAM role."
11+
value = aws_iam_role.github.name
1412
}
1513

1614
output "oidc_provider_arn" {
17-
depends_on = [aws_iam_openid_connect_provider.github]
18-
description = "ARN of the OIDC provider."
19-
value = var.enabled && var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : ""
15+
description = "The ARN of the OIDC provider."
16+
value = var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : data.aws_iam_openid_connect_provider.github[0].arn
2017
}

variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ variable "dangerously_attach_admin_policy" {
3636
type = bool
3737
}
3838

39-
variable "enabled" {
40-
default = true
41-
description = "Flag to enable/disable the creation of resources."
42-
type = bool
43-
}
44-
4539
variable "enterprise_slug" {
4640
default = ""
4741
description = "Enterprise slug for GitHub Enterprise Cloud customers."

0 commit comments

Comments
 (0)