From 1768787fcfe40e008b8810d1daf9f451203c7a75 Mon Sep 17 00:00:00 2001 From: Kevin Cameron Grismore Date: Mon, 16 Oct 2023 15:04:03 -0400 Subject: [PATCH 1/4] get project from google.auth.default if quota project is None --- CHANGELOG.md | 1 + prefect_gcp/credentials.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e43bdd..733dd8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix `list_folders` and `list_blobs` now logging bucket name and bucket path - [#184](https://github.com/PrefectHQ/prefect-gcp/pull/214) +- Fix empty `GcpCredentials` not inferring the GCP project upon initialization when running on Compute Engine, Cloud Run, and App Engine ### Security diff --git a/prefect_gcp/credentials.py b/prefect_gcp/credentials.py index 8fa805f..72743e9 100644 --- a/prefect_gcp/credentials.py +++ b/prefect_gcp/credentials.py @@ -164,8 +164,12 @@ def block_initialization(self): if self.project is None: if self.service_account_info or self.service_account_file: credentials_project = credentials.project_id - else: # google.auth.default using gcloud auth application-default login + # google.auth.default using gcloud auth application-default login + elif credentials.quota_project_id: credentials_project = credentials.quota_project_id + # compute-assigned service account via GCP metadata server + else: + _, credentials_project = google.auth.default() self.project = credentials_project if hasattr(credentials, "service_account_email"): From f03df60d4bad4259c43ae227c8259dbc3692cef2 Mon Sep 17 00:00:00 2001 From: Kevin Cameron Grismore Date: Mon, 16 Oct 2023 15:31:30 -0400 Subject: [PATCH 2/4] add link to PR in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 733dd8b..91c711d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix `list_folders` and `list_blobs` now logging bucket name and bucket path - [#184](https://github.com/PrefectHQ/prefect-gcp/pull/214) -- Fix empty `GcpCredentials` not inferring the GCP project upon initialization when running on Compute Engine, Cloud Run, and App Engine +- Fix empty `GcpCredentials` not inferring the GCP project upon initialization when running on Compute Engine, Cloud Run, and App Engine [#219](https://github.com/PrefectHQ/prefect-gcp/pull/219) ### Security From bd4f3a61808b92f787443f6ec1aa98bcfc6f9354 Mon Sep 17 00:00:00 2001 From: Kevin Cameron Grismore Date: Mon, 16 Oct 2023 15:34:18 -0400 Subject: [PATCH 3/4] fix changelog typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91c711d..75619f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix `list_folders` and `list_blobs` now logging bucket name and bucket path - [#184](https://github.com/PrefectHQ/prefect-gcp/pull/214) -- Fix empty `GcpCredentials` not inferring the GCP project upon initialization when running on Compute Engine, Cloud Run, and App Engine [#219](https://github.com/PrefectHQ/prefect-gcp/pull/219) +- Fix empty `GcpCredentials` not inferring the GCP project upon initialization when running on Compute Engine, Cloud Run, and App Engine - [#219](https://github.com/PrefectHQ/prefect-gcp/pull/219) ### Security From 4b16c59dfa8cd63f18add45e2f4e1a34a8e3d176 Mon Sep 17 00:00:00 2001 From: Kevin Cameron Grismore Date: Mon, 16 Oct 2023 15:57:37 -0400 Subject: [PATCH 4/4] added test for no quota project --- tests/conftest.py | 14 ++++++++++++++ tests/test_credentials.py | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 7e3ade2..9433e4a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,6 +42,20 @@ def google_auth(monkeypatch): return google_auth_mock +@pytest.fixture +def google_auth_no_quota_project(monkeypatch): + google_auth_mock = MagicMock() + default_credentials_mock = MagicMock( + client_id="my_client_id", quota_project_id=None + ) + google_auth_mock.default.side_effect = lambda *args, **kwargs: ( + default_credentials_mock, + "my_project", + ) + monkeypatch.setattr("google.auth", google_auth_mock) + return google_auth_mock + + @pytest.fixture def oauth2_credentials(monkeypatch): CredentialsMock = MagicMock() diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 73dacfc..9009f49 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -108,6 +108,13 @@ def test_block_initialization_gcloud_cli(google_auth, oauth2_credentials): assert gcp_credentials.project == "my_project" +def test_block_initialization_metadata_server( + google_auth_no_quota_project, oauth2_credentials +): + gcp_credentials = GcpCredentials() + assert gcp_credentials.project == "my_project" + + @pytest.mark.parametrize("override_project", [None, "override_project"]) def test_get_cloud_storage_client( override_project, service_account_info, oauth2_credentials, storage_client