|
| 1 | +# {% include '_license.j2' %} |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import Optional, Callable, Tuple, Union |
| 5 | +from google.auth.exceptions import MutualTLSChannelError |
| 6 | + |
| 7 | +try: |
| 8 | + from google.api_core.gapic_v1.client_utils import ( |
| 9 | + get_default_mtls_endpoint, |
| 10 | + get_api_endpoint, |
| 11 | + get_universe_domain, |
| 12 | + ) |
| 13 | +except ImportError: |
| 14 | + # TODO: Remove these fallbacks when google-api-core >= 2.18.0 is the minimum required version. |
| 15 | + |
| 16 | + def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]: |
| 17 | + """Converts api endpoint to mTLS endpoint.""" |
| 18 | + if not api_endpoint: |
| 19 | + return api_endpoint |
| 20 | + |
| 21 | + mtls_endpoint_re = re.compile( |
| 22 | + r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?" |
| 23 | + ) |
| 24 | + |
| 25 | + m = mtls_endpoint_re.match(api_endpoint) |
| 26 | + if m is None: |
| 27 | + # Could not parse api_endpoint; return as-is. |
| 28 | + return api_endpoint |
| 29 | + |
| 30 | + name, mtls, sandbox, googledomain = m.groups() |
| 31 | + if mtls or not googledomain: |
| 32 | + return api_endpoint |
| 33 | + |
| 34 | + if sandbox: |
| 35 | + return api_endpoint.replace( |
| 36 | + "sandbox.googleapis.com", "mtls.sandbox.googleapis.com" |
| 37 | + ) |
| 38 | + |
| 39 | + return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com") |
| 40 | + |
| 41 | + def get_api_endpoint( |
| 42 | + api_override: Optional[str], |
| 43 | + client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]], |
| 44 | + universe_domain: str, |
| 45 | + use_mtls_endpoint: str, |
| 46 | + default_universe: str, |
| 47 | + default_mtls_endpoint: str, |
| 48 | + default_endpoint_template: str, |
| 49 | + ) -> str: |
| 50 | + """Return the API endpoint used by the client.""" |
| 51 | + if api_override is not None: |
| 52 | + api_endpoint = api_override |
| 53 | + elif use_mtls_endpoint == "always" or (use_mtls_endpoint == "auto" and client_cert_source): |
| 54 | + if universe_domain != default_universe: |
| 55 | + raise MutualTLSChannelError( |
| 56 | + f"mTLS is not supported in any universe other than {default_universe}." |
| 57 | + ) |
| 58 | + api_endpoint = default_mtls_endpoint |
| 59 | + else: |
| 60 | + api_endpoint = default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain) |
| 61 | + return api_endpoint |
| 62 | + |
| 63 | + def get_universe_domain( |
| 64 | + client_universe_domain: Optional[str], |
| 65 | + universe_domain_env: Optional[str], |
| 66 | + default_universe: str, |
| 67 | + ) -> str: |
| 68 | + """Return the universe domain used by the client.""" |
| 69 | + universe_domain = default_universe |
| 70 | + if client_universe_domain is not None: |
| 71 | + universe_domain = client_universe_domain |
| 72 | + elif universe_domain_env is not None: |
| 73 | + universe_domain = universe_domain_env |
| 74 | + if len(universe_domain.strip()) == 0: |
| 75 | + raise ValueError("Universe Domain cannot be an empty string.") |
| 76 | + return universe_domain |
0 commit comments