Skip to content

Commit 0fe63b5

Browse files
committed
fix(api-core): update get_default_mtls_endpoint to use robust string slicing
1 parent f953c80 commit 0fe63b5

1 file changed

Lines changed: 8 additions & 20 deletions

File tree

  • packages/google-api-core/google/api_core/gapic_v1

packages/google-api-core/google/api_core/gapic_v1/routing.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616

1717
"""Helpers for routing and endpoint resolution."""
1818

19-
import re
2019
from typing import Any, Optional
2120

2221
from google.auth.exceptions import MutualTLSChannelError # type: ignore
2322

24-
_MTLS_ENDPOINT_RE = re.compile(
25-
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?"
26-
r"(?P<googledomain>\.googleapis\.com)?"
27-
)
28-
2923

3024
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3125
"""Converts api endpoint to mTLS endpoint.
@@ -37,24 +31,18 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3731
Returns:
3832
Optional[str]: converted mTLS api endpoint.
3933
"""
40-
if not api_endpoint:
34+
if not api_endpoint or ".mtls." in api_endpoint:
4135
return api_endpoint
4236

43-
m = _MTLS_ENDPOINT_RE.match(api_endpoint)
44-
if m is None:
45-
# Could not parse api_endpoint; return as-is.
46-
return api_endpoint
37+
if api_endpoint.endswith(".sandbox.googleapis.com"):
38+
# len(".sandbox.googleapis.com") == 23
39+
return api_endpoint[:-23] + ".mtls.sandbox.googleapis.com"
4740

48-
name, mtls_group, sandbox, googledomain = m.groups()
49-
if mtls_group or not googledomain:
50-
return api_endpoint
51-
52-
if sandbox:
53-
return api_endpoint.replace(
54-
"sandbox.googleapis.com", "mtls.sandbox.googleapis.com"
55-
)
41+
if api_endpoint.endswith(".googleapis.com"):
42+
# len(".googleapis.com") == 15
43+
return api_endpoint[:-15] + ".mtls.googleapis.com"
5644

57-
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
45+
return api_endpoint
5846

5947

6048
def get_api_endpoint(

0 commit comments

Comments
 (0)