8000 fix: apply quota project for compute cred in adc (#1177) · TJB-1/google-auth-library-python@b9aa92a · GitHub
[go: up one dir, main page]

Skip to content

Commit b9aa92a

Browse files
authored
fix: apply quota project for compute cred in adc (googleapis#1177)
* fix: apply quota project for compute cred in adc * update secret
1 parent e9e76d1 commit b9aa92a

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

google/auth/_default.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def _get_gae_credentials():
267267
return None, None
268268

269269

270-
def _get_gce_credentials(request=None):
270+
def _get_gce_credentials(request=None, quota_project_id=None):
271271
"""Gets credentials and project ID from the GCE Metadata Service."""
272272
# Ping requires a transport, but we want application default credentials
273273
# to require no arguments. So, we'll use the _http_client transport which
@@ -293,7 +293,10 @@ def _get_gce_credentials(request=None):
293293
except exceptions.TransportError:
294294
project_id = None
295295

296-
return compute_engine.Credentials(), project_id
296+
cred = compute_engine.Credentials()
297+
cred = _apply_quota_project_id(cred, quota_project_id)
298+
299+
return cred, project_id
297300
else:
298301
_LOGGER.warning(
299302
"Authentication failed using Compute Engine authentication due to unavailable metadata server."
@@ -603,7 +606,7 @@ def default(scopes=None, request=None, quota_project_id=None, default_scopes=Non
603606
lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
604607
lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),
605608
_get_gae_credentials,
606-
lambda: _get_gce_credentials(request),
609+
lambda: _get_gce_credentials(request, quota_project_id=quota_project_id),
607610
)
608611

609612
for checker in checkers:

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

tests/test__default.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,3 +1234,32 @@ def test_quota_project_from_environment(get_adc_path):
12341234
explicit_quota = "explicit_quota"
12351235
credentials, _ = _default.default(quota_project_id=explicit_quota)
12361236
assert credentials.quota_project_id == explicit_quota
1237+
1238+
1239+
@mock.patch(
1240+
"google.auth.compute_engine._metadata.ping", return_value=True, autospec=True
1241+
)
1242+
@mock.patch(
1243+
"google.auth.compute_engine._metadata.get_project_id",
1244+
return_value="example-project",
1245+
autospec=True,
1246+
)
1247+
@mock.patch.dict(os.environ)
1248+
def test_quota_gce_credentials(unused_get, unused_ping):
1249+
# No quota
1250+
credentials, project_id = _default._get_gce_credentials()
1251+
assert project_id == "example-project"
1252+
assert credentials.quota_project_id is None
1253+
1254+
# Quota from environment
1255+
quota_from_env = "quota_from_env"
1256+
os.environ[environment_vars.GOOGLE_CLOUD_QUOTA_PROJECT] = quota_from_env
1257+
credentials, project_id = _default._get_gce_credentials()
1258+
assert credentials.quota_project_id == quota_from_env
1259+
1260+
# Explicit quota
1261+
explicit_quota = "explicit_quota"
1262+
credentials, project_id = _default._get_gce_credentials(
1263+
quota_project_id=explicit_quota
1264+
)
1265+
assert credentials.quota_project_id == explicit_quota

0 commit comments

Comments
 (0)
0