Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

Expand Sentinel KQL translator alias test coverage (#393)

Bobby Okaforb51bdb1c7ed6
diff --git a/services/connectors/tests/test_kql_aliases.py b/services/connectors/tests/test_kql_aliases.py
index 2db6a70e8..fbaad9c35 100644
--- a/services/connectors/tests/test_kql_aliases.py
+++ b/services/connectors/tests/test_kql_aliases.py
@@ -1,3 +1,4 @@
+import pytest
from app.federated.query import Indicator, UnifiedQuery
from app.federated.translators import to_kql
@@ -81,3 +82,155 @@ def test_kql_translator_unknown_tables_pass_through():
kql = to_kql(q, table="SomeFutureTable")
assert "user.name" in kql
+
+
+ ("operator", "value", "expected"),
+ [
+ ("eq", "alice", 'user.name == "alice"'),
+ ("ne", "alice", 'user.name != "alice"'),
+ ("contains", "ali", 'user.name contains "ali"'),
+ ("starts_with", "ali", 'user.name startswith "ali"'),
+ ("ends_with", "ice", 'user.name endswith "ice"'),
+ ("gt", 10, "user.name > 10"),
+ ("gte", 10, "user.name >= 10"),
+ ("lt", 10, "user.name < 10"),
+ ("lte", 10, "user.name <= 10"),
+ ("in", ["alice", "bob"], 'user.name in ("alice", "bob")'),
+ ],
+)
+def test_kql_translator_renders_every_supported_operator(operator, value, expected):
+ q = UnifiedQuery(
+ indicators=(
+ Indicator(
+ field="user.name",
+ operator=operator,
+ value=value,
+ ),
+ ),
+ )
+
+ kql = to_kql(q)
+
+ assert expected in kql
+
+
+ ("value", "expected"),
+ [
+ (True, "user.enabled == true"),
+ (False, "user.enabled == false"),
+ (42, "event.count == 42"),
+ (3.14, "event.score == 3.14"),
+ ('alice "admin"', 'message == "alice \\"admin\\""'),
+ ("C:\\Temp\\tool.exe", 'process.path == "C:\\\\Temp\\\\tool.exe"'),
+ ],
+)
+def test_kql_translator_quotes_values_through_public_renderer(value, expected):
+ field = "user.enabled"
+ if isinstance(value, int) and not isinstance(value, bool):
+ field = "event.count"
+ elif isinstance(value, float):
+ field = "event.score"
+ elif isinstance(value, str) and "\\" in value:
+ field = "process.path"
+ elif isinstance(value, str):
+ field = "message"
+
+ q = UnifiedQuery(
+ indicators=(
+ Indicator(
+ field=field,
+ operator="eq",
+ value=value,
+ ),
+ ),
+ )
+
+ kql = to_kql(q)
+
+ assert expected in kql
+
+
+def test_kql_translator_renders_exact_pipeline_order():
+ q = UnifiedQuery(
+ free_text="kerberoasting",
+ indicators=(
+ Indicator(
+ field="user.name",
+ operator="eq",
+ ),
+ Indicator(
+ field="source.ip",
+ operator="eq",
+ value="10.0.0.5",
+ ),
+ ),
+ since_seconds=900,
+ limit=50,
+ )
+
+ kql = to_kql(q, table="SigninLogs")
+
+ assert kql.splitlines() == [
+ "SigninLogs",
+ "| where TimeGenerated > ago(900s)",
+ '| where * contains "kerberoasting"',
+ '| where UserPrincipalName == "[email protected]"',
+ '| where source.ip == "10.0.0.5"',
+ "| take 50",
+ ]
+
+
+def test_kql_translator_aliases_only_apply_to_indicator_fields_not_free_text():
+ q = UnifiedQuery(
+ free_text="user.name",
+ indicators=(
+ Indicator(
+ field="user.name",
+ operator="eq",
+ ),
+ ),
+ )
+
+ kql = to_kql(q, table="SigninLogs")
+ lines = kql.splitlines()
+
+ assert '| where * contains "user.name"' in lines
+ assert '| where UserPrincipalName == "[email protected]"' in lines
+
+
+def test_kql_translator_commonsecuritylog_unknown_canonical_fields_pass_through():
+ q = UnifiedQuery(
+ indicators=(
+ Indicator(
+ field="host.name",
+ operator="eq",
+ value="host01",
+ ),
+ Indicator(
+ field="source.ip",
+ operator="eq",
+ value="10.0.0.5",
+ ),
+ Indicator(
+ field="process.name",
+ operator="contains",
+ value="powershell",
+ ),
+ Indicator(
+ field="event.code",
+ operator="in",
+ value=[4624, 4625],
+ ),
+ ),
+ )
+
+ kql = to_kql(q, table="CommonSecurityLog")
+
+ assert 'host.name == "host01"' in kql
+ assert 'source.ip == "10.0.0.5"' in kql
+ assert 'process.name contains "powershell"' in kql
+ assert "event.code in (4624, 4625)" in kql