5555def _validate_gce_mds_configured_environment ():
5656 """Validates the GCE metadata server environment configuration for mTLS.
5757
58- mTLS is only supported when connecting to the default metadata host .
58+ mTLS is only supported when connecting to the default metadata server hosts .
5959 If we are in strict mode (which requires mTLS), ensure that the metadata host
60- has not been overridden (which means mTLS will fail).
60+ has not been overridden to a custom value (which means mTLS will fail).
6161
6262 Raises:
6363 google.auth.exceptions.MutualTLSChannelError: if the environment
6464 configuration is invalid for mTLS.
6565 """
6666 mode = _mtls ._parse_mds_mode ()
6767 if mode == _mtls .MdsMtlsMode .STRICT :
68- if _GCE_METADATA_HOST != _GCE_DEFAULT_HOST :
69- # mTLS is only supported when connecting to the default metadata host.
70- # Raise an exception if we are in strict mode (which requires mTLS)
71- # but the metadata host has been overridden. (which means mTLS will fail)
68+ # mTLS is only supported when connecting to the default metadata host.
69+ # Raise an exception if we are in strict mode (which requires mTLS)
70+ # but the metadata host has been overridden to a custom MDS. (which means mTLS will fail )
71+ if _GCE_METADATA_HOST not in _GCE_DEFAULT_MDS_HOSTS :
7272 raise exceptions .MutualTLSChannelError (
7373 "Mutual TLS is required, but the metadata host has been overridden. "
7474 "mTLS is only supported when connecting to the default metadata host."
@@ -143,7 +143,7 @@ def detect_gce_residency_linux():
143143 return content .startswith (_GOOGLE )
144144
145145
146- def _prepare_request_for_mds (request , use_mtls = False ):
146+ def _prepare_request_for_mds (request , use_mtls = False ) -> None :
147147 """Prepares a request for the metadata server.
148148
149149 This will check if mTLS should be used and mount the mTLS adapter if needed.
@@ -158,15 +158,16 @@ def _prepare_request_for_mds(request, use_mtls=False):
158158 If mTLS is enabled, the request will have the mTLS adapter mounted.
159159 Otherwise, the original request will be returned unchanged.
160160 """
161- if not use_mtls :
162- return request
161+ # Only modify the request if mTLS is enabled.
162+ if use_mtls :
163+ # Ensure the request has a session to mount the adapter to.
164+ if not request .session :
165+ request .session = requests .Session ()
163166
164- adapter = _mtls .MdsMtlsAdapter ()
165- if not request .session :
166- request .session = requests .Session ()
167- for host in _GCE_DEFAULT_MDS_HOSTS :
168- request .session .mount (f"https://{ host } /" , adapter )
169- return request
167+ adapter = _mtls .MdsMtlsAdapter ()
168+ # Mount the adapter for all default GCE metadata hosts.
169+ for host in _GCE_DEFAULT_MDS_HOSTS :
170+ request .session .mount (f"https://{ host } /" , adapter )
170171
171172
172173def ping (request , timeout = _METADATA_DEFAULT_TIMEOUT , retry_count = 3 ):
@@ -183,7 +184,7 @@ def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
183184 bool: True if the metadata server is reachable, False otherwise.
184185 """
185186 use_mtls = _mtls .should_use_mds_mtls ()
186- request = _prepare_request_for_mds (request , use_mtls = use_mtls )
187+ _prepare_request_for_mds (request , use_mtls = use_mtls )
187188 # NOTE: The explicit ``timeout`` is a workaround. The underlying
188189 # issue is that resolving an unknown host on some networks will take
189190 # 20-30 seconds; making this timeout short fixes the issue, but
@@ -270,14 +271,14 @@ def get(
270271 use_mtls = _mtls .should_use_mds_mtls ()
271272 # Prepare the request object for mTLS if needed.
272273 # This will create a new request object with the mTLS session.
273- request = _prepare_request_for_mds (request , use_mtls = use_mtls )
274+ _prepare_request_for_mds (request , use_mtls = use_mtls )
274275
275276 if root is None :
276277 root = _get_metadata_root (use_mtls )
277278
278279 # mTLS is only supported when connecting to the default metadata host.
279280 # If we are in strict mode (which requires mTLS), ensure that the metadata host
280- # has not been overridden (which means mTLS will fail).
281+ # has not been overridden to a non-default host value (which means mTLS will fail).
281282 _validate_gce_mds_configured_environment ()
282283
283284 base_url = urljoin (root , path )
0 commit comments