Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ def _get_gdch_service_account_credentials(filename, info):
def _apply_quota_project_id(credentials, quota_project_id):
if quota_project_id:
credentials = credentials.with_quota_project(quota_project_id)
else:
credentials = credentials.with_quota_project_from_environment()

from google.oauth2 import credentials as authorized_user_credentials

Expand Down
9 changes: 8 additions & 1 deletion google/auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"""Interfaces for credentials."""

import abc
import os

import six

from google.auth import _helpers
from google.auth import _helpers, environment_vars


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

def with_quota_project_from_environment(self):
quota_from_env = os.environ.get(environment_vars.GOOGLE_CLOUD_QUOTA_PROJECT)
if quota_from_env:
return self.with_quota_project(quota_from_env)
return self


class CredentialsWithTokenUri(Credentials):
"""Abstract base for credentials supporting ``with_token_uri`` factory"""
Expand Down
4 changes: 4 additions & 0 deletions google/auth/environment_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
situations (such as Google App Engine).
"""

GOOGLE_CLOUD_QUOTA_PROJECT = "GOOGLE_CLOUD_QUOTA_PROJECT"
"""Environment variable defining the project to be used for
quota and billing."""

CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"
"""Environment variable defining the location of Google application default
credentials."""
Expand Down
Binary file modified system_tests/secrets.tar.enc
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/test__default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,3 +1214,23 @@ def test_default_gdch_service_account_credentials(get_adc_path):
assert creds._token_uri == "https://service-identity.<Domain>/authenticate"
assert creds._ca_cert_path == "/path/to/ca/cert"
assert project == "project_foo"


@mock.patch.dict(os.environ)
@mock.patch(
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
)
def test_quota_project_from_environment(get_adc_path):
get_adc_path.return_value = AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE

credentials, _ = _default.default(quota_project_id=None)
assert credentials.quota_project_id == "quota_project_id"

quota_from_env = "quota_from_env"
os.environ[environment_vars.GOOGLE_CLOUD_QUOTA_PROJECT] = quota_from_env
credentials, _ = _default.default(quota_project_id=None)
assert credentials.quota_project_id == quota_from_env

explicit_quota = "explicit_quota"
credentials, _ = _default.default(quota_project_id=explicit_quota)
assert credentials.quota_project_id == explicit_quota