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

Commit

Permalink
Fix Tilde Paths for service accounts (#38)
Browse files Browse the repository at this point in the history
* check for tilde expanded path exists because ~ is not expanded by default in `os.path.exists`

* update changelog

* add test cases to getting service account files

* Simplify logic

* Convert to str

* Update credentials.py

* Update test_credentials.py

Co-authored-by: Andrew <[email protected]>
  • Loading branch information
wangjoshuah and ahuang11 authored Sep 7, 2022
1 parent 564dccd commit 78a3fbe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- Allowed `~` character to be used in the path for service account file - [#38](https://github.com/PrefectHQ/prefect-gcp/pull/38)

### Deprecated

Expand Down
8 changes: 2 additions & 6 deletions prefect_gcp/credentials.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module handling GCP credentials"""

import functools
import os
from pathlib import Path
from typing import Dict, Optional, Union

Expand Down Expand Up @@ -101,12 +100,9 @@ def _get_credentials_from_service_account(
"can be specified at once"
)
elif service_account_file:
if not os.path.exists(service_account_file):
service_account_file = Path(service_account_file).expanduser()
if not service_account_file.exists():
raise ValueError("The provided path to the service account is invalid")
elif isinstance(service_account_file, Path):
service_account_file = service_account_file.expanduser()
else:
service_account_file = os.path.expanduser(service_account_file)
credentials = Credentials.from_service_account_file(service_account_file)
elif service_account_info:
credentials = Credentials.from_service_account_info(service_account_info)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_credentials.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from pathlib import Path

import pytest
Expand All @@ -7,10 +8,19 @@

from prefect_gcp import GcpCredentials


def _get_first_file_in_root():
for path in os.listdir(os.path.expanduser("~")):
if os.path.isfile(os.path.join(os.path.expanduser("~"), path)):
return os.path.join("~", path)


SERVICE_ACCOUNT_FILES = [
Path(__file__).parent.absolute() / "test_credentials.py",
]
SERVICE_ACCOUNT_FILES.append(str(SERVICE_ACCOUNT_FILES[0]))
SERVICE_ACCOUNT_FILES.append(_get_first_file_in_root())
SERVICE_ACCOUNT_FILES.append(os.path.expanduser(_get_first_file_in_root()))


@pytest.fixture()
Expand All @@ -37,7 +47,7 @@ def test_get_credentials_from_service_account_file(
credentials = GcpCredentials._get_credentials_from_service_account(
service_account_file=service_account_file
)
assert credentials == service_account_file
assert str(credentials) == os.path.expanduser(service_account_file)


def test_get_credentials_from_service_account_info(
Expand Down

0 comments on commit 78a3fbe

Please sign in to comment.