Skip to content

Commit 114c8b4

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 114c8b4

File tree

2 files changed

+151
-22
lines changed

2 files changed

+151
-22
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: 139 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,150 @@ 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
18+
are opaque tokens (only decodable by Google) that start with the fixed string
19+
`ya29.`. Depending on the configuration of the workload and the Google service
20+
being called, the auth library may use additional features supported on the
21+
metadata server, such as mTLS-bound access tokens.
2522

26-
This AIP describes the flow of:
23+
This AIP describes how to:
2724

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.
25+
1. Retrieve and cache workload access tokens from the metadata server.
26+
2. Retrieve mTLS-specific configuration from the metadata server
27+
3. Request mTLS-bound access tokens from the metadata server.
3128

32-
**Note:** Because this AIP describes guidance and requirements in a language-neutral way, it uses
33-
generic terminology which may be imprecise or inappropriate in certain languages or environments.
29+
**Note:** Because this AIP describes guidance and requirements in a
30+
language-neutral way, it uses generic terminology which may be imprecise or
31+
inappropriate in certain languages or environments.
3432

3533
## Guidance
3634

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

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

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

0 commit comments

Comments
 (0)