-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathverify_remote_session_continuity.py
More file actions
366 lines (295 loc) · 12.9 KB
/
verify_remote_session_continuity.py
File metadata and controls
366 lines (295 loc) · 12.9 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
#!/usr/bin/env python3
"""Pass #10 C2 validator for remote session continuity fixtures."""
from __future__ import annotations
import argparse
import json
import re
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import yaml
REPO_ROOT = Path(__file__).resolve().parents[4]
DIGEST_RE = re.compile(r"^sha256:[0-9a-f]{64}$")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Run pass #10 C2 remote session continuity validator")
parser.add_argument(
"--cases",
default="fixtures/policy-events/session-continuity/v1/cases.json",
help="Path to fixture cases",
)
parser.add_argument(
"--report",
default="docs/roadmaps/cua/research/pass10-session-continuity-report.json",
help="Path to write machine-readable report",
)
return parser.parse_args()
def fail_code(suite: Dict[str, Any], key: str, default: str) -> str:
fail_closed = suite.get("fail_closed_codes", {})
if isinstance(fail_closed, dict):
code = fail_closed.get(key)
if isinstance(code, str) and code:
return code
return default
def validate_suite_structure(suite: Dict[str, Any], mapping: Dict[str, Any]) -> Optional[str]:
invalid = fail_code(suite, "suite_invalid", "CONT_SUITE_INVALID")
required_top = {
"suite_id",
"suite_version",
"mapping_ref",
"required_transitions",
"allowed_events",
"event_contracts",
"scenarios",
"fail_closed_codes",
}
if not required_top.issubset(suite.keys()):
return invalid
required_transitions = suite.get("required_transitions")
allowed_events = suite.get("allowed_events")
contracts = suite.get("event_contracts")
scenarios = suite.get("scenarios")
fail_closed = suite.get("fail_closed_codes")
if not isinstance(required_transitions, list) or not required_transitions:
return invalid
if not isinstance(allowed_events, list) or not allowed_events:
return invalid
if not isinstance(contracts, dict) or not isinstance(scenarios, dict):
return invalid
if not isinstance(fail_closed, dict):
return invalid
for key in (
"suite_invalid",
"scenario_unknown",
"chain_break",
"orphan_action_detected",
"audit_incomplete",
):
if not isinstance(fail_closed.get(key), str) or not fail_closed.get(key):
return invalid
allowed_set = set(allowed_events)
if not set(required_transitions).issubset(allowed_set):
return invalid
for event_name in allowed_events:
contract = contracts.get(event_name)
if not isinstance(contract, dict):
return invalid
if not isinstance(contract.get("policy_event"), str) or not contract.get("policy_event"):
return invalid
if not isinstance(contract.get("audit_event"), str) or not contract.get("audit_event"):
return invalid
for scenario_name, scenario in scenarios.items():
if not isinstance(scenario_name, str) or not isinstance(scenario, dict):
return invalid
transition = scenario.get("required_transition")
expected_result = scenario.get("expected_result")
if transition not in allowed_set:
return invalid
if expected_result not in {"pass", "fail"}:
return invalid
expected_error = scenario.get("expected_error_code")
if expected_result == "fail" and not isinstance(expected_error, str):
return invalid
flow_mappings = mapping.get("flow_mappings")
if not isinstance(flow_mappings, dict):
return invalid
def flow_events(flow: str) -> Optional[Tuple[str, str]]:
entry = flow_mappings.get(flow)
if not isinstance(entry, dict):
return None
preflight = entry.get("preflight")
post_action = entry.get("post_action")
if not isinstance(preflight, dict) or not isinstance(post_action, dict):
return None
policy_event = preflight.get("policy_event")
audit_event = post_action.get("audit_event")
if not isinstance(policy_event, str) or not isinstance(audit_event, str):
return None
return policy_event, audit_event
for event_name, flow_name in {
"connect": "connect",
"input": "input",
"reconnect": "reconnect",
"disconnect": "disconnect",
}.items():
mapped = flow_events(flow_name)
if mapped is None:
return invalid
policy_event, audit_event = mapped
contract = contracts[event_name]
if contract["policy_event"] != policy_event:
return invalid
if contract["audit_event"] != audit_event:
return invalid
return None
def validate_event_common(event: Dict[str, Any]) -> bool:
session_id = event.get("session_id")
chain_hash = event.get("chain_hash")
if not isinstance(session_id, str) or not session_id:
return False
if not isinstance(chain_hash, str) or not DIGEST_RE.match(chain_hash):
return False
prev_chain = event.get("prev_chain_hash")
if prev_chain is not None and prev_chain != "GENESIS":
if not isinstance(prev_chain, str) or not DIGEST_RE.match(prev_chain):
return False
return True
def evaluate_transcript(
suite: Dict[str, Any],
query: Dict[str, Any],
) -> Tuple[str, Optional[str], Dict[str, Any]]:
scenario_unknown = fail_code(suite, "scenario_unknown", "CONT_SCENARIO_UNKNOWN")
chain_break = fail_code(suite, "chain_break", "CONT_CHAIN_BREAK")
orphan = fail_code(suite, "orphan_action_detected", "CONT_ORPHAN_ACTION_DETECTED")
audit_incomplete = fail_code(suite, "audit_incomplete", "CONT_AUDIT_INCOMPLETE")
invalid = fail_code(suite, "suite_invalid", "CONT_SUITE_INVALID")
scenarios = suite["scenarios"]
scenario_name = query.get("scenario")
if scenario_name not in scenarios:
return "fail", scenario_unknown, {}
scenario = scenarios[scenario_name]
required_transition = scenario["required_transition"]
transcript = query.get("transcript")
if not isinstance(transcript, list) or len(transcript) == 0:
return "fail", chain_break, {"scenario": scenario_name}
contracts = suite["event_contracts"]
allowed_events = set(suite["allowed_events"])
previous_hash: Optional[str] = None
seen_transition = False
active_sessions: List[str] = []
final_hash: Optional[str] = None
for idx, event in enumerate(transcript):
if not isinstance(event, dict):
return "fail", invalid, {"scenario": scenario_name}
event_name = event.get("event")
if event_name not in allowed_events:
return "fail", invalid, {"scenario": scenario_name}
if not validate_event_common(event):
return "fail", invalid, {"scenario": scenario_name}
chain_hash = event["chain_hash"]
prev_chain_hash = event.get("prev_chain_hash")
if idx == 0:
if prev_chain_hash not in (None, "GENESIS"):
return "fail", chain_break, {"scenario": scenario_name, "index": idx}
else:
if prev_chain_hash != previous_hash:
return "fail", chain_break, {"scenario": scenario_name, "index": idx}
contract = contracts[event_name]
if event.get("policy_event") != contract["policy_event"]:
return "fail", audit_incomplete, {"scenario": scenario_name, "index": idx}
if event.get("audit_event") != contract["audit_event"]:
return "fail", audit_incomplete, {"scenario": scenario_name, "index": idx}
session_id = event["session_id"]
if event_name == "connect":
active_sessions = [session_id]
elif event_name == "input":
action_id = event.get("action_id")
if not isinstance(action_id, str) or not action_id:
return "fail", invalid, {"scenario": scenario_name, "index": idx}
if session_id not in active_sessions:
return "fail", orphan, {"scenario": scenario_name, "index": idx}
elif event_name in {"reconnect", "gateway_restart_recover"}:
if event_name == required_transition:
seen_transition = True
if contract.get("requires_continuity_hashes"):
continuity_prev = event.get("continuity_prev_session_hash")
continuity_new = event.get("continuity_new_session_hash")
if continuity_prev != previous_hash:
return "fail", chain_break, {"scenario": scenario_name, "index": idx}
if continuity_new != chain_hash:
return "fail", chain_break, {"scenario": scenario_name, "index": idx}
active_sessions = [session_id]
elif event_name == "packet_loss_recover":
if event_name == required_transition:
seen_transition = True
loss_packets = event.get("loss_packets")
if not isinstance(loss_packets, int) or loss_packets <= 0:
return "fail", invalid, {"scenario": scenario_name, "index": idx}
if session_id not in active_sessions:
return "fail", orphan, {"scenario": scenario_name, "index": idx}
elif event_name == "disconnect":
if session_id not in active_sessions:
return "fail", orphan, {"scenario": scenario_name, "index": idx}
active_sessions = []
previous_hash = chain_hash
final_hash = chain_hash
if not seen_transition:
return "fail", chain_break, {"scenario": scenario_name}
return "pass", None, {
"scenario": scenario_name,
"required_transition": required_transition,
"final_chain_hash": final_hash,
}
def expected_matches(expected: Dict[str, Any], result: str, error_code: Optional[str]) -> bool:
if expected.get("result") != result:
return False
if expected.get("error_code") != error_code:
return False
return True
def main() -> int:
args = parse_args()
cases_path = (REPO_ROOT / args.cases).resolve()
cases_doc = json.loads(cases_path.read_text(encoding="utf-8"))
suite_path = (REPO_ROOT / cases_doc["suite"]).resolve()
suite = yaml.safe_load(suite_path.read_text(encoding="utf-8"))
mapping_path = (REPO_ROOT / suite["mapping_ref"]).resolve()
mapping = yaml.safe_load(mapping_path.read_text(encoding="utf-8"))
report: Dict[str, Any] = {
"suite": str(suite_path.relative_to(REPO_ROOT)),
"mapping": str(mapping_path.relative_to(REPO_ROOT)),
"results": [],
"summary": {"total": 0, "passed": 0, "failed": 0},
}
structure_error = validate_suite_structure(suite, mapping)
if structure_error is not None:
report["summary"] = {"total": 1, "passed": 0, "failed": 1}
report["results"].append(
{
"id": "suite_structure",
"ok": False,
"expected": {"result": "pass"},
"actual": {"result": "fail", "error_code": structure_error},
}
)
report_path = (REPO_ROOT / args.report).resolve()
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
print(f"[FAIL] suite_structure -> {{'result': 'fail', 'error_code': '{structure_error}'}}")
print(f"\nSummary: 0/1 checks passed. Report: {report_path.relative_to(REPO_ROOT)}")
return 1
all_ok = True
for case in cases_doc["cases"]:
case_id = case["id"]
query = case["query"]
expected = case["expected"]
result, error_code, details = evaluate_transcript(suite, query)
ok = expected_matches(expected, result, error_code)
all_ok = all_ok and ok
report["summary"]["total"] += 1
if ok:
report["summary"]["passed"] += 1
else:
report["summary"]["failed"] += 1
actual: Dict[str, Any] = {"result": result}
if error_code is not None:
actual["error_code"] = error_code
if details:
actual["details"] = details
report["results"].append(
{
"id": case_id,
"ok": ok,
"expected": expected,
"actual": actual,
}
)
status = "PASS" if ok else "FAIL"
print(f"[{status}] {case_id} -> {actual}")
report_path = (REPO_ROOT / args.report).resolve()
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
print(
f"\nSummary: {report['summary']['passed']}/{report['summary']['total']} checks passed. "
f"Report: {report_path.relative_to(REPO_ROOT)}"
)
return 0 if all_ok else 1
if __name__ == "__main__":
raise SystemExit(main())