gcp credentialsjson
GCP credentials.json: The Complete Guide to Service Account Keys and Secure Authentication
If you have ever deployed an application on Google Cloud Platform, you have almost certainly encountered the term credentials.json. This single JSON file is the backbone of service account authentication — granting your applications programmatic access to Compute Engine, Cloud Storage, BigQuery, Vertex AI, and dozens of other GCP services. Yet many engineers treat it as a mere convenience, underestimating the security implications of how it is created, stored, rotated, and used.
This article provides an authoritative, comprehensive review of GCP credentials.json: what it is, how it works, how to generate and configure it properly, the security advantages and drawbacks of file-based service account keys, and the best practices that every cloud engineer should follow in 2025 and beyond.
Key Takeaways
- A credentials.json file is a service account key — a private key pair stored as a JSON object that authenticates non-interactive workloads to GCP APIs.
- File-based keys are convenient but risky. They persist on disk, can be accidentally committed to source control, and must be manually rotated.
- Workload Identity is the recommended alternative for workloads running on GKE, Cloud Run, and Compute Engine, eliminating the need for any credentials file.
- Always restrict keys with minimal IAM roles and monitor usage through Cloud Audit Logs to reduce the blast radius of a leak.
- Never commit credentials.json to version control. Use secret managers, environment variables, or mounted secrets instead.
What Is GCP credentials.json?
Definition and Structure
A credentials.json file is a JSON-formatted service account key that Google Cloud Platform issues when you create a key for a service account. It contains the private cryptographic key material necessary to sign JWT-based authentication requests to Google's OAuth 2.0 token endpoint. When your application presents this key, Google verifies the signature and issues an access token that grants API access.
The file typically contains the following fields:
| Field | Description |
|---|---|
type | Always service_account |
project_id | The GCP project the service account belongs to |
private_key_id | Unique identifier for this specific key |
private_key | The RSA private key (PEM-encoded) — the most sensitive field |
client_email | The email address of the service account (e.g., svc@project.iam.gserviceaccount.com) |
client_id | Numeric client ID assigned by Google |
auth_uri | OAuth authorization endpoint URL |
token_uri | OAuth token endpoint URL |
How Authentication Works
When your application loads a credentials.json file, the Google Cloud client libraries use the embedded private key to construct a signed JWT (JSON Web Token). This JWT is exchanged at Google's token_uri endpoint for an OAuth 2.0 access token. The access token is then included in the Authorization: Bearer header of subsequent API requests.
This flow is known as Service Account JSON key authentication, and it is one of the most widely used authentication methods across the GCP ecosystem. It is the default mechanism for local development, CI/CD pipelines, and any workload that cannot rely on implicit metadata-based credentials.
How to Generate a credentials.json File
Method 1: Google Cloud Console
- Navigate to IAM & Admin → Service Accounts in the Google Cloud Console.
- Select the desired service account and click Create Key.
- Choose JSON as the key type and click Create.
- The file is downloaded automatically — store it securely and never share it publicly.
Method 2: gcloud CLI
gcloud iam service-accounts keys create credentials.json \ --iam-account=my-service-account@project.iam.gserviceaccount.com
Method 3: Terraform or Infrastructure as Code
When managing GCP infrastructure programmatically, use the google_service_account_iam_member and google_service_account_key resources. Be aware that storing the key output in state files introduces its own risk surface — consider using external secret backends.
Where credentials.json Is Used
Local Development
Developers set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point at their credentials.json file. This is the standard approach for testing applications locally before deploying to the cloud.
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"
CI/CD Pipelines
GitHub Actions, GitLab CI, Cloud Build, and Jenkins pipelines commonly inject credentials.json content as a secret variable. The pipeline writes it to a temporary file and sets GOOGLE_APPLICATION_CREDENTIALS before executing GCP-facing