Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions packages/google-auth/google/auth/compute_engine/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,15 @@ def _prepare_request_for_mds(request, use_mtls=False) -> None:

Args:
request (google.auth.transport.Request): A callable used to make
HTTP requests.
HTTP requests. If mTLS is enabled, and the request supports sessions,
the request will have the mTLS adapter mounted. Otherwise, there
will be no change.
use_mtls (bool): Whether to use mTLS for the request.

Returns:
google.auth.transport.Request: A request object to use.
If mTLS is enabled, the request will have the mTLS adapter mounted.
Otherwise, the original request will be returned unchanged.

"""
# Only modify the request if mTLS is enabled.
if use_mtls:
# Only modify the request if mTLS is enabled, and request supports sessions.
if use_mtls and hasattr(request, "session"):
# Ensure the request has a session to mount the adapter to.
if not request.session:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition required anymore? Since we already check that session is present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe this is meant to attach a session if it's set to None

request.session = requests.Session()
Comment on lines +177 to 180
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using hasattr(request, "session") is a good first step, but it's not entirely safe. It's possible for a different Request implementation to have a session attribute that isn't a requests.Session object. This could lead to an AttributeError later when request.session.mount is called.

A more robust approach is to explicitly check if request is an instance of google.auth.transport.requests.Request. This ensures you're working with the correct object type and avoids potential issues with incompatible session attributes.

Suggested change
if use_mtls and hasattr(request, "session"):
# Ensure the request has a session to mount the adapter to.
if not request.session:
request.session = requests.Session()
if use_mtls:
# Only modify the request if it's a requests-based transport.
from google.auth.transport import requests as auth_requests
if isinstance(request, auth_requests.Request):
# Ensure the request has a session to mount the adapter to.
if not request.session:
request.session = requests.Session()

Expand Down
14 changes: 14 additions & 0 deletions packages/google-auth/tests/compute_engine/test__metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,17 @@ def test__prepare_request_for_mds_mtls_no_session(mock_mds_mtls_adapter):
mock_session_class.assert_called_once()
mock_mds_mtls_adapter.assert_called_once()
assert request.session.mount.call_count == len(_metadata._GCE_DEFAULT_MDS_HOSTS)


@mock.patch("google.auth.compute_engine._mtls.MdsMtlsAdapter")
def test__prepare_request_for_mds_mtls_http_request(mock_mds_mtls_adapter):
"""
http requests should be ignored.
Regression test for https://github.com/googleapis/google-cloud-python/issues/16035
"""
from google.auth.transport import _http_client

request = _http_client.Request()
_metadata._prepare_request_for_mds(request, use_mtls=True)

assert mock_mds_mtls_adapter.call_count == 0
Loading