@@ -222,6 +222,25 @@ def join_url_path(base_url: str, path: str) -> str:
222222 return urlunparse (parsed_base ._replace (path = base_path + '/' + path ))
223223
224224
225+ def to_mtls_url (url : str ) -> str :
226+ """Rewrites a googleapis.com URL to its mTLS endpoint.
227+
228+ For example, `aiplatform.googleapis.com` becomes
229+ `aiplatform.mtls.googleapis.com` and `foo.sandbox.googleapis.com` becomes
230+ `foo.mtls.sandbox.googleapis.com`. URLs on other hosts, and URLs that already
231+ point at an mTLS endpoint, are returned unchanged.
232+ """
233+ parsed = urlparse (url )
234+ netloc = parsed .netloc
235+ if netloc .endswith (('.mtls.googleapis.com' , '.mtls.sandbox.googleapis.com' )):
236+ return url
237+ for domain in ('sandbox.googleapis.com' , 'googleapis.com' ):
238+ if netloc .endswith ('.' + domain ):
239+ netloc = netloc [: - len (domain )] + 'mtls.' + domain
240+ return urlunparse (parsed ._replace (netloc = netloc ))
241+ return url
242+
243+
225244def load_auth (* , project : Union [str , None ]) -> Tuple [Credentials , str ]:
226245 """Loads google auth credentials and project id."""
227246 credentials , loaded_project_id = google .auth .default ( # type: ignore[no-untyped-call]
@@ -898,6 +917,26 @@ def __init__(
898917 self ._http_options ,
899918 vertexai = bool (self .vertexai ),
900919 )
920+ # The httpx and websocket transports present the default client certificate
921+ # only through the SSL context the SDK creates, so when the caller supplies
922+ # their own client or SSL context, only GOOGLE_API_USE_MTLS_ENDPOINT=always
923+ # switches them to the mTLS endpoint.
924+ client_args = self ._http_options .client_args or {}
925+ async_client_args = self ._http_options .async_client_args or {}
926+ custom_httpx_verify = bool (
927+ client_args .get ('verify' ) or async_client_args .get ('verify' )
928+ )
929+ self ._httpx_use_mtls_endpoint = self ._use_mtls_endpoint (
930+ sdk_ssl_ctx = not (self ._http_options .httpx_client or custom_httpx_verify )
931+ )
932+ self ._async_httpx_use_mtls_endpoint = self ._use_mtls_endpoint (
933+ sdk_ssl_ctx = not (
934+ self ._http_options .httpx_async_client or custom_httpx_verify
935+ )
936+ )
937+ self ._websocket_use_mtls_endpoint = self ._use_mtls_endpoint (
938+ sdk_ssl_ctx = not async_client_args .get ('ssl' )
939+ )
901940 self ._retry = tenacity .Retrying (** retry_kwargs )
902941 self ._async_retry = tenacity .AsyncRetrying (** retry_kwargs )
903942
@@ -913,6 +952,37 @@ def _use_google_auth_sync(self) -> bool:
913952 )
914953 )
915954
955+ def _use_mtls_endpoint (self , sdk_ssl_ctx : bool ) -> bool :
956+ """Returns whether a transport should send requests to the mTLS endpoint.
957+
958+ Certificate-bound access tokens are only accepted on `mtls.googleapis.com`,
959+ so a transport that presents the client certificate must also switch to
960+ the mTLS endpoint.
961+
962+ Args:
963+ sdk_ssl_ctx: Whether the transport uses the SSL context created by the
964+ SDK, which carries the default client certificate when one is
965+ configured.
966+ """
967+ if not self .vertexai :
968+ return False
969+ client_cert_available = bool (
970+ sdk_ssl_ctx
971+ and hasattr (mtls , 'should_use_client_cert' )
972+ and mtls .should_use_client_cert () # type: ignore[no-untyped-call]
973+ and mtls .has_default_client_cert_source () # type: ignore[no-untyped-call]
974+ )
975+ should_use_mtls_endpoint = getattr (mtls , 'should_use_mtls_endpoint' , None )
976+ if should_use_mtls_endpoint is None :
977+ return client_cert_available
978+ try :
979+ return bool (
980+ should_use_mtls_endpoint (client_cert_available = client_cert_available )
981+ )
982+ except auth_exceptions .MutualTLSChannelError as e :
983+ logger .warning ('Failed to determine whether to use mTLS endpoint: %s' , e )
984+ return client_cert_available
985+
916986 def _use_google_auth_async (self ) -> bool :
917987 try :
918988 import importlib
@@ -1294,14 +1364,26 @@ def _use_aiohttp(self) -> bool:
12941364 and (self ._http_options .httpx_async_client is None )
12951365 )
12961366
1367+ def _httpx_url (self , url : str , * , is_async : bool ) -> str :
1368+ """Returns the URL to send a request to with the httpx client."""
1369+ use_mtls_endpoint = (
1370+ self ._async_httpx_use_mtls_endpoint
1371+ if is_async
1372+ else self ._httpx_use_mtls_endpoint
1373+ )
1374+ return to_mtls_url (url ) if use_mtls_endpoint else url
1375+
12971376 def _websocket_base_url (self ) -> str :
12981377 has_sufficient_auth = (self .project and self .location ) or self .api_key
12991378 if self .custom_base_url and not has_sufficient_auth :
13001379 # API gateway proxy can use the auth in custom headers, not url.
13011380 # Enable custom url if auth is not sufficient.
13021381 return self .custom_base_url
13031382 url_parts = urlparse (self ._http_options .base_url )
1304- return url_parts ._replace (scheme = 'wss' ).geturl () # type: ignore[arg-type, return-value]
1383+ url = url_parts ._replace (scheme = 'wss' ).geturl () # type: ignore[arg-type]
1384+ if self ._websocket_use_mtls_endpoint :
1385+ url = to_mtls_url (url ) # type: ignore[arg-type]
1386+ return url # type: ignore[return-value]
13051387
13061388 def _access_token (self ) -> str :
13071389 """Retrieves the access token for the credentials."""
@@ -1494,13 +1576,8 @@ def _request_once(
14941576 self ._authorized_session .configure_mtls_channel (
14951577 client_cert_source
14961578 ) # type: ignore[no-untyped-call]
1497- if self ._authorized_session ._is_mtls and 'googleapis.com' in url :
1498- if 'sandbox' in url :
1499- url = url .replace (
1500- 'sandbox.googleapis.com' , 'mtls.sandbox.googleapis.com'
1501- )
1502- else :
1503- url = url .replace ('googleapis.com' , 'mtls.googleapis.com' )
1579+ if self ._authorized_session ._is_mtls :
1580+ url = to_mtls_url (url )
15041581 response = self ._authorized_session .request ( # type: ignore[no-untyped-call]
15051582 method = http_request .method .upper (),
15061583 url = url ,
@@ -1512,7 +1589,7 @@ def _request_once(
15121589 else :
15131590 httpx_request = self ._httpx_client .build_request ( # type: ignore[union-attr]
15141591 method = http_request .method ,
1515- url = http_request .url ,
1592+ url = self . _httpx_url ( http_request .url , is_async = False ) ,
15161593 content = data ,
15171594 headers = http_request .headers ,
15181595 timeout = http_request .timeout ,
@@ -1572,13 +1649,8 @@ async def _async_request_once(
15721649 await session .configure_mtls_channel ( # type: ignore[union-attr]
15731650 client_cert_source
15741651 )
1575- if session ._is_mtls and 'googleapis.com' in url : # type: ignore[union-attr]
1576- if 'sandbox' in url :
1577- url = url .replace (
1578- 'sandbox.googleapis.com' , 'mtls.sandbox.googleapis.com'
1579- )
1580- else :
1581- url = url .replace ('googleapis.com' , 'mtls.googleapis.com' )
1652+ if session ._is_mtls : # type: ignore[union-attr]
1653+ url = to_mtls_url (url )
15821654 try :
15831655 response = await session .request ( # type: ignore[union-attr]
15841656 method = http_request .method ,
@@ -1625,7 +1697,7 @@ async def _async_request_once(
16251697 # aiohttp is not available. Fall back to httpx.
16261698 httpx_request = self ._async_httpx_client .build_request ( # type: ignore[union-attr]
16271699 method = http_request .method ,
1628- url = http_request .url ,
1700+ url = self . _httpx_url ( http_request .url , is_async = True ) ,
16291701 content = data ,
16301702 headers = http_request .headers ,
16311703 timeout = http_request .timeout ,
@@ -1645,13 +1717,8 @@ async def _async_request_once(
16451717 await session .configure_mtls_channel ( # type: ignore[union-attr]
16461718 client_cert_source
16471719 )
1648- if session ._is_mtls and 'googleapis.com' in url : # type: ignore[union-attr]
1649- if 'sandbox' in url :
1650- url = url .replace (
1651- 'sandbox.googleapis.com' , 'mtls.sandbox.googleapis.com'
1652- )
1653- else :
1654- url = url .replace ('googleapis.com' , 'mtls.googleapis.com' )
1720+ if session ._is_mtls : # type: ignore[union-attr]
1721+ url = to_mtls_url (url )
16551722 try :
16561723 response = await session .request ( # type: ignore[union-attr]
16571724 method = http_request .method ,
@@ -1708,7 +1775,7 @@ async def _async_request_once(
17081775 # aiohttp is not available. Fall back to httpx.
17091776 client_response = await self ._async_httpx_client .request ( # type: ignore[union-attr]
17101777 method = http_request .method ,
1711- url = http_request .url ,
1778+ url = self . _httpx_url ( http_request .url , is_async = True ) ,
17121779 headers = http_request .headers ,
17131780 content = data ,
17141781 timeout = http_request .timeout ,
@@ -2054,13 +2121,8 @@ def _write_chunks(chunks: Iterator[bytes]) -> None:
20542121 self ._authorized_session .configure_mtls_channel (
20552122 client_cert_source
20562123 ) # type: ignore[no-untyped-call]
2057- if self ._authorized_session ._is_mtls and 'googleapis.com' in url :
2058- if 'sandbox' in url :
2059- url = url .replace (
2060- 'sandbox.googleapis.com' , 'mtls.sandbox.googleapis.com'
2061- )
2062- else :
2063- url = url .replace ('googleapis.com' , 'mtls.googleapis.com' )
2124+ if self ._authorized_session ._is_mtls :
2125+ url = to_mtls_url (url )
20642126 if destination is not None :
20652127 response = self ._authorized_session .request ( # type: ignore[no-untyped-call]
20662128 method = http_request .method .upper (),
@@ -2090,7 +2152,7 @@ def _write_chunks(chunks: Iterator[bytes]) -> None:
20902152 if destination is not None :
20912153 httpx_request = self ._httpx_client .build_request ( # type: ignore[union-attr]
20922154 method = http_request .method ,
2093- url = http_request .url ,
2155+ url = self . _httpx_url ( http_request .url , is_async = False ) ,
20942156 content = data ,
20952157 headers = http_request .headers ,
20962158 timeout = http_request .timeout ,
@@ -2105,7 +2167,7 @@ def _write_chunks(chunks: Iterator[bytes]) -> None:
21052167 else :
21062168 response = self ._httpx_client .request ( # type: ignore[union-attr]
21072169 method = http_request .method ,
2108- url = http_request .url ,
2170+ url = self . _httpx_url ( http_request .url , is_async = False ) ,
21092171 content = data ,
21102172 headers = http_request .headers ,
21112173 timeout = http_request .timeout ,
@@ -2428,13 +2490,8 @@ async def _write_chunks(chunks: AsyncIterator[bytes]) -> None:
24282490 await session .configure_mtls_channel ( # type: ignore[union-attr]
24292491 client_cert_source
24302492 )
2431- if session ._is_mtls and 'googleapis.com' in url : # type: ignore[union-attr]
2432- if 'sandbox' in url :
2433- url = url .replace (
2434- 'sandbox.googleapis.com' , 'mtls.sandbox.googleapis.com'
2435- )
2436- else :
2437- url = url .replace ('googleapis.com' , 'mtls.googleapis.com' )
2493+ if session ._is_mtls : # type: ignore[union-attr]
2494+ url = to_mtls_url (url )
24382495 response = await session .request ( # type: ignore[union-attr]
24392496 method = http_request .method ,
24402497 url = url ,
@@ -2465,7 +2522,7 @@ async def _write_chunks(chunks: AsyncIterator[bytes]) -> None:
24652522 if destination is not None :
24662523 httpx_request = self ._async_httpx_client .build_request ( # type: ignore[union-attr]
24672524 method = http_request .method ,
2468- url = http_request .url ,
2525+ url = self . _httpx_url ( http_request .url , is_async = True ) ,
24692526 content = data ,
24702527 headers = http_request .headers ,
24712528 timeout = http_request .timeout ,
@@ -2485,7 +2542,7 @@ async def _write_chunks(chunks: AsyncIterator[bytes]) -> None:
24852542 else :
24862543 client_response = await self ._async_httpx_client .request ( # type: ignore[union-attr]
24872544 method = http_request .method ,
2488- url = http_request .url ,
2545+ url = self . _httpx_url ( http_request .url , is_async = True ) ,
24892546 headers = http_request .headers ,
24902547 content = data ,
24912548 timeout = http_request .timeout ,
0 commit comments