← back to the blog

Day-to-day gcloud: profiles, ADC and the two auth logins

How to handle several accounts and projects with gcloud configurations, what Application Default Credentials is, and why gcloud auth login and gcloud auth application-default login are not the same thing.

There is a confusion I see all the time, even among people with years of GCP: believing that gcloud auth login leaves everything ready for code to run. It doesn’t. The CLI and your programs authenticate through different paths, and mixing them up is the classic origin of “it works in my terminal but the script throws a 403”.

This is what I use every day to keep it straight.

Two identities that live apart

When you work with GCP there are two different credentials on your machine, and keeping them separate in your head is key:

gcloud auth login CLI identity auth application-default login ADC credentials gcloud / gsutil commands your code and the libraries Terraform, SDKs, client libs
  • gcloud auth login authenticates the CLI. After it, gcloud, gsutil and bq know who you are. But your Python/Go/Terraform application sees none of this.
  • gcloud auth application-default login creates the Application Default Credentials (ADC): a credentials file that Google’s client libraries look for automatically. This is what your code uses.

In short: one is for you to send commands, the other is for your program to authenticate on its own. They are different files on disk and can even be different accounts.

What ADC actually is

Application Default Credentials is not a command, it is a lookup strategy. Any official Google library (the client libraries, Terraform, the GCP provider) looks for credentials in this order, and uses the first one it finds:

1. GOOGLE_APPLICATION_CREDENTIALS does it point to a file? use it 2. application-default login credentials the local file gcloud left behind 3. environment metadata SA attached to the VM / Cloud Run / GKE

Step 3 is the reason your code needs no credentials when it runs on GCP: the VM, the Cloud Run service or the GKE pod already has a service account attached, and ADC picks it up from the metadata automatically. Hence a good rule: locally you use application-default login, and in production you ship no keys at all, you let the environment provide the identity.

On Linux/Mac, the local ADC file ends up at:

~/.config/gcloud/application_default_credentials.json

Profiles: several accounts and projects without the fight

If you work with more than one client or more than one project, gcloud configurations are a lifesaver. A configuration groups active account + project + region/zone, and you switch context with a single command.

# create a config for one context
gcloud config configurations create client-a
gcloud config set account [email protected]
gcloud config set project client-a-prod
gcloud config set compute/region us-central1

# another one for another context
gcloud config configurations create homelab
gcloud config set account [email protected]
gcloud config set project my-lab

And the day-to-day is just jumping between them:

gcloud config configurations list        # list them all, the active one is marked
gcloud config configurations activate client-a
gcloud config list                        # show the current context

The trick I use the most: running a one-off command against another config without changing the active one, with --configuration:

gcloud compute instances list --configuration=client-a

That way I can check something on another client without losing my place.

The detail that bites: ADC ignores the active config

Here is the trap that takes a while to understand. Configurations rule over the CLI, but ADC is global: there is a single application_default_credentials.json per user, not one per configuration.

config client-a config homelab gcloud CLI ADC just one does not update itself

Switching config does not change the credentials your code will see. If you activate the client-a config and run terraform apply, Terraform may keep using the homelab ADC if that was the last time you ran application-default login. The result: you apply to the wrong project, or you get permission denied.

To align both identities to the same account, you have to refresh ADC by hand:

gcloud auth application-default login

And if you need ADC to point at a specific billing/quota project:

gcloud auth application-default set-quota-project client-a-prod

The recipe I follow to avoid mistakes

  1. One configuration per context (client, prod/dev, homelab). I switch with activate.
  2. For one-off commands in another context, --configuration= instead of switching.
  3. When I’m about to run code or Terraform locally, I make sure to refresh ADC with application-default login against the right account. I don’t assume the active config is enough.
  4. In production, zero keys. A service account attached to the resource, and ADC picks it up on its own.
  5. I avoid downloadable JSON keys. An SA key on disk is a long-lived credential that leaks easily. If I really need a different identity, I prefer impersonation:
gcloud ... --impersonate-service-account=app@my-project.iam.gserviceaccount.com

For impersonation to work you need roles/iam.serviceAccountTokenCreator on that SA, which ties straight into how permissions work in GCP.

Checking who I am right now

Two commands I run when something smells off:

# which account is the CLI using right now?
gcloud auth list

# which identity will my code see through ADC?
gcloud auth application-default print-access-token

If those two don’t match what you expected, there is your bug. Most “mysterious 403s” on a local machine come down to the CLI and ADC sitting on different accounts.

The summary

  • gcloud auth login = you sending commands. application-default login = your code authenticating.
  • ADC is a lookup strategy, and on GCP the environment provides the identity on its own: don’t ship keys.
  • Configurations organize the CLI, but ADC is global: refresh it separately when you switch context.
  • When in doubt, gcloud auth list and print-access-token tell you the truth.