Skip to content

Commit

Permalink
refactor: hidden_kwargs 2 rm sensitive keys audit
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jun 24, 2024
1 parent 3c4fd8e commit 54ded90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
37 changes: 22 additions & 15 deletions eox_nelp/pearson_vue/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
39 changes: 32 additions & 7 deletions eox_nelp/pearson_vue/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()

0 comments on commit 54ded90

Please sign in to comment.