Skip to content

Commit b41c24e

Browse files
Disable SA creation option, sa name override option, SA additional roles (#20)
* Disable SA creation option, sa name override option, SA additional roles option * terraform-docs: automated action * removed IAM role disablement from SA role disablement * Added support for passing existing sa email as well * terraform-docs: automated action * Adding support for passing creation of SA key * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 1b2b8bf commit b41c24e

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Truefoundry Google Cloud platform features module
3131
| [google_project_iam_custom_role.truefoundry_platform_feature_cluster_integration_role](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_custom_role) | resource |
3232
| [google_project_iam_custom_role.truefoundry_platform_feature_gcs_bucket_role](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_custom_role) | resource |
3333
| [google_project_iam_custom_role.truefoundry_platform_feature_secret_manager_role](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_custom_role) | resource |
34+
| [google_project_iam_member.truefoundry_platform_feature_additional_roles_binding](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_member) | resource |
3435
| [google_project_iam_member.truefoundry_platform_feature_artifact_registry_role_binding](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_member) | resource |
3536
| [google_project_iam_member.truefoundry_platform_feature_cluster_integration_role_binding](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_member) | resource |
3637
| [google_project_iam_member.truefoundry_platform_feature_gcs_role_binding](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_member) | resource |
@@ -51,13 +52,19 @@ Truefoundry Google Cloud platform features module
5152
| <a name="input_blob_storage_force_destroy"></a> [blob\_storage\_force\_destroy](#input\_blob\_storage\_force\_destroy) | Force destroy for mlfoundry s3 bucket | `bool` | `true` | no |
5253
| <a name="input_blob_storage_override_name"></a> [blob\_storage\_override\_name](#input\_blob\_storage\_override\_name) | S3 bucket name. Only used if s3\_enable\_override is enabled | `string` | `""` | no |
5354
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the cluster | `string` | n/a | yes |
55+
| <a name="input_existing_service_account_email"></a> [existing\_service\_account\_email](#input\_existing\_service\_account\_email) | Use existing service account email | `string` | `""` | no |
5456
| <a name="input_feature_blob_storage_enabled"></a> [feature\_blob\_storage\_enabled](#input\_feature\_blob\_storage\_enabled) | Enable blob storage feature in the platform | `bool` | `true` | no |
5557
| <a name="input_feature_cluster_integration_enabled"></a> [feature\_cluster\_integration\_enabled](#input\_feature\_cluster\_integration\_enabled) | Enable cluster integration feature in the platform | `bool` | `true` | no |
5658
| <a name="input_feature_docker_registry_enabled"></a> [feature\_docker\_registry\_enabled](#input\_feature\_docker\_registry\_enabled) | Enable docker registry feature in the platform | `bool` | `true` | no |
5759
| <a name="input_feature_logs_viewer_enabled"></a> [feature\_logs\_viewer\_enabled](#input\_feature\_logs\_viewer\_enabled) | Enable logs viewer permission in the platform | `bool` | `true` | no |
5860
| <a name="input_feature_secrets_enabled"></a> [feature\_secrets\_enabled](#input\_feature\_secrets\_enabled) | Enable secrets manager feature in the platform | `bool` | `true` | no |
5961
| <a name="input_project"></a> [project](#input\_project) | GCP Project | `string` | n/a | yes |
6062
| <a name="input_region"></a> [region](#input\_region) | region | `string` | n/a | yes |
63+
| <a name="input_service_account_additional_roles"></a> [service\_account\_additional\_roles](#input\_service\_account\_additional\_roles) | List of additional IAM roles to be added to the service account | `list(string)` | `[]` | no |
64+
| <a name="input_service_account_enable_override"></a> [service\_account\_enable\_override](#input\_service\_account\_enable\_override) | Enable overriding name of service account. This will only be used if service\_account\_enabled is enabled. You need to pass service\_account\_override\_name to pass the service account name | `bool` | `false` | no |
65+
| <a name="input_service_account_enabled"></a> [service\_account\_enabled](#input\_service\_account\_enabled) | Enable service account feature in the platform | `bool` | `true` | no |
66+
| <a name="input_service_account_key_creation_enabled"></a> [service\_account\_key\_creation\_enabled](#input\_service\_account\_key\_creation\_enabled) | Enable service account key creation | `bool` | `true` | no |
67+
| <a name="input_service_account_override_name"></a> [service\_account\_override\_name](#input\_service\_account\_override\_name) | Service account name. Only used if service\_account\_enable\_override is enabled | `string` | `""` | no |
6168
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
6269

6370
## Outputs

iam.tf

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// service account for truefoundry platform feature
22
resource "google_service_account" "truefoundry_platform_feature_service_account" {
3-
account_id = local.serviceaccount_name
3+
count = var.service_account_enabled ? 1 : 0
4+
account_id = var.service_account_enable_override ? var.service_account_override_name : local.serviceaccount_name
45
project = var.project
56
display_name = "Terraform-managed truefoundry platform service account"
67
description = "Truefoundry platform user with access to artifact registry, blob storage and secrets manager"
@@ -33,7 +34,7 @@ resource "google_project_iam_member" "truefoundry_platform_feature_secret_manage
3334

3435
project = var.project
3536
role = google_project_iam_custom_role.truefoundry_platform_feature_secret_manager_role[count.index].id
36-
member = "serviceAccount:${google_service_account.truefoundry_platform_feature_service_account.email}"
37+
member = "serviceAccount:${local.serviceaccount_email}"
3738

3839
condition {
3940
title = "Condition to allow access to secrets starting with 'tfy'"
@@ -77,7 +78,7 @@ resource "google_project_iam_member" "truefoundry_platform_feature_gcs_role_bind
7778

7879
project = var.project
7980
role = google_project_iam_custom_role.truefoundry_platform_feature_gcs_bucket_role[count.index].id
80-
member = "serviceAccount:${google_service_account.truefoundry_platform_feature_service_account.email}"
81+
member = "serviceAccount:${local.serviceaccount_email}"
8182

8283
condition {
8384
title = "Condition to allow access to truefoundry bucket"
@@ -108,7 +109,7 @@ resource "google_project_iam_member" "truefoundry_platform_feature_cluster_integ
108109

109110
project = var.project
110111
role = google_project_iam_custom_role.truefoundry_platform_feature_cluster_integration_role[count.index].id
111-
member = "serviceAccount:${google_service_account.truefoundry_platform_feature_service_account.email}"
112+
member = "serviceAccount:${local.serviceaccount_email}"
112113
}
113114

114115
// artifact registry role
@@ -153,7 +154,7 @@ resource "google_project_iam_member" "truefoundry_platform_feature_artifact_regi
153154

154155
project = var.project
155156
role = google_project_iam_custom_role.truefoundry_platform_feature_artifact_registry_role[count.index].id
156-
member = "serviceAccount:${google_service_account.truefoundry_platform_feature_service_account.email}"
157+
member = "serviceAccount:${local.serviceaccount_email}"
157158
}
158159

159160
// role binding token creator role to service account
@@ -162,18 +163,33 @@ resource "google_project_iam_member" "truefoundry_platform_feature_token_creator
162163

163164
project = var.project
164165
role = "roles/iam.serviceAccountTokenCreator"
165-
member = "serviceAccount:${google_service_account.truefoundry_platform_feature_service_account.email}"
166+
member = "serviceAccount:${local.serviceaccount_email}"
166167
}
167168

168169
// role binding logs viewer role to service account
169170
resource "google_project_iam_member" "truefoundry_platform_feature_logs_viewer_role_binding" {
170171
count = var.feature_logs_viewer_enabled ? 1 : 0
171172
project = var.project
172173
role = "roles/logging.viewer"
173-
member = "serviceAccount:${google_service_account.truefoundry_platform_feature_service_account.email}"
174+
member = "serviceAccount:${local.serviceaccount_email}"
175+
}
176+
177+
// Adding support for passing additional IAM roles to the service account
178+
resource "google_project_iam_member" "truefoundry_platform_feature_additional_roles_binding" {
179+
count = length(var.service_account_additional_roles) > 0 ? 1 : 0
180+
project = var.project
181+
role = var.service_account_additional_roles[count.index]
182+
member = "serviceAccount:${local.serviceaccount_email}"
174183
}
175184

176185
// service account key
177186
resource "google_service_account_key" "truefoundry_platform_feature_service_account_key" {
178-
service_account_id = google_service_account.truefoundry_platform_feature_service_account.id
187+
count = var.service_account_enabled && var.service_account_key_creation_enabled ? 1 : 0
188+
service_account_id = google_service_account.truefoundry_platform_feature_service_account[0].id
189+
}
190+
191+
// moved block
192+
moved {
193+
from = google_service_account.truefoundry_platform_feature_service_account
194+
to = google_service_account.truefoundry_platform_feature_service_account[0]
179195
}

locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ locals {
1010
truefoundry_platform_resources = "${var.cluster_name}-platform-${random_string.random_id.result}"
1111
truefoundry_blob_storage_name = var.blob_storage_enable_override ? var.blob_storage_override_name : "${local.truefoundry_platform_resources}-bucket"
1212
serviceaccount_name = trimsuffix(substr("${local.truefoundry_platform_resources}-user", 0, 30), "-")
13+
serviceaccount_email = var.service_account_enabled ? google_service_account.truefoundry_platform_feature_service_account[0].email : var.existing_service_account_email != "" ? var.existing_service_account_email : ""
1314
}

output.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# IAM serviceaccount
33
################################################################################
44
output "sa_auth_data" {
5-
value = base64decode(google_service_account_key.truefoundry_platform_feature_service_account_key.private_key)
5+
value = var.service_account_enabled ? base64decode(google_service_account_key.truefoundry_platform_feature_service_account_key[0].private_key) : ""
66
sensitive = true
77
description = "Private key of the service account"
88
}
@@ -42,11 +42,11 @@ output "artifact_registry_url" {
4242
# IAM serviceaccount
4343
################################################################################
4444
output "serviceaccount_name" {
45-
value = local.serviceaccount_name
45+
value = var.service_account_enabled && var.service_account_enable_override ? var.service_account_override_name : local.serviceaccount_name
4646
description = "Name of the service account"
4747
}
4848
output "serviceaccount_key" {
49-
value = base64decode(google_service_account_key.truefoundry_platform_feature_service_account_key.private_key)
49+
value = var.service_account_enabled && var.service_account_key_creation_enabled ? base64decode(google_service_account_key.truefoundry_platform_feature_service_account_key[0].private_key) : ""
5050
sensitive = true
5151
description = "Service account keys"
5252
}

variables.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@ variable "project" {
2929
type = string
3030
}
3131

32+
################################################################################
33+
# Service Account
34+
################################################################################
35+
36+
variable "service_account_enabled" {
37+
description = "Enable service account feature in the platform"
38+
type = bool
39+
default = true
40+
}
41+
42+
variable "service_account_key_creation_enabled" {
43+
description = "Enable service account key creation"
44+
type = bool
45+
default = true
46+
}
47+
48+
variable "existing_service_account_email" {
49+
description = "Use existing service account email"
50+
type = string
51+
default = ""
52+
}
53+
54+
variable "service_account_enable_override" {
55+
description = "Enable overriding name of service account. This will only be used if service_account_enabled is enabled. You need to pass service_account_override_name to pass the service account name"
56+
type = bool
57+
default = false
58+
}
59+
60+
variable "service_account_override_name" {
61+
description = "Service account name. Only used if service_account_enable_override is enabled"
62+
type = string
63+
default = ""
64+
}
65+
66+
variable "service_account_additional_roles" {
67+
description = "List of additional IAM roles to be added to the service account"
68+
type = list(string)
69+
default = []
70+
}
71+
3272
################################################################################
3373
# Blob Storage
3474
################################################################################

0 commit comments

Comments
 (0)