TL;DR Because otherwise gcloud auth application-default
issued credentials consume quota in a Google-owned project which may (not) have the service enabled.
For Google APIs that require authentication and authorization of an "identity", OAuth is used.
In Google's implementation, Google services (specific APIs) must be enabled before use and APIs are enabled (and aggregated) in Google Projects.
There are 2 important types of "identity", (human) users (e.g. gmail.com) and software (sometimes called robot) users. Unassisted (!) software users are identified by Service Accounts.
gcloud
is software but it primarily operates on behalf of a (human) user. Because gcloud
generally needs to be able to identify its human user (e.g. your gmail.com account), it operates in a delegated manner. gcloud
does have its own identity but it (mostly) operates as the human user.
NOTE You can use gcloud with Service Accounts too.
Importantly, gcloud
is a so-called installed app. When a user issues gcloud
commands, gcloud
provides the user's (!) identity and its own identity (called an OAuth Client ID) to a Google Project (!) that Google owns|runs.
NOTE This explains why you can e.g. create projects and enable services with gcloud
, because another project (with the correct services enabled) is effecting these API calls on your behalf.
When you develop apps against Google's services, you must decide how the code authenticates to Google. If it represents users, then you'll also need to create a Client ID and route users' requests through that. If it runs as a standalone solution (without user intervention), then it will run as a Service Account.
Google's SDKs include an elegant feature called Application Default Credentials (ADCs). ADCs automate the discovery of Service Account credentials (!), reduce (and thus simplify) code, provide portability and preserve security.
If you can use ADCs, then you should.
This creates (!) a challenge. For developers writing non-user code, how could they test the software without creating a Service Account (and download a key)? The solution was (!) gcloud auth application-default
.
When you use gcloud auth application-default
, the user (e.g. gmail.com) credentials are used to create credentials that behave just like ADCs. The developer can test their code without having to create a Service Account and keys.
However (!) Google passively (!) discourages the use of gcloud auth application-default
.
In part, this is because:
- The Google Project that backs requests made using
gcloud auth application-default
is a Google-owned project not your project; Google is "billed" not you.
- The credentials that result have all the powers that the auth'd user's credentials have rather than the (hopefully) more specific (often IAM-controlled) permissions of a Service Account
- The Google-owned Project may not have the service that you want to use enabled.
- The process is opaque, standalone code may magically authenticate against Google Cloud using ADCs using
gcloud auth application-defaut
which can be surprising (and shouldn't be).
You should not use gcloud auth application-default
credentials for the above reasons.
If you are using gcloud auth application-default
credentials you should consider why and choose a better alternative:
- Either use user credentials directly
- Or use a Service Account
If off Google Cloud, preferably use a Service Account through Workload Identity Federation