From ab2be5de829e830979514683582c11f98fa943c7 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Wed, 15 Jul 2020 16:49:27 -0700 Subject: [PATCH 1/2] fix: don't add empty quota project (#560) --- google/auth/_default.py | 15 ++++++++++----- tests/test__default.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/google/auth/_default.py b/google/auth/_default.py index f3e498bb5..694033f3c 100644 --- a/google/auth/_default.py +++ b/google/auth/_default.py @@ -116,11 +116,13 @@ def load_credentials_from_file(filename, scopes=None, quota_project_id=None): try: credentials = credentials.Credentials.from_authorized_user_info( info, scopes=scopes - ).with_quota_project(quota_project_id) + ) except ValueError as caught_exc: msg = "Failed to load authorized user credentials from {}".format(filename) new_exc = exceptions.DefaultCredentialsError(msg, caught_exc) six.raise_from(new_exc, caught_exc) + if quota_project_id: + credentials = credentials.with_quota_project(quota_project_id) if not credentials.quota_project_id: _warn_about_problematic_credentials(credentials) return credentials, None @@ -131,11 +133,13 @@ def load_credentials_from_file(filename, scopes=None, quota_project_id=None): try: credentials = service_account.Credentials.from_service_account_info( info, scopes=scopes - ).with_quota_project(quota_project_id) + ) except ValueError as caught_exc: msg = "Failed to load service account credentials from {}".format(filename) new_exc = exceptions.DefaultCredentialsError(msg, caught_exc) six.raise_from(new_exc, caught_exc) + if quota_project_id: + credentials = credentials.with_quota_project(quota_project_id) return credentials, info.get("project_id") else: @@ -317,9 +321,10 @@ def default(scopes=None, request=None, quota_project_id=None): for checker in checkers: credentials, project_id = checker() if credentials is not None: - credentials = with_scopes_if_required( - credentials, scopes - ).with_quota_project(quota_project_id) + credentials = with_scopes_if_required(credentials, scopes) + if quota_project_id: + credentials = credentials.with_quota_project(quota_project_id) + effective_project_id = explicit_project_id or project_id if not effective_project_id: _LOGGER.warning( diff --git a/tests/test__default.py b/tests/test__default.py index 0665efab2..55a14c207 100644 --- a/tests/test__default.py +++ b/tests/test__default.py @@ -165,6 +165,15 @@ def test_load_credentials_from_file_service_account_with_scopes(): assert credentials.scopes == ["https://www.google.com/calendar/feeds"] +def test_load_credentials_from_file_service_account_with_quota_project(): + credentials, project_id = _default.load_credentials_from_file( + SERVICE_ACCOUNT_FILE, quota_project_id="project-foo" + ) + assert isinstance(credentials, service_account.Credentials) + assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"] + assert credentials.quota_project_id == "project-foo" + + def test_load_credentials_from_file_service_account_bad_format(tmpdir): filename = tmpdir.join("serivce_account_bad.json") filename.write(json.dumps({"type": "service_account"})) @@ -465,6 +474,18 @@ def test_default_scoped(with_scopes, unused_get): with_scopes.assert_called_once_with(MOCK_CREDENTIALS, scopes) +@mock.patch( + "google.auth._default._get_explicit_environ_credentials", + return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id), + autospec=True, +) +def test_default_quota_project(with_quota_project): + credentials, project_id = _default.default(quota_project_id="project-foo") + + MOCK_CREDENTIALS.with_quota_project.assert_called_once_with("project-foo") + assert project_id == mock.sentinel.project_id + + @mock.patch( "google.auth._default._get_explicit_environ_credentials", return_value=(MOCK_CREDENTIALS, mock.sentinel.project_id), From c497661db8feba2ba27c6aa37b2002df8556e4bf Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Wed, 15 Jul 2020 17:55:49 -0700 Subject: [PATCH 2/2] chore: release 1.19.1 (#562) --- CHANGELOG.md | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cef09d5ef..593596e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ [1]: https://pypi.org/project/google-auth/#history +### [1.19.1](https://www.github.com/googleapis/google-auth-library-python/compare/v1.19.0...v1.19.1) (2020-07-15) + + +### Bug Fixes + +* don't add empty quota project ([#560](https://www.github.com/googleapis/google-auth-library-python/issues/560)) ([ab2be5d](https://www.github.com/googleapis/google-auth-library-python/commit/ab2be5de829e830979514683582c11f98fa943c7)) + ## [1.19.0](https://www.github.com/googleapis/google-auth-library-python/compare/v1.18.0...v1.19.0) (2020-07-09) diff --git a/setup.py b/setup.py index 9327cf6d1..52033def6 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ with io.open("README.rst", "r") as fh: long_description = fh.read() -version = "1.19.0" +version = "1.19.1" setup( name="google-auth",