-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cql.py
More file actions
529 lines (426 loc) · 19.2 KB
/
Copy pathtest_cql.py
File metadata and controls
529 lines (426 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
"""Tests for UIAO_108 / §3.2 Compliance Query Language."""
from __future__ import annotations
import pytest
from uiao.governance.cql import (
CQLEvaluator,
CQLParseError,
CQLPredicate,
CQLQuery,
adapters_resolver,
graph_findings_resolver,
journal_records_resolver,
load_canonical_queries,
make_default_resolver,
parse_query,
)
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
def _findings_records() -> list[dict]:
return [
{"id": "f1", "drift_class": "DRIFT-AUTHZ", "status": "Open", "severity": "High", "control_id": "AC-2"},
{"id": "f2", "drift_class": "DRIFT-AUTHZ", "status": "Closed", "severity": "Medium", "control_id": "AC-2"},
{"id": "f3", "drift_class": "DRIFT-IDENTITY", "status": "Open", "severity": "High", "control_id": "IA-2"},
{"id": "f4", "drift_class": "DRIFT-SEMANTIC", "status": "Open", "severity": "Low", "control_id": "AC-2"},
]
def _enforcement_records() -> list[dict]:
return [
{
"policy_id": "epl:block-out-of-scope",
"action": "block",
"target": "rogue",
"dispatched_at": "2026-04-26T10:00:00+00:00",
},
{
"policy_id": "epl:enforce-mfa",
"action": "remediate",
"target": "entra-id",
"dispatched_at": "2026-04-26T11:00:00+00:00",
},
{
"policy_id": "epl:audit-schema-drift",
"action": "alert",
"target": "scuba",
"dispatched_at": "2026-04-26T12:00:00+00:00",
},
]
# ---------------------------------------------------------------------------
# Parser
# ---------------------------------------------------------------------------
class TestParse:
def test_minimal_query(self):
q = parse_query({"source": "findings"})
assert q.source == "findings"
assert q.where == ()
assert q.limit == 0
def test_yaml_string_round_trip(self):
q = parse_query("source: findings\nwhere:\n status: Open\nlimit: 10\n")
assert q.source == "findings"
assert q.limit == 10
assert any(p.field == "status" and p.value == "Open" for p in q.where)
def test_select_list(self):
q = parse_query({"source": "findings", "select": ["id", "severity"]})
assert q.select == ("id", "severity")
def test_select_string_normalized_to_tuple(self):
q = parse_query({"source": "findings", "select": "id"})
assert q.select == ("id",)
def test_predicate_eq_default(self):
q = parse_query({"source": "findings", "where": {"status": "Open"}})
assert q.where[0].op == "eq"
assert q.where[0].value == "Open"
def test_predicate_op_form(self):
q = parse_query(
{
"source": "findings",
"where": {
"severity": {"op": "in", "value": ["High", "Critical"]},
},
}
)
assert q.where[0].op == "in"
assert q.where[0].value == ["High", "Critical"]
def test_unknown_source_raises(self):
with pytest.raises(CQLParseError):
parse_query({"source": "phantom"})
def test_unknown_op_raises(self):
with pytest.raises(CQLParseError):
parse_query({"source": "findings", "where": {"x": {"op": "fuzzy"}}})
def test_invalid_order_raises(self):
with pytest.raises(CQLParseError):
parse_query({"source": "findings", "order": "sideways"})
def test_negative_limit_raises(self):
with pytest.raises(CQLParseError):
parse_query({"source": "findings", "limit": -1})
def test_invalid_yaml_raises(self):
with pytest.raises(CQLParseError):
parse_query(":: not yaml ::")
def test_non_mapping_raises(self):
with pytest.raises(CQLParseError):
parse_query([1, 2, 3])
# ---------------------------------------------------------------------------
# Predicate matchers
# ---------------------------------------------------------------------------
class TestPredicateMatching:
def test_eq_and_ne(self):
rec = {"status": "Open"}
assert CQLPredicate("status", "eq", "Open").matches(rec)
assert not CQLPredicate("status", "eq", "Closed").matches(rec)
assert CQLPredicate("status", "ne", "Closed").matches(rec)
def test_in_and_not_in(self):
rec = {"severity": "High"}
assert CQLPredicate("severity", "in", ["High", "Critical"]).matches(rec)
assert not CQLPredicate("severity", "in", ["Low"]).matches(rec)
assert CQLPredicate("severity", "not_in", ["Low"]).matches(rec)
def test_contains(self):
# contains works on strings (substring) and lists (membership).
assert CQLPredicate("name", "contains", "foo").matches({"name": "foo-bar"})
assert CQLPredicate("tags", "contains", "x").matches({"tags": ["x", "y"]})
assert not CQLPredicate("tags", "contains", "z").matches({"tags": ["x", "y"]})
def test_gte_and_lte(self):
rec = {"count": 5}
assert CQLPredicate("count", "gte", 5).matches(rec)
assert CQLPredicate("count", "lte", 10).matches(rec)
assert not CQLPredicate("count", "gte", 6).matches(rec)
def test_exists(self):
rec = {"a": None, "b": "x"}
assert CQLPredicate("a", "exists").matches(rec)
assert CQLPredicate("b", "exists").matches(rec)
assert not CQLPredicate("c", "exists").matches(rec)
def test_type_mismatch_safe(self):
# contains on an int target falls through to False (no TypeError).
assert not CQLPredicate("x", "contains", "y").matches({"x": 42})
# ---------------------------------------------------------------------------
# Evaluator
# ---------------------------------------------------------------------------
class TestEvaluator:
def test_simple_filter_and_count(self):
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(
CQLQuery(source="findings", where=(CQLPredicate("status", "eq", "Open"),))
)
assert result.count == 3
assert all(r["status"] == "Open" for r in result.rows)
def test_multiple_predicates_and(self):
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(
parse_query(
{
"source": "findings",
"where": {"drift_class": "DRIFT-AUTHZ", "status": "Open"},
}
)
)
assert result.count == 1
assert result.rows[0]["id"] == "f1"
def test_order_by_desc(self):
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(
parse_query({"source": "findings", "order_by": "id", "order": "desc"})
)
assert [r["id"] for r in result.rows] == ["f4", "f3", "f2", "f1"]
def test_limit(self):
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(
parse_query({"source": "findings", "limit": 2, "order_by": "id"})
)
assert result.count == 2
def test_select_projects(self):
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(
parse_query({"source": "findings", "select": ["id", "severity"]})
)
assert all(set(r.keys()) == {"id", "severity"} for r in result.rows)
def test_unknown_source_returns_empty(self):
resolver = make_default_resolver()
# Bypass parse_query to construct an invalid-source query
# directly — the evaluator falls through to an empty result.
result = CQLEvaluator(resolver=resolver).evaluate(CQLQuery(source="findings"))
assert result.count == 0
def test_in_predicate_via_evaluator(self):
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(
parse_query(
{
"source": "findings",
"where": {"severity": {"op": "in", "value": ["High"]}},
}
)
)
assert result.count == 2
assert {r["id"] for r in result.rows} == {"f1", "f3"}
def test_ordering_handles_none(self):
resolver = make_default_resolver(findings=[{"id": "a"}, {"id": "b", "severity": "High"}, {"id": "c"}])
result = CQLEvaluator(resolver=resolver).evaluate(
parse_query({"source": "findings", "order_by": "severity", "order": "asc"})
)
# None sorts before any string; output stable.
assert result.rows[-1]["severity"] == "High"
# ---------------------------------------------------------------------------
# Source resolvers
# ---------------------------------------------------------------------------
class TestGraphResolver:
def test_projects_finding_nodes(self):
from uiao.evidence.graph import EvidenceGraph, FindingNode
g = EvidenceGraph()
g.add_finding(
FindingNode(
id="F-1",
severity="High",
control_id="AC-2",
drift_class="DRIFT-AUTHZ",
status="Open",
extra={"adapter_id": "rogue", "run_id": "schedrun-x"},
)
)
rows = graph_findings_resolver(g)
assert len(rows) == 1
assert rows[0]["id"] == "F-1"
assert rows[0]["adapter_id"] == "rogue"
class TestJournalResolver:
def test_records_with_as_dict(self):
class R:
def as_dict(self):
return {"policy_id": "epl:t", "action": "log"}
rows = journal_records_resolver([R()])
assert rows == [{"policy_id": "epl:t", "action": "log"}]
def test_dict_records_passthrough(self):
rows = journal_records_resolver([{"a": 1}])
assert rows == [{"a": 1}]
class TestAdaptersResolver:
def test_drops_nested_objects(self):
rows = adapters_resolver(
[
{"id": "x", "status": "active", "scope": ["a", "b"], "nested": {"k": 1}},
]
)
assert rows[0]["id"] == "x"
# Nested dicts dropped (not flat).
assert "nested" not in rows[0]
# List values retained.
assert rows[0]["scope"] == ["a", "b"]
# ---------------------------------------------------------------------------
# Canonical queries smoke
# ---------------------------------------------------------------------------
class TestCanonicalQueries:
def test_canonical_dir_loads_all(self):
queries = load_canonical_queries()
assert "open-drift-authz-findings" in queries
assert "recent-blocks" in queries
assert "high-severity-findings" in queries
assert "archive-recent" in queries
assert "active-modernization-adapters" in queries
def test_canonical_queries_parse_clean(self):
queries = load_canonical_queries()
for name, q in queries.items():
assert q.source in ("findings", "enforcement", "archive", "adapters"), name
def test_canonical_query_evaluates_against_realistic_inputs(self):
queries = load_canonical_queries()
q = queries["open-drift-authz-findings"]
resolver = make_default_resolver(findings=_findings_records())
result = CQLEvaluator(resolver=resolver).evaluate(q)
# Only f1 in the fixture is DRIFT-AUTHZ + Open.
assert result.count == 1
assert result.rows[0]["id"] == "f1"
def test_recent_blocks_query_runs(self):
queries = load_canonical_queries()
q = queries["recent-blocks"]
resolver = make_default_resolver(enforcement=_enforcement_records())
result = CQLEvaluator(resolver=resolver).evaluate(q)
# Only one record has action=block.
assert result.count == 1
assert result.rows[0]["policy_id"] == "epl:block-out-of-scope"
# ---------------------------------------------------------------------------
# CQL API router
# ---------------------------------------------------------------------------
pytest.importorskip("httpx")
pytest.importorskip("fastapi")
from fastapi import FastAPI # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
from uiao.api.routes import cql as cql_router # noqa: E402
AUTH_HEADERS = {"Authorization": "Bearer test-token"}
@pytest.fixture
def client() -> TestClient:
app = FastAPI()
app.include_router(cql_router.router, prefix="/api/v1/cql")
return TestClient(app)
class TestCQLApi:
def test_list_queries(self, client):
r = client.get("/api/v1/cql/queries", headers=AUTH_HEADERS)
assert r.status_code == 200
names = {q["name"] for q in r.json()["queries"]}
assert "open-drift-authz-findings" in names
def test_get_named_query(self, client):
r = client.get("/api/v1/cql/queries/recent-blocks", headers=AUTH_HEADERS)
assert r.status_code == 200
assert r.json()["source"] == "enforcement"
def test_unknown_query_404(self, client):
r = client.get("/api/v1/cql/queries/phantom", headers=AUTH_HEADERS)
assert r.status_code == 404
def test_evaluate_adhoc_against_adapters(self, client):
# The adapters resolver reads canon, which has adapters with
# status=active in modernization-registry.yaml.
r = client.post(
"/api/v1/cql/evaluate",
headers=AUTH_HEADERS,
json={"source": "adapters", "where": {"status": "active"}, "limit": 3},
)
assert r.status_code == 200
body = r.json()
assert body["count"] >= 1
def test_evaluate_named(self, client):
r = client.post(
"/api/v1/cql/evaluate/active-modernization-adapters",
headers=AUTH_HEADERS,
)
assert r.status_code == 200
body = r.json()
# Active phase-1 adapters from canon — at least entra-id today.
ids = {row["id"] for row in body["rows"] if "id" in row}
assert "entra-id" in ids
def test_evaluate_invalid_query_400(self, client):
r = client.post(
"/api/v1/cql/evaluate",
headers=AUTH_HEADERS,
json={"source": "phantom"},
)
assert r.status_code == 400
def test_evaluate_named_unknown_404(self, client):
r = client.post(
"/api/v1/cql/evaluate/phantom",
headers=AUTH_HEADERS,
)
assert r.status_code == 404
def test_no_auth_returns_401(self, client):
r = client.get("/api/v1/cql/queries")
assert r.status_code == 401
# ---------------------------------------------------------------------------
# UIAO_119 v2 wave 2 — experimental operators (regex)
# ---------------------------------------------------------------------------
class TestExperimentalOperators:
def _enabled_flag(self):
from uiao.governance.feature_flags import FeatureFlag, FeatureFlagRegistry
from uiao.governance.tenancy import Environment, TenantClass
flag = FeatureFlag(
name="auditor-api.cql.experimental-ops",
enabled_environments=frozenset({Environment.DEV, Environment.STAGE, Environment.PROD}),
enabled_tenant_classes=frozenset(
{
TenantClass.INTERNAL,
TenantClass.CANARY,
TenantClass.STANDARD,
TenantClass.REGULATED,
}
),
)
return FeatureFlagRegistry(flags={flag.name: flag})
def _disabled_flag(self):
from uiao.governance.feature_flags import FeatureFlag, FeatureFlagRegistry
flag = FeatureFlag(
name="auditor-api.cql.experimental-ops",
enabled_environments=frozenset(),
enabled_tenant_classes=frozenset(),
)
return FeatureFlagRegistry(flags={flag.name: flag})
def test_regex_op_rejected_by_default(self):
# No flags / context → strict default → experimental op denied.
body = {
"source": "findings",
"where": {"control_id": {"op": "regex", "value": "AC-.*"}},
}
with pytest.raises(CQLParseError) as exc:
parse_query(body)
assert "experimental" in str(exc.value)
assert "auditor-api.cql.experimental-ops" in str(exc.value)
def test_regex_op_rejected_when_flag_disabled(self):
from uiao.governance.tenancy import Environment, TenantContext
ctx = TenantContext(tenant_id="acme", environment=Environment.DEV)
body = {
"source": "findings",
"where": {"control_id": {"op": "regex", "value": "AC-.*"}},
}
with pytest.raises(CQLParseError):
parse_query(body, flags=self._disabled_flag(), tenant_context=ctx)
def test_regex_op_accepted_when_flag_enabled(self):
from uiao.governance.tenancy import Environment, TenantContext
ctx = TenantContext(tenant_id="acme", environment=Environment.DEV)
body = {
"source": "findings",
"where": {"control_id": {"op": "regex", "value": "AC-.*"}},
}
query = parse_query(body, flags=self._enabled_flag(), tenant_context=ctx)
assert len(query.where) == 1
assert query.where[0].op == "regex"
assert query.where[0].value == "AC-.*"
def test_regex_predicate_matches_string(self):
from uiao.governance.cql import CQLPredicate
pred = CQLPredicate(field="control_id", op="regex", value="AC-2|AC-6")
assert pred.matches({"control_id": "AC-2"})
assert pred.matches({"control_id": "AC-6"})
assert not pred.matches({"control_id": "IA-2"})
def test_regex_predicate_handles_missing_field(self):
from uiao.governance.cql import CQLPredicate
pred = CQLPredicate(field="control_id", op="regex", value=".*")
assert not pred.matches({})
assert not pred.matches({"control_id": None})
def test_regex_predicate_handles_invalid_pattern(self):
from uiao.governance.cql import CQLPredicate
# Invalid regex (unmatched paren) is treated as no-match rather
# than crashing the evaluator.
pred = CQLPredicate(field="x", op="regex", value="(unclosed")
assert not pred.matches({"x": "anything"})
def test_regex_predicate_coerces_non_string_actuals(self):
from uiao.governance.cql import CQLPredicate
pred = CQLPredicate(field="severity", op="regex", value=r"^[1-3]$")
# Integer field — re.search calls str() so this works.
assert pred.matches({"severity": 2})
assert not pred.matches({"severity": 5})
def test_unknown_op_still_unknown_with_experimental_enabled(self):
from uiao.governance.tenancy import Environment, TenantContext
ctx = TenantContext(tenant_id="acme", environment=Environment.DEV)
body = {
"source": "findings",
"where": {"x": {"op": "phantom-op", "value": 1}},
}
with pytest.raises(CQLParseError) as exc:
parse_query(body, flags=self._enabled_flag(), tenant_context=ctx)
assert "unknown operator" in str(exc.value)