Skip to content

Commit

Permalink
Merge pull request #24 from certego/fix_wrong_match
Browse files Browse the repository at this point in the history
Fixed a bug when an existing fields didn't match
  • Loading branch information
ManofWax committed Mar 8, 2024
2 parents 5e214b9 + 86a1424 commit 8bf56f1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 2.2.x
### 2.2.8
#### Bugfix
* Fixed a bug when an existing fields didn't match
### 2.2.7
#### Bugfix
* Fixed bug in exist filter
Expand Down
25 changes: 11 additions & 14 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
import os
import sys
import sphinx_rtd_theme

# sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0, os.path.abspath(".."))


# -- Project information -----------------------------------------------------

project = 'routingfilter'
copyright = '2020, Certego'
author = 'Certego S.r.l.'
project = "routingfilter"
copyright = "2020, Certego"
author = "Certego S.r.l."

# The full version, including alpha/beta/rc tags
release = '1.0'
release = "1.0"

master_doc = "index" # Tell ReadTheDocs the master doc file

Expand All @@ -33,29 +34,25 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'recommonmark',
'sphinx_rtd_theme',
'sphinx.ext.autodoc'
]
extensions = ["recommonmark", "sphinx_rtd_theme", "sphinx.ext.autodoc"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]
11 changes: 11 additions & 0 deletions routing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def setUp(self):
self.test_event_17 = load_test_data("test_event_17")
self.test_event_18 = load_test_data("test_event_18")
self.test_event_19 = load_test_data("test_event_19")
self.test_event_20 = load_test_data("test_event_20")
self.test_event_with_list_1 = load_test_data("test_event_with_list_1")
self.test_event_with_list_2 = load_test_data("test_event_with_list_2")

Expand Down Expand Up @@ -474,6 +475,16 @@ def test_exist_source_ip(self):
self.assertTrue(match)
self.assertDictEqual(match[0].output, {"Workshop": {"workers_needed": 1}})

def test_multiple_fields_equal(self):
self.routing.load_from_dicts([load_test_data("test_rule_32_multiple_fields_equal")])
match = self.routing.match(self.test_event_20)
self.assertTrue(match)
self.assertDictEqual(match[0].output, {"Workshop": {"workers_needed": 1}})

self.test_event_20["ip"] = "4.4.4.4"
match = self.routing.match(self.test_event_20)
self.assertFalse(match)


if __name__ == "__main__":
unittest.main()
28 changes: 21 additions & 7 deletions routingfilter/filters/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for value in event_value:
return self._check_startswith(str(value))
if self._check_startswith(str(value)):
return True
return False

def _check_startswith(self, value: str) -> bool:
"""
Expand Down Expand Up @@ -166,7 +168,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for value in event_value:
return self._check_endswith(str(value))
if self._check_endswith(str(value)):
return True
return False

def _check_endswith(self, value):
"""
Expand Down Expand Up @@ -199,7 +203,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for value in event_value:
return self._check_keyword(str(value))
if self._check_keyword(str(value)):
return True
return False

def _check_keyword(self, value: str) -> bool:
"""
Expand Down Expand Up @@ -247,7 +253,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for value in event_value:
return self._check_regex(str(value))
if self._check_regex(str(value)):
return True
return False

def _check_regex(self, value: str) -> bool:
"""
Expand Down Expand Up @@ -299,7 +307,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for ip_address in event_value:
return self._check_network(str(ip_address))
if self._check_network(str(ip_address)):
return True
return False

def _check_network(self, ip_address: str) -> bool:
"""
Expand Down Expand Up @@ -364,7 +374,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for value in event_value:
return self._check_domain(str(value))
if self._check_domain(str(value)):
return True
return False

def _check_domain(self, value: str) -> bool:
"""
Expand Down Expand Up @@ -428,7 +440,9 @@ def match(self, event: DictQuery) -> bool:
event_value = event.get(key, [])
event_value = event_value if isinstance(event_value, list) else [event_value]
for value in event_value:
return self._compare(value)
if self._compare(value):
return True
return False

def _compare(self, value: float) -> bool:
"""
Expand Down
10 changes: 10 additions & 0 deletions test_data/test_event_20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tags": ["mountain_bike", "city_bike"],
"wheel_model": "Superlight",
"frame": "aluminium",
"gears": "1x12",
"suspension": "full",
"ip": "127.0.0.1",
"ip2": "100.0.0.1",
"price": 600
}
30 changes: 30 additions & 0 deletions test_data/test_rule_32_multiple_fields_equal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"streams": {
"rules": {
"mountain_bike": [
{
"id": "equals-ffh4981",
"filters": [
{
"id": 5540,
"key": [
"ip2",
"ip",
"random_field"
],
"type": "NETWORK",
"value": [
"127.0.0.0/24"
]
}
],
"streams": {
"Workshop": {
"workers_needed": 1
}
}
}
]
}
}
}

0 comments on commit 8bf56f1

Please sign in to comment.