From 54ded90163233a4c729e7d9524a7193577bf2383 Mon Sep 17 00:00:00 2001 From: Johan Castiblanco Date: Mon, 24 Jun 2024 12:18:53 -0500 Subject: [PATCH] refactor: hidden_kwargs 2 rm sensitive keys audit --- eox_nelp/pearson_vue/pipeline.py | 37 +++++++++++-------- eox_nelp/pearson_vue/tests/test_pipeline.py | 39 +++++++++++++++++---- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/eox_nelp/pearson_vue/pipeline.py b/eox_nelp/pearson_vue/pipeline.py index 24b3979f..9f2fdbd4 100644 --- a/eox_nelp/pearson_vue/pipeline.py +++ b/eox_nelp/pearson_vue/pipeline.py @@ -33,7 +33,7 @@ ) from eox_nelp.pearson_vue.utils import generate_client_authorization_id, update_xml_with_dict from eox_nelp.signals.utils import get_completed_and_graded -from eox_nelp.utils import find_class_with_attribute_value +from eox_nelp.utils import find_class_with_attribute_value, remove_keys_from_dict try: from eox_audit_model.decorators import audit_method @@ -414,33 +414,40 @@ def build_ead_request( } -def audit_pearson_error(*args, **kwargs): +def audit_pearson_error(exception_data=None, hidden_kwargs=None, **kwargs): """ Method to save an error with eox-audit. Args: - *args, **kwargs + exception_data(dict): dict with exception_type key. + hidden_kwargs(list: str): List with keys name that dont want to be added + the audit model and the logger error. + **kwargs Logs: LogError: Log everything with name error. Returns: None """ - audit_action = "Pearson Vue Exception" - if exception_data := kwargs.get("exception_data"): - audit_action = f"{audit_action}~{exception_data['exception_type']}" - pearson_exception = find_class_with_attribute_value( - exceptions, - "exception_type", - exception_data['exception_type'], - ) or PearsonBaseError - else: + if not exception_data: return + if hidden_kwargs is None: + hidden_kwargs = [] + hidden_kwargs.append("hidden_kwargs") # Clean also the same hidden. + audit_kwargs = remove_keys_from_dict(kwargs, hidden_kwargs) + + audit_action = "Pearson Vue Exception" + audit_action = f"{audit_action}~{exception_data['exception_type']}" + pearson_exception = find_class_with_attribute_value( + exceptions, + "exception_type", + exception_data['exception_type'], + ) or PearsonBaseError @audit_method(action=audit_action) - def raise_audit_pearson_exception(*args, **kwargs): - raise pearson_exception(*args, kwargs) + def raise_audit_pearson_exception(exception_data, **audit_kwargs): + raise pearson_exception(exception_data, audit_kwargs) try: - raise_audit_pearson_exception(*args, **kwargs) + raise_audit_pearson_exception(exception_data, **audit_kwargs) except PearsonBaseError as exc: logger.error(exc) diff --git a/eox_nelp/pearson_vue/tests/test_pipeline.py b/eox_nelp/pearson_vue/tests/test_pipeline.py index b98db427..ca360b05 100644 --- a/eox_nelp/pearson_vue/tests/test_pipeline.py +++ b/eox_nelp/pearson_vue/tests/test_pipeline.py @@ -920,16 +920,42 @@ def test_audit_pearson_error(self): "exception_type": "attribute-error" } } - args = ("vaderio", 3244) log_error = [ - f"ERROR:{pipeline.__name__}:('attribute-error', 'vaderio', 3244, " - "{'exception_data': {'error': ['String to short.'], 'exception_type': " - "'attribute-error'}})" + f"ERROR:{pipeline.__name__}:('attribute-error', {kwargs['exception_data']}, {{}})" + ] with self.assertLogs(pipeline.__name__, level="ERROR") as logs: - self.assertIsNone(audit_pearson_error(*args, **kwargs)) + self.assertIsNone(audit_pearson_error(**kwargs)) + self.assertListEqual(log_error, logs.output) + + def test_audit_pearson_error_removing_sensitive_kwargs(self): + """Test correct behaviour calling audit_pearson_error. + + Expected behavior: + - The result is the expected value(None). + - Expected log error. Not loggin passwords or api auth keys. + """ + kwargs = { + "exception_data": { + "error": ["String to short."], + "exception_type": "attribute-error" + }, + "passwords": { + "top_secret": "secret" + }, + "api_auth": { + "api_secret": "1212323424" + }, + "hidden_kwargs": ["passwords", "api_auth"], + } + log_error = [ + f"ERROR:{pipeline.__name__}:('attribute-error', {kwargs['exception_data']}, {{}})" + ] + + with self.assertLogs(pipeline.__name__, level="ERROR") as logs: + self.assertIsNone(audit_pearson_error(**kwargs)) self.assertListEqual(log_error, logs.output) @patch("eox_nelp.pearson_vue.pipeline.logger") @@ -942,7 +968,6 @@ def test_not_audit_pearson_error(self, logger_mock): - Not expected log error. """ kwargs = {} - args = ("vaderio", 3244) - self.assertIsNone(audit_pearson_error(*args, **kwargs)) + self.assertIsNone(audit_pearson_error(**kwargs)) logger_mock.error.assert_not_called()