Skip to content

Commit

Permalink
fix(mapping): when using AND in matcher (keephq#2919)
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren authored Dec 29, 2024
1 parent edcabaf commit 3e3a0e6
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions keep/api/bl/enrichments_bl.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def get_nested_attribute(obj: AlertDto, attr_path: str):
# We can access it by using "results.some@@attribute" so we won't think its a nested attribute
if attr is not None and "@@" in attr:
attr = attr.replace("@@", ".")
obj = getattr(obj, attr, obj.get(attr, None) if isinstance(obj, dict) else None)
obj = getattr(
obj,
attr,
obj.get(attr, None) if isinstance(obj, dict) else None,
)
if obj is None:
return None
return obj
Expand Down Expand Up @@ -315,6 +319,8 @@ def _check_alert_matches_rule(self, alert: AlertDto, rule: MappingRule) -> bool:
is_matcher = True
break
if not is_matcher:
# If the key has . (dot) in it, it'll be added as is while it needs to be nested.
# @tb: fix when somebody will be complaining about this.
enrichments[key] = value
break

Expand Down Expand Up @@ -370,15 +376,17 @@ def _check_matcher(self, alert: AlertDto, row: dict, matcher: str) -> bool:
- bool: True if alert matches the matcher, False otherwise.
"""
try:
if " && " in matcher:
# Split by " && " for AND condition
conditions = matcher.split(" && ")
if "&&" in matcher:
# Split by "&&" for AND condition
conditions = matcher.split("&&")
return all(
self._is_match(
get_nested_attribute(alert, attribute), row.get(attribute)
get_nested_attribute(alert, attribute.strip()),
row.get(attribute.strip()),
)
or get_nested_attribute(alert, attribute) == row.get(attribute)
or row.get(attribute) == "*" # Wildcard match
or get_nested_attribute(alert, attribute.strip())
== row.get(attribute.strip())
or row.get(attribute.strip()) == "*" # Wildcard match
for attribute in conditions
)
else:
Expand Down

0 comments on commit 3e3a0e6

Please sign in to comment.