Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pseudonymizer late error on non existing regexmapping #617

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This release limits the maximum python version to `3.12.3` because of the issue

* fixes a bug where it could happen that a config value could be overwritten by a default in a later configuration in a multi source config scenario
* fixes a bug in the `field_manager` where extending a non list target leads to a processing failure
* fixes a bug in `pseudonymizer` where a missing regex_mapping from an existing config_file causes logprep to crash continuously

## 12.0.0

Expand Down
5 changes: 5 additions & 0 deletions logprep/processor/pseudonymizer/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from urlextract import URLExtract

from logprep.abc.processor import Processor
from logprep.factory_error import InvalidConfigurationError
from logprep.metrics.metrics import CounterMetric, GaugeMetric
from logprep.processor.field_manager.processor import FieldManager
from logprep.processor.pseudonymizer.rule import PseudonymizerRule
Expand Down Expand Up @@ -241,6 +242,10 @@ def _replace_regex_keywords_by_regex_expression(self):
for dotted_field, regex_keyword in rule.pseudonyms.items():
if regex_keyword in self._regex_mapping:
rule.pseudonyms[dotted_field] = re.compile(self._regex_mapping[regex_keyword])
elif isinstance(regex_keyword, str): # after the first run, the regex is compiled
raise InvalidConfigurationError(
f"Regex keyword '{regex_keyword}' not found in regex_mapping '{self._config.regex_mapping}'"
)

def _apply_rules(self, event: dict, rule: PseudonymizerRule):
source_dict = {}
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/processor/pseudonymizer/test_pseudonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest

from logprep.factory import Factory
from logprep.factory_error import InvalidConfigurationError
from logprep.util.pseudo.encrypter import (
DualPKCS1HybridCTREncrypter,
DualPKCS1HybridGCMEncrypter,
Expand Down Expand Up @@ -1065,3 +1066,20 @@ def test_uses_encrypter(self, mode, encrypter_class):
config["mode"] = mode
object_with_encrypter = Factory.create({"pseudonymizer": config})
assert isinstance(object_with_encrypter._encrypter, encrypter_class)

def test_setup_raises_invalid_configuration_on_missing_regex_mapping(self):
rule_dict = {
"filter": "winlog.event_id: 1234 AND winlog.provider_name: Test456",
"pseudonymizer": {
"mapping": {
"winlog.event_data.param2": "RE_WHOLE_FIELD",
}
},
}
self._load_specific_rule(rule_dict)
self.object._specific_rules[0].mapping["winlog.event_data.param2"] = "RE_DOES_NOT_EXIST"
error_message = (
r"Regex keyword 'RE_DOES_NOT_EXIST' not found in regex_mapping '.*\/regex_mapping.yml'"
)
with pytest.raises(InvalidConfigurationError, match=error_message):
self.object.setup()
Loading