Skip to content

Commit d478b4d

Browse files
feat: Read Quota Project from Environment Variable (#1163)
* feat: Read Quota Project from Environment Variable * remove unused code * Update google/auth/credentials.py Co-authored-by: Carl Lundin <108372512+clundin25@users.noreply.github.com> * update rt Co-authored-by: Carl Lundin <108372512+clundin25@users.noreply.github.com>
1 parent 80f7bc4 commit d478b4d

5 files changed

Lines changed: 34 additions & 1 deletion

File tree

packages/google-auth/google/auth/_default.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ def _get_gdch_service_account_credentials(filename, info):
479479
def _apply_quota_project_id(credentials, quota_project_id):
480480
if quota_project_id:
481481
credentials = credentials.with_quota_project(quota_project_id)
482+
else:
483+
credentials = credentials.with_quota_project_from_environment()
482484

483485
from google.oauth2 import credentials as authorized_user_credentials
484486

packages/google-auth/google/auth/credentials.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
"""Interfaces for credentials."""
1717

1818
import abc
19+
import os
1920

2021
import six
2122

22-
from google.auth import _helpers
23+
from google.auth import _helpers, environment_vars
2324

2425

2526
@six.add_metaclass(abc.ABCMeta)
@@ -149,6 +150,12 @@ def with_quota_project(self, quota_project_id):
149150
"""
150151
raise NotImplementedError("This credential does not support quota project.")
151152

153+
def with_quota_project_from_environment(self):
154+
quota_from_env = os.environ.get(environment_vars.GOOGLE_CLOUD_QUOTA_PROJECT)
155+
if quota_from_env:
156+
return self.with_quota_project(quota_from_env)
157+
return self
158+
152159

153160
class CredentialsWithTokenUri(Credentials):
154161
"""Abstract base for credentials supporting ``with_token_uri`` factory"""

packages/google-auth/google/auth/environment_vars.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
situations (such as Google App Engine).
3030
"""
3131

32+
GOOGLE_CLOUD_QUOTA_PROJECT = "GOOGLE_CLOUD_QUOTA_PROJECT"
33+
"""Environment variable defining the project to be used for
34+
quota and billing."""
35+
3236
CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"
3337
"""Environment variable defining the location of Google application default
3438
credentials."""
1 Byte
Binary file not shown.

packages/google-auth/tests/test__default.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,3 +1214,23 @@ def test_default_gdch_service_account_credentials(get_adc_path):
12141214
assert creds._token_uri == "https://service-identity.<Domain>/authenticate"
12151215
assert creds._ca_cert_path == "/path/to/ca/cert"
12161216
assert project == "project_foo"
1217+
1218+
1219+
@mock.patch.dict(os.environ)
1220+
@mock.patch(
1221+
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
1222+
)
1223+
def test_quota_project_from_environment(get_adc_path):
1224+
get_adc_path.return_value = AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE
1225+
1226+
credentials, _ = _default.default(quota_project_id=None)
1227+
assert credentials.quota_project_id == "quota_project_id"
1228+
1229+
quota_from_env = "quota_from_env"
1230+
os.environ[environment_vars.GOOGLE_CLOUD_QUOTA_PROJECT] = quota_from_env
1231+
credentials, _ = _default.default(quota_project_id=None)
1232+
assert credentials.quota_project_id == quota_from_env
1233+
1234+
explicit_quota = "explicit_quota"
1235+
credentials, _ = _default.default(quota_project_id=explicit_quota)
1236+
assert credentials.quota_project_id == explicit_quota

0 commit comments

Comments
 (0)