Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Get project id from google.auth.default() in empty GcpCredentials block if quota project is None #219

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 - [#219](https://github.com/PrefectHQ/prefect-gcp/pull/219)

### Security

Expand Down
6 changes: 5 additions & 1 deletion prefect_gcp/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down