This document shows you how to use Workload Identity Federation with Google Cloud authentication libraries, known as auth libraries, to authenticate workloads from third-party identity providers such as AWS, Microsoft Azure, and providers that support OpenID Connect (OIDC) or SAML 2.0.
Workload Identity Federation lets applications running outside Google Cloud access Google Cloud resources without using service account keys. The Google auth libraries enable this by exchanging external credentials for short-lived Google Cloud access tokens.
For authentication, you can obtain external credentials using the following methods:
- Standard mechanism for common setups.
- Custom credential suppliers for complex workflows that require you to write your own code.
Before you begin
Enable the required APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.Configure Workload Identity Federation with your identity provider.
Authenticate using the standard credential mechanism
For commonly supported third-party identity providers, you can use the Google Cloud auth library's built-in capabilities to authenticate your workloads by generating a credential configuration file. This file provides the necessary information for the auth libraries to federate identities from external providers.
The credential configuration file, typically loaded using the
GOOGLE_APPLICATION_CREDENTIALS environment variable, can instruct the auth
libraries to obtain the third-party subject token using one of the following methods:
- File-sourced: The library reads the subject token from a local file. A separate process must ensure this file contains a valid, unexpired token.
- URL-sourced: The library fetches the subject token by making a request to a specified local URL endpoint.
- Executable-sourced: The library runs a configured executable command. The standard output of the executable is expected to contain the subject token.
Generate the credential configuration file for your specific provider:
- AWS
- Azure
OIDC or SAML identity provider
This page includes instructions for the following:
Use the credential configuration file to authenticate.
To let the Google Cloud client libraries automatically locate and use your credential configuration file, set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to the path of the generated JSON file.Export the environment variable in your shell:
bash export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/config.jsonAfter you set the environment variable, the client libraries handle the authentication flow.
The following code sample shows how to make an authenticated call to a Google Cloud API:
Node.js
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Node.js API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Authenticate using custom credential suppliers
If your environment doesn't support the built-in capabilities of the Google auth library or if you want to implement custom logic to supply credentials to the Google auth library, use custom credential suppliers to authenticate your workloads.
Access resources from AWS
When you initialize the authentication client, provide a custom implementation of a credential supplier. The client instance defers to the supplier to retrieve AWS security credentials to exchange for a Google Cloud access token. The supplier must return valid and unexpired credentials when the client calls it.
The authentication client doesn't cache the returned AWS security credentials or region, so implement caching in the supplier to prevent redundant requests for the same resources.
The following code samples show how you can set up access to Google Cloud resources from AWS with a custom credential supplier.
Node.js
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Node.js API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Access resources from OIDC and SAML
When you initialize the authentication client, provide a custom token supplier to provide a subject token that is exchanged for a Google Cloud access token. The supplier must return a valid and unexpired subject token when the client calls it.
The authentication client doesn't cache the returned token, so implement caching in the supplier to prevent redundant requests for the same subject token.
The following code samples show how you can set up access to Google Cloud resources from providers that support OpenID Connect (OIDC) or SAML 2.0 with a custom credential supplier.
Node.js
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Node.js API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation.
To authenticate to IAM, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
What's next
- Learn more about Workload Identity Federation.