# workload-identity / gcp / t1 "Standard" # # Short-lived Google Cloud access for GitHub Actions, limited to named # branches or deployment environments of one repository, optionally for one # GKE service account, and with every impersonation recorded. locals { # Exact matches on GitHub's subject claim, which names the repository and # the branch or environment in one value. subject_condition = join(" || ", [for s in var.allowed_subjects : "assertion.sub == '${s}'"]) } resource "google_project_service" "needed" { for_each = toset(["iam.googleapis.com", "iamcredentials.googleapis.com", "sts.googleapis.com"]) project = var.project_id service = each.value disable_on_destroy = false } resource "random_id" "pool" { byte_length = 2 } resource "google_iam_workload_identity_pool" "github" { project = var.project_id workload_identity_pool_id = "github-${random_id.pool.hex}" display_name = "GitHub Actions" depends_on = [google_project_service.needed] } resource "google_iam_workload_identity_pool_provider" "github" { project = var.project_id workload_identity_pool_id = google_iam_workload_identity_pool.github.workload_identity_pool_id workload_identity_pool_provider_id = "github" attribute_mapping = { "google.subject" = "assertion.sub" "attribute.repository" = "assertion.repository" "attribute.ref" = "assertion.ref" } attribute_condition = local.subject_condition oidc { issuer_uri = "https://token.actions.githubusercontent.com" } } resource "google_service_account" "deploy" { project = var.project_id account_id = "github-deploy" display_name = "Used by ${var.repository} through workload identity federation" } resource "google_service_account_iam_member" "github" { service_account_id = google_service_account.deploy.name role = "roles/iam.workloadIdentityUser" member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.github.name}/attribute.repository/${var.repository}" } resource "google_project_iam_member" "deploy" { for_each = toset(var.roles) project = var.project_id role = each.value member = "serviceAccount:${google_service_account.deploy.email}" } # GKE Workload Identity: one Kubernetes service account may act as this one. # Anyone able to run a pod with that Kubernetes service account in that # namespace can therefore act as it too. resource "google_service_account_iam_member" "gke" { count = var.gke_namespace == "" ? 0 : 1 service_account_id = google_service_account.deploy.name role = "roles/iam.workloadIdentityUser" member = "serviceAccount:${var.project_id}.svc.id.goog[${var.gke_namespace}/${var.gke_service_account}]" } # Every token minted for a service account is a Data Access log on the IAM # credentials API, which is off by default. Without this, nobody can say which # workflow or pod acted as which service account. # # This resource owns the audit settings for that API in the whole project. If # something else already manages them, merge the two first. resource "google_project_iam_audit_config" "iamcredentials" { project = var.project_id service = "iamcredentials.googleapis.com" audit_log_config { log_type = "DATA_READ" } }