Spaces:
Runtime error
Runtime error
File size: 6,781 Bytes
f549fda 80ef9e0 f549fda 80ef9e0 f549fda 80ef9e0 f549fda 80ef9e0 f549fda 80ef9e0 f549fda 80ef9e0 f549fda 80ef9e0 | 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 | """Compile SnapshotSpec into lightweight canonical graph views.
These helpers intentionally stay small and dependency-free. The validator uses
them to reason about host membership, dependency edges, trust edges, evidence
locations, and mutation targets before any live container checks run.
"""
from __future__ import annotations
from dataclasses import dataclass
from open_range.protocols import SnapshotSpec
@dataclass(frozen=True, slots=True)
class CompiledGraphs:
"""Canonical graph-like views derived from a snapshot."""
hosts: frozenset[str]
users: frozenset[str]
principals: frozenset[str]
zones_by_host: dict[str, str]
services_by_host: dict[str, frozenset[str]]
dependency_edges: frozenset[tuple[str, str]]
trust_edges: frozenset[tuple[str, str, str]]
vuln_ids: frozenset[str]
evidence_locations: frozenset[str]
def compile_snapshot_graphs(snapshot: SnapshotSpec) -> CompiledGraphs:
"""Compile a snapshot into canonical graph views."""
topology = snapshot.topology or {}
hosts = _compile_hosts(topology)
users = _compile_users(topology)
principals = _compile_principals(topology, users)
zones_by_host = _compile_zones(topology, hosts)
services_by_host = _compile_services(topology, hosts)
dependency_edges = _compile_dependency_edges(topology)
trust_edges = _compile_trust_edges(topology)
vuln_ids = frozenset(v.id for v in snapshot.truth_graph.vulns if v.id)
evidence_locations = frozenset(item.location for item in snapshot.evidence_spec if item.location)
return CompiledGraphs(
hosts=hosts,
users=users,
principals=principals,
zones_by_host=zones_by_host,
services_by_host=services_by_host,
dependency_edges=dependency_edges,
trust_edges=trust_edges,
vuln_ids=vuln_ids,
evidence_locations=evidence_locations,
)
def _compile_hosts(topology: dict[str, object]) -> frozenset[str]:
raw_hosts = topology.get("hosts", [])
hosts: set[str] = set()
for raw in raw_hosts if isinstance(raw_hosts, list) else []:
if isinstance(raw, dict):
name = str(raw.get("name", "")).strip()
if name:
hosts.add(name)
else:
name = str(raw).strip()
if name:
hosts.add(name)
return frozenset(hosts)
def _compile_users(topology: dict[str, object]) -> frozenset[str]:
raw_users = topology.get("users", [])
users: set[str] = set()
for raw in raw_users if isinstance(raw_users, list) else []:
if not isinstance(raw, dict):
continue
username = str(raw.get("username", "")).strip()
if username:
users.add(username)
return frozenset(users)
def _compile_services(
topology: dict[str, object],
hosts: frozenset[str],
) -> dict[str, frozenset[str]]:
host_details = topology.get("host_details", {})
host_catalog = topology.get("host_catalog", {})
compiled: dict[str, frozenset[str]] = {}
for host in hosts:
detail = {}
if isinstance(host_details, dict):
raw_detail = host_details.get(host, {})
if isinstance(raw_detail, dict):
detail = raw_detail
if not detail and isinstance(host_catalog, dict):
raw_catalog = host_catalog.get(host, {})
if isinstance(raw_catalog, dict):
detail = raw_catalog
services = detail.get("services", [])
if not isinstance(services, list):
services = []
compiled[host] = frozenset(str(service) for service in services if service)
return compiled
def _compile_dependency_edges(topology: dict[str, object]) -> frozenset[tuple[str, str]]:
raw_edges = topology.get("dependency_edges", [])
edges: set[tuple[str, str]] = set()
for raw in raw_edges if isinstance(raw_edges, list) else []:
if not isinstance(raw, dict):
continue
source = str(raw.get("source", "")).strip()
target = str(raw.get("target", "")).strip()
if source and target:
edges.add((source, target))
if edges:
return frozenset(edges)
host_details = topology.get("host_details", {})
if isinstance(host_details, dict):
for source, raw_detail in host_details.items():
if not isinstance(raw_detail, dict):
continue
raw_targets = raw_detail.get("connects_to", [])
if not isinstance(raw_targets, list):
continue
for raw_target in raw_targets:
target = str(raw_target).strip()
if source and target:
edges.add((str(source).strip(), target))
return frozenset(edges)
def _compile_trust_edges(topology: dict[str, object]) -> frozenset[tuple[str, str, str]]:
raw_edges = topology.get("trust_edges", [])
edges: set[tuple[str, str, str]] = set()
for raw in raw_edges if isinstance(raw_edges, list) else []:
if not isinstance(raw, dict):
continue
source = str(raw.get("source", "")).strip()
target = str(raw.get("target", "")).strip()
edge_type = str(raw.get("type", "")).strip()
if source and target:
edges.add((source, target, edge_type))
return frozenset(edges)
def _compile_principals(
topology: dict[str, object],
users: frozenset[str],
) -> frozenset[str]:
principals = set(users)
raw_catalog = topology.get("principal_catalog", {})
if isinstance(raw_catalog, dict):
for raw_name in raw_catalog:
name = str(raw_name).strip()
if name:
principals.add(name)
return frozenset(principals)
def _compile_zones(
topology: dict[str, object],
hosts: frozenset[str],
) -> dict[str, str]:
zones_by_host: dict[str, str] = {}
raw_zones = topology.get("zones", {})
if isinstance(raw_zones, dict):
for raw_zone, raw_hosts in raw_zones.items():
zone = str(raw_zone).strip()
if not zone or not isinstance(raw_hosts, list):
continue
for raw_host in raw_hosts:
host = str(raw_host).strip()
if host:
zones_by_host[host] = zone
host_details = topology.get("host_details", {})
if isinstance(host_details, dict):
for raw_host, raw_detail in host_details.items():
host = str(raw_host).strip()
if not host or host not in hosts or not isinstance(raw_detail, dict):
continue
zone = str(raw_detail.get("zone", "")).strip()
if zone and host not in zones_by_host:
zones_by_host[host] = zone
return zones_by_host
|