| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4c6d549 commit 4e91c54
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,6 +184,34 @@ def _get_default_mtls_endpoint(api_endpoint): | |||
| 184 | 184 | _DEFAULT_ENDPOINT_TEMPLATE = "storage.{UNIVERSE_DOMAIN}" | |
| 185 | 185 | _DEFAULT_UNIVERSE = "googleapis.com" | |
| 186 | 186 | ||
| 187 | + @staticmethod | ||
| 188 | + def _use_client_cert_effective(): | ||
| 189 | + """Returns whether client certificate should be used for mTLS if the | ||
| 190 | + google-auth version supports should_use_client_cert automatic mTLS enablement. | ||
| 191 | + | ||
| 192 | + Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. | ||
| 193 | + | ||
| 194 | + Returns: | ||
| 195 | + bool: whether client certificate should be used for mTLS | ||
| 196 | + Raises: | ||
| 197 | + ValueError: (If using a version of google-auth without should_use_client_cert and | ||
| 198 | + GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.) | ||
| 199 | + """ | ||
| 200 | + # check if google-auth version supports should_use_client_cert for automatic mTLS enablement | ||
| 201 | + if hasattr(mtls, "should_use_client_cert"): | ||
| 202 | + return mtls.should_use_client_cert() | ||
| 203 | + else: | ||
| 204 | + # if unsupported, fallback to reading from env var | ||
| 205 | + use_client_cert_str = os.getenv( | ||
| 206 | + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" | ||
| 207 | + ).lower() | ||
| 208 | + if use_client_cert_str not in ("true", "false"): | ||
| 209 | + raise ValueError( | ||
| 210 | + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" | ||
| 211 | + " either `true` or `false`" | ||
| 212 | + ) | ||
| 213 | + return use_client_cert_str == "true" | ||
| 214 | + | ||
| 187 | 215 | @classmethod | |
| 188 | 216 | def from_service_account_info(cls, info: dict, *args, **kwargs): | |
| 189 | 217 | """Creates an instance of this client using the provided credentials | |
@@ -390,20 +418,16 @@ def get_mtls_endpoint_and_cert_source( | |||
| 390 | 418 | ) | |
| 391 | 419 | if client_options is None: | |
| 392 | 420 | client_options = client_options_lib.ClientOptions() | |
| 393 | - use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") | ||
| 421 | + use_client_cert = StorageClient._use_client_cert_effective() | ||
| 394 | 422 | use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") | |
| 395 | - if use_client_cert not in ("true", "false"): | ||
| 396 | - raise ValueError( | ||
| 397 | - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" | ||
| 398 | - ) | ||
| 399 | 423 | if use_mtls_endpoint not in ("auto", "never", "always"): | |
| 400 | 424 | raise MutualTLSChannelError( | |
| 401 | 425 | "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" | |
| 402 | 426 | ) | |
| 403 | 427 | ||
| 404 | 428 | # Figure out the client cert source to use. | |
| 405 | 429 | client_cert_source = None | |
| 406 | - if use_client_cert == "true": | ||
| 430 | + if use_client_cert: | ||
| 407 | 431 | if client_options.client_cert_source: | |
| 408 | 432 | client_cert_source = client_options.client_cert_source | |
| 409 | 433 | elif mtls.has_default_client_cert_source(): | |
@@ -435,20 +459,14 @@ def _read_environment_variables(): | |||
| 435 | 459 | google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT | |
| 436 | 460 | is not any of ["auto", "never", "always"]. | |
| 437 | 461 | """ | |
| 438 | - use_client_cert = os.getenv( | ||
| 439 | - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" | ||
| 440 | - ).lower() | ||
| 462 | + use_client_cert = StorageClient._use_client_cert_effective() | ||
| 441 | 463 | use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() | |
| 442 | 464 | universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") | |
| 443 | - if use_client_cert not in ("true", "false"): | ||
| 444 | - raise ValueError( | ||
| 445 | - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" | ||
| 446 | - ) | ||
| 447 | 465 | if use_mtls_endpoint not in ("auto", "never", "always"): | |
| 448 | 466 | raise MutualTLSChannelError( | |
| 449 | 467 | "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" | |
| 450 | 468 | ) | |
| 451 | - return use_client_cert == "true", use_mtls_endpoint, universe_domain_env | ||
| 469 | + return use_client_cert, use_mtls_endpoint, universe_domain_env | ||
| 452 | 470 | ||
| 453 | 471 | @staticmethod | |
| 454 | 472 | def _get_client_cert_source(provided_cert_source, use_cert_flag): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,10 +111,6 @@ def _virtual_hosted_style_base_url(url, bucket, trailing_slash=False): | |||
| 111 | 111 | return base_url | |
| 112 | 112 | ||
| 113 | 113 | ||
| 114 | - def _use_client_cert(): | ||
| 115 | - return os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE") == "true" | ||
| 116 | - | ||
| 117 | - | ||
| 118 | 114 | def _get_environ_project(): | |
| 119 | 115 | return os.getenv( | |
| 120 | 116 | environment_vars.PROJECT, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,11 +20,12 @@ | |||
| 20 | 20 | import datetime | |
| 21 | 21 | import functools | |
| 22 | 22 | import json | |
| 23 | + import os | ||
| 23 | 24 | import warnings | |
| 24 | 25 | import google.api_core.client_options | |
| 25 | 26 | ||
| 26 | 27 | from google.auth.credentials import AnonymousCredentials | |
| 27 | - | ||
| 28 | + from google.auth.transport import mtls | ||
| 28 | 29 | from google.api_core import page_iterator | |
| 29 | 30 | from google.cloud._helpers import _LocalStack | |
| 30 | 31 | from google.cloud.client import ClientWithProject | |
@@ -35,7 +36,6 @@ | |||
| 35 | 36 | from google.cloud.storage._helpers import _get_api_endpoint_override | |
| 36 | 37 | from google.cloud.storage._helpers import _get_environ_project | |
| 37 | 38 | from google.cloud.storage._helpers import _get_storage_emulator_override | |
| 38 | - from google.cloud.storage._helpers import _use_client_cert | ||
| 39 | 39 | from google.cloud.storage._helpers import _virtual_hosted_style_base_url | |
| 40 | 40 | from google.cloud.storage._helpers import _DEFAULT_UNIVERSE_DOMAIN | |
| 41 | 41 | from google.cloud.storage._helpers import _DEFAULT_SCHEME | |
@@ -218,7 +218,15 @@ def __init__( | |||
| 218 | 218 | # The final decision of whether to use mTLS takes place in | |
| 219 | 219 | # google-auth-library-python. We peek at the environment variable | |
| 220 | 220 | # here only to issue an exception in case of a conflict. | |
| 221 | - if _use_client_cert(): | ||
| 221 | + use_client_cert = False | ||
| 222 | + if hasattr(mtls, "should_use_client_cert"): | ||
| 223 | + use_client_cert = mtls.should_use_client_cert() | ||
| 224 | + else: | ||
| 225 | + use_client_cert = ( | ||
| 226 | + os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE") == "true" | ||
| 227 | + ) | ||
| 228 | + | ||
| 229 | + if use_client_cert: | ||
| 222 | 230 | raise ValueError( | |
| 223 | 231 | 'The "GOOGLE_API_USE_CLIENT_CERTIFICATE" env variable is ' | |
| 224 | 232 | 'set to "true" and a non-default universe domain is ' | |
| Back | FazBrowse Home | New Git URL |
0 commit comments