# workload-identity / gcp / t0 "Quick start"
#
# Lets one GitHub repository's main branch act as a Google Cloud service
# account with a short-lived token, so no service account key exists at all.
#
# Change the project and repository below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#   terraform output workflow
#
# Paste the printed workflow into .github/workflows/ in that repository and run
# it. It signs in and describes the project. Any other branch or repository is
# refused.

locals {
  project_id = "your-project-id"    # change me
  repository = "your-org/your-repo" # change me
  branch     = "main"
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 8.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.6"
    }
  }
}

provider "google" {
  project = local.project_id
}

resource "google_project_service" "needed" {
  for_each           = toset(["iam.googleapis.com", "iamcredentials.googleapis.com", "sts.googleapis.com"])
  service            = each.value
  disable_on_destroy = false
}

# A deleted pool keeps its ID for 30 days, so the suffix lets you recreate it.
resource "random_id" "pool" {
  byte_length = 2
}

resource "google_iam_workload_identity_pool" "github" {
  workload_identity_pool_id = "github-${random_id.pool.hex}"
  display_name              = "GitHub Actions"
  depends_on                = [google_project_service.needed]
}

# The attribute condition is the whole control. Without it, any repository on
# GitHub could present a token to this pool. GitHub's subject claim names the
# repository and the branch in one value, so one exact match covers both.
resource "google_iam_workload_identity_pool_provider" "github" {
  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 = "assertion.sub == 'repo:${local.repository}:ref:refs/heads/${local.branch}'"
  oidc {
    issuer_uri = "https://token.actions.githubusercontent.com"
  }
}

resource "google_service_account" "github" {
  account_id   = "github-deploy"
  display_name = "Used by ${local.repository} through workload identity federation"
}

resource "google_service_account_iam_member" "github" {
  service_account_id = google_service_account.github.name
  role               = "roles/iam.workloadIdentityUser"
  member             = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.github.name}/attribute.repository/${local.repository}"
}

# Enough to prove the sign-in: read the project's own details.
resource "google_project_iam_member" "browser" {
  project = local.project_id
  role    = "roles/browser"
  member  = "serviceAccount:${google_service_account.github.email}"
}

output "workflow" {
  description = "A workflow that proves the sign-in. id-token: write is what lets it ask GitHub for a token."
  value       = <<-EOT
    name: prove-gcp-sign-in
    on: workflow_dispatch
    permissions:
      id-token: write
      contents: read
    jobs:
      whoami:
        runs-on: ubuntu-latest
        steps:
          - uses: google-github-actions/auth@v2
            with:
              workload_identity_provider: ${google_iam_workload_identity_pool_provider.github.name}
              service_account: ${google_service_account.github.email}
          - uses: google-github-actions/setup-gcloud@v2
          - run: gcloud projects describe ${local.project_id} --format='value(projectId)'
  EOT
}
