Skip to content

Commit

Permalink
fix: make requests import conditional for gce universe domain (#1476)
Browse files Browse the repository at this point in the history
* fix: make requests import conditional for gce universe domain

* remove if for creating request object

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
sai-sunder-s and gcf-owl-bot[bot] committed Feb 15, 2024
1 parent 30743b0 commit 9bb64c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
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

0 comments on commit 9bb64c8

Please sign in to comment.