Google Cloud client libraries: local user ADC versus Cloud Run service account interoperability
0 reputation · 26 Nov 2020, 00:01 UTC
0 reputation · 26 Nov 2020, 00:01 UTC
Run the same Google Cloud client-library code locally and on Cloud Run without changing credential initialization. Locally, credentials come from gcloud auth application-default login; in production, Cloud Run supplies a default service account through the instance metadata server.
Application Default Credentials resolution checks environment variables, local gcloud credentials, then the metadata server. A local user account and a production service account are not interchangeable identities, and IAM role grants do not carry over automatically. Client-library behavior can also vary by language and version, particularly for OAuth audiences and scopes.
Version assumption: the library follows the standard ADC resolution order. Practical verification: compare the effective credential type in each environment and review the production service account's IAM policy for the target resource.
Specific questions:
34775 reputation · 26 Nov 2020, 09:59 UTC
1. Does the client library transparently switch from local user credentials to the production service account when running on Cloud Run?
Yes. The Application Default Credentials (ADC) resolver follows a fixed order:
GOOGLE_APPLICATION_CREDENTIALSgcloud auth application‑default login)2. Must the production service account receive the same IAM roles granted to the local developer account for identical API access?
Yes. IAM is identity‑specific. The permissions that your local developer user has do not carry over to the Cloud Run service account. For every API you call, the service account must be granted at least the same roles (or a custom role that contains the same permissions). Otherwise the calls will fail with PERMISSION_DENIED, even though the library has switched credentials.
3. Do differing OAuth audiences or scopes between user credentials and service account tokens require separate client configuration?
Generally no. Both token types are issued with the same audience (the service’s API endpoint) and the library automatically requests the minimal set of scopes needed for the API you’re using. The only time you need to adjust configuration is if you explicitly set a custom scopes or audience in code or via environment variables. In that case you must keep those values identical in both environments.
scopes parameter that defaults to a minimal set; if your code overrides it, you must ensure the same value locally and on Cloud Run.# Python example
from google.auth import default
creds, _ = default()
print('is_user:', creds._is_user())
print('is_service_account:', creds._is_service_account())
is_user: True.is_service_account: True.gcloud iam service-accounts get-iam-policy SERVICE_ACCOUNT_EMAIL
Compare the roles granted to the service account with those granted to your local user (via gcloud projects get-iam-policy or the Cloud Console). Add any missing roles with gcloud projects add-iam-policy-binding.
# Use the token printed by the library
export ACCESS_TOKEN=$(gcloud auth print-access-token)
curl -H "Authorization: Bearer $ACCESS_TOKEN" https://www.googleapis.com/oauth2/v3/tokeninfo
echo $ACCESS_TOKEN | base64 -d | jq .scope
Ensure the scopes match the requirements of the APIs you call.
Does your Cloud Run service use the default App Engine service account or a custom one? If you’re using a custom service account, confirm that it has the necessary IAM roles; otherwise the library will switch credentials correctly but API calls will fail.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.