Files
cp1-agents/scripts/build_estate_snapshot.py
Jonathan Boniface f659564208 Platform scripts, MCP servers, deployment and docs for the KAB runs
- Scripts to run workflows from a YAML context or run inputs (kab_run_workflow.py, with
  --input name=@file), store per-project discovery connections, bootstrap agent MCP keys,
  refresh the KAB token, publish reports and build estate snapshots.
- Hyperscaler discovery servers extended in place; cloud-knowledge folded into them.
- deploy/crucible-kab-dev.yaml: the Discovery package agent and the Assessment agents.
- Platform issue write-ups and current-wiring notes under docs/platform-issues/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 22:13:25 +00:00

106 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""Turn collected Google Cloud discovery results into a snapshot the sample server can serve.
For a trial without the Vault Service: the collection runs where the credential is, and only resource metadata reaches
the cluster. The input is one JSON object of tool results, keyed "<tool>/<kind>", with `items` lists as gcp-discovery
returns them. Keep the output out of this repository: it describes a real estate.
Usage: build_estate_snapshot.py <results.json> <snapshot.json> --engagement <id> prints counts only
"""
import argparse
import json
import os
from pathlib import Path
def short(name: str) -> str:
return str(name or "").rstrip("/").rsplit("/", 1)[-1]
def region(location: str) -> str:
"""A zone's region: `site` is a region, the unit network data is collected for."""
parts = str(location or "unknown").split("-")
return "-".join(parts[:2]) if len(parts) > 2 else str(location or "unknown")
def resource(item: dict) -> dict:
versions = item.get("versionedResources") or [{}]
return versions[0].get("resource") or {}
def entry(item: dict, role: str, asset_class: str, depends_on: list, notes: str = "") -> dict:
extra = item.get("additionalAttributes") or {}
ips = extra.get("internalIPs") or [None]
return {"hostname": item.get("displayName") or short(item.get("name")), "role": role, "site": region(item.get("location")),
"asset_class": asset_class, "os": None, "support_status": "unknown", "services": [], "depends_on": depends_on,
"compliance_scope": [], "serial": str(extra.get("id") or item.get("name")), "fqdn": None, "ip": ips[0], "notes": notes}
def build(results: dict, engagement: str) -> dict:
assets = results.get("list_resources/None") or []
def of(kind: str) -> list:
return [a for a in assets if str(a.get("assetType")).lower() == kind]
estate, ids = {}, {}
def add(item: dict, role: str, asset_class: str, depends_on: list) -> None:
key = f"SE-G{len(estate) + 1:02d}"
ids[item.get("name")] = key
estate[key] = entry(item, role, asset_class, depends_on, str(resource(item).get("description") or ""))
def named(*links) -> list:
return sorted({ids[n] for n in ids for link in links if link and short(n) == short(link)})
for network in of("compute.googleapis.com/network"):
add(network, "network", "network_device", [])
for subnet in of("compute.googleapis.com/subnetwork"):
add(subnet, "subnetwork", "network_device", named(resource(subnet).get("network")))
for route in of("compute.googleapis.com/route"):
add(route, "route", "network_device", named(resource(route).get("network")))
for disk in of("compute.googleapis.com/disk"):
add(disk, "disk", "storage_array", [])
for vm in of("compute.googleapis.com/instance"):
extra = vm.get("additionalAttributes") or {}
disks = [d for d in of("compute.googleapis.com/disk") if short(vm.get("name")) in [short(u) for u in (d.get("additionalAttributes") or {}).get("users") or []]]
add(vm, "server", "server", named(*(extra.get("networkInterfaceNetworks") or []), *[d.get("name") for d in disks]))
images = [short(resource(d).get("sourceImage")) for d in disks if resource(d).get("sourceImage")]
estate[ids[vm.get("name")]]["os"] = images[0] if images else None
for bucket in of("storage.googleapis.com/bucket"):
add(bucket, "storage", "storage_array", [])
for account in of("iam.googleapis.com/serviceaccount"):
add(account, "identity", "cloud_resource", [])
sites = {}
def site(name: str) -> dict:
return sites.setdefault(name, {"devices": [], "links": [], "ranges": [], "firewall_rules": []})
for subnet in of("compute.googleapis.com/subnetwork"):
cidr = resource(subnet).get("ipCidrRange")
site(region(subnet.get("location")))["ranges"].append(
{"cidr": cidr, "site": region(subnet.get("location")), "segment": subnet.get("displayName")})
for key, value in estate.items():
site(value["site"])
if value["role"] == "network":
site(value["site"])["devices"].append({"name": value["hostname"], "scope_entry_id": key, "type": "vpc", "os": None,
"support_status": "unknown"})
for rule in results.get("get_network_topology/firewall_rules") or []:
next(iter(sites.values()))["firewall_rules"].append({"rule_id": rule.get("displayName"), "owner": None, "description": "firewall rule"})
project = (of("cloudresourcemanager.googleapis.com/project") or [{}])[0].get("displayName") or engagement
return {"engagement_id": engagement, "estate": estate, "sites": sites,
"apps": [{"app_id": "APP-PROJECT", "name": project, "assets": list(estate)}],
"sources": [{"connector_id": "CONN-GCP-SNAPSHOT", "type": "estate_scan", "mcp_tool": "alz-estate.scan_target", "read_only": True}]}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("results", type=Path)
parser.add_argument("snapshot", type=Path)
parser.add_argument("--engagement", required=True)
args = parser.parse_args()
snapshot = build(json.loads(args.results.read_text()), args.engagement)
descriptor = os.open(args.snapshot, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
with os.fdopen(descriptor, "w") as handle:
json.dump(snapshot, handle)
print({"entries": len(snapshot["estate"]), "sites": len(snapshot["sites"]), "bytes": args.snapshot.stat().st_size,
"roles": sorted({v["role"] for v in snapshot["estate"].values()}),
"ranges": sum(len(s["ranges"]) for s in snapshot["sites"].values())})
return 0
if __name__ == "__main__":
raise SystemExit(main())