If your CI/CD pipeline authenticates to Google Cloud with a service account key stored in a CI variable, you have a problem. You might not know it yet.

That JSON key file is a static credential. It doesn’t expire (unless you rotate it, which you don’t), and it carries no context about who or what is using it. If it leaks (and CI variables leak more often than anyone admits), an attacker gets the same access your pipeline has, forever or until someone notices.

So I built a POC of the alternative: a pipeline from GitLab to Google Cloud with no service account keys and no secrets in CI variables, which signs its images and refuses to deploy vulnerable ones.

How the pipeline works#

The pipeline does six things, in order. The two amber stages are the security gates, where a vulnerable or unsigned image stops, and the purple step at the end, promotion to production, is manual.

%%{init: {"flowchart": {"nodeSpacing": 30, "rankSpacing": 36, "padding": 10}}}%% flowchart TB accTitle: The pipeline stages accDescr: GitLab authenticates to Google Cloud through OIDC federation. The pipeline then builds the image, pushes it to Artifact Registry, scans it for vulnerabilities, signs it through Binary Authorization and deploys it to QA. The vulnerability scan and Binary Authorization are the security gates. Promotion to production is a manual step. A["OIDC federation"] --> B["Build"] B --> C["Push to
Artifact Registry"] C --> D["Vulnerability scan"] D --> E["Binary Authorization"] E --> F["Deploy to QA"] F -.->|manual| G["Promote to PROD"] classDef gate fill:#40361d,stroke:#eec35e,stroke-width:2px,color:#eceae5 classDef manual fill:#221f15,stroke:#b48ead,stroke-width:1.5px,color:#eceae5 class D,E gate class G manual linkStyle 0,1,2,3,4 stroke:#b8b5ad,stroke-width:1.5px linkStyle 5 stroke:#b48ead,stroke-width:1.5px,stroke-dasharray:5 4,color:#b48ead

Each stage has a security purpose:

  1. OIDC federation: GitLab authenticates to Google Cloud with a JWT token, without keys.
  2. Build: Docker-in-Docker builds the container image and pushes it to GitLab’s own registry first.
  3. Push to Artifact Registry: the image is copied to Google Artifact Registry, authenticated through Workload Identity Federation.
  4. Vulnerability scan: Google Container Analysis scans the image. If it finds critical vulnerabilities, the pipeline stops.
  5. Binary Authorization: if the scan passes, the image gets cryptographically signed. Unsigned images cannot be deployed.
  6. Deploy: Cloud Deploy creates a release to QA. Production requires a manual promotion, and the Binary Authorization policy rejects unsigned images.

The entire infrastructure is defined in Terraform: no ClickOps.

Workload Identity Federation: the key that isn’t a key#

Instead of giving GitLab a service account key, you tell Google Cloud: “trust JWT tokens issued by GitLab, but only for this specific project, and only with these specific claims.”

This is the exchange on every pipeline run, with GitLab’s side in pink and Google Cloud’s in blue:

%%{init: {"sequence": {"actorMargin": 16, "boxMargin": 8, "messageFontSize": 15, "actorFontSize": 15, "noteFontSize": 15, "mirrorActors": false, "wrap": true, "width": 180}}}%% sequenceDiagram accTitle: Keyless authentication from GitLab CI to Google Cloud accDescr: The GitLab CI job requests a JWT from GitLab's OIDC provider and gets back a token with claims such as project, role and ref. It presents the JWT to Workload Identity Federation, which validates the issuer, maps the claims to attributes and returns a short-lived access token. The job uses that token to call Google Cloud APIs. No service account key is involved. box rgba(211, 134, 155, 0.12) GitLab participant GL as GitLab CI participant OIDC as GitLab OIDC end box rgba(131, 165, 152, 0.14) Google Cloud participant WIF as Workload Identity
Federation participant GCP as Google Cloud
APIs end GL->>OIDC: Request JWT token OIDC-->>GL: JWT with claims
(project, role, ref) GL->>WIF: Present JWT token Note over WIF: Validate issuer,
map claims
to attributes WIF-->>GL: Short-lived
access token GL->>GCP: Authenticated API call Note over GL,GCP: No service account key involved

The Terraform configuration creates an identity pool and maps GitLab’s JWT claims to Google Cloud attributes:

resource "google_iam_workload_identity_pool_provider" "gitlab_provider" {
  attribute_mapping = {
    "google.subject"             = "assertion.sub"
    "attribute.project_path"     = "assertion.project_path"
    "attribute.developer_access" = "assertion.developer_access"
    "attribute.ref"              = "assertion.ref"
  }

  oidc {
    issuer_uri = "https://auth.gcp.gitlab.com/oidc/${var.gitlab_namespace}"
  }
}

Every pipeline run gets a short-lived token scoped to exactly what it needs, and the token expires when the pipeline finishes. There’s no long-lived key to steal, leak or rotate.

The IAM bindings follow the GitLab role instead of a static key that has everything: a developer pushing code gets artifactregistry.writer and logging.logWriter, and a guest gets artifactregistry.reader.

The deliberate vulnerability#

This is the part of the demo I like most. The Dockerfile uses a vulnerable base image on purpose:

FROM alpine:3.14.2

Alpine 3.14.2 has known CVEs. The first time the pipeline runs, the vulnerability scan catches them and Binary Authorization refuses to sign the image, so the deployment to production fails, by design.

The fix is one line:

FROM alpine:3.18

Push this change and the pipeline turns green: the scan passes, the image gets signed and the deployment succeeds. Here are the two runs side by side, orange for the vulnerable image and green for the fixed one:

%%{init: {"flowchart": {"nodeSpacing": 40, "rankSpacing": 34, "padding": 10}}}%% flowchart TB accTitle: The pipeline before and after the fix accDescr: With alpine 3.14.2, the vulnerability scan finds CVEs, the image is blocked, Binary Authorization refuses to sign it and the deploy fails. With alpine 3.18, the scan is clean, the image passes, it gets signed and the deploy succeeds. A["git push
alpine:3.14.2"] --> B["Build"] B --> C["Vulnerability
scan"] C -->|"CVEs found"| D["BLOCKED"] D --> E["Binary Auth
refuses to sign"] E --> F["Deploy FAILS"] A2["git push
alpine:3.18"] --> B2["Build"] B2 --> C2["Vulnerability
scan"] C2 -->|"Clean"| D2["PASSED"] D2 --> E2["Image signed"] E2 --> F2["Deploy succeeds"] classDef bad fill:#d65d0e,stroke:#d65d0e,color:#1a170f classDef good fill:#a8c373,stroke:#a8c373,color:#1a170f class D,F bad class D2,F2 good linkStyle 0,1,5,6 stroke:#b8b5ad,stroke-width:1.5px linkStyle 2,3,4 stroke:#d65d0e,stroke-width:2px,color:#d65d0e linkStyle 7,8,9 stroke:#a8c373,stroke-width:2px,color:#a8c373

Binary Authorization: trust, but verify (and sign)#

Binary Authorization enforces one simple rule: no container runs in production unless it has been scanned and signed.

The Terraform setup creates a GPG key pair, a Container Analysis note and an attestor, and the policy requires the attestor’s signature:

resource "google_binary_authorization_policy" "policy" {
  default_admission_rule {
    evaluation_mode  = "REQUIRE_ATTESTATION"
    enforcement_mode = "ENFORCED_BLOCK_AND_AUDIT_LOG"
    require_attestations_by = [
      google_binary_authorization_attestor.vulnz_attestor.name
    ]
  }
}

The pipeline’s sign stage uses the GPG private key (stored in Secret Manager, not in CI variables) to create an attestation, and Cloud Run checks that attestation before it lets the container run.

The policy is enforced by the platform, so it also covers images that never went through the pipeline: if someone pushes an image straight to Artifact Registry, Binary Authorization still blocks it from running.

Automatic to QA, manual to production#

The Cloud Deploy pipeline has two targets. QA gets every release automatically once the scan and the signing pass. PROD needs a manual promotion: you go to the Cloud Deploy console, review the release and click “Promote”, and Binary Authorization still enforces the signature.

That’s on purpose. Fully automated production deployments are a luxury you earn after your team trusts the process.

Both targets run on Cloud Run, which keeps the infrastructure cost near zero for a demo.

What I learned building this#

Stop using service account keys in CI/CD. Seriously. Workload Identity Federation exists, it works, and once you’ve Terraformed it, it’s done. The setup is more complex up front, but you only pay that once.

Break things on purpose. I’ve found the deliberately vulnerable Dockerfile works way better than any slide deck for getting people to care about supply chain security. People nod politely when you explain it, and they pay attention when they see a deploy get blocked.

Terraform all of it. The entire setup is about 400 lines of HCL, and reproducing it in a different project takes minutes. Doing it by hand in the console would take hours, and you’d forget a step.

Think about security before production, not after. Most organizations bolt it on after deployment: WAFs, monitoring, incident response. But if your pipeline doesn’t verify what it deploys, your monitoring is just a faster way to find out you’ve been compromised.


The full source code is at gitlab.com/thekoma/secure-gitlab-gcp-cicd. It’s MIT-licensed and meant to be forked and adapted.