Lesson 4 of 14 · 13m
A Service Account JSON Key Is a Password That Never Expires
The key.json file is a bearer credential — no expiry, no MFA, no network boundary — and ADC will happily read it.
What the file actually is
gcloud iam service-accounts keys create key.json --iam-account=ci@project.iam.gserviceaccount.com produces:
{
"type": "service_account",
"project_id": "project",
"private_key_id": "b23f...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "ci@project.iam.gserviceaccount.com",
"token_uri": "https://oauth2.googleapis.com/token"
}Here is what happens when something uses it. The client builds a JWT — issuer client_email, audience token_uri, scopes like https://www.googleapis.com/auth/cloud-platform — signs it with private_key, posts it to token_uri, and receives an OAuth access token good for whatever the service account can reach. That is the entire exchange. No MFA prompt, no device posture, no IP restriction. The file is a bearer credential valid from any machine on earth until the key is disabled or deleted — keys do not expire on their own, and each SA can hold up to ten. GitHub's push protection can even block commits containing GCP service account keys where secret scanning is enabled — a second net, never the first.
GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json is the Application Default Credentials hook every Google client library checks first — before the metadata server, before gcloud credentials. Any process with that env var set silently becomes the service account.
ADC is a chain, not a file: GOOGLE_APPLICATION_CREDENTIALS env var → the well-known ~/.config/gcloud/application_default_credentials.json written by gcloud auth application-default login → the metadata server. Two confusions follow. gcloud auth application-default login writes user OAuth credentials to that file — running a script 'as me' and running it 'as the SA' are different acts with the same API shape. And because the env var wins, a stale GOOGLE_APPLICATION_CREDENTIALS pointing at a downloaded key silently overrides the attached identity you meant to use — in a container image, an inherited env, a CI runner template. Grep your deployment surfaces for it.
Why it keeps leaking
The wrong mental model is "it's just how GCP authentication works." It isn't. It is the easy path, kept alive for compatibility and legacy tooling. On-platform workloads should ride the attached identity — the metadata server mints tokens for the SA bound to the VM, function, or service; no file exists anywhere. Off-platform workloads should use Workload Identity Federation. The JSON key is for the shrinking set of cases in between, and it remains the industry's most reliable GCP initial-access primitive: GitHub secret scanning and TruffleHog flag the type: service_account shape on sight because leaked keys in public repos are a textbook entry. The access token a key mints typically carries the cloud-platform scope — everything the SA can do, not one service.
When a key leaks, the private_key_id is your tracer: it surfaces as kid in the JWT header and lands in audit logs, so you can attribute which key minted which tokens. Disable first — gcloud iam service-accounts keys disable KEY_ID --iam-account=... — investigate usage, then delete. Disabling is reversible; deletion isn't, which is why you disable while you look.
For inventory, gcloud iam service-accounts keys list --iam-account=... --managed-by=user filters out Google-managed keys (which exist for GKE and other plumbing and aren't your problem) so the output shows only keys somebody minted.
Land it
Set constraints/iam.disableServiceAccountKeyCreation at the org node — key creation fails everywhere underneath it, and the leak class dies with it. Pair with constraints/iam.disableServiceAccountKeyUpload so foreign keys can't be imported. Inventory existing keys (gcloud iam service-accounts keys list --iam-account=... per SA) and treat anything older than your last review as suspect — you cannot prove it never leaked. If a workload genuinely requires a key, rotate on a schedule, bound its roles so a leak buys the attacker the smallest possible project footprint, and know that you chose a password-shaped risk on purpose rather than by accident.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
Big Tech Plans to Work with the Public Sector for Cybersecurity
Both Microsoft, Inc. and Alphabet, Inc. will put a combined $30 billion dollars into cybersecurity over the next five years and work with the public sector on several initiatives to help improve cybersecurity.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.
What is SSRF? Server-Side Request Forgery and the Attack That Breached Capital One
SSRF tricks your server into making requests to internal systems — cloud metadata services, internal APIs, AWS credentials endpoints. It turned a WAF misconfiguration into 100 million stolen Capital One records