Skip to content

Commit edaaa5c

Browse files
committed
Merge pull request #676 from Intevation/dev-filterexpert
ENH: Filterexpert can now use RegEx Signed-off-by: Sebastian Wagner <[email protected]>
2 parents cad7c15 + f5bf3fb commit edaaa5c

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

intelmq/bots/BOTS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@
702702
"parameters": {
703703
"filter_action": "<keep/drop>",
704704
"filter_key": "<source.geolocation.cc>",
705+
"filter_regex": "",
705706
"filter_value": "<PT>"
706707
}
707708
},

intelmq/bots/experts/filter/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
### Filter Bot
1+
# Filter Bot
22

3-
Bot allowing out specific events
3+
The filter bot is capable of filtering specific events.
44

5-
Parameters for filtering with key/value attributes:
6-
* filter_key - key from data harmonization
7-
* filter_value - value for the key
8-
* filter_action - action when a message match to the criteria (possible actions: keep/drop)
5+
## Parameters for filtering with key/value attributes:
6+
* `filter_key` - key from data harmonization
7+
* `filter_value` - value for the key
8+
* `filter_action` - action when a message match to the criteria (possible actions: keep/drop)
9+
* `filter_regex` - attribute determines if the `filter_value` shall be treated as regular expression or not.
10+
If this attribute is not empty, the bot uses python's "search" function to evaluate the filter.
911

10-
Parameters for time based filtering:
11-
* not_before - events before this time will be dropped
12-
* not_after - events after this time will be dropped
12+
## Parameters for time based filtering:
13+
* `not_before` - events before this time will be dropped
14+
* `not_after` - events after this time will be dropped
1315

1416
Both parameters accept string values describing absolute or relative time:
1517
* absolute
1618
* basically anything parsable by datetime parser, eg. "2015-09-012T06:22:11+00:00"
17-
* time.source taken from the event will be compared to this value to decide the filter behavior
19+
* `time.source` taken from the event will be compared to this value to decide the filter behavior
1820
* relative
1921
* accepted string formatted like this "<integer> <epoch>", where epoch could be any of following strings (could optionally end with trailing 's'): hour, day, week, month, year
2022
* time.source taken from the event will be compared to the value (now - relative) to decide the filter behavior

intelmq/bots/experts/filter/expert.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def init(self):
6767
self.logger.info("Filter_action parameter definition unknown.")
6868
self.filter = False
6969

70+
self.use_regex = False
71+
if hasattr(self.parameters, 'filter_regex') and self.parameters.filter_regex:
72+
self.use_regex = True
73+
7074
if not (self.filter or self.not_after is not None or self.not_before is not None):
7175
self.logger.error("No relevant filter configuration found, stopping...")
7276
self.stop()
@@ -102,9 +106,8 @@ def process(self):
102106

103107
# key/value based filtering
104108
if self.filter and self.parameters.filter_action == "drop":
105-
if (event.contains(self.parameters.filter_key) and
106-
event.get(self.parameters.filter_key) ==
107-
self.parameters.filter_value):
109+
if self.doFilter(event, self.parameters.filter_key,
110+
self.parameters.filter_value):
108111
self.acknowledge_message()
109112
return
110113
else:
@@ -113,9 +116,8 @@ def process(self):
113116
return
114117

115118
if self.filter and self.parameters.filter_action == "keep":
116-
if (event.contains(self.parameters.filter_key) and
117-
event.get(self.parameters.filter_key) ==
118-
self.parameters.filter_value):
119+
if self.doFilter(event, self.parameters.filter_key,
120+
self.parameters.filter_value):
119121
self.send_message(event)
120122
self.acknowledge_message()
121123
return
@@ -126,6 +128,23 @@ def process(self):
126128
self.send_message(event)
127129
self.acknowledge_message()
128130

131+
def doFilter(self, event, key, condition):
132+
if self.use_regex:
133+
return self.regexSearchFilter(event, key, condition)
134+
else:
135+
return self.equalsFilter(event, key, condition)
136+
137+
def equalsFilter(self, event, key, value):
138+
return (event.contains(key) and
139+
event.get(key) == value)
140+
141+
def regexSearchFilter(self, event, key, regex):
142+
if event.contains(key):
143+
exp = re.compile(regex)
144+
return exp.search(str(event.get(key)))
145+
else:
146+
return False
147+
129148
if __name__ == "__main__":
130149
bot = FilterExpertBot(sys.argv[1])
131150
bot.start()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import unittest
4+
5+
import intelmq.lib.test as test
6+
from intelmq.bots.experts.filter.expert import FilterExpertBot
7+
8+
EXAMPLE_INPUT = {"__type": "Event",
9+
"classification.type": "defacement",
10+
"time.source": "2005-01-01T00:00:00+00:00",
11+
"time.observation": "2015-09-12T00:00:00+00:00",
12+
"feed.name": "test-feed",
13+
"raw": "fds56gf4jh4jhgh4j6"
14+
}
15+
16+
17+
class TestFilterExpertBot(test.BotTestCase, unittest.TestCase):
18+
"""
19+
A TestCase for FilterExpertBot.
20+
"""
21+
22+
@classmethod
23+
def set_bot(cls):
24+
cls.bot_reference = FilterExpertBot
25+
cls.input_message = EXAMPLE_INPUT
26+
cls.sysconfig = {'filter_regex': 'search',
27+
'filter_key': 'feed.name',
28+
'filter_value': 'feed'}
29+
30+
def test_searchRegex(self):
31+
self.run_bot()
32+
self.assertMessageEqual(0, EXAMPLE_INPUT)
33+
34+
if __name__ == '__main__':
35+
unittest.main()

0 commit comments

Comments
 (0)