"""One dependency graph from the scan's recorded dependencies and the CMDB (ADR 0019), on run exec_93742bdd7e29's data.""" import json import sys from pathlib import Path import jsonschema ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from app import graph # noqa: E402 OUTPUT = json.loads((ROOT / "schemas" / "output.json").read_text()) DATA = json.loads((ROOT / "tests" / "fixtures" / "crucible-kab-dev-2026-11-05.json").read_text()) BY_NAME = {r["scope_entry_id"].split("/")[-1]: r["canonical_id"] for r in DATA["records"]} AKS, PG, PE = BY_NAME["crucible-kab-dev-aks"], BY_NAME["crucible-kab-dev-pg"], BY_NAME["crucible-kab-dev-pg-pe"] CIS = [ # the CMDB step's results for cmdb-export.csv {"ci_ref": "CI0010001", "name": "crucible-kab-dev-aks", "result": "matched", "matched_canonical_id": AKS, "match_basis": "cloud_resource_id", "evidence_ref": "upload:cmdb-export.csv#:0", "lifecycle": "active"}, {"ci_ref": "CI0010002", "name": "crucible-kab-dev-pg", "result": "matched", "matched_canonical_id": PG, "match_basis": "cloud_resource_id", "evidence_ref": "upload:cmdb-export.csv#:1", "lifecycle": "active"}, {"ci_ref": "CI0010008", "name": "crucible-kab-dev-pg-pe", "result": "matched", "matched_canonical_id": PE, "match_basis": "name", "evidence_ref": "upload:cmdb-export.csv#:7", "lifecycle": "active"}, {"ci_ref": "CI0010009", "name": "crucible-kab-dev-redis", "result": "ghost", "matched_canonical_id": None, "evidence_ref": "upload:cmdb-export.csv#:8", "lifecycle": "active"}, ] RELS = [ # cmdb-relationships.csv, resolved {"parent_ref": "CI0010008", "child_ref": "CI0010002", "type": "app_to_db", "type_raw": "Depends on::Used by", "evidence_ref": "upload:cmdb-relationships.csv#:0"}, {"parent_ref": "CI0010001", "child_ref": "CI0010002", "type": "app_to_db", "type_raw": "Depends on::Used by", "evidence_ref": "upload:cmdb-relationships.csv#:1"}, {"parent_ref": "CI0010009", "child_ref": "CI0010002", "type": "app_to_app", "type_raw": "Depends on::Used by", "evidence_ref": "upload:cmdb-relationships.csv#:9"}, ] def payload(**extra): return {"engagement_id": "E", "run_id": "RUN-1", "provider": "azure", "findings": DATA["findings"], "records": DATA["records"], **extra} def by_kind(result, kind): return {n["id"]: n for n in result["nodes"] if n["kind"] == kind} def test_every_recorded_dependency_is_an_edge_an_external_node_or_folded(): result = graph.build(payload()) jsonschema.validate(result, OUTPUT) cloud = [e for e in result["edges"] if e["type"] != "represents"] assert len(cloud) + len(result["folded_dependencies"]) == 7 pairs = {(e["source"], e["target"].split("/")[-1] if e["target"].startswith("/") else e["target"]) for e in cloud} assert (PG, PE) in pairs and (PE, PG) in pairs and (AKS, "e5f2efbd-457c-4181-bdfd-1711d2deab59") in pairs assert (AKS, "crucible-kab-dev-aks-subnet") in pairs and (PE, "crucible-kab-dev-pe-subnet") in pairs assert [f["target"].split("/")[-1][:24] for f in result["folded_dependencies"]] == ["crucible-kab-dev-pg-pe.n"] def test_no_edge_the_scan_never_recorded(): result = graph.build(payload()) assert not [e for e in result["edges"] if e["target"].lower().endswith("virtualnetworks/crucible-platform-dev-vnet")] def test_outside_the_scope_is_an_external_node_with_its_container(): external = by_kind(graph.build(payload()), "external") names = {n["name"]: n for n in external.values()} assert set(names) == {"crucible-kab-dev-aks-subnet", "crucible-kab-dev-pe-subnet", "e5f2efbd-457c-4181-bdfd-1711d2deab59"} assert names["crucible-kab-dev-aks-subnet"]["container"].endswith("/resourcegroups/crucible-net-dev") assert names["e5f2efbd-457c-4181-bdfd-1711d2deab59"]["reason"] == "outside_scope_root" assert names["crucible-kab-dev-aks-subnet"]["referenced_by"] == [AKS] def test_cloud_edges_are_typed_and_trusted(): edges = {(e["source"], e["target"]): e for e in graph.build(payload())["edges"]} assert edges[(PE, PG)]["type"] == "app_to_db" and edges[(PE, PG)]["validation"] == "auto_approved" subnet = next(e for (s, t), e in edges.items() if s == AKS and t.endswith("crucible-kab-dev-aks-subnet")) assert subnet["type"] == "network_attachment" and subnet["evidence"][0]["kind"] == "config" def test_one_graph_ci_nodes_represent_their_resources(): result = graph.build(payload(cmdb_cis=CIS, cmdb_relationships=RELS)) jsonschema.validate(result, OUTPUT) cis = by_kind(result, "ci") assert set(cis) == {"cmdb:CI0010001", "cmdb:CI0010002", "cmdb:CI0010008", "cmdb:CI0010009"} assert cis["cmdb:CI0010009"]["result"] == "ghost" links = {(e["source"], e["target"]): e for e in result["edges"] if e["type"] == "represents"} assert links[("cmdb:CI0010002", PG)]["validation"] == "auto_approved" assert links[("cmdb:CI0010008", PE)]["validation"] == "sme_review_recommended" # matched by name only assert not [e for e in result["edges"] if e["type"] == "represents" and e["source"] == "cmdb:CI0010009"] def test_the_comparison_walks_the_one_graph(): result = graph.build(payload(cmdb_cis=CIS, cmdb_relationships=RELS)) found = {(c["result"], c.get("parent_ci"), c.get("child_ci")) for c in result["graph_comparison"]} assert ("confirmed", "CI0010008", "CI0010002") in found assert ("cmdb_only", "CI0010001", "CI0010002") in found assert ("not_comparable", "CI0010009", "CI0010002") in found # a ghost has no resource assert ("discovered_only", "CI0010002", "CI0010008") in found # PostgreSQL -> its private endpoint edges = {(e["source"], e["target"]): e for e in result["edges"]} assert edges[(PE, PG)]["confidence"] == 0.95 and {"kind": "cmdb_relationship", "ref": "upload:cmdb-relationships.csv#:0"} in edges[(PE, PG)]["evidence"] assert edges[("cmdb:CI0010001", "cmdb:CI0010002")]["validation"] == "sme_review_recommended" def test_fan_in_counts_dependencies_not_represents_links(): nodes = {n["id"]: n for n in graph.build(payload(cmdb_cis=CIS, cmdb_relationships=RELS))["nodes"]} assert nodes[PG]["fan_in"] == 1 and nodes[PG]["retire_constraint"] == "cannot_retire_without_mitigation" assert nodes["cmdb:CI0010002"]["fan_in"] == 3 def test_without_a_cmdb_there_is_no_comparison(): result = graph.build(payload()) assert result["graph_comparison"] == [] and not by_kind(result, "ci") def test_orphans_are_resources_with_no_dependency(): result = graph.build(payload()) orphans = {n["name"] for n in result["nodes"] if n["id"] in result["orphans"]} assert "crucible-kab-dev-kv" in orphans and "crucible-kab-dev-aks" not in orphans def test_types_from_ids(): assert graph.type_of("azure", "/subscriptions/s/resourceGroups/g/providers/Microsoft.Network/virtualNetworks/v/subnets/x") \ == "microsoft.network/virtualnetworks/subnets" assert graph.type_of("gcp", "//compute.googleapis.com/projects/p/zones/z/disks/d") == "compute.googleapis.com/disk" def test_the_scope_rules_are_the_planners(): planner = ROOT.parent / "cp1-discovery-planner-agent" / "app" / "scope.py" assert (ROOT / "app" / "scope.py").read_text() == planner.read_text()