Skip to content

Commit 43dcb9a

Browse files
committed
AIP 4115/4110: How to retrieve tokens from the metadata server
Add a complete discussion of how auth libraries and workloads should cache and refresh tokens when loading them from the metadata server. Existing GCP auth libraries handle this properly, but occasionally it is not possible or desirable to use a preexisting auth library. This new section of the AIP provides guidance for those who need to directly retrieve tokens.
1 parent 925c824 commit 43dcb9a

File tree

2 files changed

+139
-20
lines changed

2 files changed

+139
-20
lines changed

aip/auth/4110.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ ADC, developers should be able to run the code in different environments and the
2020
supporting systems fetch the appropriate credentials based on each environment
2121
in an effortless manner.
2222

23+
When running on a Google Cloud Platform compute environment such as GCE, GKE, or
24+
App Engine, in general no configuration input is required for auth libraries as
25+
described in this AIP. The auth library will automatically load the correct
26+
credentials from the environment's metadata server.
27+
2328
Auth libraries following the standards in these AIPs are known as _"Google
2429
Unified Auth Clients"_, or _GUAC_ for short. The resulting libraries are
2530
colloquially called _GUACs_.
@@ -42,6 +47,12 @@ auth libraries **must** support this credential type.
4247
needs to authenticate to access Google APIs. The auth libraries **must** support
4348
this credential type.
4449

50+
- **Workload Access Token**: A credential retrieved from the metadata server
51+
running in your GCP compute environment. This identifies either a service
52+
account (typical in case of GCE and App Engine) or a federated non-Google
53+
credential (typical in case of GKE). The auth libraries **must** support this
54+
credential type.
55+
4556
- **OAuth Client ID**: A credential that identifies the client application which
4657
allows human users to sign-in through [3-legged OAuth flow][1], which grants the
4758
permissions to the application to access Google APIs on behalf of the human
@@ -133,7 +144,7 @@ digraph d_front_back {
133144
1. If the credential is [an external account][8] JSON, go to step (4)
134145
1. If the credential is unknown type, return an error saying that _[END]_
135146
1. Credentials not found _[END]_
136-
1. **Check workload credentials (on GCE, GKE, GAE and Serverless)**
147+
1. **Check workload access token (on GCE, GKE, GAE and Serverless)**
137148
1. If true,
138149
1. If identity binding is enabled, by meeting the requirements in
139150
[mTLS Token Binding][9], use the mTLS Token Binding flow to fetch an

aip/auth/4115.md

Lines changed: 127 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,140 @@ created: 2020-08-13
77

88
# Default Credentials For Google Cloud Virtual Environments
99

10-
If the client runs on Google cloud virtual environments such as [Google Compute Engine (GCE)][0],
11-
[Serverless][1], or [Google Kubernetes Engine (GKE)][2], the auth library **may** leverage
12-
Google’s default mutual TLS (mTLS) credentials and obtain bound tokens for the instance.
13-
The auth library **may** use the default mTLS credentials and bound tokens to access Google APIs.
14-
15-
mTLS authentication enables authentication of both client and server identities in a TLS handshake.
16-
Applications running in Google virtual environments can authenticate to Google APIs using X.509
17-
SPIFFE Verifiable Identity Documents (SVIDs). These SVIDs are X.509 certificates that contain SPIFFE
18-
IDs specifying the identity of the certificate owner.
19-
20-
Bound tokens are access tokens that are bound to some property of the credentials used to establish
21-
the mTLS connection. The advantage of bound tokens is that they can be used over secure channels
22-
established via mTLS credentials with the correct binding information, when appropriate access
23-
policies have been put in place. Therefore, using bound tokens is more secure than bearer tokens,
24-
which can be stolen and adversarially replayed.
10+
If the client runs on Google cloud compute environments such as [Google Compute
11+
Engine (GCE)][0], [Serverless][1], or [Google Kubernetes Engine (GKE)][2],
12+
absent any explicit configuration the auth library will follow the Application
13+
Default Credentials flow described in AIP-4110. It will detect that it is
14+
running on a platform with an available metadata server API, and configure
15+
itself to retrieve workload credentials from the metadata server.
16+
17+
Typically, these workload credentials will be Google oauth access tokens, which are opaque tokens (only decodable by Google) that start with the fixed string `ya29.`. Depending on the configuration of the workload and the Google service being called, the auth library may use additional features supported on the metadata server, such as mTLS-bound access tokens.
2518

26-
This AIP describes the flow of:
19+
This AIP describes how to:
2720

28-
1. Retrieving a configuration through a metadata server (MDS) endpoint. The configuration specifies
29-
how to access Google’s default mTLS credentials.
30-
2. Requesting bound tokens.
21+
1. Retrieve and cache workload access tokens from the metadata server.
22+
2. Retrieve mTLS-specific configuration from the metadata server
23+
3. Request mTLS-bound access tokens from the metadata server.
3124

3225
**Note:** Because this AIP describes guidance and requirements in a language-neutral way, it uses
3326
generic terminology which may be imprecise or inappropriate in certain languages or environments.
3427

3528
## Guidance
3629

30+
### Metadata Server API
31+
32+
The metadata server is a special API that your workload can access using the
33+
special hostname `metadata.google.internal`. This special hostname is
34+
configured to resolve to the address `169.254.169.254` across all GCP
35+
compute environments.
36+
37+
The metadata server serves an HTTP API. The precise set of paths available on
38+
this API is platform-specific, but the main paths used for authenticating to
39+
Google APIs are
40+
41+
#### Workload Access Token
42+
43+
The access token endpoint returns an opaque access token that can be used as a bearer token to authenticate to Google APIs.
44+
45+
Request: `GET http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token`
46+
47+
Response: A JSON object with the following keys:
48+
* `access_token` (String): The access token.
49+
* `expires_in` (Number): The number of seconds until the token expires.
50+
* `token_type` (String): Always the static string `Bearer`.
51+
52+
#### Workload Identity Token
53+
54+
The identity token endpoint returns a JWT asserting the workload's identity in a
55+
way that can be verified by non-Google third parties. Third parties that you
56+
present the token to will expect a specific audience set on the token.
57+
58+
Request: `GET http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/identity`. Accepts the following query parameters:
59+
* `audience` (Required): The audience for which this token should be issued.
60+
* `format` (Optional): `full`, or `standard`. Only understood on GCE.
61+
* `licenses`: (Optional): `TRUE` or `FALSE`. Only understood on GCE.
62+
63+
Response: A JSON Web Token, no additional framing.
64+
65+
Note that the claims in the JWT can vary based on which compute platform you are using:
66+
* On GCE, the returned JWT is the [VM Identity Token](https://cloud.google.com/compute/docs/instances/verifying-instance-identity).
67+
* On GKE:
68+
* When Workload Identity Federation for GKE is disabled, your workload talks
69+
to the underlying GCE metadata server, so the behavior is the same as the
70+
GCE case.
71+
* When Workload Identity Federation for GKE is enabled, and the pod is not
72+
configured with service account impersonation (the default), the identity
73+
token endpoint always returns an error.
74+
* When Workload Identity Federation for GKE is enabled, and the pod is
75+
configured with service account impersonation, the identity token endpoint
76+
returns a JWT as issued by
77+
[generateIdToken](https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateIdToken)
78+
for the Google service account being impersonated.
79+
80+
### Retrieve and Cache Workload Access Tokens
81+
82+
When retrieving workload access tokens from the metadata server on GCE, GKE, App
83+
Engine, or other GCP compute platforms, the following recommendations apply.
84+
Google-provided auth libraries adhere to these recommendations. If your
85+
workload directly communicates with the metadata server in order to retrieve
86+
workload access tokens, you should adhere to these recommendations. If you do
87+
not, your workload may suffer from intermittent and difficult-to-debug
88+
authentication errors.
89+
90+
**Cache the access token in memory:** Your workload should not make a call to
91+
the metadata server every time you make a call to a GCP API. Doing so will
92+
cause your workload to be rate-limited by the metadata server, or GCP IAM.
93+
Instead, you should maintain an in-memory cache of the access token, and use the
94+
cached token across all of your outbound requests.
95+
96+
**Use a robust refresh strategy:** Each time you attempt to use the cached
97+
access token, check the remaining lifetime of the token.
98+
* If it is greater than 225 seconds, the token is fresh. Proceed to use it.
99+
* If it is less than 0 seconds, then the token is expired. Refresh the cached
100+
token, then proceed with the current thread.
101+
* If it is less than 225 seconds, but more than 0 seconds, then the token is
102+
stale. You can handle the current thread in one of two ways:
103+
* (Background Refresh) If the token has greater than two minutes of validity
104+
remaining, you can refresh the cached token in the background, allowing the
105+
current thread to immediately proceed with the stale token, OR
106+
* (Blocking Refresh) Refresh the cached token, blocking the current thread
107+
until the refresh is complete.
108+
109+
It is not safe to refresh the cached access token in the background on a
110+
schedule without additionally checking the status of the token before using it
111+
to make a request. Clock skew between your workload and the metadata server may
112+
result in your background refresh attempt still receiving a stale access token.
113+
114+
Note that the access token returned by the metadata server may itself be stale.
115+
Certain implementations of the metadata server use the "Background Refresh"
116+
strategy described above for managing their own internal caches of tokens. For
117+
example, when running on GKE with Workload Identity Federation for GKE enabled,
118+
gke-metadata-server will not reliably return a refreshed access token until 120
119+
seconds before the token expires. As long as your workload follows the robust
120+
refresh strategy described above, this will not cause problems.
121+
122+
**Limit concurrency in initial fill and refresh of the cached token:** Use a
123+
single-flight mechanism, or locking, to ensure that your workload only makes a
124+
single concurrent call to the metadata server to retrieve the access token, no
125+
matter how many threads or coroutines might have triggered the refresh. This
126+
ensures that your workload won't accidentally get rate-limited by the metadata
127+
server when your workload is under high load.
128+
129+
### mTLS and Bound Tokens
130+
131+
mTLS authentication enables authentication of both client and server identities
132+
in a TLS handshake. Applications running in Google virtual environments can
133+
authenticate to Google APIs using X.509 SPIFFE Verifiable Identity Documents
134+
(SVIDs). These SVIDs are X.509 certificates that contain SPIFFE IDs specifying
135+
the identity of the certificate owner.
136+
137+
Bound tokens are access tokens that are bound to some property of the
138+
credentials used to establish the mTLS connection. The advantage of bound tokens
139+
is that they can be used over secure channels established via mTLS credentials
140+
with the correct binding information, when appropriate access policies have been
141+
put in place. Therefore, using bound tokens is more secure than bearer tokens,
142+
which can be stolen and adversarially replayed.
143+
37144
### Access Default mTLS Credentials
38145

39146
**Note:** Before trying to use Google’s default mTLS credentials, the client **must** first check if the remote
@@ -118,6 +225,7 @@ tokens expire.
118225
- **2020-12-14**: Replace note on scopes with more detailed discussion.
119226
- **2021-07-13**: Clarify GCE equivalent runtimes
120227
- **2023-02-16**: Add mTLS configuration endpoint and unify the token binding flow.
228+
- **2025-01-09**: Describe how to retrieve and cache standard access tokens.
121229

122230
<!-- prettier-ignore-start -->
123231
[0]: https://cloud.google.com/compute

0 commit comments

Comments
 (0)