From 494f1e4b9d5952fccc9df3035ddcebbd9f35ab81 Mon Sep 17 00:00:00 2001 From: adisun Date: Wed, 31 May 2023 18:15:47 -0400 Subject: [PATCH 1/6] add unit test --- gestalt/vault.py | 2 +- requirements.test.txt | 1 + tests/test_gestalt.py | 32 ++++++++++++++++++++++++++++++ tests/testvault/testsfdynamic.json | 3 +++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/testvault/testsfdynamic.json diff --git a/gestalt/vault.py b/gestalt/vault.py index b332926..574aa36 100644 --- a/gestalt/vault.py +++ b/gestalt/vault.py @@ -90,7 +90,7 @@ def get(self, key: str, path: str, filter: str) -> Any: dynamic_token = ("dynamic", response['lease_id'], response['lease_duration']) self.dynamic_token_queue.put_nowait(dynamic_token) - requested_data = response["data"]["data"] + requested_data = response["data"].get("data", response["data"]) except hvac.exceptions.InvalidPath: raise RuntimeError( "Gestalt Error: The secret path or mount is set incorrectly") diff --git a/requirements.test.txt b/requirements.test.txt index 48859d8..66ff369 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -4,6 +4,7 @@ mypy==0.910 mypy-extensions==0.4.3 pytest==6.1.0 pytest-cov==2.8.1 +pytest-mock==3.10.0 codecov==2.0.16 hvac>=1.0.2,<1.1.0 types-requests==2.25.2 diff --git a/tests/test_gestalt.py b/tests/test_gestalt.py index 5d44c39..887aac0 100644 --- a/tests/test_gestalt.py +++ b/tests/test_gestalt.py @@ -9,6 +9,28 @@ import os import gestalt import hvac +import requests + + +class MockSession(requests.Session): + def request(self, *_, **__): + resp = {'request_id': '230f5e67-e55d-bdae-bd24-c7bc13c1a3e9', 'lease_id': '', 'renewable': False, 'lease_duration': 0, 'data': {'last_vault_rotation': '2023-05-31T14:24:41.724285249Z', 'password': 'foo', 'rotation_period': 60, 'ttl': 0, 'username': 'foo'}, 'wrap_info': None, 'warnings': None, 'auth': None} + return MockResponse(resp, 200) + + +class MockResponse: + def __init__(self, json_data, status_code): + self.json_data = json_data + self.status_code = status_code + self.ok = True + + def json(self): + return self.json_data + + +@pytest.fixture +def mock_db_role_request(mocker): + mocker.patch("requests.Session", MockSession) # Testing member function @@ -564,6 +586,7 @@ def nested_setup(env_setup): path="testnested", secret=dict(slack={"token": "random-token"})) + def test_nest_key_for_vault(env_setup, nested_setup): g = gestalt.Gestalt() g.add_config_file("./tests/testvault/testnested.json") @@ -575,6 +598,15 @@ def test_nest_key_for_vault(env_setup, nested_setup): assert secret_slack == "random-token" +def test_read_no_nest_db_role(env_setup, mock_db_role_request): + g = gestalt.Gestalt() + g.add_config_file("./tests/testvault/testsfdynamic.json") + g.configure_provider("vault", Vault(role=None, jwt=None)) + g.build_config() + secret_username = g.get_string("snowflake.username") + assert secret_username == "foo" + + def test_set_vault_key(env_setup, nested_setup): g = gestalt.Gestalt() g.configure_provider("vault", Vault(role=None, jwt=None)) diff --git a/tests/testvault/testsfdynamic.json b/tests/testvault/testsfdynamic.json new file mode 100644 index 0000000..ebf9e9a --- /dev/null +++ b/tests/testvault/testsfdynamic.json @@ -0,0 +1,3 @@ +{ + "snowflake": "ref+vault://database/creds/my-role#" +} \ No newline at end of file From b9be19fa29fc6d4a517e88a27055e648848c38f4 Mon Sep 17 00:00:00 2001 From: adisun Date: Thu, 1 Jun 2023 09:38:15 -0400 Subject: [PATCH 2/6] downgrade pytest mock --- requirements.test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.test.txt b/requirements.test.txt index 66ff369..bb9d431 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -4,7 +4,7 @@ mypy==0.910 mypy-extensions==0.4.3 pytest==6.1.0 pytest-cov==2.8.1 -pytest-mock==3.10.0 +pytest-mock==3.2.0 codecov==2.0.16 hvac>=1.0.2,<1.1.0 types-requests==2.25.2 From 7abea6c8661d070f36f5b715067ba6e57af6a9d7 Mon Sep 17 00:00:00 2001 From: adisun Date: Thu, 1 Jun 2023 09:40:04 -0400 Subject: [PATCH 3/6] tests --- requirements.test.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.test.txt b/requirements.test.txt index bb9d431..48859d8 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -4,7 +4,6 @@ mypy==0.910 mypy-extensions==0.4.3 pytest==6.1.0 pytest-cov==2.8.1 -pytest-mock==3.2.0 codecov==2.0.16 hvac>=1.0.2,<1.1.0 types-requests==2.25.2 From 6ff80ccd67328e4ee5e197061b54390005b69fe1 Mon Sep 17 00:00:00 2001 From: adisun Date: Thu, 1 Jun 2023 09:48:49 -0400 Subject: [PATCH 4/6] rm coverage --- .github/workflows/pythonpackage.yml | 1 - requirements.test.txt | 2 +- setup.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index b688c40..ade84d2 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -60,7 +60,6 @@ jobs: VAULT_TOKEN: "myroot" run: | pytest -s --cov=gestalt tests/*.py - codecov - name: Typecheck with mypy run: | # run mypy strict mode on gestalt diff --git a/requirements.test.txt b/requirements.test.txt index 48859d8..b3d9cfb 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -4,7 +4,7 @@ mypy==0.910 mypy-extensions==0.4.3 pytest==6.1.0 pytest-cov==2.8.1 -codecov==2.0.16 +pytest-mock==3.2.0 hvac>=1.0.2,<1.1.0 types-requests==2.25.2 types-PyYAML==5.4.6 diff --git a/setup.py b/setup.py index d5378a5..6a3f1de 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ def readme(): reqs_list = list(map(lambda x: x.rstrip(), reqs)) setup(name='gestalt-cfg', - version='3.1.1', + version='3.1.2', description='A sensible configuration library for Python', long_description=readme(), long_description_content_type="text/markdown", From 048e19255e27d8cb44176641e63dfffa3766a3bc Mon Sep 17 00:00:00 2001 From: adisun Date: Thu, 1 Jun 2023 09:54:00 -0400 Subject: [PATCH 5/6] yapf --- tests/test_gestalt.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/test_gestalt.py b/tests/test_gestalt.py index 887aac0..b712102 100644 --- a/tests/test_gestalt.py +++ b/tests/test_gestalt.py @@ -14,7 +14,22 @@ class MockSession(requests.Session): def request(self, *_, **__): - resp = {'request_id': '230f5e67-e55d-bdae-bd24-c7bc13c1a3e9', 'lease_id': '', 'renewable': False, 'lease_duration': 0, 'data': {'last_vault_rotation': '2023-05-31T14:24:41.724285249Z', 'password': 'foo', 'rotation_period': 60, 'ttl': 0, 'username': 'foo'}, 'wrap_info': None, 'warnings': None, 'auth': None} + resp = { + 'request_id': '230f5e67-e55d-bdae-bd24-c7bc13c1a3e9', + 'lease_id': '', + 'renewable': False, + 'lease_duration': 0, + 'data': { + 'last_vault_rotation': '2023-05-31T14:24:41.724285249Z', + 'password': 'foo', + 'rotation_period': 60, + 'ttl': 0, + 'username': 'foo' + }, + 'wrap_info': None, + 'warnings': None, + 'auth': None + } return MockResponse(resp, 200) @@ -586,7 +601,6 @@ def nested_setup(env_setup): path="testnested", secret=dict(slack={"token": "random-token"})) - def test_nest_key_for_vault(env_setup, nested_setup): g = gestalt.Gestalt() g.add_config_file("./tests/testvault/testnested.json") From aa3ccfb6be55a6fb02edf78aa76e700b3118993e Mon Sep 17 00:00:00 2001 From: adisun Date: Thu, 1 Jun 2023 09:58:13 -0400 Subject: [PATCH 6/6] minor version bump --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 6a3f1de..83ee1cd 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ def readme(): reqs_list = list(map(lambda x: x.rstrip(), reqs)) setup(name='gestalt-cfg', - version='3.1.2', + version='3.2.0', description='A sensible configuration library for Python', long_description=readme(), long_description_content_type="text/markdown",