Skip to content

Commit 7c61c7d

Browse files
authored
fix: remove unnecessary call to mds service (#1769)
1 parent 554f967 commit 7c61c7d

File tree

4 files changed

+10
-93
lines changed

4 files changed

+10
-93
lines changed

google/auth/compute_engine/credentials.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,6 @@ def __init__(
8787
self._universe_domain = universe_domain
8888
self._universe_domain_cached = True
8989

90-
def _retrieve_info(self, request):
91-
"""Retrieve information about the service account.
92-
93-
Updates the scopes and retrieves the full service account email.
94-
95-
Args:
96-
request (google.auth.transport.Request): The object used to make
97-
HTTP requests.
98-
"""
99-
info = _metadata.get_service_account_info(
100-
request, service_account=self._service_account_email
101-
)
102-
103-
self._service_account_email = info["email"]
104-
105-
# Don't override scopes requested by the user.
106-
if self._scopes is None:
107-
self._scopes = info["scopes"]
108-
10990
def _metric_header_for_usage(self):
11091
return metrics.CRED_TYPE_SA_MDS
11192

@@ -123,7 +104,6 @@ def refresh(self, request):
123104
"""
124105
scopes = self._scopes if self._scopes is not None else self._default_scopes
125106
try:
126-
self._retrieve_info(request)
127107
self.token, self.expiry = _metadata.get_service_account_token(
128108
request, service_account=self._service_account_email, scopes=scopes
129109
)

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

system_tests/system_tests_sync/test_compute_engine.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@ def check_gce_environment(http_request):
3535
pytest.skip("Compute Engine metadata service is not available.")
3636

3737

38-
def test_refresh(http_request, token_info):
38+
def test_refresh(http_request):
3939
credentials = compute_engine.Credentials()
4040

4141
credentials.refresh(http_request)
4242

4343
assert credentials.token is not None
4444
assert credentials.service_account_email is not None
4545

46-
info = token_info(credentials.token)
47-
info_scopes = _helpers.string_to_scopes(info["scope"])
48-
assert set(info_scopes) == set(credentials.scopes)
46+
assert credentials.scopes is None
4947

5048

5149
def test_default(verify_refresh):

tests/compute_engine/test_credentials.py

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,7 @@ def test_default_state(self):
9999
)
100100
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
101101
def test_refresh_success(self, get, utcnow):
102-
get.side_effect = [
103-
{
104-
# First request is for sevice account info.
105-
"email": "service-account@example.com",
106-
"scopes": ["one", "two"],
107-
},
108-
{
109-
# Second request is for the token.
110-
"access_token": "token",
111-
"expires_in": 500,
112-
},
113-
]
102+
get.side_effect = [{"access_token": "token", "expires_in": 500}]
114103

115104
# Refresh credentials
116105
self.credentials.refresh(None)
@@ -120,8 +109,8 @@ def test_refresh_success(self, get, utcnow):
120109
assert self.credentials.expiry == (utcnow() + datetime.timedelta(seconds=500))
121110

122111
# Check the credential info
123-
assert self.credentials.service_account_email == "service-account@example.com"
124-
assert self.credentials._scopes == ["one", "two"]
112+
assert self.credentials.service_account_email == "default"
113+
assert self.credentials._scopes is None
125114

126115
# Check that the credentials are valid (have a token and are not
127116
# expired)
@@ -137,18 +126,7 @@ def test_refresh_success(self, get, utcnow):
137126
)
138127
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
139128
def test_refresh_success_with_scopes(self, get, utcnow, mock_metrics_header_value):
140-
get.side_effect = [
141-
{
142-
# First request is for sevice account info.
143-
"email": "service-account@example.com",
144-
"scopes": ["one", "two"],
145-
},
146-
{
147-
# Second request is for the token.
148-
"access_token": "token",
149-
"expires_in": 500,
150-
},
151-
]
129+
get.side_effect = [{"access_token": "token", "expires_in": 500}]
152130

153131
# Refresh credentials
154132
scopes = ["three", "four"]
@@ -160,7 +138,7 @@ def test_refresh_success_with_scopes(self, get, utcnow, mock_metrics_header_valu
160138
assert self.credentials.expiry == (utcnow() + datetime.timedelta(seconds=500))
161139

162140
# Check the credential info
163-
assert self.credentials.service_account_email == "service-account@example.com"
141+
assert self.credentials.service_account_email == "default"
164142
assert self.credentials._scopes == scopes
165143

166144
# Check that the credentials are valid (have a token and are not
@@ -184,18 +162,7 @@ def test_refresh_error(self, get):
184162

185163
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
186164
def test_before_request_refreshes(self, get):
187-
get.side_effect = [
188-
{
189-
# First request is for sevice account info.
190-
"email": "service-account@example.com",
191-
"scopes": "one two",
192-
},
193-
{
194-
# Second request is for the token.
195-
"access_token": "token",
196-
"expires_in": 500,
197-
},
198-
]
165+
get.side_effect = [{"access_token": "token", "expires_in": 500}]
199166

200167
# Credentials should start as invalid
201168
assert not self.credentials.valid
@@ -473,20 +440,6 @@ def test_with_target_audience_integration(self):
473440
have been mocked.
474441
"""
475442

476-
# mock information about credentials
477-
responses.add(
478-
responses.GET,
479-
"http://metadata.google.internal/computeMetadata/v1/instance/"
480-
"service-accounts/default/?recursive=true",
481-
status=200,
482-
content_type="application/json",
483-
json={
484-
"scopes": "email",
485-
"email": "service-account@example.com",
486-
"aliases": ["default"],
487-
},
488-
)
489-
490443
# mock information about universe_domain
491444
responses.add(
492445
responses.GET,
@@ -501,7 +454,7 @@ def test_with_target_audience_integration(self):
501454
responses.add(
502455
responses.GET,
503456
"http://metadata.google.internal/computeMetadata/v1/instance/"
504-
"service-accounts/service-account@example.com/token",
457+
"service-accounts/default/token",
505458
status=200,
506459
content_type="application/json",
507460
json={
@@ -641,25 +594,11 @@ def test_with_quota_project_integration(self):
641594
have been mocked.
642595
"""
643596

644-
# mock information about credentials
645-
responses.add(
646-
responses.GET,
647-
"http://metadata.google.internal/computeMetadata/v1/instance/"
648-
"service-accounts/default/?recursive=true",
649-
status=200,
650-
content_type="application/json",
651-
json={
652-
"scopes": "email",
653-
"email": "service-account@example.com",
654-
"aliases": ["default"],
655-
},
656-
)
657-
658597
# mock token for credentials
659598
responses.add(
660599
responses.GET,
661600
"http://metadata.google.internal/computeMetadata/v1/instance/"
662-
"service-accounts/service-account@example.com/token",
601+
"service-accounts/default/token",
663602
status=200,
664603
content_type="application/json",
665604
json={

0 commit comments

Comments
 (0)