@@ -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