Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no warning if quota_project_id is given #537

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
# Warning when using Cloud SDK user credentials
_CLOUD_SDK_CREDENTIALS_WARNING = """\
Your application has authenticated using end user credentials from Google \
Cloud SDK. We recommend that most server applications use service accounts \
instead. If your application continues to use end user credentials from Cloud \
SDK, you might receive a "quota exceeded" or "API not enabled" error. For \
more information about service accounts, see \
https://cloud.google.com/docs/authentication/"""
Cloud SDK without a quota project. You might receive a "quota exceeded" \
or "API not enabled" error. We recommend rerun \
arithmetic1728 marked this conversation as resolved.
Show resolved Hide resolved
`gcloud auth application-default login` and make sure a quota project is \
added. Or you can use service accounts instead. For more information \
about service accounts, see https://cloud.google.com/docs/authentication/"""


def _warn_about_problematic_credentials(credentials):
Expand Down Expand Up @@ -114,8 +114,8 @@ def _load_credentials_from_file(filename):
msg = "Failed to load authorized user credentials from {}".format(filename)
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
six.raise_from(new_exc, caught_exc)
# Authorized user credentials do not contain the project ID.
_warn_about_problematic_credentials(credentials)
if not credentials.quota_project_id:
_warn_about_problematic_credentials(credentials)
return credentials, None

elif credential_type == _SERVICE_ACCOUNT_TYPE:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"client_id": "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com",
"client_secret": "secret",
"refresh_token": "alabalaportocala",
"type": "authorized_user",
"quota_project_id": "quota_project_id"
}
11 changes: 11 additions & 0 deletions tests/test__default.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
DATA_DIR, "authorized_user_cloud_sdk.json"
)

AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE = os.path.join(
DATA_DIR, "authorized_user_cloud_sdk_with_quota_project_id.json"
)

SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json")

with open(SERVICE_ACCOUNT_FILE) as fh:
Expand Down Expand Up @@ -101,6 +105,13 @@ def test__load_credentials_from_file_authorized_user_cloud_sdk():
assert isinstance(credentials, google.oauth2.credentials.Credentials)
assert project_id is None

# No warning if the json file has quota project id.
credentials, project_id = _default._load_credentials_from_file(
AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE
)
assert isinstance(credentials, google.oauth2.credentials.Credentials)
assert project_id is None


def test__load_credentials_from_file_service_account():
credentials, project_id = _default._load_credentials_from_file(SERVICE_ACCOUNT_FILE)
Expand Down