google-auth 2.60.0__tar.gz → 2.61.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {google_auth-2.60.0/google_auth.egg-info → google_auth-2.61.0}/PKG-INFO +1 -1
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_agent_identity_utils.py +105 -53
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/transport/sessions.py +3 -13
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/compute_engine/_metadata.py +65 -16
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/compute_engine/credentials.py +71 -52
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/environment_vars.py +13 -1
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/identity_pool.py +5 -4
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/_mtls_helper.py +108 -31
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/mtls.py +15 -4
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/version.py +1 -1
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/_id_token_async.py +21 -2
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/id_token.py +38 -4
- {google_auth-2.60.0 → google_auth-2.61.0/google_auth.egg-info}/PKG-INFO +1 -1
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/compute_engine/test__metadata.py +92 -41
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/compute_engine/test_credentials.py +368 -49
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/conftest.py +32 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_id_token.py +47 -10
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_agent_identity_utils.py +442 -94
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_identity_pool.py +32 -11
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/aio/test_sessions_mtls.py +19 -7
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test__mtls_helper.py +335 -8
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test_mtls.py +88 -6
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test_requests.py +22 -9
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test_urllib3.py +21 -8
- {google_auth-2.60.0 → google_auth-2.61.0}/LICENSE +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/MANIFEST.in +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/README.rst +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_cache.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_cloud_sdk.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_credentials_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_credentials_base.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_default.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_default_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_exponential_backoff.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_helpers.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_jwt_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_oauth2client.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_refresh_worker.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_regional_access_boundary_utils.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/_service_account_info.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/_helpers.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/transport/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/transport/aiohttp.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aio/transport/mtls.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/api_key.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/app_engine.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/aws.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/compute_engine/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/compute_engine/_mtls.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/_cryptography_rsa.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/_helpers.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/_python_rsa.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/base.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/es.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/es256.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/crypt/rsa.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/downscoped.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/exceptions.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/external_account.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/external_account_authorized_user.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/iam.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/impersonated_credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/jwt.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/metrics.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/pluggable.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/py.typed +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/_aiohttp_requests.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/_custom_tls_signer.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/_http_client.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/_requests_base.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/grpc.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/requests.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/auth/transport/urllib3.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/_client.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/_client_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/_credentials_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/_reauth_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/_service_account_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/challenges.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/gdch_credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/py.typed +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/reauth.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/service_account.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/sts.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/utils.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/webauthn_handler.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/webauthn_handler_factory.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google/oauth2/webauthn_types.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google_auth.egg-info/SOURCES.txt +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google_auth.egg-info/dependency_links.txt +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google_auth.egg-info/requires.txt +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/google_auth.egg-info/top_level.txt +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/setup.cfg +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/setup.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/aio/test__helpers.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/compute_engine/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/compute_engine/data/smbios_product_name +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/compute_engine/data/smbios_product_name_non_google +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/compute_engine/test__mtls.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/test__cryptography_rsa.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/test__python_rsa.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/test_crypt.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/test_es.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/test_es256.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/crypt/test_rsa.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/authorized_user.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/authorized_user_cloud_sdk.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/authorized_user_cloud_sdk_with_quota_project_id.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/authorized_user_with_rapt_token.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/client_secrets.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/context_aware_metadata.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/enterprise_cert_invalid.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/enterprise_cert_valid.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/enterprise_cert_valid_provider.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es256_privatekey.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es256_public_cert.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es256_publickey.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es256_service_account.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es384_privatekey.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es384_public_cert.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es384_publickey.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/es384_service_account.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/external_account_authorized_user.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/external_account_authorized_user_non_gdu.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/external_subject_token.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/external_subject_token.txt +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/gdch_service_account.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/impersonated_service_account_authorized_user_source.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/impersonated_service_account_external_account_authorized_user_source.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/impersonated_service_account_service_account_source.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/impersonated_service_account_with_quota_project.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/old_oauth_credentials_py3.pickle +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/other_cert.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/pem_from_pkcs12.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/privatekey.p12 +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/privatekey.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/privatekey.pub +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/public_cert.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/service_account.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/service_account_non_gdu.json +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/trust_chain_with_leaf.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/trust_chain_without_leaf.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/data/trust_chain_wrong_order.pem +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test__client.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_challenges.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_gdch_credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_reauth.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_service_account.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_sts.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_utils.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_webauthn_handler.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_webauthn_handler_factory.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/oauth2/test_webauthn_types.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__cache.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__cloud_sdk.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__default.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__exponential_backoff.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__helpers.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__oauth2client.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__refresh_worker.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__regional_access_boundary_utils.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test__service_account_info.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_api_key.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_app_engine.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_aws.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_credentials_async.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_downscoped.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_exceptions.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_external_account.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_external_account_authorized_user.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_iam.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_impersonated_credentials.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_jwt.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_metrics.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_packaging.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/test_pluggable.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/__init__.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/aio/conftest.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/aio/test_aiohttp.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/aio/test_mtls.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/aio/test_sessions.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/aioresponses_compat.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/compliance.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test__custom_tls_signer.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test__http_client.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test_aio_mtls_helper.py +0 -0
- {google_auth-2.60.0 → google_auth-2.61.0}/tests/transport/test_grpc.py +0 -0
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
import base64
|
|
18
18
|
import hashlib
|
|
19
|
+
import json
|
|
19
20
|
import os
|
|
20
21
|
import re
|
|
21
22
|
import stat
|
|
@@ -73,13 +74,32 @@ def _is_certificate_file_ready(path):
|
|
|
73
74
|
return False
|
|
74
75
|
|
|
75
76
|
|
|
77
|
+
def _is_in_well_known_dir(path):
|
|
78
|
+
"""Checks if the given path is inside the well-known Agent Identity directory."""
|
|
79
|
+
if not path:
|
|
80
|
+
return False
|
|
81
|
+
well_known_dir = os.path.dirname(_WELL_KNOWN_CERT_PATH)
|
|
82
|
+
try:
|
|
83
|
+
real_path = os.path.realpath(path)
|
|
84
|
+
real_well_known_dir = os.path.realpath(well_known_dir)
|
|
85
|
+
return (
|
|
86
|
+
os.path.commonpath([real_well_known_dir, real_path]) == real_well_known_dir
|
|
87
|
+
)
|
|
88
|
+
except ValueError:
|
|
89
|
+
return False
|
|
90
|
+
|
|
91
|
+
|
|
76
92
|
def get_agent_identity_certificate_path():
|
|
77
|
-
"""Gets the agent certificate path from the certificate config file.
|
|
93
|
+
"""Gets the agent certificate path from the certificate config file or GKE bundle.
|
|
78
94
|
|
|
79
95
|
The path to the certificate config file is read from the
|
|
80
96
|
GOOGLE_API_CERTIFICATE_CONFIG environment variable. This function
|
|
81
97
|
can optionally trigger polling to handle cases where the environment
|
|
82
98
|
variable is set before the files are available on the filesystem.
|
|
99
|
+
When GOOGLE_API_CERTIFICATE_CONFIG and
|
|
100
|
+
CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH are unset and no
|
|
101
|
+
implicit certificate_config.json is present, it falls back to checking
|
|
102
|
+
the GKE workload credential bundle path without polling.
|
|
83
103
|
|
|
84
104
|
Returns:
|
|
85
105
|
Optional[str]: The path to the agent's certificate file, or None if unavailable.
|
|
@@ -88,9 +108,18 @@ def get_agent_identity_certificate_path():
|
|
|
88
108
|
google.auth.exceptions.RefreshError: If the certificate config file
|
|
89
109
|
or the certificate file cannot be found after retries.
|
|
90
110
|
"""
|
|
111
|
+
from google.auth.transport import _mtls_helper
|
|
112
|
+
|
|
91
113
|
cert_config_path = os.environ.get(environment_vars.GOOGLE_API_CERTIFICATE_CONFIG)
|
|
92
114
|
|
|
93
115
|
if not cert_config_path:
|
|
116
|
+
if (
|
|
117
|
+
not _mtls_helper._has_explicit_cert_config_env()
|
|
118
|
+
and _mtls_helper._should_use_gke_credential_bundle(
|
|
119
|
+
_mtls_helper._get_cert_config_path()
|
|
120
|
+
)
|
|
121
|
+
):
|
|
122
|
+
return _mtls_helper._GKE_CREDENTIAL_BUNDLE_PATH
|
|
94
123
|
return None
|
|
95
124
|
|
|
96
125
|
# We trigger polling only if the config path points to the well-known directory.
|
|
@@ -98,16 +127,7 @@ def get_agent_identity_certificate_path():
|
|
|
98
127
|
# config file and the certificate file may experience a brief startup latency.
|
|
99
128
|
# For all other paths, we return early to avoid introducing unnecessary startup
|
|
100
129
|
# delays.
|
|
101
|
-
|
|
102
|
-
try:
|
|
103
|
-
abs_cert_path = os.path.abspath(cert_config_path)
|
|
104
|
-
abs_well_known_dir = os.path.abspath(well_known_dir)
|
|
105
|
-
should_poll = (
|
|
106
|
-
os.path.commonpath([abs_well_known_dir, abs_cert_path])
|
|
107
|
-
== abs_well_known_dir
|
|
108
|
-
)
|
|
109
|
-
except ValueError:
|
|
110
|
-
should_poll = False
|
|
130
|
+
should_poll = _is_in_well_known_dir(cert_config_path)
|
|
111
131
|
|
|
112
132
|
return _get_cert_path_with_optional_polling(cert_config_path, should_poll)
|
|
113
133
|
|
|
@@ -141,9 +161,9 @@ def _get_cert_path_with_optional_polling(cert_config_path, should_poll):
|
|
|
141
161
|
if _is_certificate_file_ready(cert_path):
|
|
142
162
|
return cert_path
|
|
143
163
|
|
|
144
|
-
# The config was parsed, but the cert file is not ready yet
|
|
145
|
-
if
|
|
146
|
-
|
|
164
|
+
# The config was parsed, but the cert file is not ready yet.
|
|
165
|
+
# Only poll if both the config path and cert path are in the well-known directory.
|
|
166
|
+
if not (should_poll and _is_in_well_known_dir(cert_path)):
|
|
147
167
|
return None
|
|
148
168
|
|
|
149
169
|
if not has_logged_cert_warning:
|
|
@@ -182,7 +202,7 @@ def _get_cert_path_with_optional_polling(cert_config_path, should_poll):
|
|
|
182
202
|
raise exceptions.RefreshError(
|
|
183
203
|
"Certificate config or certificate file not found after multiple retries. "
|
|
184
204
|
f"Token binding protection is failing. You can turn off this protection by setting "
|
|
185
|
-
f"{environment_vars.
|
|
205
|
+
f"{environment_vars.GOOGLE_API_ENABLE_RUNTIME_BOUND_TOKEN} to false "
|
|
186
206
|
"to fall back to unbound tokens."
|
|
187
207
|
)
|
|
188
208
|
|
|
@@ -203,8 +223,6 @@ def _parse_cert_path_from_config(cert_config_path):
|
|
|
203
223
|
KeyError: If the certificate config file does not contain the
|
|
204
224
|
expected structure.
|
|
205
225
|
"""
|
|
206
|
-
import json
|
|
207
|
-
|
|
208
226
|
with open(cert_config_path, "r", encoding="utf-8") as f:
|
|
209
227
|
cert_config = json.load(f)
|
|
210
228
|
|
|
@@ -221,64 +239,105 @@ def _parse_cert_path_from_config(cert_config_path):
|
|
|
221
239
|
return workload_config["cert_path"]
|
|
222
240
|
|
|
223
241
|
|
|
224
|
-
def
|
|
242
|
+
def _is_bound_token_opted_out():
|
|
243
|
+
"""Returns True only if bound tokens are explicitly disabled via env vars."""
|
|
244
|
+
val = os.environ.get(
|
|
245
|
+
environment_vars.GOOGLE_API_ENABLE_RUNTIME_BOUND_TOKEN, ""
|
|
246
|
+
).strip()
|
|
247
|
+
if not val:
|
|
248
|
+
# Fall back to the deprecated env var for backward compatibility
|
|
249
|
+
val = os.environ.get(
|
|
250
|
+
environment_vars.GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES,
|
|
251
|
+
"true",
|
|
252
|
+
).strip()
|
|
253
|
+
return val.lower() == "false"
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def get_agent_identity_certificate_and_bytes():
|
|
225
257
|
"""Gets and parses the agent identity certificate if not opted out.
|
|
226
258
|
|
|
227
259
|
Checks if the user has opted out of certificate-bound tokens. If not,
|
|
228
260
|
it gets the certificate path, reads the file, and parses it.
|
|
229
261
|
|
|
230
262
|
Returns:
|
|
231
|
-
|
|
263
|
+
Tuple[Optional[cryptography.x509.Certificate], Optional[bytes]]: A tuple
|
|
264
|
+
of (parsed certificate object, certificate bytes) if found and not
|
|
265
|
+
opted out, otherwise (None, None).
|
|
232
266
|
"""
|
|
267
|
+
from google.auth.transport import _mtls_helper
|
|
268
|
+
|
|
233
269
|
# If the user has opted out of cert bound tokens, there is no need to
|
|
234
270
|
# look up the certificate.
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
environment_vars.GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES,
|
|
238
|
-
"true",
|
|
239
|
-
).lower()
|
|
240
|
-
== "false"
|
|
241
|
-
)
|
|
242
|
-
if is_opted_out:
|
|
243
|
-
return None
|
|
271
|
+
if _is_bound_token_opted_out():
|
|
272
|
+
return None, None
|
|
244
273
|
|
|
245
274
|
# Respect explicit opt-out of mTLS / client certs
|
|
246
|
-
from google.auth.transport import _mtls_helper
|
|
247
|
-
|
|
248
275
|
env_override = _mtls_helper._check_use_client_cert_env()
|
|
249
276
|
if env_override is False:
|
|
250
|
-
return None
|
|
277
|
+
return None, None
|
|
251
278
|
|
|
252
279
|
cert_path = get_agent_identity_certificate_path()
|
|
253
280
|
if not cert_path:
|
|
254
|
-
return None
|
|
281
|
+
return None, None
|
|
255
282
|
|
|
256
283
|
try:
|
|
257
284
|
with open(cert_path, "rb") as cert_file:
|
|
258
|
-
|
|
259
|
-
except
|
|
285
|
+
raw_bytes = cert_file.read()
|
|
286
|
+
except OSError as e:
|
|
260
287
|
warnings.warn(
|
|
261
288
|
f"Failed to read agent identity certificate file at {cert_path}: {e}. "
|
|
262
289
|
"Token binding protection cannot be enabled. Falling back to unbound tokens."
|
|
263
290
|
)
|
|
264
|
-
return None
|
|
291
|
+
return None, None
|
|
265
292
|
|
|
266
|
-
|
|
293
|
+
cert_blocks = _mtls_helper._CERT_REGEX.findall(raw_bytes)
|
|
294
|
+
if not cert_blocks:
|
|
295
|
+
warnings.warn(
|
|
296
|
+
f"No PEM certificate blocks found in {cert_path}. "
|
|
297
|
+
"Token binding protection cannot be enabled. Falling back to unbound tokens."
|
|
298
|
+
)
|
|
299
|
+
return None, None
|
|
300
|
+
|
|
301
|
+
cert_bytes = b"\n".join(block.strip() for block in cert_blocks) + b"\n"
|
|
302
|
+
try:
|
|
303
|
+
return parse_certificate(raw_bytes), cert_bytes
|
|
304
|
+
except (ValueError, ImportError) as e:
|
|
305
|
+
warnings.warn(
|
|
306
|
+
f"Failed to parse agent identity certificate at {cert_path}: {e}. "
|
|
307
|
+
"Token binding protection cannot be enabled. Falling back to unbound tokens."
|
|
308
|
+
)
|
|
309
|
+
return None, None
|
|
267
310
|
|
|
268
311
|
|
|
269
312
|
def parse_certificate(cert_bytes):
|
|
270
|
-
"""Parses a PEM-encoded certificate.
|
|
313
|
+
"""Parses a PEM-encoded certificate or certificate chain and returns the leaf certificate.
|
|
271
314
|
|
|
272
315
|
Args:
|
|
273
316
|
cert_bytes (bytes): The PEM-encoded certificate bytes.
|
|
274
317
|
|
|
275
318
|
Returns:
|
|
276
|
-
cryptography.x509.Certificate: The parsed certificate object.
|
|
319
|
+
cryptography.x509.Certificate: The leaf (first) parsed certificate object.
|
|
320
|
+
|
|
321
|
+
Raises:
|
|
322
|
+
ValueError: If no certificates are found or any certificate in the chain
|
|
323
|
+
is malformed.
|
|
324
|
+
ImportError: If the cryptography library is not installed.
|
|
277
325
|
"""
|
|
326
|
+
from google.auth.transport import _mtls_helper
|
|
327
|
+
|
|
328
|
+
if not cert_bytes:
|
|
329
|
+
raise ValueError("Certificate bytes cannot be empty or None.")
|
|
330
|
+
|
|
278
331
|
try:
|
|
279
332
|
from cryptography import x509
|
|
280
333
|
|
|
281
|
-
|
|
334
|
+
cert_blocks = _mtls_helper._CERT_REGEX.findall(cert_bytes)
|
|
335
|
+
if not cert_blocks:
|
|
336
|
+
return x509.load_pem_x509_certificate(cert_bytes)
|
|
337
|
+
if _mtls_helper._has_unmatched_pem_markers(cert_bytes, cert_blocks):
|
|
338
|
+
raise ValueError("Malformed or truncated PEM certificate chain.")
|
|
339
|
+
certs = [x509.load_pem_x509_certificate(block) for block in cert_blocks]
|
|
340
|
+
return certs[0]
|
|
282
341
|
except ImportError as e:
|
|
283
342
|
raise ImportError(CRYPTOGRAPHY_NOT_FOUND_ERROR) from e
|
|
284
343
|
|
|
@@ -350,8 +409,9 @@ def calculate_certificate_fingerprint(cert):
|
|
|
350
409
|
def should_request_bound_token(cert):
|
|
351
410
|
"""Determines if a bound token should be requested.
|
|
352
411
|
|
|
353
|
-
This is based on the
|
|
354
|
-
|
|
412
|
+
This is based on the GOOGLE_API_ENABLE_RUNTIME_BOUND_TOKEN env var
|
|
413
|
+
(falls back to the deprecated GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES
|
|
414
|
+
if unset or empty) and whether the certificate is an agent identity cert.
|
|
355
415
|
|
|
356
416
|
Args:
|
|
357
417
|
cert (cryptography.x509.Certificate): The parsed certificate object.
|
|
@@ -359,25 +419,17 @@ def should_request_bound_token(cert):
|
|
|
359
419
|
Returns:
|
|
360
420
|
bool: True if a bound token should be requested, False otherwise.
|
|
361
421
|
"""
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
environment_vars.GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES,
|
|
366
|
-
"true",
|
|
367
|
-
).lower()
|
|
368
|
-
== "true"
|
|
369
|
-
)
|
|
370
|
-
if not (is_agent_cert and is_opted_in):
|
|
422
|
+
from google.auth.transport import _mtls_helper
|
|
423
|
+
|
|
424
|
+
if _is_bound_token_opted_out():
|
|
371
425
|
return False
|
|
372
426
|
|
|
373
427
|
# Respect explicit opt-out of mTLS / client certs
|
|
374
|
-
from google.auth.transport import _mtls_helper
|
|
375
|
-
|
|
376
428
|
env_override = _mtls_helper._check_use_client_cert_env()
|
|
377
429
|
if env_override is False:
|
|
378
430
|
return False
|
|
379
431
|
|
|
380
|
-
return
|
|
432
|
+
return _is_agent_identity_certificate(cert)
|
|
381
433
|
|
|
382
434
|
|
|
383
435
|
def get_cached_cert_fingerprint(cached_cert):
|
|
@@ -19,7 +19,6 @@ import http.client as http_client
|
|
|
19
19
|
import inspect
|
|
20
20
|
import logging
|
|
21
21
|
import time
|
|
22
|
-
import urllib.parse
|
|
23
22
|
import warnings
|
|
24
23
|
from contextlib import asynccontextmanager
|
|
25
24
|
from typing import TYPE_CHECKING, Mapping, Optional, Union
|
|
@@ -44,11 +43,6 @@ else:
|
|
|
44
43
|
ClientTimeout = None
|
|
45
44
|
|
|
46
45
|
_LOGGER = logging.getLogger(__name__)
|
|
47
|
-
_MTLS_URL_PREFIXES = [
|
|
48
|
-
"mtls.googleapis.com",
|
|
49
|
-
"mtls.sandbox.googleapis.com",
|
|
50
|
-
"p.googleapis.com",
|
|
51
|
-
]
|
|
52
46
|
|
|
53
47
|
# Tracks the internal aiohttp installation and usage
|
|
54
48
|
try:
|
|
@@ -482,13 +476,9 @@ class AsyncAuthorizedSession:
|
|
|
482
476
|
async def _recover_auth_state():
|
|
483
477
|
is_mtls_endpoint = False
|
|
484
478
|
if self._is_mtls:
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
hostname == prefix
|
|
489
|
-
or hostname.endswith("." + prefix)
|
|
490
|
-
for prefix in _MTLS_URL_PREFIXES
|
|
491
|
-
)
|
|
479
|
+
is_mtls_endpoint = (
|
|
480
|
+
google.auth.transport._mtls_helper.is_mtls_endpoint(url)
|
|
481
|
+
)
|
|
492
482
|
# Snapshot the stale certificate state BEFORE acquiring the lock.
|
|
493
483
|
# This represents the cert that caused the 401 rejection.
|
|
494
484
|
if is_mtls_endpoint:
|
|
@@ -27,7 +27,14 @@ from urllib.parse import urljoin
|
|
|
27
27
|
|
|
28
28
|
import requests
|
|
29
29
|
|
|
30
|
-
from google.auth import
|
|
30
|
+
from google.auth import (
|
|
31
|
+
_agent_identity_utils,
|
|
32
|
+
_helpers,
|
|
33
|
+
environment_vars,
|
|
34
|
+
exceptions,
|
|
35
|
+
metrics,
|
|
36
|
+
transport,
|
|
37
|
+
)
|
|
31
38
|
from google.auth._exponential_backoff import ExponentialBackoff
|
|
32
39
|
from google.auth.compute_engine import _mtls
|
|
33
40
|
|
|
@@ -250,6 +257,8 @@ def get(
|
|
|
250
257
|
headers=None,
|
|
251
258
|
return_none_for_not_found_error=False,
|
|
252
259
|
timeout=_METADATA_DEFAULT_TIMEOUT,
|
|
260
|
+
method="GET",
|
|
261
|
+
body=None,
|
|
253
262
|
):
|
|
254
263
|
"""Fetch a resource from the metadata server.
|
|
255
264
|
|
|
@@ -271,6 +280,8 @@ def get(
|
|
|
271
280
|
return_none_for_not_found_error (Optional[bool]): If True, returns None
|
|
272
281
|
for 404 error instead of throwing an exception.
|
|
273
282
|
timeout (int): How long to wait, in seconds for the metadata server to respond.
|
|
283
|
+
method (str): The HTTP method to use for the request. Defaults to "GET".
|
|
284
|
+
body (Optional[bytes]): The HTTP request body payload to send. Defaults to None.
|
|
274
285
|
|
|
275
286
|
Returns:
|
|
276
287
|
Union[Mapping, str]: If the metadata server returns JSON, a mapping of
|
|
@@ -283,8 +294,12 @@ def get(
|
|
|
283
294
|
google.auth.exceptions.MutualTLSChannelError: if using mtls and the environment
|
|
284
295
|
configuration is invalid for mTLS (for example, the metadata host
|
|
285
296
|
has been overridden in strict mTLS mode).
|
|
297
|
+
ValueError: if a request body is specified with the GET method.
|
|
286
298
|
|
|
287
299
|
"""
|
|
300
|
+
if body is not None and method.upper() == "GET":
|
|
301
|
+
raise ValueError("Request body cannot be specified with GET method.")
|
|
302
|
+
|
|
288
303
|
use_mtls = _mtls.should_use_mds_mtls()
|
|
289
304
|
# Prepare the request object for mTLS if needed.
|
|
290
305
|
# This will create a new request object with the mTLS session.
|
|
@@ -314,9 +329,15 @@ def get(
|
|
|
314
329
|
last_exception = None
|
|
315
330
|
for attempt in backoff:
|
|
316
331
|
try:
|
|
317
|
-
|
|
318
|
-
url
|
|
319
|
-
|
|
332
|
+
kwargs = {
|
|
333
|
+
"url": url,
|
|
334
|
+
"method": method,
|
|
335
|
+
"headers": headers_to_use,
|
|
336
|
+
"timeout": timeout,
|
|
337
|
+
}
|
|
338
|
+
if body is not None:
|
|
339
|
+
kwargs["body"] = body
|
|
340
|
+
response = request(**kwargs)
|
|
320
341
|
if response.status in transport.DEFAULT_RETRYABLE_STATUS_CODES:
|
|
321
342
|
_LOGGER.warning(
|
|
322
343
|
"Compute Engine Metadata server unavailable on "
|
|
@@ -460,6 +481,40 @@ def get_service_account_info(request, service_account="default"):
|
|
|
460
481
|
return get(request, path, params={"recursive": "true"})
|
|
461
482
|
|
|
462
483
|
|
|
484
|
+
def _build_token_request_options(metrics_header_value, bind_token=True):
|
|
485
|
+
"""Returns (method, body, headers) for a metadata server token request.
|
|
486
|
+
|
|
487
|
+
Defaults to a standard GET request with the x-goog-api-client metrics header.
|
|
488
|
+
If ``bind_token`` is ``True``, upgrades to a POST request with a JSON
|
|
489
|
+
certificate_chain body and Content-Type header if an Agent Identity
|
|
490
|
+
certificate is present and bound tokens are enabled.
|
|
491
|
+
|
|
492
|
+
Args:
|
|
493
|
+
metrics_header_value (str): Value for the x-goog-api-client header.
|
|
494
|
+
bind_token (bool): Whether to bind the token to the Agent Identity
|
|
495
|
+
certificate. Defaults to ``True``. If ``False``, returns a ``GET``
|
|
496
|
+
request without reading the certificate. If ``True``, requests a
|
|
497
|
+
bound token via ``POST`` when a valid Agent Identity certificate is
|
|
498
|
+
present and token binding is enabled.
|
|
499
|
+
|
|
500
|
+
Returns:
|
|
501
|
+
Tuple[str, Optional[bytes], Mapping[str, str]]: A tuple of
|
|
502
|
+
(HTTP method, request body bytes, request headers).
|
|
503
|
+
"""
|
|
504
|
+
headers = {metrics.API_CLIENT_HEADER: metrics_header_value}
|
|
505
|
+
if not bind_token:
|
|
506
|
+
return "GET", None, headers
|
|
507
|
+
|
|
508
|
+
cert, cert_bytes = _agent_identity_utils.get_agent_identity_certificate_and_bytes()
|
|
509
|
+
if cert and _agent_identity_utils.should_request_bound_token(cert):
|
|
510
|
+
headers["Content-Type"] = "application/json"
|
|
511
|
+
body = json.dumps({"certificate_chain": cert_bytes.decode("utf-8")}).encode(
|
|
512
|
+
"utf-8"
|
|
513
|
+
)
|
|
514
|
+
return "POST", body, headers
|
|
515
|
+
return "GET", None, headers
|
|
516
|
+
|
|
517
|
+
|
|
463
518
|
def get_service_account_token(request, service_account="default", scopes=None):
|
|
464
519
|
"""Get the OAuth 2.0 access token for a service account.
|
|
465
520
|
|
|
@@ -478,26 +533,20 @@ def get_service_account_token(request, service_account="default", scopes=None):
|
|
|
478
533
|
google.auth.exceptions.TransportError: if an error occurred while
|
|
479
534
|
retrieving metadata.
|
|
480
535
|
"""
|
|
481
|
-
from google.auth import _agent_identity_utils
|
|
482
|
-
|
|
483
536
|
params = {}
|
|
484
537
|
if scopes:
|
|
485
538
|
if not isinstance(scopes, str):
|
|
486
539
|
scopes = ",".join(scopes)
|
|
487
540
|
params["scopes"] = scopes
|
|
488
541
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
fingerprint = _agent_identity_utils.calculate_certificate_fingerprint(cert)
|
|
493
|
-
params["bindCertificateFingerprint"] = fingerprint
|
|
494
|
-
|
|
495
|
-
metrics_header = {
|
|
496
|
-
metrics.API_CLIENT_HEADER: metrics.token_request_access_token_mds()
|
|
497
|
-
}
|
|
542
|
+
method, body, headers = _build_token_request_options(
|
|
543
|
+
metrics.token_request_access_token_mds()
|
|
544
|
+
)
|
|
498
545
|
|
|
499
546
|
path = "instance/service-accounts/{0}/token".format(service_account)
|
|
500
|
-
token_json = get(
|
|
547
|
+
token_json = get(
|
|
548
|
+
request, path, params=params, headers=headers, method=method, body=body
|
|
549
|
+
)
|
|
501
550
|
token_expiry = _helpers.utcnow() + datetime.timedelta(
|
|
502
551
|
seconds=token_json["expires_in"]
|
|
503
552
|
)
|
|
@@ -21,12 +21,14 @@ Compute Engine using the Compute Engine metadata server.
|
|
|
21
21
|
|
|
22
22
|
import datetime
|
|
23
23
|
import logging
|
|
24
|
+
import os
|
|
24
25
|
from typing import TYPE_CHECKING, Optional
|
|
25
26
|
|
|
26
27
|
from google.auth import (
|
|
27
28
|
_helpers,
|
|
28
29
|
_regional_access_boundary_utils,
|
|
29
30
|
credentials,
|
|
31
|
+
environment_vars,
|
|
30
32
|
exceptions,
|
|
31
33
|
iam,
|
|
32
34
|
jwt,
|
|
@@ -340,6 +342,7 @@ class IDTokenCredentials(
|
|
|
340
342
|
signer=None,
|
|
341
343
|
use_metadata_identity_endpoint=False,
|
|
342
344
|
quota_project_id=None,
|
|
345
|
+
bind_id_token=None,
|
|
343
346
|
):
|
|
344
347
|
"""
|
|
345
348
|
Args:
|
|
@@ -364,18 +367,34 @@ class IDTokenCredentials(
|
|
|
364
367
|
otherwise ValueError will be raised.
|
|
365
368
|
quota_project_id (Optional[str]): The project ID used for quota and
|
|
366
369
|
billing.
|
|
370
|
+
bind_id_token (Optional[bool]): Controls whether to request a
|
|
371
|
+
certificate-bound ID token. Can only be set when
|
|
372
|
+
``use_metadata_identity_endpoint`` is ``True``.
|
|
373
|
+
If ``True``, requests a bound token whenever a valid Agent
|
|
374
|
+
Identity certificate is available and token binding is not
|
|
375
|
+
disabled via ``GOOGLE_API_ENABLE_RUNTIME_BOUND_TOKEN``, falling
|
|
376
|
+
back to an unbound token otherwise (or raising
|
|
377
|
+
:class:`~google.auth.exceptions.RefreshError` if a configured
|
|
378
|
+
certificate is not found after retries). If ``False``, always
|
|
379
|
+
requests an unbound token. If ``None`` (default), token binding
|
|
380
|
+
is determined automatically by the library. Set ``True`` or
|
|
381
|
+
``False`` explicitly if your application requires a specific
|
|
382
|
+
behavior.
|
|
367
383
|
|
|
368
384
|
Raises:
|
|
369
385
|
ValueError:
|
|
370
386
|
If ``use_metadata_identity_endpoint`` is set to True, and one of
|
|
371
387
|
``token_uri``, ``additional_claims``, ``service_account_email``,
|
|
372
|
-
|
|
388
|
+
``signer`` arguments is set, or if
|
|
389
|
+
``use_metadata_identity_endpoint`` is False and
|
|
390
|
+
``bind_id_token`` is not None.
|
|
373
391
|
"""
|
|
374
392
|
super(IDTokenCredentials, self).__init__()
|
|
375
393
|
|
|
376
394
|
self._quota_project_id = quota_project_id
|
|
377
395
|
self._use_metadata_identity_endpoint = use_metadata_identity_endpoint
|
|
378
396
|
self._target_audience = target_audience
|
|
397
|
+
self._bind_id_token = bind_id_token
|
|
379
398
|
|
|
380
399
|
if use_metadata_identity_endpoint:
|
|
381
400
|
if token_uri or additional_claims or service_account_email or signer:
|
|
@@ -387,8 +406,13 @@ class IDTokenCredentials(
|
|
|
387
406
|
self._token_uri = None
|
|
388
407
|
self._additional_claims = None
|
|
389
408
|
self._signer = None
|
|
409
|
+
elif bind_id_token is not None:
|
|
410
|
+
raise ValueError(
|
|
411
|
+
"If use_metadata_identity_endpoint is False, bind_id_token "
|
|
412
|
+
"must not be set"
|
|
413
|
+
)
|
|
390
414
|
|
|
391
|
-
if service_account_email is None:
|
|
415
|
+
if service_account_email is None and request is not None:
|
|
392
416
|
sa_info = _metadata.get_service_account_info(request)
|
|
393
417
|
self._service_account_email = sa_info["email"]
|
|
394
418
|
else:
|
|
@@ -409,79 +433,60 @@ class IDTokenCredentials(
|
|
|
409
433
|
else:
|
|
410
434
|
self._additional_claims = {}
|
|
411
435
|
|
|
412
|
-
def
|
|
413
|
-
"""Create a copy of these credentials with the specified target
|
|
414
|
-
audience.
|
|
415
|
-
Args:
|
|
416
|
-
target_audience (str): The intended audience for these credentials,
|
|
417
|
-
used when requesting the ID Token.
|
|
418
|
-
Returns:
|
|
419
|
-
google.auth.service_account.IDTokenCredentials: A new credentials
|
|
420
|
-
instance.
|
|
421
|
-
"""
|
|
436
|
+
def _make_copy(self):
|
|
422
437
|
# since the signer is already instantiated,
|
|
423
438
|
# the request is not needed
|
|
424
439
|
if self._use_metadata_identity_endpoint:
|
|
425
|
-
|
|
440
|
+
cred = self.__class__(
|
|
426
441
|
None,
|
|
427
|
-
target_audience=
|
|
442
|
+
target_audience=self._target_audience,
|
|
428
443
|
use_metadata_identity_endpoint=True,
|
|
429
444
|
quota_project_id=self._quota_project_id,
|
|
430
445
|
)
|
|
446
|
+
cred._service_account_email = self._service_account_email
|
|
447
|
+
cred._bind_id_token = getattr(self, "_bind_id_token", None)
|
|
448
|
+
return cred
|
|
431
449
|
else:
|
|
432
450
|
return self.__class__(
|
|
433
451
|
None,
|
|
434
452
|
service_account_email=self._service_account_email,
|
|
435
453
|
token_uri=self._token_uri,
|
|
436
|
-
target_audience=
|
|
454
|
+
target_audience=self._target_audience,
|
|
437
455
|
additional_claims=self._additional_claims.copy(),
|
|
438
456
|
signer=self.signer,
|
|
439
457
|
use_metadata_identity_endpoint=False,
|
|
440
458
|
quota_project_id=self._quota_project_id,
|
|
441
459
|
)
|
|
442
460
|
|
|
461
|
+
def with_target_audience(self, target_audience):
|
|
462
|
+
"""Create a copy of these credentials with the specified target
|
|
463
|
+
audience.
|
|
464
|
+
Args:
|
|
465
|
+
target_audience (str): The intended audience for these credentials,
|
|
466
|
+
used when requesting the ID Token.
|
|
467
|
+
Returns:
|
|
468
|
+
google.auth.service_account.IDTokenCredentials: A new credentials
|
|
469
|
+
instance.
|
|
470
|
+
"""
|
|
471
|
+
cred = self._make_copy()
|
|
472
|
+
cred._target_audience = target_audience
|
|
473
|
+
return cred
|
|
474
|
+
|
|
443
475
|
@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
|
|
444
476
|
def with_quota_project(self, quota_project_id):
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
return self.__class__(
|
|
449
|
-
None,
|
|
450
|
-
target_audience=self._target_audience,
|
|
451
|
-
use_metadata_identity_endpoint=True,
|
|
452
|
-
quota_project_id=quota_project_id,
|
|
453
|
-
)
|
|
454
|
-
else:
|
|
455
|
-
return self.__class__(
|
|
456
|
-
None,
|
|
457
|
-
service_account_email=self._service_account_email,
|
|
458
|
-
token_uri=self._token_uri,
|
|
459
|
-
target_audience=self._target_audience,
|
|
460
|
-
additional_claims=self._additional_claims.copy(),
|
|
461
|
-
signer=self.signer,
|
|
462
|
-
use_metadata_identity_endpoint=False,
|
|
463
|
-
quota_project_id=quota_project_id,
|
|
464
|
-
)
|
|
477
|
+
cred = self._make_copy()
|
|
478
|
+
cred._quota_project_id = quota_project_id
|
|
479
|
+
return cred
|
|
465
480
|
|
|
466
481
|
@_helpers.copy_docstring(credentials.CredentialsWithTokenUri)
|
|
467
482
|
def with_token_uri(self, token_uri):
|
|
468
|
-
# since the signer is already instantiated,
|
|
469
|
-
# the request is not needed
|
|
470
483
|
if self._use_metadata_identity_endpoint:
|
|
471
484
|
raise ValueError(
|
|
472
485
|
"If use_metadata_identity_endpoint is set, token_uri must not be set"
|
|
473
486
|
)
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
service_account_email=self._service_account_email,
|
|
478
|
-
token_uri=token_uri,
|
|
479
|
-
target_audience=self._target_audience,
|
|
480
|
-
additional_claims=self._additional_claims.copy(),
|
|
481
|
-
signer=self.signer,
|
|
482
|
-
use_metadata_identity_endpoint=False,
|
|
483
|
-
quota_project_id=self.quota_project_id,
|
|
484
|
-
)
|
|
487
|
+
cred = self._make_copy()
|
|
488
|
+
cred._token_uri = token_uri
|
|
489
|
+
return cred
|
|
485
490
|
|
|
486
491
|
def _make_authorization_grant_assertion(self):
|
|
487
492
|
"""Create the OAuth 2.0 assertion.
|
|
@@ -530,11 +535,25 @@ class IDTokenCredentials(
|
|
|
530
535
|
try:
|
|
531
536
|
path = "instance/service-accounts/default/identity"
|
|
532
537
|
params = {"audience": self._target_audience, "format": "full"}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
538
|
+
bind_token = getattr(self, "_bind_id_token", None)
|
|
539
|
+
if bind_token is None:
|
|
540
|
+
# Temporary gate: keep default ID tokens unbound when
|
|
541
|
+
# GOOGLE_API_CERTIFICATE_CONFIG is set (Cloud Run).
|
|
542
|
+
bind_token = not os.environ.get(
|
|
543
|
+
environment_vars.GOOGLE_API_CERTIFICATE_CONFIG
|
|
544
|
+
)
|
|
545
|
+
method, body, headers = _metadata._build_token_request_options(
|
|
546
|
+
metrics.token_request_id_token_mds(),
|
|
547
|
+
bind_token=bind_token,
|
|
548
|
+
)
|
|
549
|
+
|
|
536
550
|
id_token = _metadata.get(
|
|
537
|
-
request,
|
|
551
|
+
request,
|
|
552
|
+
path,
|
|
553
|
+
params=params,
|
|
554
|
+
headers=headers,
|
|
555
|
+
method=method,
|
|
556
|
+
body=body,
|
|
538
557
|
)
|
|
539
558
|
except exceptions.TransportError as caught_exc:
|
|
540
559
|
new_exc = exceptions.RefreshError(caught_exc)
|