Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make requests import conditional for gce universe domain #1476

Merged
merged 3 commits into from
Feb 15, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions google/auth/compute_engine/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from google.auth import jwt
from google.auth import metrics
from google.auth.compute_engine import _metadata
from google.auth.transport import requests as google_auth_requests
from google.oauth2 import _client


Expand Down Expand Up @@ -84,7 +83,6 @@ def __init__(
self._scopes = scopes
self._default_scopes = default_scopes
self._universe_domain_cached = False
self._universe_domain_request = google_auth_requests.Request()
if universe_domain:
self._universe_domain = universe_domain
self._universe_domain_cached = True
Expand Down Expand Up @@ -150,8 +148,11 @@ def requires_scopes(self):
def universe_domain(self):
if self._universe_domain_cached:
return self._universe_domain

from google.auth.transport import requests as google_auth_requests

self._universe_domain = _metadata.get_universe_domain(
self._universe_domain_request
google_auth_requests.Request()
)
self._universe_domain_cached = True
return self._universe_domain
Expand Down
8 changes: 2 additions & 6 deletions tests/compute_engine/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,12 @@ def test_universe_domain(self, get_universe_domain):
assert self.credentials.universe_domain == "fake_universe_domain"
assert self.credentials._universe_domain == "fake_universe_domain"
assert self.credentials._universe_domain_cached
get_universe_domain.assert_called_once_with(
self.credentials._universe_domain_request
)
get_universe_domain.assert_called_once()

# calling the universe_domain property the second time should use the
# cached value instead of calling get_universe_domain
assert self.credentials.universe_domain == "fake_universe_domain"
get_universe_domain.assert_called_once_with(
self.credentials._universe_domain_request
)
get_universe_domain.assert_called_once()

@mock.patch("google.auth.compute_engine._metadata.get_universe_domain")
def test_user_provided_universe_domain(self, get_universe_domain):
Expand Down