@@ -152,6 +152,34 @@ def _get_default_mtls_endpoint(api_endpoint):
152152 _DEFAULT_ENDPOINT_TEMPLATE = "logging.{UNIVERSE_DOMAIN}"
153153 _DEFAULT_UNIVERSE = "googleapis.com"
154154
155+ @staticmethod
156+ def _use_client_cert_effective ():
157+ """Returns whether client certificate should be used for mTLS if the
158+ google-auth version supports should_use_client_cert automatic mTLS enablement.
159+
160+ Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var.
161+
162+ Returns:
163+ bool: whether client certificate should be used for mTLS
164+ Raises:
165+ ValueError: (If using a version of google-auth without should_use_client_cert and
166+ GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.)
167+ """
168+ # check if google-auth version supports should_use_client_cert for automatic mTLS enablement
169+ if hasattr (mtls , "should_use_client_cert" ): # pragma: NO COVER
170+ return mtls .should_use_client_cert ()
171+ else : # pragma: NO COVER
172+ # if unsupported, fallback to reading from env var
173+ use_client_cert_str = os .getenv (
174+ "GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false"
175+ ).lower ()
176+ if use_client_cert_str not in ("true" , "false" ):
177+ raise ValueError (
178+ "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be"
179+ " either `true` or `false`"
180+ )
181+ return use_client_cert_str == "true"
182+
155183 @classmethod
156184 def from_service_account_info (cls , info : dict , * args , ** kwargs ):
157185 """Creates an instance of this client using the provided credentials
@@ -334,20 +362,16 @@ def get_mtls_endpoint_and_cert_source(
334362 )
335363 if client_options is None :
336364 client_options = client_options_lib .ClientOptions ()
337- use_client_cert = os . getenv ( "GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" )
365+ use_client_cert = LoggingServiceV2Client . _use_client_cert_effective ( )
338366 use_mtls_endpoint = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
339- if use_client_cert not in ("true" , "false" ):
340- raise ValueError (
341- "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
342- )
343367 if use_mtls_endpoint not in ("auto" , "never" , "always" ):
344368 raise MutualTLSChannelError (
345369 "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
346370 )
347371
348372 # Figure out the client cert source to use.
349373 client_cert_source = None
350- if use_client_cert == "true" :
374+ if use_client_cert :
351375 if client_options .client_cert_source :
352376 client_cert_source = client_options .client_cert_source
353377 elif mtls .has_default_client_cert_source ():
@@ -379,20 +403,14 @@ def _read_environment_variables():
379403 google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
380404 is not any of ["auto", "never", "always"].
381405 """
382- use_client_cert = os .getenv (
383- "GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false"
384- ).lower ()
406+ use_client_cert = LoggingServiceV2Client ._use_client_cert_effective ()
385407 use_mtls_endpoint = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" ).lower ()
386408 universe_domain_env = os .getenv ("GOOGLE_CLOUD_UNIVERSE_DOMAIN" )
387- if use_client_cert not in ("true" , "false" ):
388- raise ValueError (
389- "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
390- )
391409 if use_mtls_endpoint not in ("auto" , "never" , "always" ):
392410 raise MutualTLSChannelError (
393411 "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
394412 )
395- return use_client_cert == "true" , use_mtls_endpoint , universe_domain_env
413+ return use_client_cert , use_mtls_endpoint , universe_domain_env
396414
397415 @staticmethod
398416 def _get_client_cert_source (provided_cert_source , use_cert_flag ):
0 commit comments