|
| 1 | +data "google_project" "project" { |
| 2 | + project_id = var.gcp_project_id |
| 3 | +} |
| 4 | + |
| 5 | +resource "google_iam_workload_identity_pool" "pool" { |
| 6 | + workload_identity_pool_id = var.gcp_workload_identity_pool_id |
| 7 | + display_name = "Humanitec Identity Pool" |
| 8 | + description = "Identity pool for platform orchiestration" |
| 9 | +} |
| 10 | + |
| 11 | +resource "google_iam_workload_identity_pool_provider" "pool_provider" { |
| 12 | + workload_identity_pool_id = google_iam_workload_identity_pool.pool.workload_identity_pool_id |
| 13 | + workload_identity_pool_provider_id = var.gcp_workload_identity_pool_provider_id |
| 14 | + attribute_mapping = { |
| 15 | + "google.subject" = "assertion.sub" |
| 16 | + } |
| 17 | + oidc { |
| 18 | + issuer_uri = "https://idtoken.humanitec.io" |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +resource "google_service_account" "service_account" { |
| 23 | + account_id = var.gcp_service_account_id |
| 24 | + display_name = "Humanitec GCP dynamic cloud account" |
| 25 | + description = "Used by Humanitec Platform Orchestrator Cloud Account" |
| 26 | +} |
| 27 | + |
| 28 | +resource "humanitec_resource_account" "cloud_account" { |
| 29 | + id = "humanitec-gcp-dynamic-cloud-account" |
| 30 | + name = "Humanitec GCP dynamic cloud account" |
| 31 | + type = "gcp-identity" |
| 32 | + credentials = jsonencode({ |
| 33 | + "gcp_service_account" = "${google_service_account.service_account.account_id}@${var.gcp_project_id}.iam.gserviceaccount.com" |
| 34 | + "gcp_audience" = "//iam.googleapis.com/projects/${data.google_project.project.number}/locations/global/workloadIdentityPools/${google_iam_workload_identity_pool.pool.workload_identity_pool_id}/providers/${google_iam_workload_identity_pool_provider.pool_provider.workload_identity_pool_provider_id}" |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +resource "google_service_account_iam_binding" "iam-binding" { |
| 39 | + service_account_id = google_service_account.service_account.name |
| 40 | + role = "roles/iam.workloadIdentityUser" |
| 41 | + |
| 42 | + members = [ |
| 43 | + "principal://iam.googleapis.com/projects/${data.google_project.project.number}/locations/global/workloadIdentityPools/humanitec-wif-pool/subject/${var.humanitec_org}/${humanitec_resource_account.cloud_account.id}", |
| 44 | + ] |
| 45 | +} |
| 46 | + |
| 47 | +resource "google_project_iam_member" "cloud_account_container_role" { |
| 48 | + project = var.gcp_project_id |
| 49 | + role = "roles/container.admin" |
| 50 | + member = "serviceAccount:${google_service_account.service_account.email}" |
| 51 | +} |
0 commit comments