gcp credentialsjson
GCP credentials.json: The Complete Guide to Service Account Authentication
Google Cloud Platform (GCP) offers multiple authentication mechanisms, but few are as commonly used—and as frequently misunderstood—as the credentials.json service account key file. Whether you are automating deployments, configuring CI/CD pipelines, or running workloads locally against GCP APIs, understanding how to generate, secure, and rotate credentials.json files is essential infrastructure knowledge.
This guide provides an authoritative breakdown of what credentials.json is, when to use it, the security tradeoffs involved, and industry best practices for managing it at scale.
Key Takeaways
- credentials.json is a JSON-formatted service account key that contains a private RSA key and GoogleOAuth2 token endpoint information.
- It is the primary mechanism for non-interactive, programmatic authentication to Google Cloud services.
- Service account keys are a high-value security target—leaked keys grant full access under the associated IAM roles.
- GCP recommends using Workload Identity or Application Default Credentials (ADC) instead of static key files wherever possible.
- If you must use a credentials.json file, treat it like a password: store it in a secrets manager, rotate it every 90 days, and never commit it to source control.
What Is credentials.json in GCP?
A credentials.json file is a downloaded JSON key associated with a Google Cloud service account. When you create a service account key through the Google Cloud Console, the API, or the gcloud CLI, GCP generates a JSON document containing:
- type: Always set to
"service_account" - project_id: The GCP project the service account belongs to
- private_key_id: A unique identifier for the key (used for rotation tracking)
- private_key: A PKCS#1-encoded RSA private key (2048-bit or 4096-bit)
- client_email: The email address of the service account (e.g.,
my-sa@project.iam.gserviceaccount.com) - client_id: A numeric identifier for the service account
- auth_uri, token_uri, auth_provider_x509_cert_url, client_x509_cert_url: OAuth2 endpoint metadata
This file is what Google's client libraries use to sign JWT assertions and exchange them for short-lived OAuth2 access tokens. It effectively acts as a long-lived credential that proves your identity to Google Cloud APIs.
How credentials.json Differs from Other GCP Authentication Methods
| Authentication Method | Credential Type | Lifetime | Use Case |
|---|---|---|---|
| credentials.json (Service Account Key) | Long-lived private key | Indefinite until rotated/deleted | Local dev, CI/CD, external systems |
| Application Default Credentials (ADC) | Auto-resolved token source | Short-lived (minutes) | GCP-native workloads (Cloud Run, GKE) |
| Workload Identity Federation | Token exchange | Short-lived (minutes) | On-prem, AWS, GitHub Actions |
| gcloud auth login | User OAuth2 token | 1 hour (refreshable) | Interactive developer sessions |
| Impersonated Service Account | Delegated tokens | Short-lived (minutes) | Elevating scoped credentials |
When to Use credentials.json (and When Not To)
The credentials.json approach is not wrong—but it is increasingly considered a legacy pattern by GCP security teams. Below is an analysis of the scenarios where it makes sense and where you should choose a different approach.
Appropriate Use Cases
- Local development: Running GCP client libraries on your laptop outside GCP infrastructure
- Legacy CI/CD systems: Pipelines that cannot integrate with Workload Identity or OIDC federation
- Third-party tooling: External SaaS tools (e.g., Terraform Cloud, certain monitoring agents) that require a key file
- Hybrid cloud setups: On-premises servers authenticating to GCP without Workload Identity support
When to Avoid credentials.json
- Workloads running on GCP: Use ADC or Workload Identity instead—no key file is needed
- GitHub Actions or modern OIDC-supported pipelines: Use Workload Identity Federation to exchange tokens without long-lived secrets
- Highly regulated environments: Static keys are harder to audit and rotate; prefer short-lived tokens
- Multi-tenant SaaS products: Each tenant should authenticate via its own identity, not shared static keys
Advantages and Drawbacks: A Balanced Review
Understanding both sides is critical for making an informed architectural decision.
Advantages
- Simple to provision: A few clicks in the Console or one
gcloud iam service-accounts keys createcommand generates the key - Universal compatibility: Every Google Cloud client library supports credentials.json out of the box
- No special infrastructure required: Works anywhere you can store a file—laptops, VMs, Kubernetes pods, container images
- Full IAM control: Each key is independently scannable via Cloud Audit Logs and can be rotated without affecting other keys
- Offline operation: Unlike ADC-based metadata server lookups, a key file works without network access to the GCP metadata service
Drawbacks
- Long-lived secret exposure: The private key never expires on its own. If leaked, it remains valid indefinitely until manually rotated