Skip to content

Commit 2142db3

Browse files
authored
docs: add instructions for using workload identity federation (#564)
* docs: add instructions for using workload identity federation * docs: address review comments
1 parent 7eccff1 commit 2142db3

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

README.md

+219
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ access tokens
106106
- `ServiceAccountJwtAccessCredentials`: credentials for a Service Account - use JSON Web Token (JWT)
107107
directly in the request metadata to provide authorization
108108
- `UserCredentials`: credentials for a user identity and consent
109+
- `ExternalAccountCredentials`: base class for credentials using workload identity federation to
110+
access Google Cloud resources from non-Google Cloud platforms
111+
- `IdentityPoolCredentials`: credentials using workload identity federation to access Google Cloud
112+
resources from Microsoft Azure or any identity provider that supports OpenID Connect (OIDC)
113+
- `AwsCredentials`: credentials using workload identity federation to access Google Cloud resources
114+
from Amazon Web Services (AWS)
109115

110116
To get Application Default Credentials use `GoogleCredentials.getApplicationDefault()` or
111117
`GoogleCredentials.getApplicationDefault(HttpTransportFactory)`. These methods return the
@@ -159,6 +165,219 @@ for (Bucket b : storage_service.list().iterateAll())
159165
System.out.println(b);
160166
```
161167

168+
### Workload Identity Federation
169+
170+
Using workload identity federation, your application can access Google Cloud resources from
171+
Amazon Web Services (AWS), Microsoft Azure, or any identity provider that supports OpenID Connect
172+
(OIDC).
173+
174+
Traditionally, applications running outside Google Cloud have used service account keys to access
175+
Google Cloud resources. Using identity federation, your workload can impersonate a service account.
176+
This lets the external workload access Google Cloud resources directly, eliminating the maintenance
177+
and security burden associated with service account keys.
178+
179+
#### Accessing resources from AWS
180+
181+
In order to access Google Cloud resources from Amazon Web Services (AWS), the following requirements
182+
are needed:
183+
- A workload identity pool needs to be created.
184+
- AWS needs to be added as an identity provider in the workload identity pool (the Google [organization policy](https://cloud.google.com/iam/docs/manage-workload-identity-pools-providers#restrict) needs to allow federation from AWS).
185+
- Permission to impersonate a service account needs to be granted to the external identity.
186+
187+
Follow the detailed [instructions](https://cloud.google.com/iam/docs/access-resources-aws) on how to
188+
configure workload identity federation from AWS.
189+
190+
After configuring the AWS provider to impersonate a service account, a credential configuration file
191+
needs to be generated. Unlike service account credential files, the generated credential
192+
configuration file contains non-sensitive metadata to instruct the library on how to
193+
retrieve external subject tokens and exchange them for service account access tokens.
194+
The configuration file can be generated by using the [gcloud CLI](https://cloud.google.com/sdk/).
195+
196+
To generate the AWS workload identity configuration, run the following command:
197+
198+
```bash
199+
# Generate an AWS configuration file.
200+
gcloud iam workload-identity-pools create-cred-config \
201+
projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$AWS_PROVIDER_ID \
202+
--service-account $SERVICE_ACCOUNT_EMAIL \
203+
--aws \
204+
--output-file /path/to/generated/config.json
205+
```
206+
207+
Where the following variables need to be substituted:
208+
- `$PROJECT_NUMBER`: The Google Cloud project number.
209+
- `$POOL_ID`: The workload identity pool ID.
210+
- `$AWS_PROVIDER_ID`: The AWS provider ID.
211+
- `$SERVICE_ACCOUNT_EMAIL`: The email of the service account to impersonate.
212+
213+
This generates the configuration file in the specified output file.
214+
215+
You can now [use the Auth library](#using-external-identities) to call Google Cloud
216+
resources from AWS.
217+
218+
#### Access resources from Microsoft Azure
219+
220+
In order to access Google Cloud resources from Microsoft Azure, the following requirements are
221+
needed:
222+
- A workload identity pool needs to be created.
223+
- Azure needs to be added as an identity provider in the workload identity pool (the Google [organization policy](https://cloud.google.com/iam/docs/manage-workload-identity-pools-providers#restrict) needs to allow federation from Azure).
224+
- The Azure tenant needs to be configured for identity federation.
225+
- Permission to impersonate a service account needs to be granted to the external identity.
226+
227+
Follow the detailed [instructions](https://cloud.google.com/iam/docs/access-resources-azure) on how
228+
to configure workload identity federation from Microsoft Azure.
229+
230+
After configuring the Azure provider to impersonate a service account, a credential configuration
231+
file needs to be generated. Unlike service account credential files, the generated credential
232+
configuration file contains non-sensitive metadata to instruct the library on how to
233+
retrieve external subject tokens and exchange them for service account access tokens.
234+
The configuration file can be generated by using the [gcloud CLI](https://cloud.google.com/sdk/).
235+
236+
To generate the Azure workload identity configuration, run the following command:
237+
238+
```bash
239+
# Generate an Azure configuration file.
240+
gcloud iam workload-identity-pools create-cred-config \
241+
projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$AZURE_PROVIDER_ID \
242+
--service-account $SERVICE_ACCOUNT_EMAIL \
243+
--azure \
244+
--output-file /path/to/generated/config.json
245+
```
246+
247+
Where the following variables need to be substituted:
248+
- `$PROJECT_NUMBER`: The Google Cloud project number.
249+
- `$POOL_ID`: The workload identity pool ID.
250+
- `$AZURE_PROVIDER_ID`: The Azure provider ID.
251+
- `$SERVICE_ACCOUNT_EMAIL`: The email of the service account to impersonate.
252+
253+
This generates the configuration file in the specified output file.
254+
255+
You can now [use the Auth library](#using-external-identities) to call Google Cloud
256+
resources from Azure.
257+
258+
#### Accessing resources from an OIDC identity provider
259+
260+
In order to access Google Cloud resources from an identity provider that supports [OpenID Connect (OIDC)](https://openid.net/connect/), the following requirements are needed:
261+
- A workload identity pool needs to be created.
262+
- An OIDC identity provider needs to be added in the workload identity pool (the Google [organization policy](https://cloud.google.com/iam/docs/manage-workload-identity-pools-providers#restrict) needs to allow federation from the identity provider).
263+
- Permission to impersonate a service account needs to be granted to the external identity.
264+
265+
Follow the detailed [instructions](https://cloud.google.com/iam/docs/access-resources-oidc) on how
266+
to configure workload identity federation from an OIDC identity provider.
267+
268+
After configuring the OIDC provider to impersonate a service account, a credential configuration
269+
file needs to be generated. Unlike service account credential files, the generated credential
270+
configuration file contains non-sensitive metadata to instruct the library on how to
271+
retrieve external subject tokens and exchange them for service account access tokens.
272+
The configuration file can be generated by using the [gcloud CLI](https://cloud.google.com/sdk/).
273+
274+
For OIDC providers, the Auth library can retrieve OIDC tokens either from a local file location
275+
(file-sourced credentials) or from a local server (URL-sourced credentials).
276+
277+
**File-sourced credentials**
278+
For file-sourced credentials, a background process needs to be continuously refreshing the file
279+
location with a new OIDC token prior to expiration. For tokens with one hour lifetimes, the token
280+
needs to be updated in the file every hour. The token can be stored directly as plain text or in
281+
JSON format.
282+
283+
To generate a file-sourced OIDC configuration, run the following command:
284+
285+
```bash
286+
# Generate an OIDC configuration file for file-sourced credentials.
287+
gcloud iam workload-identity-pools create-cred-config \
288+
projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$OIDC_PROVIDER_ID \
289+
--service-account $SERVICE_ACCOUNT_EMAIL \
290+
--credential-source-file $PATH_TO_OIDC_ID_TOKEN \
291+
# Optional arguments for file types. Default is "text":
292+
# --credential-source-type "json" \
293+
# Optional argument for the field that contains the OIDC credential.
294+
# This is required for json.
295+
# --credential-source-field-name "id_token" \
296+
--output-file /path/to/generated/config.json
297+
```
298+
299+
Where the following variables need to be substituted:
300+
- `$PROJECT_NUMBER`: The Google Cloud project number.
301+
- `$POOL_ID`: The workload identity pool ID.
302+
- `$OIDC_PROVIDER_ID`: The OIDC provider ID.
303+
- `$SERVICE_ACCOUNT_EMAIL`: The email of the service account to impersonate.
304+
- `$PATH_TO_OIDC_ID_TOKEN`: The file path used to retrieve the OIDC token.
305+
306+
This generates the configuration file in the specified output file.
307+
308+
**URL-sourced credentials**
309+
For URL-sourced credentials, a local server needs to host a GET endpoint to return the OIDC token.
310+
The response can be in plain text or JSON. Additional required request headers can also be
311+
specified.
312+
313+
To generate a URL-sourced OIDC workload identity configuration, run the following command:
314+
315+
```bash
316+
# Generate an OIDC configuration file for URL-sourced credentials.
317+
gcloud iam workload-identity-pools create-cred-config \
318+
projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$POOL_ID/providers/$OIDC_PROVIDER_ID \
319+
--service-account $SERVICE_ACCOUNT_EMAIL \
320+
--credential-source-url $URL_TO_GET_OIDC_TOKEN \
321+
--credential-source-headers $HEADER_KEY=$HEADER_VALUE \
322+
# Optional arguments for file types. Default is "text":
323+
# --credential-source-type "json" \
324+
# Optional argument for the field that contains the OIDC credential.
325+
# This is required for json.
326+
# --credential-source-field-name "id_token" \
327+
--output-file /path/to/generated/config.json
328+
```
329+
330+
Where the following variables need to be substituted:
331+
- `$PROJECT_NUMBER`: The Google Cloud project number.
332+
- `$POOL_ID`: The workload identity pool ID.
333+
- `$OIDC_PROVIDER_ID`: The OIDC provider ID.
334+
- `$SERVICE_ACCOUNT_EMAIL`: The email of the service account to impersonate.
335+
- `$URL_TO_GET_OIDC_TOKEN`: The URL of the local server endpoint to call to retrieve the OIDC token.
336+
- `$HEADER_KEY` and `$HEADER_VALUE`: The additional header key/value pairs to pass along the GET
337+
request to `$URL_TO_GET_OIDC_TOKEN`, e.g. `Metadata-Flavor=Google`.
338+
339+
You can now [use the Auth library](#using-external-identities) to call Google Cloud
340+
resources from an OIDC provider.
341+
342+
#### Using External Identities
343+
344+
External identities (AWS, Azure, and OIDC-based providers) can be used with
345+
`Application Default Credentials`. In order to use external identities with Application Default
346+
Credentials, you need to generate the JSON credentials configuration file for your external identity
347+
as described above. Once generated, store the path to this file in the
348+
`GOOGLE_APPLICATION_CREDENTIALS` environment variable.
349+
350+
```bash
351+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/config.json
352+
```
353+
354+
The library can now choose the right type of client and initialize credentials from the context
355+
provided in the configuration file.
356+
357+
```java
358+
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault();
359+
360+
String projectId = "your-project-id";
361+
String url = "https://storage.googleapis.com/storage/v1/b?project=" + projectId;
362+
363+
HttpCredentialsAdapter credentialsAdapter = new HttpCredentialsAdapter(googleCredentials);
364+
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(credentialsAdapter);
365+
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url));
366+
367+
JsonObjectParser parser = new JsonObjectParser(GsonFactory.getDefaultInstance());
368+
request.setParser(parser);
369+
370+
HttpResponse response = request.execute();
371+
System.out.println(response.parseAsString());
372+
```
373+
374+
You can also explicitly initialize external account clients using the generated configuration file.
375+
376+
```java
377+
ExternalAccountCredentials credentials =
378+
ExternalAccountCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
379+
```
380+
162381
## Configuring a Proxy
163382

164383
For HTTP clients, a basic proxy can be configured by using `http.proxyHost` and related system properties as documented

0 commit comments

Comments
 (0)