|
2 | 2 |
|
3 | 3 | """A compatibility module for older versions of google-api-core.""" |
4 | 4 |
|
| 5 | +import os |
| 6 | +import re |
5 | 7 | import uuid |
| 8 | +from typing import Optional, Callable, Tuple, Union |
| 9 | +from google.auth.exceptions import MutualTLSChannelError |
| 10 | + |
| 11 | +try: |
| 12 | + from google.api_core.universe import ( |
| 13 | + get_default_mtls_endpoint, |
| 14 | + get_api_endpoint, |
| 15 | + get_universe_domain, |
| 16 | + ) |
| 17 | +except ImportError: |
| 18 | + # TODO(https://github.com/googleapis/google-cloud-python/issues/17813): Remove these fallbacks when google-api-core >= 2.18.0 is the minimum required version. |
| 19 | + def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]: |
| 20 | + """Converts api endpoint to mTLS endpoint.""" |
| 21 | + if not api_endpoint: |
| 22 | + return api_endpoint |
| 23 | + |
| 24 | + mtls_endpoint_re = re.compile( |
| 25 | + r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?" |
| 26 | + ) |
| 27 | + |
| 28 | + m = mtls_endpoint_re.match(api_endpoint) |
| 29 | + if m is None: |
| 30 | + # Could not parse api_endpoint; return as-is. |
| 31 | + return api_endpoint |
| 32 | + |
| 33 | + name, mtls, sandbox, googledomain = m.groups() |
| 34 | + if mtls or not googledomain: |
| 35 | + return api_endpoint |
| 36 | + |
| 37 | + if sandbox: |
| 38 | + return api_endpoint.replace( |
| 39 | + "sandbox.googleapis.com", "mtls.sandbox.googleapis.com" |
| 40 | + ) |
| 41 | + |
| 42 | + return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com") |
| 43 | + |
| 44 | + def get_api_endpoint( |
| 45 | + api_override: Optional[str], |
| 46 | + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]], |
| 47 | + universe_domain: str, |
| 48 | + use_mtls_endpoint: str, |
| 49 | + default_universe: str, |
| 50 | + default_mtls_endpoint: Optional[str], |
| 51 | + default_endpoint_template: str, |
| 52 | + ) -> Optional[str]: |
| 53 | + """Return the API endpoint used by the client.""" |
| 54 | + if api_override is not None: |
| 55 | + api_endpoint = api_override |
| 56 | + elif use_mtls_endpoint == "always" or (use_mtls_endpoint == "auto" and client_cert_source): |
| 57 | + if universe_domain != default_universe: |
| 58 | + raise MutualTLSChannelError( |
| 59 | + f"mTLS is not supported in any universe other than {default_universe}." |
| 60 | + ) |
| 61 | + api_endpoint = default_mtls_endpoint |
| 62 | + else: |
| 63 | + api_endpoint = default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain) |
| 64 | + return api_endpoint |
| 65 | + |
| 66 | + def get_universe_domain( |
| 67 | + client_universe_domain: Optional[str], |
| 68 | + universe_domain_env: Optional[str], |
| 69 | + default_universe: str, |
| 70 | + ) -> str: |
| 71 | + """Return the universe domain used by the client.""" |
| 72 | + universe_domain = default_universe |
| 73 | + if client_universe_domain is not None: |
| 74 | + universe_domain = client_universe_domain |
| 75 | + elif universe_domain_env is not None: |
| 76 | + universe_domain = universe_domain_env |
| 77 | + if len(universe_domain.strip()) == 0: |
| 78 | + raise ValueError("Universe Domain cannot be an empty string.") |
| 79 | + return universe_domain |
| 80 | + |
| 81 | + |
| 82 | +try: |
| 83 | + from google.api_core.gapic_v1.config import ( |
| 84 | + use_client_cert_effective, |
| 85 | + get_client_cert_source, |
| 86 | + read_environment_variables, |
| 87 | + ) |
| 88 | +except ImportError: |
| 89 | + from google.auth.transport import mtls # type: ignore |
| 90 | + |
| 91 | + # TODO(https://github.com/googleapis/google-cloud-python/issues/17813): Remove these fallbacks when google-api-core >= 2.18.0 is the minimum required version. |
| 92 | + |
| 93 | + def use_client_cert_effective() -> bool: |
| 94 | + """Returns whether client certificate should be used for mTLS.""" |
| 95 | + if hasattr(mtls, "should_use_client_cert"): |
| 96 | + return mtls.should_use_client_cert() |
| 97 | + else: |
| 98 | + use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower() |
| 99 | + if use_client_cert_str not in ("true", "false"): |
| 100 | + raise ValueError( |
| 101 | + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be" |
| 102 | + " either `true` or `false`" |
| 103 | + ) |
| 104 | + return use_client_cert_str == "true" |
| 105 | + |
| 106 | + def get_client_cert_source( |
| 107 | + provided_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]], |
| 108 | + use_cert_flag: bool, |
| 109 | + ) -> Optional[Callable[[], Tuple[bytes, bytes]]]: |
| 110 | + """Return the client cert source to be used by the client.""" |
| 111 | + client_cert_source = None |
| 112 | + if use_cert_flag: |
| 113 | + if provided_cert_source: |
| 114 | + client_cert_source = provided_cert_source |
| 115 | + elif ( |
| 116 | + hasattr(mtls, "has_default_client_cert_source") |
| 117 | + and mtls.has_default_client_cert_source() |
| 118 | + ): |
| 119 | + client_cert_source = mtls.default_client_cert_source() |
| 120 | + else: |
| 121 | + raise ValueError( |
| 122 | + "Client certificate is required for mTLS, but no client certificate source was provided or found." |
| 123 | + ) |
| 124 | + return client_cert_source |
| 125 | + |
| 126 | + def read_environment_variables() -> Tuple[bool, str, Optional[str]]: |
| 127 | + """Returns the environment variables used by the client.""" |
| 128 | + use_client_cert = use_client_cert_effective() |
| 129 | + use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() |
| 130 | + universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") |
| 131 | + if use_mtls_endpoint not in ("auto", "never", "always"): |
| 132 | + raise MutualTLSChannelError( |
| 133 | + "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` " |
| 134 | + "must be `never`, `auto` or `always`" |
| 135 | + ) |
| 136 | + return use_client_cert, use_mtls_endpoint, universe_domain_env |
| 137 | + |
6 | 138 |
|
7 | 139 | try: |
8 | 140 | from google.api_core.gapic_v1.request import setup_request_id # type: ignore |
@@ -40,4 +172,3 @@ except ImportError: |
40 | 172 | else: |
41 | 173 | if not getattr(request, field_name, None): |
42 | 174 | setattr(request, field_name, request_id_val) |
43 | | - |
|
0 commit comments