"""A live run's scope: every resource under the connection's scope root (ADR 0017). The planner plans from this listing and the critic checks coverage against its own run of it, so both copies of this file must stay identical (tests/test_scope.py in each agent compares them). No runtime imports. Every resource listed ends in exactly one place: - a scope entry: an asset the run scans, inventories and reports; - folded: a child component recorded with its parent's rule (a disk, a network interface, a node pool), never dropped; - structure: the estate's subscriptions, resource groups, projects and folders, which are not assets. """ from __future__ import annotations from typing import Any, Callable, Dict, List, Optional, Tuple PAGE_SIZE = 200 # the servers' maximum page MAX_PAGES = 100 # 20,000 resources bounds a runaway; the largest estate seen so far is about 650 STRUCTURE = { "azure": ("microsoft.resources/subscriptions", "microsoft.resources/subscriptions/resourcegroups", "microsoft.management/managementgroups"), "gcp": ("cloudresourcemanager.googleapis.com/project", "cloudresourcemanager.googleapis.com/folder", "cloudresourcemanager.googleapis.com/organization"), } FOLDED = { "azure": { "microsoft.compute/disks": "a disk is part of the VM it is attached to", "microsoft.network/networkinterfaces": "a network interface is part of the resource it is attached to", "microsoft.compute/virtualmachines/extensions": "an extension is part of its VM", "microsoft.compute/virtualmachinescalesets/virtualmachines": "an instance is part of its scale set", }, "gcp": { "compute.googleapis.com/disk": "a disk is part of the instance it is attached to", "container.googleapis.com/nodepool": "a node pool is part of its cluster", }, } DATA_STORES = { # the types the servers' list_data_stores kinds cover; each gets a classify_data task "azure": ("microsoft.sql/servers", "microsoft.sql/servers/databases", "microsoft.documentdb/databaseaccounts", "microsoft.storage/storageaccounts", "microsoft.dbforpostgresql/flexibleservers", "microsoft.dbforpostgresql/servers", "microsoft.dbformysql/flexibleservers", "microsoft.dbformysql/servers", "microsoft.cache/redis", "microsoft.cache/redisenterprise"), "gcp": ("sqladmin.googleapis.com/instance", "firestore.googleapis.com/database", "storage.googleapis.com/bucket"), } Call = Callable[[str, Dict[str, Any]], Any] def identity(provider: str, item: Dict[str, Any]) -> Tuple[str, str, str]: """(id, type in lower case, location) of one listed resource.""" if provider == "gcp": return str(item.get("name") or ""), str(item.get("assetType") or "").lower(), str(item.get("location") or "") return str(item.get("id") or ""), str(item.get("type") or "").lower(), str(item.get("location") or "") def _data(result: Any) -> Dict[str, Any]: if isinstance(result, str): import json result = json.loads(result) if not isinstance(result, dict) or result.get("success") is False: error = (result or {}).get("error") if isinstance(result, dict) else None raise RuntimeError((error or {}).get("message") or "the tool refused the call") return result.get("data") or {} def list_all(provider: str, call: Call) -> Tuple[List[Dict[str, Any]], List[str], List[str]]: """(every resource listed, evidence refs, issues). An issue means the listing is incomplete.""" tool = f"{provider}_list_resources" items: List[Dict[str, Any]] = [] refs: List[str] = [] token: Optional[str] = None for _ in range(MAX_PAGES): args: Dict[str, Any] = {"page_size": PAGE_SIZE} if token: args["page_token"] = token try: data = _data(call(tool, args)) except Exception as error: # noqa: BLE001 - a refused or failed page is an incomplete listing return items, refs, [f"{tool}: {str(error)[:200]}"] items += list(data.get("items") or []) if data.get("evidence_ref"): refs.append(data["evidence_ref"]) token = data.get("next_page_token") if not token: return items, refs, [] return items, refs, [f"{tool}: stopped after {MAX_PAGES} pages"] def account(provider: str, items: List[Dict[str, Any]]) -> Dict[str, Any]: """Every listed resource in exactly one place: entries, folded or structure. Repeats are counted once.""" entries, folded, structure, seen = [], [], [], set() for item in items: rid, rtype, location = identity(provider, item) if not rid or rid in seen: continue seen.add(rid) if rtype in STRUCTURE.get(provider, ()): structure.append(rid) elif rtype in FOLDED.get(provider, {}): folded.append({"id": rid, "type": rtype, "reason": FOLDED[provider][rtype]}) else: entries.append({"id": rid, "type": rtype, "location": location}) entries.sort(key=lambda e: e["id"].lower()) return {"listed": len(seen), "entries": entries, "folded": folded, "structure": structure} def region(location: str) -> str: """A GCP zone such as europe-west2-b is its region europe-west2; anything else is used as it is.""" parts = location.split("-") if len(parts) == 3 and len(parts[2]) == 1 and parts[2].isalpha(): return "-".join(parts[:2]) return location