Skip to content

Commit 0be91e2

Browse files
authored
Revert "fix: remove unnecessary call to mds service (#1769)" (#1777)
This reverts commit 7c61c7d.
1 parent 25660bc commit 0be91e2

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

google/auth/compute_engine/credentials.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ 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+
90109
def _metric_header_for_usage(self):
91110
return metrics.CRED_TYPE_SA_MDS
92111

@@ -104,6 +123,7 @@ def refresh(self, request):
104123
"""
105124
scopes = self._scopes if self._scopes is not None else self._default_scopes
106125
try:
126+
self._retrieve_info(request)
107127
self.token, self.expiry = _metadata.get_service_account_token(
108128
request, service_account=self._service_account_email, scopes=scopes
109129
)

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

system_tests/system_tests_sync/test_compute_engine.py

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

3737

38-
def test_refresh(http_request):
38+
def test_refresh(http_request, token_info):
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-
assert credentials.scopes is None
46+
info = token_info(credentials.token)
47+
info_scopes = _helpers.string_to_scopes(info["scope"])
48+
assert set(info_scopes) == set(credentials.scopes)
4749

4850

4951
def test_default(verify_refresh):

tests/compute_engine/test_credentials.py

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,18 @@ 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 = [{"access_token": "token", "expires_in": 500}]
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+
]
103114

104115
# Refresh credentials
105116
self.credentials.refresh(None)
@@ -109,8 +120,8 @@ def test_refresh_success(self, get, utcnow):
109120
assert self.credentials.expiry == (utcnow() + datetime.timedelta(seconds=500))
110121

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

115126
# Check that the credentials are valid (have a token and are not
116127
# expired)
@@ -126,7 +137,18 @@ def test_refresh_success(self, get, utcnow):
126137
)
127138
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
128139
def test_refresh_success_with_scopes(self, get, utcnow, mock_metrics_header_value):
129-
get.side_effect = [{"access_token": "token", "expires_in": 500}]
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+
]
130152

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

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

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

163185
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
164186
def test_before_request_refreshes(self, get):
165-
get.side_effect = [{"access_token": "token", "expires_in": 500}]
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+
]
166199

167200
# Credentials should start as invalid
168201
assert not self.credentials.valid
@@ -440,6 +473,20 @@ def test_with_target_audience_integration(self):
440473
have been mocked.
441474
"""
442475

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+
443490
# mock information about universe_domain
444491
responses.add(
445492
responses.GET,
@@ -454,7 +501,7 @@ def test_with_target_audience_integration(self):
454501
responses.add(
455502
responses.GET,
456503
"http://metadata.google.internal/computeMetadata/v1/instance/"
457-
"service-accounts/default/token",
504+
"service-accounts/service-account@example.com/token",
458505
status=200,
459506
content_type="application/json",
460507
json={
@@ -594,11 +641,25 @@ def test_with_quota_project_integration(self):
594641
have been mocked.
595642
"""
596643

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+
597658
# mock token for credentials
598659
responses.add(
599660
responses.GET,
600661
"http://metadata.google.internal/computeMetadata/v1/instance/"
601-
"service-accounts/default/token",
662+
"service-accounts/service-account@example.com/token",
602663
status=200,
603664
content_type="application/json",
604665
json={

0 commit comments

Comments
 (0)