Add support for the GCLOUD_PROJECT environment variable (#73)
diff --git a/google/auth/_default.py b/google/auth/_default.py index 356780b..b6014e7 100644 --- a/google/auth/_default.py +++ b/google/auth/_default.py
@@ -254,7 +254,9 @@ If no credentials were found, or if the credentials found were invalid. """ - explicit_project_id = os.environ.get(environment_vars.PROJECT) + explicit_project_id = os.environ.get( + environment_vars.PROJECT, + os.environ.get(environment_vars.LEGACY_PROJECT)) checkers = ( _get_explicit_environ_credentials,
diff --git a/google/auth/environment_vars.py b/google/auth/environment_vars.py index 9785c34..b4ed2b2 100644 --- a/google/auth/environment_vars.py +++ b/google/auth/environment_vars.py
@@ -22,6 +22,13 @@ environment variable is also used by the Google Cloud Python Library. """ +LEGACY_PROJECT = 'GCLOUD_PROJECT' +"""Previously used environment variable defining the default project. + +This environment variable is used instead of the current one in some +situations (such as Google App Engine). +""" + CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS' """Environment variable defining the location of Google application default credentials."""
diff --git a/tests/test__default.py b/tests/test__default.py index e244b3d..c33db13 100644 --- a/tests/test__default.py +++ b/tests/test__default.py
@@ -268,6 +268,15 @@ @mock.patch( 'google.auth._default._get_explicit_environ_credentials', + return_value=(mock.sentinel.credentials, mock.sentinel.project_id)) +def test_default_explict_legacy_project_id(get_mock, monkeypatch): + monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env') + assert _default.default() == ( + mock.sentinel.credentials, 'explicit-env') + + [email protected]( + 'google.auth._default._get_explicit_environ_credentials', return_value=(None, None)) @mock.patch( 'google.auth._default._get_gcloud_sdk_credentials',