Skip to content

Commit 8153a3a

Browse files
authored
Merge pull request #184 from statisticsnorway/173-add-use-of-google-secret-manager
173 add use of google secret manager
2 parents f63332e + 980cf79 commit 8153a3a

File tree

6 files changed

+1336
-1216
lines changed

6 files changed

+1336
-1216
lines changed

poetry.lock

Lines changed: 1280 additions & 1214 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dapla-toolbelt"
3-
version = "3.0.2"
3+
version = "3.1.2"
44
description = "Dapla Toolbelt"
55
authors = ["Dapla Developers <dapla-platform-developers@ssb.no>"]
66
license = "MIT"
@@ -27,6 +27,8 @@ pyjwt = ">=2.6.0"
2727
tomli = ">=1.1.0"
2828
google-cloud-pubsub = ">=2.14.1"
2929
fsspec = ">=2023.12.2"
30+
google-cloud-secret-manager = "^2.21.0"
31+
pytest-mock = "^3.14.0"
3032

3133
[tool.poetry.group.dev.dependencies]
3234
pygments = ">=2.10.0"

src/dapla/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .doctor import Doctor
1414
from .files import FileClient
1515
from .git import repo_root_dir
16+
from .gsm import get_secret_version
1617
from .guardian import GuardianClient
1718
from .jupyterhub import generate_api_token
1819
from .pandas import read_pandas
@@ -33,6 +34,7 @@
3334
"read_pandas",
3435
"write_pandas",
3536
"trigger_source_data_processing",
37+
"get_secret_version",
3638
]
3739

3840

src/dapla/gsm.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Optional
2+
3+
from google.cloud.secretmanager import SecretManagerServiceClient
4+
5+
from .auth import AuthClient
6+
7+
8+
def get_secret_version(
9+
project_id: str, shortname: str, version_id: Optional[str] = "latest"
10+
) -> str:
11+
"""Access the payload for a given secret version.
12+
13+
The user's google credentials are used to authorize that the user have permission
14+
to access the secret_id.
15+
16+
Args:
17+
project_id (str): ID of the Google Cloud project where the secret is stored.
18+
shortname (str): Name (not full path) of the secret in Secret Manager.
19+
version_id (str, optional): The version of the secret to access. Defaults to 'latest'.
20+
21+
Returns:
22+
str: The payload of the secret version as a UTF-8 decoded string.
23+
"""
24+
client = SecretManagerServiceClient(
25+
credentials=AuthClient.fetch_google_credentials()
26+
)
27+
secret_name = f"projects/{project_id}/secrets/{shortname}/versions/{version_id}"
28+
response = client.access_secret_version(name=secret_name)
29+
return str(response.payload.data.decode("UTF-8"))

src/dapla/pandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def read_pandas(
9191
gcs_path = FileClient._remove_gcs_uri_prefix(gcs_path)
9292

9393
parquet_ds = pq.ParquetDataset(
94-
gcs_path,
94+
gcs_path, # type: ignore [arg-type]
9595
filesystem=fs,
9696
filters=filters, # type: ignore [arg-type]
9797
) # Stubs show the incorrect type -

tests/test_gsm.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from unittest.mock import Mock
2+
3+
from pytest_mock import MockerFixture
4+
5+
from dapla.gsm import get_secret_version
6+
7+
PKG = "dapla.gsm"
8+
9+
10+
def test_get_secret_version(mocker: MockerFixture) -> None:
11+
mock_smclient = mocker.patch(f"{PKG}.SecretManagerServiceClient")
12+
mock_authclient = mocker.patch(f"{PKG}.AuthClient")
13+
14+
fake_creds = Mock()
15+
mock_authclient.fetch_google_credentials.return_value = fake_creds
16+
17+
project_id = "tester-a92f"
18+
shortname = "supersecret"
19+
20+
get_secret_version(project_id=project_id, shortname=shortname)
21+
mock_smclient.assert_called_once_with(credentials=fake_creds)

0 commit comments

Comments
 (0)