From e9e808ee2a0b1359353c48918042ceb1cb3f8c14 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Mon, 11 Nov 2024 15:24:48 -0800 Subject: [PATCH 1/5] Add support for configured hana username/password Add unit test case for invalid login --- neon_utils/hana_utils.py | 17 +++++++++++------ tests/hana_util_tests.py | 14 +++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/neon_utils/hana_utils.py b/neon_utils/hana_utils.py index b4d16666..d2edb345 100644 --- a/neon_utils/hana_utils.py +++ b/neon_utils/hana_utils.py @@ -25,6 +25,8 @@ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +from _socket import gethostname +from datetime import datetime import requests import json @@ -33,6 +35,7 @@ from os import makedirs from os.path import join, isfile, isdir, dirname from time import time + from ovos_utils.log import LOG from ovos_utils.xdg_utils import xdg_cache_home @@ -91,20 +94,22 @@ def _init_client(backend_address: str): _headers = {"Authorization": f"Bearer {_client_config['access_token']}"} -def _get_token(backend_address: str, username: str = "guest", - password: str = "password"): +def _get_token(backend_address: str): """ Get new auth tokens from the specified server. This will cache the returned token, overwriting any previous data at the cache path. @param backend_address: Hana server URL to connect to - @param username: Username to authorize - @param password: Password for specified username """ + from ovos_config.config import Configuration global _client_config - # TODO: username/password from configuration + hana_config = Configuration().get('hana', {}) + username = hana_config.get("username") or "guest" + password = hana_config.get("password") or "password" + token_name = f"{gethostname()} {datetime.utcnow().isoformat()}" resp = requests.post(f"{backend_address}/auth/login", json={"username": username, - "password": password}) + "password": password, + "token_name": token_name}) if not resp.ok: raise ServerException(f"Error logging into {backend_address}. " f"{resp.status_code}: {resp.text}") diff --git a/tests/hana_util_tests.py b/tests/hana_util_tests.py index 2944c046..25f50384 100644 --- a/tests/hana_util_tests.py +++ b/tests/hana_util_tests.py @@ -96,18 +96,26 @@ def test_request_backend_refresh_token(self, refresh_token, config_path): neon_utils.hana_utils._client_config = real_client_config @patch("neon_utils.hana_utils._get_client_config_path") - def test_00_get_token(self, config_path): + @patch("ovos_config.configuration.Configuration") + def test_00_get_token(self, config, config_path): + config.return_value = {} config_path.return_value = self.test_path from neon_utils.hana_utils import _get_token - # Test valid request + # Test valid default request _get_token(self.test_server) from neon_utils.hana_utils import _client_config self.assertTrue(isfile(self.test_path)) with open(self.test_path) as f: credentials_on_disk = json.load(f) self.assertEqual(credentials_on_disk, _client_config) - # TODO: Test invalid request, rate-limited request + + # Test with configured invalid login + config.return_value = {"hana": {"username": "guest", + "password": "password"}} + from neon_utils.hana_utils import ServerException + with self.assertRaises(ServerException): + _get_token(self.test_server) @patch("neon_utils.hana_utils._get_client_config_path") @patch("neon_utils.hana_utils._get_token") From f962b157e770c49f7f53b2d2f96a331175d40a0e Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Mon, 11 Nov 2024 15:36:40 -0800 Subject: [PATCH 2/5] Fix typo in test config patching --- tests/hana_util_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/hana_util_tests.py b/tests/hana_util_tests.py index 25f50384..73bf883a 100644 --- a/tests/hana_util_tests.py +++ b/tests/hana_util_tests.py @@ -96,7 +96,7 @@ def test_request_backend_refresh_token(self, refresh_token, config_path): neon_utils.hana_utils._client_config = real_client_config @patch("neon_utils.hana_utils._get_client_config_path") - @patch("ovos_config.configuration.Configuration") + @patch("ovos_config.config.Configuration") def test_00_get_token(self, config, config_path): config.return_value = {} config_path.return_value = self.test_path From 1b04d04aa01b74989cfe1b6d4632cb196f283687 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Wed, 13 Nov 2024 08:50:22 -0800 Subject: [PATCH 3/5] Refactor token name to be URL-safe Fix logical error in unit test --- neon_utils/hana_utils.py | 2 +- tests/hana_util_tests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/neon_utils/hana_utils.py b/neon_utils/hana_utils.py index d2edb345..971b1f05 100644 --- a/neon_utils/hana_utils.py +++ b/neon_utils/hana_utils.py @@ -105,7 +105,7 @@ def _get_token(backend_address: str): hana_config = Configuration().get('hana', {}) username = hana_config.get("username") or "guest" password = hana_config.get("password") or "password" - token_name = f"{gethostname()} {datetime.utcnow().isoformat()}" + token_name = f"{gethostname()}_{datetime.utcnow().isoformat()}" resp = requests.post(f"{backend_address}/auth/login", json={"username": username, "password": password, diff --git a/tests/hana_util_tests.py b/tests/hana_util_tests.py index 73bf883a..8effe5ab 100644 --- a/tests/hana_util_tests.py +++ b/tests/hana_util_tests.py @@ -112,7 +112,7 @@ def test_00_get_token(self, config, config_path): # Test with configured invalid login config.return_value = {"hana": {"username": "guest", - "password": "password"}} + "password": "fake_password"}} from neon_utils.hana_utils import ServerException with self.assertRaises(ServerException): _get_token(self.test_server) From 08bf93206b5b33f1f7443546484d30d3d34131cc Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Wed, 13 Nov 2024 08:57:31 -0800 Subject: [PATCH 4/5] Refactor log aggregators init --- neon_utils/log_aggregators/__init__.py | 11 +++++++---- neon_utils/log_aggregators/sentry.py | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/neon_utils/log_aggregators/__init__.py b/neon_utils/log_aggregators/__init__.py index 10d80db5..48a4d79f 100644 --- a/neon_utils/log_aggregators/__init__.py +++ b/neon_utils/log_aggregators/__init__.py @@ -37,10 +37,13 @@ def init_log_aggregators(config: dict = None): from ovos_config.config import Configuration config = config or Configuration() for service_name, handler in _service_name_to_handler.items(): - service_config = _get_log_aggregator_config(config=config, name=service_name) - if bool(service_config.pop('enabled', False)): - service_module = importlib.import_module(f'.{service_name}', __name__) - getattr(service_module, handler)(config=service_config) + try: + service_config = _get_log_aggregator_config(config=config, name=service_name) + if bool(service_config.pop('enabled', False)): + service_module = importlib.import_module(f'.{service_name}', __name__) + getattr(service_module, handler)(config=service_config) + except Exception as e: + pass def _get_log_aggregator_config(config: dict, name: str): diff --git a/neon_utils/log_aggregators/sentry.py b/neon_utils/log_aggregators/sentry.py index 29fecb74..b3294367 100644 --- a/neon_utils/log_aggregators/sentry.py +++ b/neon_utils/log_aggregators/sentry.py @@ -26,12 +26,13 @@ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import sentry_sdk SENTRY_SDK_REQUIRED_KEYS = {'dsn'} def init_sentry(config: dict): + import sentry_sdk + missing_required_keys = SENTRY_SDK_REQUIRED_KEYS.difference(config.keys()) if missing_required_keys: raise KeyError(f'Sentry SDK configuration missing required keys: {missing_required_keys}') From 5b9787eaba4762cb222f578cad621d7762bac4b3 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Wed, 13 Nov 2024 09:47:03 -0800 Subject: [PATCH 5/5] Update test case to account for refreshed token expiration rounding to the same value --- tests/hana_util_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/hana_util_tests.py b/tests/hana_util_tests.py index 8effe5ab..9bb47c41 100644 --- a/tests/hana_util_tests.py +++ b/tests/hana_util_tests.py @@ -152,8 +152,8 @@ def _write_token(*_, **__): new_credentials['client_id']) self.assertEqual(credentials_on_disk['username'], new_credentials['username']) - self.assertGreater(new_credentials['expiration'], - credentials_on_disk['expiration']) + self.assertGreaterEqual(new_credentials['expiration'], + credentials_on_disk['expiration']) self.assertNotEqual(credentials_on_disk['access_token'], new_credentials['access_token']) self.assertNotEqual(credentials_on_disk['refresh_token'],