# 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"], } 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"} 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 = str(connection.get("scope_root", "")).strip("/").partition("/") if not identifier or "/" in identifier or kind not in ("managementGroups", "subscriptions"): raise ValueError("scope_root must be managementGroups/ or subscriptions/") return {kind: [identifier]} 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": query, "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 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") base = f"{_azure_endpoints(connection)['arm']}/subscriptions/{subscription}/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"})