diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index ade84d2..cebe508 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -16,7 +16,7 @@ jobs: # Label used to access the service container vault: # Docker Hub image - image: vault + image: hashicorp/vault:1.13.3 options: --name vault-test ports: # Opens tcp port 8200 on the host and service container diff --git a/gestalt/__init__.py b/gestalt/__init__.py index 2562277..1e5b10d 100644 --- a/gestalt/__init__.py +++ b/gestalt/__init__.py @@ -3,7 +3,7 @@ import os import glob -from typing import Dict, List, Type, Union, Optional, MutableMapping, Text, Any +from typing import Dict, List, Type, Union, Optional, Text, Any import yaml import re import json diff --git a/gestalt/vault.py b/gestalt/vault.py index 7ab141c..9ee1575 100644 --- a/gestalt/vault.py +++ b/gestalt/vault.py @@ -5,7 +5,7 @@ from jsonpath_ng import parse # type: ignore from typing import Optional, Tuple, Any, Dict, Union, List import hvac # type: ignore -import asyncio +from queue import Queue import os from threading import Thread from retry import retry @@ -31,8 +31,8 @@ def __init__(self, with role and jwt string from kubernetes """ self._scheme: str = scheme - self.dynamic_token_queue: asyncio.Queue[Any] = asyncio.Queue(maxsize=0) - self.kubes_token_queue: asyncio.Queue[Any] = asyncio.Queue(maxsize=0) + self.dynamic_token_queue: Queue[Tuple[str, str, str]] = Queue() + self.kubes_token_queue: Queue[Tuple[str, str, str]] = Queue() self.vault_client = hvac.Client(url=url, token=token, @@ -66,16 +66,15 @@ def __init__(self, except requests.exceptions.ConnectionError: raise RuntimeError("Gestalt Error: Couldn't connect to Vault") - dynamic_ttl_renew = Thread(name='dynamic-token-renew', - target=asyncio.run, - daemon=True, - args=(self.worker( - self.dynamic_token_queue), )) + dynamic_ttl_renew = Thread( + name='dynamic-token-renew', + target=self.worker, + daemon=True, + args=(self.dynamic_token_queue, )) # noqa: F841 kubernetes_ttl_renew = Thread(name="kubes-token-renew", - target=asyncio.run, + target=self.worker, daemon=True, - args=(self.worker( - self.kubes_token_queue), )) + args=(self.kubes_token_queue, )) kubernetes_ttl_renew.start() @retry(RuntimeError, delay=3, tries=3) # type: ignore @@ -154,7 +153,7 @@ def _set_secrets_ttl(self, requested_data: Dict[str, Any], secret_expires_dt = last_vault_rotation_dt + timedelta(seconds=ttl) self._secret_expiry_times[key] = secret_expires_dt - async def worker(self, token_queue: Any) -> None: + def worker(self, token_queue: Queue) -> None: # type: ignore """ Worker function to renew lease on expiry """ @@ -162,7 +161,7 @@ async def worker(self, token_queue: Any) -> None: try: while True: if not token_queue.empty(): - token_type, token_id, token_duration = token = await token_queue.get( + token_type, token_id, token_duration = token = token_queue.get( ) if token_type == "kubernetes": self.vault_client.auth.token.renew(token_id) diff --git a/setup.py b/setup.py index 00bd948..f77ba0e 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.3.0', + version='3.3.1', description='A sensible configuration library for Python', long_description=readme(), long_description_content_type="text/markdown", diff --git a/tests/test_gestalt.py b/tests/test_gestalt.py index a5fc2de..61bdb0a 100644 --- a/tests/test_gestalt.py +++ b/tests/test_gestalt.py @@ -4,11 +4,11 @@ from gestalt.vault import Vault from gestalt import merge_into -import asyncio import pytest import os import gestalt import hvac +from queue import Queue # Testing member function @@ -541,8 +541,7 @@ def test_set_vault_key(nested_setup): assert secret == "ref+vault://secret/data/testnested#.slack.token" -@pytest.mark.asyncio -async def test_vault_worker_dynamic(mock_vault_workers, mock_vault_k8s_auth): +def test_vault_worker_dynamic(mock_vault_workers, mock_vault_k8s_auth): mock_dynamic_renew, mock_k8s_renew = mock_vault_workers mock_sleep = None @@ -560,11 +559,11 @@ def except_once(self, **kwargs): mock_k8s_renew.start.assert_called() - test_token_queue = asyncio.Queue(maxsize=0) - await test_token_queue.put(("dynamic", 1, 100)) + test_token_queue = Queue(maxsize=0) + test_token_queue.put(("dynamic", 1, 100)) with pytest.raises(RuntimeError): - await v.worker(test_token_queue) + v.worker(test_token_queue) mock_sleep.assert_called() mock_client().sys.renew_lease.assert_called() @@ -574,8 +573,7 @@ def except_once(self, **kwargs): mock_k8s_renew.stop() -@pytest.mark.asyncio -async def test_vault_worker_k8s(mock_vault_workers): +def test_vault_worker_k8s(mock_vault_workers): mock_dynamic_renew, mock_k8s_renew = mock_vault_workers mock_sleep = None @@ -592,11 +590,11 @@ def except_once(self, **kwargs): mock_k8s_renew.start.assert_called() - test_token_queue = asyncio.Queue(maxsize=0) - await test_token_queue.put(("kubernetes", 1, 100)) + test_token_queue = Queue(maxsize=0) + test_token_queue.put(("kubernetes", 1, 100)) with pytest.raises(RuntimeError): - await v.worker(test_token_queue) + v.worker(test_token_queue) mock_sleep.assert_called() mock_client().auth.token.renew.assert_called() @@ -606,8 +604,7 @@ def except_once(self, **kwargs): mock_k8s_renew.stop() -@pytest.mark.asyncio -async def test_vault_start_dynamic_lease(mock_vault_workers): +def test_vault_start_dynamic_lease(mock_vault_workers): mock_response = { "lease_id": "1", "lease_duration": 5, @@ -622,7 +619,7 @@ async def test_vault_start_dynamic_lease(mock_vault_workers): mock_dynamic_token_queue = Mock() mock_kube_token_queue = Mock() with patch( - "gestalt.vault.asyncio.Queue", + "gestalt.vault.Queue", side_effect=[mock_dynamic_token_queue, mock_kube_token_queue]) as mock_queues: