GCP Service Account
Obtain temporary credentials from Google Cloud with Workload Identity Federation.
This guide shows how to authenticate with Google Cloud using Workload Identity Federation. Your applications get temporary credentials without embedding long-term credentials such as service account keys.
Google Cloud returns a short-lived access token, which your applications use to access Google Cloud resources.
Prerequisites
- A useGrant provider with a client
- A Google Cloud project
- The gcloud CLI installed on your system
Before you start, plan your security strategy so that credentials are only issued in a predictable way. Define at least one condition on your workload identity provider, so untrusted identities can't request credentials for your cloud resources.
Step 1: Create a workload identity pool
- Sign in to the Google Cloud console and open IAM & Admin.
- Select Workload Identity Federation.
- Click Create pool, then add a provider to the pool.
- Configure the provider:
- Provider type: OpenID Connect (OIDC)
- Issuer URL: your provider's issuer URL, shown on the provider's domain settings page in useGrant, or your custom domain.
- JWK file: leave empty. Google Cloud fetches the public keys from your issuer's JWKS endpoint.
- Allowed audiences: the audience you set when creating the client in useGrant, for example
sts.googleapis.com.
- Configure the attribute mapping: map
google.subjecttoassertion.sub. Add more mappings if needed, for exampleattribute.audtoassertion.aud.
See the Google Cloud documentation on Workload Identity Federation for more details and options.
Step 2: Create a service account
The workload identity pool and provider handle authentication to Google Cloud. To authorize access to resources, create a service account with the permissions your workload needs.
- In IAM & Admin, select Service accounts.
- Click Create service account.
- Grant the service account the roles your workload needs.
See the Google Cloud documentation on service account impersonation for more details.
Step 3: Grant the pool access to the service account
- In IAM & Admin, select Workload Identity Federation.
- Select the pool you created and click Grant access.
- Select Grant access using service account impersonation.
- Select the service account you created in the previous step.
- Restrict access to your useGrant client by selecting the
subjectattribute and entering the client's subject. This grants theroles/iam.workloadIdentityUserrole on the service account.
- Click Save. In the dialog that opens, download the configuration file.
The configuration file looks like this:
{
"universe_domain": "googleapis.com",
"type": "external_account",
"audience": "//iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID",
"subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
"token_url": "https://sts.googleapis.com/v1/token",
"service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/SERVICE_ACCOUNT_EMAIL:generateAccessToken",
"credential_source": {
"file": "token.jwt",
"format": {
"type": "text"
}
}
}Step 4: Get temporary credentials
Create an access token for your client in useGrant and save it to the file set in credential_source.file, token.jwt in this example.
Then authenticate with the gcloud CLI:
gcloud auth login --cred-file=gcp-config.jsonPrint an access token:
gcloud auth print-access-tokenTo test the login, list your projects:
gcloud projects listThe service account needs the Browser role to list projects. Without it, the
list may be empty or the command may fail.
Alternative: using curl
Create an access token for your client and export it as an environment variable:
export USEGRANT_OIDC_TOKEN=$(curl -s -X POST "https://sdk.usegrant.dev/v1/providers/{providerId}/clients/{clientId}/tokens" \
-H "Authorization: Bearer uga_<key>" \
| jq -r '.accessToken')Exchange it for a federated token with Google Cloud STS:
PAYLOAD="$(cat <<EOF
{
"audience": "//iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID",
"grantType": "urn:ietf:params:oauth:grant-type:token-exchange",
"requestedTokenType": "urn:ietf:params:oauth:token-type:access_token",
"scope": "https://www.googleapis.com/auth/cloud-platform",
"subjectTokenType": "urn:ietf:params:oauth:token-type:jwt",
"subjectToken": "${USEGRANT_OIDC_TOKEN}"
}
EOF
)"FEDERATED_TOKEN="$(curl --fail "https://sts.googleapis.com/v1/token" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data "${PAYLOAD}" \
| jq -r '.access_token'
)"Where:
PROJECT_NUMBERis the number of your Google Cloud projectPOOL_IDis the ID of the workload identity poolPROVIDER_IDis the ID of the workload identity providerUSEGRANT_OIDC_TOKENis the access token created for your useGrant client
Then use the federated token to impersonate the service account from step 2:
ACCESS_TOKEN="$(curl --fail-with-body "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/SERVICE_ACCOUNT_EMAIL:generateAccessToken" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${FEDERATED_TOKEN}" \
--data '{"scope": ["https://www.googleapis.com/auth/cloud-platform"]}' \
| jq -r '.accessToken'
)"Where:
SERVICE_ACCOUNT_EMAILis the email of the service account from step 2FEDERATED_TOKENis the federated token from the previous command
The result is a Google Cloud OAuth 2.0 access token. Use it as a Bearer token to call most Google Cloud APIs,
or pass it to the gcloud CLI with the CLOUDSDK_AUTH_ACCESS_TOKEN environment variable.