From e64e40e20c7dff510aeeb131ff2936906f660dc8 Mon Sep 17 00:00:00 2001 From: Federico Foschini Date: Thu, 15 Jun 2023 18:57:08 +0200 Subject: [PATCH] Adde rule type_ in routing_history --- CHANGELOG.md | 3 +++ routingfilter/routing.py | 18 ++++++++++++------ setup.py | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a81e237..08b123e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ## 1.6.x +### 1.6.2 +#### Bugfix +* Added filter in routing_filter ### 1.6.1 #### Bugfix * Fixed TypeError if output is None diff --git a/routingfilter/routing.py b/routingfilter/routing.py index 494aebd..d31e65e 100644 --- a/routingfilter/routing.py +++ b/routingfilter/routing.py @@ -64,7 +64,7 @@ def match(self, event: dict, type_: str = "streams", tag_field_name: str = "tags for rule in rules: # check if ALL the filters are matching filters = [ConfigFilter(f) for f in rule.get("filters", [])] - if all(f.is_matching(event) for f in filters) and not self.rule_in_routing_history(event, rule): + if all(f.is_matching(event) for f in filters) and not self.rule_in_routing_history(type_, event, rule): matching_rules.append(rule) break if not matching_rules: @@ -72,7 +72,7 @@ def match(self, event: dict, type_: str = "streams", tag_field_name: str = "tags for rule in self.rules[type_]["rules"].get(tag_field_name, []): # check if ALL the filters are matching config_filters = [ConfigFilter(f) for f in rule.get("filters", [])] - if config_filters and all(f.is_matching(event) for f in config_filters) and not self.rule_in_routing_history(event, rule): + if config_filters and all(f.is_matching(event) for f in config_filters) and not self.rule_in_routing_history(type_, event, rule): matching_rules.append(rule) break # the first matching rule wins if it doesn't exist in the output field # Rename "filters" to "rules" and "type" to "output" to be more generic @@ -82,7 +82,8 @@ def match(self, event: dict, type_: str = "streams", tag_field_name: str = "tags mr["rules"] = mr.pop("filters") if type_ in mr: mr["output"] = mr.pop(type_) - if mr["output"]: + # We are only interested in dict type + if mr["output"] and isinstance(mr["output"], dict): for k in mr["output"].keys(): event["certego"]["routing_history"][k] = datetime.now().isoformat() return matching_rules @@ -193,17 +194,22 @@ def _validate_rules(self, rules: dict) -> None: config_filter_obj = ConfigFilter(filter_) config_filter_obj.is_matching({}) - def rule_in_routing_history(self, event, rule): + def rule_in_routing_history(self, type_, event, rule): """Checking if the given rule has already been processed + :param type_: The type_ of the event + :type type_: dict :param event: The entire event to process :type event: dict :param rule: The rule to check :type rule: dict """ - if rule["streams"] is None: + if rule[type_] is None: return False - for key in rule["streams"].keys(): + # we are only interested in rule[type_] containing dict + if not isinstance(rule[type_], dict): + return False + for key in rule[type_].keys(): if key in event["certego"]["routing_history"]: return True return False \ No newline at end of file diff --git a/setup.py b/setup.py index 88f6b1c..75226ab 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='routingfilter', - version='1.6.1', + version='1.6.2', packages=['routingfilter'], include_package_data=True, install_requires=["IPy~=1.1", "macaddress~=2.0.2"],