Provides expert guidance on authenticating and authorizing to Google Cloud services and APIs, covering human users, service identities, Application Default Credentials (ADC), and best practices for secure access.
Authentication is the process of proving who you are. In Google Cloud, you represent a Principal (an identity like a user or a service). This is the first step before Authorization (determining what you can do).
Before providing a specific solution, clarify the following with the user:
For users to access Google Cloud, they need an identity that Google Cloud can recognize.
Google Cloud supports several ways to configure identities for your internal workforce (developers, administrators, employees):
Used for interacting with Google Cloud resources and APIs during development and management.
gcloud auth login): Used to authenticate the CLI itself so you can run
management commands (e.g., gcloud compute instances list). It uses a
Credential (like an OAuth 2.0 refresh token) stored locally.gcloud auth application-default login): This is different from CLI
auth. It creates a local JSON file that Google Cloud Client Libraries
(Python, Java, etc.) use to act as "you" when you run code on your laptop.gcloud auth login) and use Service Account Impersonation to run CLI commands or
generate short-lived credentials. This is a critical best practice for local
development and troubleshooting.Used when a human (who is not a developer) needs to access a web application you've deployed on Google Cloud. Note: These are distinct from workforce identities.
When code runs in production, it should use a Service Account rather than a human user account.
Instead of using Service Account Keys (dangerous JSON files), you should attach a custom service account to the Google Cloud resource. The resource's environment then provides a Token (a short-lived digital object) via a local metadata server.
Use Workload Identity Federation for GKE to map Kubernetes identities to IAM principal identifiers. This grants specific Kubernetes workloads access to specific Google Cloud APIs. Learn more here.
For code running outside Google Cloud (e.g., AWS, Azure, or on-prem), do not use keys. Instead, use Workload Identity Federation to exchange an external token (like an AWS IAM role) for a short-lived Google Cloud access token.
API keys are encrypted strings used for public data (e.g., Google Maps) or simplified access like Vertex AI Express Mode, which allows fast testing of Gemini models without complex setup. Both humans and services (e.g., Cloud Run-based AI agent) can use API keys, for the services that support it.
Note: API keys should be restricted to specific APIs and projects to minimize security risks. Store API keys in a secrets manager like Secret Manager to prevent accidental exposure.
While IAM is the modern way to handle authorization, legacy Compute Engine VMs and GKE node pools still rely on Access Scopes alongside IAM. If a VM's scope is restricted, the attached service account will fail to make API calls even if it has the correct IAM permissions. Check this first if attached service accounts are failing unexpectedly.
The underlying mechanism for impersonation and secure service-to-service communication is the IAM Service Account Credentials API. This API generates short-lived access tokens, OpenID Connect (OIDC) ID tokens, or self-signed JSON Web Tokens (JWTs) dynamically, removing the need for static credentials.
After Authentication, Google Cloud uses Identity and Access Management (IAM) to determine what the authenticated principal can do.
roles/storage.objectViewer or
roles/bigquery.dataEditor. Always try to use these first.gcloud auth application-default login to create local
credentials (ADC).roles/storage.objectViewer role on a
bucket.storage.Client(). It automatically finds your
local credentials via ADC. Note: ADC searches in a specific order—first
checking the GOOGLE_APPLICATION_CREDENTIALS environment variable, then the
local gcloud JSON file, and finally the attached service account metadata
server.roles/cloudsql.client role on
the project.When calling a private Cloud Run service from another service, the caller
generates a Google-signed OpenID Connect (OIDC) ID Token and passes it in
the Authorization: Bearer <TOKEN> header.
gcloud auth application-default login or Service Account Impersonation.skillbazaar install google-cloud-recipe-auth --agent claudeSign in (free) to install skills with the CLI.
Author
on GitHub
Published by