Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bfd9322

Browse filesBrowse files
fix(auth): fix request session error (#16050)
Fixes #16035 only google.auth.transport.**requests**.Request objects have a session attached. Other Request subclasses don't so they raise an error when mts is enabled. This PR ignores unsupported request types, instead of raising an exception Thanks @sakshamgoyal-01 for the detailed bug report! --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 35bf915 commit bfd9322
Copy full SHA for bfd9322

2 files changed

+20-7Lines changed: 20 additions & 7 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/google-auth/google/auth/compute_engine/_metadata.py‎

Copy file name to clipboardExpand all lines: packages/google-auth/google/auth/compute_engine/_metadata.py
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,15 @@ def _prepare_request_for_mds(request, use_mtls=False) -> None:
166166
167167
Args:
168168
request (google.auth.transport.Request): A callable used to make
169-
HTTP requests.
169+
HTTP requests. If mTLS is enabled, and the request supports sessions,
170+
the request will have the mTLS adapter mounted. Otherwise, there
171+
will be no change.
170172
use_mtls (bool): Whether to use mTLS for the request.
171173
172-
Returns:
173-
google.auth.transport.Request: A request object to use.
174-
If mTLS is enabled, the request will have the mTLS adapter mounted.
175-
Otherwise, the original request will be returned unchanged.
174+
176175
"""
177-
# Only modify the request if mTLS is enabled.
178-
if use_mtls:
176+
# Only modify the request if mTLS is enabled, and request supports sessions.
177+
if use_mtls and hasattr(request, "session"):
179178
# Ensure the request has a session to mount the adapter to.
180179
if not request.session:
181180
request.session = requests.Session()
Collapse file

‎packages/google-auth/tests/compute_engine/test__metadata.py‎

Copy file name to clipboardExpand all lines: packages/google-auth/tests/compute_engine/test__metadata.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,3 +955,17 @@ def test__prepare_request_for_mds_mtls_no_session(mock_mds_mtls_adapter):
955955
mock_session_class.assert_called_once()
956956
mock_mds_mtls_adapter.assert_called_once()
957957
assert request.session.mount.call_count == len(_metadata._GCE_DEFAULT_MDS_HOSTS)
958+
959+
960+
@mock.patch("google.auth.compute_engine._mtls.MdsMtlsAdapter")
961+
def test__prepare_request_for_mds_mtls_http_request(mock_mds_mtls_adapter):
962+
"""
963+
http requests should be ignored.
964+
Regression test for https://github.com/googleapis/google-cloud-python/issues/16035
965+
"""
966+
from google.auth.transport import _http_client
967+
968+
request = _http_client.Request()
969+
_metadata._prepare_request_for_mds(request, use_mtls=True)
970+
971+
assert mock_mds_mtls_adapter.call_count == 0

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.