Skip to content

Commit 3357336

Browse files
authored
fix: Improve ADC related errors and warnings (#1237)
* fix: Improve ADC related errors and warnings * Run lint and blacken * Refresh system test credentials
1 parent 0289abb commit 3357336

4 files changed

Lines changed: 13 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,18 @@
4848
)
4949

5050
# Help message when no credentials can be found.
51-
_HELP_MESSAGE = """\
52-
Could not automatically determine credentials. Please set {env} or \
53-
explicitly create credentials and re-run the application. For more \
54-
information, please see \
55-
https://cloud.google.com/docs/authentication/getting-started
56-
""".format(
57-
env=environment_vars.CREDENTIALS
58-
).strip()
51+
_CLOUD_SDK_MISSING_CREDENTIALS = """\
52+
Your default credentials were not found. To set up Application Default Credentials, \
53+
see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.\
54+
"""
5955

6056
# Warning when using Cloud SDK user credentials
6157
_CLOUD_SDK_CREDENTIALS_WARNING = """\
6258
Your application has authenticated using end user credentials from Google \
6359
Cloud SDK without a quota project. You might receive a "quota exceeded" \
64-
or "API not enabled" error. We recommend you rerun \
65-
`gcloud auth application-default login` and make sure a quota project is \
66-
added. Or you can use service accounts instead. For more information \
67-
about service accounts, see https://cloud.google.com/docs/authentication/"""
60+
or "API not enabled" error. See the following page for troubleshooting: \
61+
https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds. \
62+
"""
6863

6964
# The subject token type used for AWS external_account credentials.
7065
_AWS_SUBJECT_TOKEN_TYPE = "urn:ietf:params:aws:token-type:aws4_request"
@@ -650,4 +645,4 @@ def default(scopes=None, request=None, quota_project_id=None, default_scopes=Non
650645
)
651646
return credentials, effective_project_id
652647

653-
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
648+
raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)

packages/google-auth/google/auth/_default_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,4 @@ def default_async(scopes=None, request=None, quota_project_id=None):
281281
)
282282
return credentials, effective_project_id
283283

284-
raise exceptions.DefaultCredentialsError(_default._HELP_MESSAGE)
284+
raise exceptions.DefaultCredentialsError(_default._CLOUD_SDK_MISSING_CREDENTIALS)
0 Bytes
Binary file not shown.

packages/google-auth/tests/test__default.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,11 @@ def test_default_without_project_id(
916916
autospec=True,
917917
)
918918
def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
919-
with pytest.raises(exceptions.DefaultCredentialsError):
919+
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
920920
assert _default.default()
921921

922+
assert excinfo.match(_default._CLOUD_SDK_MISSING_CREDENTIALS)
923+
922924

923925
@mock.patch(
924926
"google.auth._default._get_explicit_environ_credentials",
@@ -1128,7 +1130,7 @@ def test_default_environ_external_credentials_bad_format(monkeypatch, tmpdir):
11281130
def test_default_warning_without_quota_project_id_for_user_creds(get_adc_path):
11291131
get_adc_path.return_value = AUTHORIZED_USER_CLOUD_SDK_FILE
11301132

1131-
with pytest.warns(UserWarning, match="Cloud SDK"):
1133+
with pytest.warns(UserWarning, match=_default._CLOUD_SDK_CREDENTIALS_WARNING):
11321134
credentials, project_id = _default.default(quota_project_id=None)
11331135

11341136

0 commit comments

Comments
 (0)