Files
cp1-agents/mcp-servers/_common/azure_support.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

210 lines
11 KiB
Python

# azure-discovery: every read goes through Azure Resource Graph, so the scope sent with each query is the boundary.
AZURE_CLOUDS = {
"public": {"login": "https://login.microsoftonline.com", "arm": "https://management.azure.com"},
"usgov": {"login": "https://login.microsoftonline.us", "arm": "https://management.usgovcloudapi.net"},
"china": {"login": "https://login.chinacloudapi.cn", "arm": "https://management.chinacloudapi.cn"},
}
AZURE_GRAPH_VERSION = "2022-10-01"
AZURE_LOCKS_VERSION = "2020-05-01"
AZURE_TABLES = ("resources", "resourcecontainers", "policyresources", "advisorresources", "networkresources")
AZURE_TOKENS = {}
AZURE_SERVERS = {
"virtual_machines": ["microsoft.compute/virtualmachines"],
"scale_sets": ["microsoft.compute/virtualmachinescalesets"],
"disks": ["microsoft.compute/disks"],
}
AZURE_WORKLOADS = {
"app_service": ["microsoft.web/sites", "microsoft.web/serverfarms"],
"container_apps": ["microsoft.app/containerapps", "microsoft.app/managedenvironments"],
"container_instances": ["microsoft.containerinstance/containergroups"],
}
AZURE_KUBERNETES = {"clusters": ["microsoft.containerservice/managedclusters"]}
AZURE_NETWORK = {
"virtual_networks": ["microsoft.network/virtualnetworks"],
"route_tables": ["microsoft.network/routetables"],
"network_security_groups": ["microsoft.network/networksecuritygroups"],
"gateways": ["microsoft.network/virtualnetworkgateways", "microsoft.network/expressroutecircuits",
"microsoft.network/natgateways", "microsoft.network/azurefirewalls",
"microsoft.network/applicationgateways"],
"load_balancers": ["microsoft.network/loadbalancers"],
"private_endpoints": ["microsoft.network/privateendpoints"],
"public_ips": ["microsoft.network/publicipaddresses"],
}
AZURE_DATA = {
"sql": ["microsoft.sql/servers", "microsoft.sql/servers/databases"],
"cosmos_db": ["microsoft.documentdb/databaseaccounts"],
"storage_accounts": ["microsoft.storage/storageaccounts"],
"postgresql": ["microsoft.dbforpostgresql/flexibleservers", "microsoft.dbforpostgresql/servers"],
"mysql": ["microsoft.dbformysql/flexibleservers", "microsoft.dbformysql/servers"],
"redis": ["microsoft.cache/redis", "microsoft.cache/redisenterprise"],
}
AZURE_ORG_POLICIES = {
"assignments": ["microsoft.authorization/policyassignments"],
"definitions": ["microsoft.authorization/policydefinitions"],
"initiatives": ["microsoft.authorization/policysetdefinitions"],
"exemptions": ["microsoft.authorization/policyexemptions"],
"compliance": ["microsoft.policyinsights/policystates"],
}
AZURE_RESOURCE_POLICIES = {
"key_vault_access": ["microsoft.keyvault/vaults"],
"storage_network_rules": ["microsoft.storage/storageaccounts"],
}
AZURE_PILLARS = {"cost": "cost", "security": "security", "highavailability": "reliability",
"performance": "performance", "operationalexcellence": "operations"}
AZURE_RESOURCE_GROUP = r"[-\w.()]{1,90}"
def _azure_root(connection: Dict[str, Any]) -> tuple:
"""(managementGroups or subscriptions, its id, the resource group or None) from the connection's scope root."""
parts = str(connection.get("scope_root", "")).strip("/").split("/")
if len(parts) == 2 and parts[0] in ("managementGroups", "subscriptions") and parts[1]:
return parts[0], parts[1], None
if (len(parts) == 4 and parts[0] == "subscriptions" and parts[1] and parts[2].lower() == "resourcegroups"
and re.fullmatch(AZURE_RESOURCE_GROUP, parts[3]) and not parts[3].endswith(".")):
return "subscriptions", parts[1], parts[3]
raise ValueError("scope_root must be managementGroups/<id>, subscriptions/<id> or "
"subscriptions/<id>/resourceGroups/<name>")
def _azure_scope(connection: Dict[str, Any]) -> Dict[str, List[str]]:
"""The query scope: the connection's root, and nothing the caller can widen."""
kind, identifier, _ = _azure_root(connection)
return {kind: [identifier]}
def _azure_scoped_query(query: str, connection: Dict[str, Any]) -> str:
"""The query narrowed to the connection's resource group, when its scope root names one.
Resource Graph scopes by subscription or management group only, so a resource group is a filter placed straight
after the table. Rows outside any resource group (the subscription itself, subscription-wide policy assignments
and Advisor findings) still apply to the group, so they stay. A join, union or lookup could read a second table
without the filter, so a resource-group scope refuses them.
"""
_, _, group = _azure_root(connection)
if group is None:
return query
if re.search(r"\b(join|union|lookup)\b", query, re.IGNORECASE):
raise PermissionError("a resource-group scope allows no join, union or lookup")
table, bar, rest = query.partition("|")
narrowed = f"{table.strip()} | where isempty(resourceGroup) or resourceGroup =~ '{group}'"
return f"{narrowed} |{rest}" if bar else narrowed
def _azure_endpoints(connection: Dict[str, Any]) -> Dict[str, str]:
return AZURE_CLOUDS.get(str(connection.get("cloud") or "public"), AZURE_CLOUDS["public"])
def _azure_client():
return get_upstream_client("azure", lambda c: httpx.AsyncClient(timeout=100))
async def _azure_token(connection: Dict[str, Any]) -> str:
"""A token for Resource Graph: the short-lived one stored for a trial, or one signed in with the app registration."""
if connection.get("access_token"):
return str(connection["access_token"])
if not all(connection.get(f) for f in ("tenant_id", "client_id", "client_secret")):
raise ValueError("the connection needs access_token, or tenant_id, client_id and client_secret")
key = hashlib.sha256(json.dumps(connection, sort_keys=True).encode()).hexdigest()
cached = AZURE_TOKENS.get(key)
now = time.time()
if cached and cached[1] > now + 60:
return cached[0]
endpoints = _azure_endpoints(connection)
response = await _azure_client().post(
f"{endpoints['login']}/{connection['tenant_id']}/oauth2/v2.0/token",
data={"grant_type": "client_credentials", "client_id": connection["client_id"],
"client_secret": connection["client_secret"], "scope": f"{endpoints['arm']}/.default"})
response.raise_for_status()
body = response.json()
AZURE_TOKENS[key] = (body["access_token"], now + int(body.get("expires_in", 3600)))
return body["access_token"]
async def _azure_graph(query: str, top: int, skip_token: Optional[str] = None) -> Dict[str, Any]:
connection = get_connection()
options = {"$top": top, "resultFormat": "objectArray"}
if skip_token:
options["$skipToken"] = skip_token
token = await _azure_token(connection)
response = await _with_backoff(lambda: _azure_client().post(
f"{_azure_endpoints(connection)['arm']}/providers/Microsoft.ResourceGraph/resources",
params={"api-version": AZURE_GRAPH_VERSION}, headers={"Authorization": f"Bearer {token}"},
json={"query": _azure_scoped_query(query, connection), "options": options, **_azure_scope(connection)}))
response.raise_for_status()
return response.json()
async def _azure_page(tool: str, arguments: Dict[str, Any], query: str, finish=None) -> Dict[str, Any]:
try:
upstream, skip = _read_page(arguments)
except (ValueError, KeyError, TypeError):
return _refuse("invalid_page_token", "page_token isn't one this server issued")
try:
body = await _azure_graph(query, _page_size(arguments), upstream)
except PermissionError as error:
return _refuse("outside_scope", str(error))
except Exception as error:
return _upstream_failed("azure", error)
items = [finish(i) for i in body.get("data", [])] if finish else body.get("data", [])
return _page_result("azure", tool, arguments, items, upstream, body.get("$skipToken"), skip)
def _azure_type_query(table: str, types: List[str]) -> str:
listed = ", ".join(f"'{t}'" for t in types)
return f"{table} | where type in~ ({listed}) | order by id asc"
async def _azure_kind(tool: str, arguments: Dict[str, Any], kinds: Dict[str, List[str]], table: str = "Resources",
default: Optional[str] = None, finish=None) -> Dict[str, Any]:
kind = arguments.get("kind") or default or next(iter(kinds))
if kind not in kinds:
return _refuse("unknown_kind", "kind must be one of: " + ", ".join(kinds))
return await _azure_page(tool, arguments, _azure_type_query(table, kinds[kind]), finish)
def _azure_recommendation(item: Dict[str, Any]) -> Dict[str, Any]:
category = str(item.get("properties", {}).get("category", ""))
return {**item, "pillar": AZURE_PILLARS.get(category.lower(), "other"), "provider_category": category}
async def _azure_lock_subscription(arguments: Dict[str, Any]):
"""The subscription to read locks from, or None. Resource Graph, under the connection's scope, sets the boundary."""
kind, identifiers = next(iter(_azure_scope(get_connection()).items()))
wanted = arguments.get("subscription_id")
if kind == "subscriptions":
return identifiers[0] if wanted in (None, identifiers[0]) else None
if not wanted or not re.fullmatch(r"[A-Za-z0-9\-]+", str(wanted)):
return None
query = f"ResourceContainers | where type =~ 'microsoft.resources/subscriptions' and subscriptionId =~ '{wanted}'"
return wanted if (await _azure_graph(query, 1)).get("data") else None
async def _azure_locks(tool: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
"""Resource locks, from ARM: Resource Graph doesn't hold them, and answers 0 whether or not any exist."""
try:
upstream, skip = _read_page(arguments)
except (ValueError, KeyError, TypeError):
return _refuse("invalid_page_token", "page_token isn't one this server issued")
connection = get_connection()
try:
subscription = await _azure_lock_subscription(arguments)
if not subscription:
return _refuse("outside_scope", "subscription_id must name a subscription under this connection's scope")
group = _azure_root(connection)[2]
within = f"/resourceGroups/{group}" if group else ""
base = f"{_azure_endpoints(connection)['arm']}/subscriptions/{subscription}{within}/providers/Microsoft.Authorization/locks"
# A page token carries ARM's next link, and the bearer token goes wherever it points, so it must stay here.
url = upstream or f"{base}?api-version={AZURE_LOCKS_VERSION}"
if not url.startswith(f"{base}?"):
return _refuse("invalid_page_token", "page_token isn't one this server issued")
token = await _azure_token(connection)
response = await _with_backoff(lambda: _azure_client().get(url, headers={"Authorization": f"Bearer {token}"}))
response.raise_for_status()
body = response.json()
except Exception as error:
return _upstream_failed("azure", error)
return _page_result("azure", tool, arguments, body.get("value", []), upstream, body.get("nextLink"), skip,
{"subscription_id": subscription, "kind": "resource_locks"})