2626from typing import cast , Generator , List , Optional , Tuple , Union
2727
2828from google .auth import _agent_identity_utils
29+ from google .auth import _cloud_sdk
2930from google .auth import environment_vars
3031from google .auth import exceptions
3132
5152_LOGGER = logging .getLogger (__name__ )
5253
5354
54- # A flag to track if we have already logged a warning about mTLS auto-enablement failures.
55- # This prevents log spam when client libraries create transports or session instances
56- # frequently within a single process.
57- _has_logged_mtls_warning = False
58-
59-
6055_PASSPHRASE_REGEX = re .compile (
6156 b"-----BEGIN PASSPHRASE-----(.+)-----END PASSPHRASE-----" , re .DOTALL
6257)
6358
64- # Temporary patch to accomodate incorrect cert config in Cloud Run prod environment.
65- _WELL_KNOWN_CLOUD_RUN_CERT_PATH = (
66- "/var/run/secrets/workload-spiffe-credentials/certificates.pem"
67- )
68- _WELL_KNOWN_CLOUD_RUN_KEY_PATH = (
69- "/var/run/secrets/workload-spiffe-credentials/private_key.pem"
70- )
71- _INCORRECT_CLOUD_RUN_CERT_PATH = (
72- "/var/lib/volumes/certificate/workload-certificates/certificates.pem"
73- )
74- _INCORRECT_CLOUD_RUN_KEY_PATH = (
75- "/var/lib/volumes/certificate/workload-certificates/private_key.pem"
76- )
77-
7859
7960class _MemfdCreationError (OSError ):
8061 """Raised when Linux in-memory virtual file creation (memfd) fails."""
@@ -436,22 +417,37 @@ def _get_cert_config_path(certificate_config_path=None, include_context_aware=Tr
436417 The absolute path of the certificate config file, and None if the file does not exist.
437418 """
438419
420+ source = "function argument"
421+ is_explicit = True
439422 if certificate_config_path is None :
440423 env_path = environ .get (environment_vars .GOOGLE_API_CERTIFICATE_CONFIG , None )
441424 if env_path is not None and env_path != "" :
442425 certificate_config_path = env_path
426+ source = (
427+ f"environment variable { environment_vars .GOOGLE_API_CERTIFICATE_CONFIG } "
428+ )
443429 else :
444430 env_path = environ .get (
445431 environment_vars .CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH ,
446432 None ,
447433 )
448434 if include_context_aware and env_path is not None and env_path != "" :
449435 certificate_config_path = env_path
436+ source = f"environment variable { environment_vars .CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH } "
450437 else :
451- certificate_config_path = CERTIFICATE_CONFIGURATION_DEFAULT_PATH
438+ certificate_config_path = os .path .join (
439+ _cloud_sdk .get_config_path (), "certificate_config.json"
440+ )
441+ is_explicit = False
452442
453443 certificate_config_path = path .expanduser (certificate_config_path )
454444 if not path .exists (certificate_config_path ):
445+ if is_explicit :
446+ _LOGGER .debug (
447+ "Certificate configuration file explicitly specified via %s at %s does not exist" ,
448+ source ,
449+ certificate_config_path ,
450+ )
455451 return None
456452 return certificate_config_path
457453
@@ -489,25 +485,6 @@ def _get_workload_cert_and_key_paths(config_path, include_context_aware=True):
489485 cert_path = workload ["cert_path" ]
490486 key_path = workload ["key_path" ]
491487
492- # == BEGIN Temporary Cloud Run PATCH ==
493- # See https://github.com/googleapis/google-auth-library-python/issues/1881
494- if (cert_path == _INCORRECT_CLOUD_RUN_CERT_PATH ) and (
495- key_path == _INCORRECT_CLOUD_RUN_KEY_PATH
496- ):
497- if not path .exists (cert_path ) and not path .exists (key_path ):
498- _LOGGER .debug (
499- "Applying Cloud Run certificate path patch. "
500- "Configured paths not found: %s, %s. "
501- "Using well-known paths: %s, %s" ,
502- cert_path ,
503- key_path ,
504- _WELL_KNOWN_CLOUD_RUN_CERT_PATH ,
505- _WELL_KNOWN_CLOUD_RUN_KEY_PATH ,
506- )
507- cert_path = _WELL_KNOWN_CLOUD_RUN_CERT_PATH
508- key_path = _WELL_KNOWN_CLOUD_RUN_KEY_PATH
509- # == END Temporary Cloud Run PATCH ==
510-
511488 return cert_path , key_path
512489
513490
@@ -755,36 +732,33 @@ def check_use_client_cert():
755732 will default to False.
756733 If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred
757734 as True (auto-enabled) if a workload config file exists (pointed at by
758- GOOGLE_API_CERTIFICATE_CONFIG) containing a "workload" section.
735+ GOOGLE_API_CERTIFICATE_CONFIG or CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH,
736+ or the default path like ~/.config/gcloud/certificate_config.json)
737+ containing a "workload" section.
759738 Otherwise, it returns False.
760739
761740 Returns:
762741 bool: Whether the client certificate should be used for mTLS connection.
763742 """
764- global _has_logged_mtls_warning
765743 env_override = _check_use_client_cert_env ()
766744 if env_override is not None :
767745 return env_override
768746
769747 # Auto-enablement checks (when GOOGLE_API_USE_CLIENT_CERTIFICATE is not set)
770748
771- # Check if the value of GOOGLE_API_CERTIFICATE_CONFIG is set.
772- cert_path = getenv (environment_vars .GOOGLE_API_CERTIFICATE_CONFIG ) or getenv (
773- environment_vars .CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH
774- )
749+ # Check if a workload config file exists.
750+ cert_path = _get_cert_config_path (include_context_aware = True )
775751
776752 if cert_path :
777753 try :
778754 with open (cert_path , "r" ) as f :
779755 content = json .load (f )
780756 except (FileNotFoundError , OSError , json .JSONDecodeError ) as e :
781- if not _has_logged_mtls_warning :
782- _LOGGER .warning (
783- "mTLS auto-enablement failed: Could not read/parse certificate file at %s. Error: %s" ,
784- cert_path ,
785- e ,
786- )
787- _has_logged_mtls_warning = True
757+ _LOGGER .debug (
758+ "mTLS auto-enablement failed: Could not read/parse certificate file at %s. Error: %s" ,
759+ cert_path ,
760+ e ,
761+ )
788762 return False
789763
790764 # Structural validation
@@ -794,12 +768,10 @@ def check_use_client_cert():
794768 return True
795769
796770 # If we got here, the file exists but the expected structure is missing
797- if not _has_logged_mtls_warning :
798- _LOGGER .warning (
799- "mTLS auto-enablement failed: Certificate configuration file at %s is missing the required ['cert_configs']['workload'] section." ,
800- cert_path ,
801- )
802- _has_logged_mtls_warning = True
771+ _LOGGER .debug (
772+ "mTLS auto-enablement failed: Certificate configuration file at %s is missing the required ['cert_configs']['workload'] section." ,
773+ cert_path ,
774+ )
803775 return False
804776
805777
0 commit comments