-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from statisticsnorway/173-add-use-of-google-s…
…ecret-manager 173 add use of google secret manager
- Loading branch information
Showing
6 changed files
with
1,336 additions
and
1,216 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "dapla-toolbelt" | ||
version = "3.0.2" | ||
version = "3.1.2" | ||
description = "Dapla Toolbelt" | ||
authors = ["Dapla Developers <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -27,6 +27,8 @@ pyjwt = ">=2.6.0" | |
tomli = ">=1.1.0" | ||
google-cloud-pubsub = ">=2.14.1" | ||
fsspec = ">=2023.12.2" | ||
google-cloud-secret-manager = "^2.21.0" | ||
pytest-mock = "^3.14.0" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pygments = ">=2.10.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Optional | ||
|
||
from google.cloud.secretmanager import SecretManagerServiceClient | ||
|
||
from .auth import AuthClient | ||
|
||
|
||
def get_secret_version( | ||
project_id: str, shortname: str, version_id: Optional[str] = "latest" | ||
) -> str: | ||
"""Access the payload for a given secret version. | ||
The user's google credentials are used to authorize that the user have permission | ||
to access the secret_id. | ||
Args: | ||
project_id (str): ID of the Google Cloud project where the secret is stored. | ||
shortname (str): Name (not full path) of the secret in Secret Manager. | ||
version_id (str, optional): The version of the secret to access. Defaults to 'latest'. | ||
Returns: | ||
str: The payload of the secret version as a UTF-8 decoded string. | ||
""" | ||
client = SecretManagerServiceClient( | ||
credentials=AuthClient.fetch_google_credentials() | ||
) | ||
secret_name = f"projects/{project_id}/secrets/{shortname}/versions/{version_id}" | ||
response = client.access_secret_version(name=secret_name) | ||
return str(response.payload.data.decode("UTF-8")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from unittest.mock import Mock | ||
|
||
from pytest_mock import MockerFixture | ||
|
||
from dapla.gsm import get_secret_version | ||
|
||
PKG = "dapla.gsm" | ||
|
||
|
||
def test_get_secret_version(mocker: MockerFixture) -> None: | ||
mock_smclient = mocker.patch(f"{PKG}.SecretManagerServiceClient") | ||
mock_authclient = mocker.patch(f"{PKG}.AuthClient") | ||
|
||
fake_creds = Mock() | ||
mock_authclient.fetch_google_credentials.return_value = fake_creds | ||
|
||
project_id = "tester-a92f" | ||
shortname = "supersecret" | ||
|
||
get_secret_version(project_id=project_id, shortname=shortname) | ||
mock_smclient.assert_called_once_with(credentials=fake_creds) |