useGrant

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

  1. Sign in to the Google Cloud console and open IAM & Admin.
  2. Select Workload Identity Federation.
  3. Click Create pool, then add a provider to the pool.
  4. 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.
  5. Configure the attribute mapping: map google.subject to assertion.sub. Add more mappings if needed, for example attribute.aud to assertion.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.

  1. In IAM & Admin, select Service accounts.
  2. Click Create service account.
  3. 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

  1. In IAM & Admin, select Workload Identity Federation.
  2. Select the pool you created and click Grant access.
  3. 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 subject attribute and entering the client's subject. This grants the roles/iam.workloadIdentityUser role on the service account.
  4. Click Save. In the dialog that opens, download the configuration file.

The configuration file looks like this:

gcp-config.json
{
  "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.json

Print an access token:

gcloud auth print-access-token

To test the login, list your projects:

gcloud projects list

The 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_NUMBER is the number of your Google Cloud project
  • POOL_ID is the ID of the workload identity pool
  • PROVIDER_ID is the ID of the workload identity provider
  • USEGRANT_OIDC_TOKEN is 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_EMAIL is the email of the service account from step 2
  • FEDERATED_TOKEN is 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.

On this page