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

Fix cardinality in multi-document generation #104

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion geneve/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ def _solver(cls, field, value, constraints):
if len(v) > 1:
raise ValueError(f"Too many arguments for cardinality of '{field}': {v}")
v = v[0]
cardinality = int(v)
v = int(v)
if not cardinality or cardinality == v:
cardinality = v
else:
raise ConflictError(f"is already {cardinality}, cannot set to {v}", field, k)
history = cls.fields_history.setdefault(field, [])
if max_attempts is None:
max_attempts = _max_attempts
Expand Down
6 changes: 4 additions & 2 deletions geneve/events_emitter_eql.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ def cc_join_branch(seq: List[Tuple[Constraints, List[str]]]) -> Branch:
for join_col in zip(*join_rows):
field0 = None
for field, c in join_col:
field0 = field0 or field
constraints[0].append_constraint(field0, "join_value", (field, c))
if field0 is None:
field0 = field
else:
constraints[0].append_constraint(field0, "join_value", (field, c))
return Branch(constraints)


Expand Down
38 changes: 36 additions & 2 deletions tests/reports/documents_from_queries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2092,16 +2092,50 @@
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"id": "3c7a3d01",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[{'event': {'category': ['network']}, 'destination': {'ip': '1::f09b', 'port': 265}},\n",
" {'event': {'category': ['network']}, 'source': {'ip': '1::f09b', 'port': 265}, 'destination': {'ip': '1::1cf0', 'port': 22623}}],\n",
" [{'event': {'category': ['network']}, 'destination': {'ip': '1::f09b', 'port': 691}},\n",
" {'event': {'category': ['network']}, 'source': {'ip': '1::f09b', 'port': 691}, 'destination': {'ip': '1::1cf0', 'port': 57576}}],\n",
" [{'event': {'category': ['network']}, 'destination': {'ip': '1::f09b', 'port': 173}},\n",
" {'event': {'category': ['network']}, 'source': {'ip': '1::f09b', 'port': 173}, 'destination': {'ip': '1::1cf0', 'port': 3897}}],\n",
" [{'event': {'category': ['network']}, 'destination': {'ip': '1::f09b', 'port': 685}},\n",
" {'event': {'category': ['network']}, 'source': {'ip': '1::f09b', 'port': 685}, 'destination': {'ip': '1::f09b', 'port': 58442}}],\n",
" [{'event': {'category': ['network']}, 'destination': {'ip': '1::f09b', 'port': 929}},\n",
" {'event': {'category': ['network']}, 'source': {'ip': '1::f09b', 'port': 929}, 'destination': {'ip': '1::1cf0', 'port': 54423}}]]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emit('''\n",
" sequence\n",
" [network where _cardinality(destination.ip, 1) and destination.port < 1024] by destination.ip, destination.port\n",
" [network where _cardinality(destination.ip, 2) and destination.port > 1024] by source.ip, source.port\n",
"''', count=5)"
]
},
{
"cell_type": "markdown",
"id": "5090731a",
"metadata": {},
"source": [
"## Any oddities?"
]
},
{
"cell_type": "markdown",
"id": "5090731a",
"id": "c3fb7b7a",
"metadata": {},
"source": [
"Did you find anything odd reviewing the report or playing with the documents emitter?\n",
Expand Down
7 changes: 7 additions & 0 deletions tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,13 @@
],
"""Too many arguments for cardinality of 'test_var': (0, 1)""",
),
(
[
("cardinality", 1),
("cardinality", 0),
],
"Unsolvable constraints cardinality: test_var (is already 1, cannot set to 0)",
),
]


Expand Down
49 changes: 49 additions & 0 deletions tests/test_emitter_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,55 @@
[{"event": {"category": ["network"]}, "destination": {"ip": "1::f14", "port": 443}}],
],
),
(
"""sequence
[network where _cardinality(destination.ip, 1) and destination.port < 1024] by destination.ip, destination.port
[network where _cardinality(destination.ip, 2) and destination.port > 1024] by source.ip, source.port
""",
1,
[
[
{"event": {"category": ["network"]}, "destination": {"ip": "1::f09b", "port": 265}},
{
"event": {"category": ["network"]},
"source": {"ip": "1::f09b", "port": 265},
"destination": {"ip": "1::1cf0", "port": 22623},
},
],
[
{"event": {"category": ["network"]}, "destination": {"ip": "1::f09b", "port": 691}},
{
"event": {"category": ["network"]},
"source": {"ip": "1::f09b", "port": 691},
"destination": {"ip": "1::1cf0", "port": 57576},
},
],
[
{"event": {"category": ["network"]}, "destination": {"ip": "1::f09b", "port": 173}},
{
"event": {"category": ["network"]},
"source": {"ip": "1::f09b", "port": 173},
"destination": {"ip": "1::1cf0", "port": 3897},
},
],
[
{"event": {"category": ["network"]}, "destination": {"ip": "1::f09b", "port": 685}},
{
"event": {"category": ["network"]},
"source": {"ip": "1::f09b", "port": 685},
"destination": {"ip": "1::f09b", "port": 58442},
},
],
[
{"event": {"category": ["network"]}, "destination": {"ip": "1::f09b", "port": 929}},
{
"event": {"category": ["network"]},
"source": {"ip": "1::f09b", "port": 929},
"destination": {"ip": "1::1cf0", "port": 54423},
},
],
],
),
]


Expand Down