Skip to content

Commit

Permalink
Check for the project ID env var before warning about missing project…
Browse files Browse the repository at this point in the history
… ID (googleapis#227)

* Check for project_id envvar before warning

Resolves googleapis#221

* Add missed env check and fix line length

* Remove redundant missing-project log statements

* Add _default.default() test without project_id

* Fix line length
  • Loading branch information
JacobHayes authored and Jon Wayne Parrott committed Dec 13, 2017
1 parent 369e2a7 commit 15af07b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
25 changes: 8 additions & 17 deletions google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ def _get_gcloud_sdk_credentials():
if not project_id:
project_id = _cloud_sdk.get_project_id()

if not project_id:
_LOGGER.warning(
'No project ID could be determined from the Cloud SDK '
'configuration. Consider running `gcloud config set project` or '
'setting the %s environment variable', environment_vars.PROJECT)

return credentials, project_id


Expand All @@ -147,12 +141,6 @@ def _get_explicit_environ_credentials():
credentials, project_id = _load_credentials_from_file(
os.environ[environment_vars.CREDENTIALS])

if not project_id:
_LOGGER.warning(
'No project ID could be determined from the credentials at %s '
'Consider setting the %s environment variable',
environment_vars.CREDENTIALS, environment_vars.PROJECT)

return credentials, project_id

else:
Expand Down Expand Up @@ -188,10 +176,6 @@ def _get_gce_credentials(request=None):
try:
project_id = _metadata.get_project_id(request=request)
except exceptions.TransportError:
_LOGGER.warning(
'No project ID could be determined from the Compute Engine '
'metadata service. Consider setting the %s environment '
'variable.', environment_vars.PROJECT)
project_id = None

return compute_engine.Credentials(), project_id
Expand Down Expand Up @@ -287,6 +271,13 @@ def default(scopes=None, request=None):
credentials, project_id = checker()
if credentials is not None:
credentials = with_scopes_if_required(credentials, scopes)
return credentials, explicit_project_id or project_id
effective_project_id = explicit_project_id or project_id
if not effective_project_id:
_LOGGER.warning(
'No project ID could be determined. Consider running '
'`gcloud config set project` or setting the %s '
'environment variable',
environment_vars.PROJECT)
return credentials, effective_project_id

raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
24 changes: 23 additions & 1 deletion tests/test__default.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,28 @@ def test_default_explict_legacy_project_id(unused_get, monkeypatch):
mock.sentinel.credentials, 'explicit-env')


@mock.patch(
'logging.Logger.warning',
autospec=True)
@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
return_value=(mock.sentinel.credentials, None), autospec=True)
@mock.patch(
'google.auth._default._get_gcloud_sdk_credentials',
return_value=(mock.sentinel.credentials, None), autospec=True)
@mock.patch(
'google.auth._default._get_gae_credentials',
return_value=(mock.sentinel.credentials, None), autospec=True)
@mock.patch(
'google.auth._default._get_gce_credentials',
return_value=(mock.sentinel.credentials, None), autospec=True)
def test_default_without_project_id(
unused_gce, unused_gae, unused_sdk, unused_explicit, logger_warning):
assert _default.default() == (
mock.sentinel.credentials, None)
logger_warning.assert_called_with(mock.ANY, mock.ANY, mock.ANY)


@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
return_value=(None, None), autospec=True)
Expand All @@ -324,7 +346,7 @@ def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
autospec=True)
@mock.patch(
'google.auth.credentials.with_scopes_if_required', autospec=True)
def test_default_scoped(with_scopes, get):
def test_default_scoped(with_scopes, unused_get):
scopes = ['one', 'two']

credentials, project_id = _default.default(scopes=scopes)
Expand Down

0 comments on commit 15af07b

Please sign in to comment.