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

Filter expert: Docs fix, treat false as false for filter_regex and add logging #2499

Merged
merged 3 commits into from
Jul 9, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#### Experts
- `intelmq.bots.experts.sieve.expert`:
- For `:contains`, `=~` and `!~`, convert the value to string before matching avoiding an exception. If the value is a dict, convert the value to JSON (PR#2500 by Sebastian Wagner).
- `intelmq.bots.experts.filter.expert`:
- Treat value `false` for parameter `filter_regex` as false (PR#2499 by Sebastian Wagner).

#### Outputs
- `intelmq.bots.outputs.misp.output_feed`: Handle failures if saved current event wasn't saved or is incorrect (PR by Kamil Mankowski).
Expand Down
14 changes: 6 additions & 8 deletions docs/user/bots.md
Original file line number Diff line number Diff line change
Expand Up @@ -2730,25 +2730,23 @@ A simple filter for messages (drop or pass) based on a exact string comparison o

**`filter_key`**

() - key from data format
(required, string) - key from data format

**`filter_value`**

() - value for the key
(required, string) - value for the key

**`filter_action`**

() - action when a message match to the criteria
(required, string) - action when a message match to the criteria
(possible actions: keep/drop)

**`filter_regex`**

() - attribute determines if the `filter_value` shall be treated as regular expression or not.
(optional, boolean) - attribute determines if the `filter_value` shall be treated as regular expression or not.

If this attribute is not empty (can be `true`, `yes` or whatever), the bot uses python's `` `re.search ``
<<https://docs.python.org/3/library/re.html#re.search>>`_ function to evaluate the filter with regular expressions. If
this attribute is empty or evaluates to false, an exact string comparison is performed. A check on string *
inequality* can be achieved with the usage of *Paths* described below.
If this attribute is not empty (can be `true`, `yes` or whatever), the bot uses python's [`re.search`](https://docs.python.org/3/library/re.html#re.search) function to evaluate the filter with regular expressions. If
this attribute is empty or evaluates to false, an exact string comparison is performed. A check on string *inequality* can be achieved with the usage of *Paths* described below.

*Parameters for time based filtering*

Expand Down
4 changes: 3 additions & 1 deletion intelmq/bots/experts/filter/expert.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def init(self):
self.filter = False

self.regex = False
if self.filter_regex is not None:
if self.filter_regex:
self.regex = re.compile(self.filter_value)

self.time_filter = self.not_after is not None or self.not_before is not None
Expand Down Expand Up @@ -148,10 +148,12 @@ def doFilter(self, event, key, condition):
return self.equalsFilter(event, key, condition)

def equalsFilter(self, event, key, value):
self.logger.debug('Equality check: %r (event value) == %r (filter value).', event.get(key), value)
return (key in event and
event.get(key) == value)

def regexSearchFilter(self, event, key):
self.logger.debug('Regex filter: Matching %r against %r.', str(event.get(key)), self.filter_value)
if key in event:
return self.regex.search(str(event.get(key)))
else:
Expand Down
Loading