|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Shared crosswalk engine for OSCAL-native regulator deliverables. |
| 4 | +
|
| 5 | +One source of truth for: |
| 6 | + - loading the Sentinel OSCAL catalogs into enriched control dicts |
| 7 | + (statement, feasibility-tier, freshness-sla, evidence-query, resolved |
| 8 | + regime citations); |
| 9 | + - the control -> live assurance-evidence map (CONTROL_EVIDENCE) and a |
| 10 | + cached runner that records whether each control's backing check passed; |
| 11 | + - the OSCAL conformance gate (refuse to assemble on a non-conformant catalog). |
| 12 | +
|
| 13 | +Used by: |
| 14 | + generate_annex_iv_dossier.py (EU AI Act Annex IV) |
| 15 | + generate_dora_ict_register.py (DORA ICT-risk register) |
| 16 | + generate_nist_rmf_crosswalk.py (NIST AI RMF profile crosswalk) |
| 17 | +
|
| 18 | +Evidence-status semantics (shared honesty model): |
| 19 | + SATISFIED - >=1 mapped control whose runnable check passed this run. |
| 20 | + PARTIAL - has runnable-backed controls but none passed this run. |
| 21 | + PENDING-EVIDENCE - mapped only to organisational/hardware evidence, or no |
| 22 | + controls mapped (i.e. a genuine coverage gap). |
| 23 | +""" |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +import json |
| 27 | +import subprocess |
| 28 | +import sys |
| 29 | +from datetime import datetime, timezone |
| 30 | +from pathlib import Path |
| 31 | + |
| 32 | +OSCAL_DIR = Path(__file__).resolve().parent |
| 33 | +GA_DIR = OSCAL_DIR.parent |
| 34 | +REPO_ROOT = GA_DIR.parent |
| 35 | +DEFAULT_CATALOGS = [ |
| 36 | + "catalog_sentinel_v24_excerpt.json", |
| 37 | + "catalog_sentinel_v24_env_rte.json", |
| 38 | +] |
| 39 | + |
| 40 | +# Control -> live assurance evidence. `kind` describes the evidence character |
| 41 | +# truthfully; `command` is what a regulator re-runs (None = organisational |
| 42 | +# evidence, reported PENDING). Kept here so all generators agree on what each |
| 43 | +# control's evidence actually is. |
| 44 | +CONTROL_EVIDENCE = { |
| 45 | + "con-04": { |
| 46 | + "check": "TLA+ KillSwitchAbstract reachability / dead-man's switch", |
| 47 | + "kind": "model-checked", |
| 48 | + "command": "java -cp governance_artifacts/tla/tools/tla2tools.jar tlc2.TLC " |
| 49 | + "-config governance_artifacts/tla/KillSwitchAbstract.cfg " |
| 50 | + "governance_artifacts/tla/KillSwitchAbstract.tla", |
| 51 | + }, |
| 52 | + "con-07": { |
| 53 | + "check": "TLA+ KillSwitchAbstract one-way ratchet (ASA cannot de-escalate)", |
| 54 | + "kind": "model-checked", |
| 55 | + "command": "java -cp governance_artifacts/tla/tools/tla2tools.jar tlc2.TLC " |
| 56 | + "-config governance_artifacts/tla/KillSwitchAbstract.cfg " |
| 57 | + "governance_artifacts/tla/KillSwitchAbstract.tla", |
| 58 | + }, |
| 59 | + "cry-02": { |
| 60 | + "check": "PQC WORM audit log (ML-DSA-65 sign + hash chain + tamper detect)", |
| 61 | + "kind": "cryptographically-verified", |
| 62 | + "command": "python3 -m pytest governance_artifacts/kafka/test_pqc_worm_logger_v2.py -q", |
| 63 | + }, |
| 64 | + "cry-05": { |
| 65 | + "check": "SRC-1 Groth16 systemic-risk concentration bound proof", |
| 66 | + "kind": "zk-proven", |
| 67 | + "command": "bash governance_artifacts/zk/run_src1_proof.sh", |
| 68 | + }, |
| 69 | + "env-01": { |
| 70 | + "check": "TLA+ AdmissionWithAttestation (no T0 run without valid attestation)", |
| 71 | + "kind": "model-checked", |
| 72 | + "command": "java -cp governance_artifacts/tla/tools/tla2tools.jar tlc2.TLC " |
| 73 | + "-config governance_artifacts/tla/AdmissionWithAttestation.cfg " |
| 74 | + "governance_artifacts/tla/AdmissionWithAttestation.tla", |
| 75 | + }, |
| 76 | + "env-02": { |
| 77 | + "check": "Enclave-bound PQC key custody (hardware-dependent)", |
| 78 | + "kind": "organisational-record-PENDING", |
| 79 | + "command": None, |
| 80 | + }, |
| 81 | + "rte-01": { |
| 82 | + "check": "SARA/ACR MoE routing stabilization invariants", |
| 83 | + "kind": "simulated", |
| 84 | + "command": "python3 -m pytest governance_artifacts/routing/test_sara_acr_router.py -q", |
| 85 | + }, |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +def now_iso() -> str: |
| 90 | + return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") |
| 91 | + |
| 92 | + |
| 93 | +def load_catalogs(catalog_names: list[str] | None = None) -> dict[str, dict]: |
| 94 | + """Return {control_id: enriched control dict} across the named catalogs.""" |
| 95 | + names = catalog_names or DEFAULT_CATALOGS |
| 96 | + controls: dict[str, dict] = {} |
| 97 | + for name in names: |
| 98 | + path = OSCAL_DIR / name |
| 99 | + if not path.is_file(): |
| 100 | + raise FileNotFoundError(f"catalog not found: {path}") |
| 101 | + cat = json.loads(path.read_text())["catalog"] |
| 102 | + anchors = {r["uuid"]: r.get("title", r["uuid"]) |
| 103 | + for r in cat.get("back-matter", {}).get("resources", []) |
| 104 | + if r.get("uuid")} |
| 105 | + |
| 106 | + def walk(groups): |
| 107 | + for g in groups: |
| 108 | + for c in g.get("controls", []): |
| 109 | + props = {p["name"]: p["value"] for p in c.get("props", [])} |
| 110 | + stmt = next((p["prose"] for p in c.get("parts", []) |
| 111 | + if p.get("name") == "statement"), "") |
| 112 | + regimes = [] |
| 113 | + for link in c.get("links", []): |
| 114 | + href = link.get("href", "") |
| 115 | + if href.startswith("#"): |
| 116 | + a = href[1:] |
| 117 | + regimes.append({ |
| 118 | + "rel": link.get("rel", "regime"), |
| 119 | + "anchor": a, |
| 120 | + "citation": anchors.get(a, a), |
| 121 | + }) |
| 122 | + controls[c["id"]] = { |
| 123 | + "id": c["id"], |
| 124 | + "title": c.get("title", ""), |
| 125 | + "statement": stmt, |
| 126 | + "catalog": name, |
| 127 | + "feasibility_tier": props.get("feasibility-tier"), |
| 128 | + "freshness_sla": props.get("freshness-sla"), |
| 129 | + "evidence_query": props.get("evidence-query"), |
| 130 | + "regimes": regimes, |
| 131 | + } |
| 132 | + walk(g.get("groups", [])) |
| 133 | + walk(cat.get("groups", [])) |
| 134 | + return controls |
| 135 | + |
| 136 | + |
| 137 | +def run_conformance() -> dict: |
| 138 | + """Run oscal_conformance.py --json; raise if non-conformant.""" |
| 139 | + proc = subprocess.run( |
| 140 | + [sys.executable, str(OSCAL_DIR / "oscal_conformance.py"), "--json"], |
| 141 | + cwd=REPO_ROOT, capture_output=True, text=True, |
| 142 | + ) |
| 143 | + if proc.returncode != 0: |
| 144 | + raise RuntimeError( |
| 145 | + "OSCAL conformance failed; refusing to assemble a deliverable on a " |
| 146 | + f"non-conformant catalog:\n{proc.stdout}\n{proc.stderr}" |
| 147 | + ) |
| 148 | + return json.loads(proc.stdout) |
| 149 | + |
| 150 | + |
| 151 | +class EvidenceRunner: |
| 152 | + """Runs (and caches) each control's backing assurance check.""" |
| 153 | + |
| 154 | + def __init__(self, verify: bool = True): |
| 155 | + self.verify = verify |
| 156 | + self._cache: dict[str, bool | None] = {} |
| 157 | + |
| 158 | + def evidence(self, control_id: str) -> dict: |
| 159 | + desc = CONTROL_EVIDENCE.get(control_id, { |
| 160 | + "check": "(no runnable check mapped)", |
| 161 | + "kind": "organisational-record-PENDING", |
| 162 | + "command": None, |
| 163 | + }) |
| 164 | + if control_id not in self._cache: |
| 165 | + if self.verify and desc["command"]: |
| 166 | + proc = subprocess.run(desc["command"], cwd=REPO_ROOT, shell=True, |
| 167 | + capture_output=True, text=True) |
| 168 | + self._cache[control_id] = proc.returncode == 0 |
| 169 | + else: |
| 170 | + self._cache[control_id] = None |
| 171 | + return { |
| 172 | + "control_id": control_id, |
| 173 | + "check": desc["check"], |
| 174 | + "evidence_kind": desc["kind"], |
| 175 | + "command": desc["command"], |
| 176 | + "passed": self._cache[control_id], |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | +def status_for(control_entries: list[dict]) -> str: |
| 181 | + """Shared evidence-status rule given a section/element's resolved controls |
| 182 | + (each carrying a 'live_evidence' dict).""" |
| 183 | + if not control_entries: |
| 184 | + return "PENDING-EVIDENCE" |
| 185 | + any_passed = any(c["live_evidence"]["passed"] is True for c in control_entries) |
| 186 | + any_runnable = any(c["live_evidence"]["command"] for c in control_entries) |
| 187 | + if any_passed: |
| 188 | + return "SATISFIED" |
| 189 | + if any_runnable: |
| 190 | + return "PARTIAL" |
| 191 | + return "PENDING-EVIDENCE" |
| 192 | + |
| 193 | + |
| 194 | +def resolve_controls(control_ids: list[str], catalog: dict[str, dict], |
| 195 | + runner: EvidenceRunner) -> tuple[list[dict], list[str]]: |
| 196 | + """Resolve control ids -> enriched entries with live_evidence. Returns |
| 197 | + (resolved, unknown_ids).""" |
| 198 | + resolved, unknown = [], [] |
| 199 | + for cid in control_ids: |
| 200 | + if cid not in catalog: |
| 201 | + unknown.append(cid) |
| 202 | + continue |
| 203 | + entry = dict(catalog[cid]) |
| 204 | + entry["live_evidence"] = runner.evidence(cid) |
| 205 | + resolved.append(entry) |
| 206 | + return resolved, unknown |
0 commit comments