- 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>
276 lines
12 KiB
Python
276 lines
12 KiB
Python
import asyncio
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
import mcp_connection # noqa: E402
|
|
import server # noqa: E402
|
|
|
|
CONNECTION = {"tenant_id": "tenant-1", "client_id": "client", "client_secret": "s3cret",
|
|
"scope_root": "managementGroups/mg-root"}
|
|
|
|
|
|
class Azure:
|
|
def __init__(self, rows=None, skip=None, status=200):
|
|
self.rows, self.skip, self.status, self.queries = rows or [], skip, status, []
|
|
|
|
def __call__(self, request):
|
|
if "login.microsoftonline.com" in str(request.url):
|
|
assert b"client_secret=s3cret" in request.content
|
|
return httpx.Response(200, json={"access_token": "tok", "expires_in": 3600})
|
|
assert request.headers["Authorization"] == "Bearer tok"
|
|
assert "Microsoft.ResourceGraph" in str(request.url)
|
|
self.queries.append(json.loads(request.content))
|
|
if self.status != 200:
|
|
return httpx.Response(self.status, json={"error": "said too much about ana@customer.example"})
|
|
body = {"data": self.rows, "count": len(self.rows)}
|
|
if self.skip:
|
|
body["$skipToken"] = self.skip
|
|
return httpx.Response(200, json=body)
|
|
|
|
|
|
def call(tool, arguments, azure, connection=CONNECTION):
|
|
server.AZURE_TOKENS.clear()
|
|
|
|
async def run():
|
|
client = httpx.AsyncClient(transport=httpx.MockTransport(azure))
|
|
original = server.get_upstream_client
|
|
server.get_upstream_client = lambda kind, build: client
|
|
bound = mcp_connection.bind(connection)
|
|
try:
|
|
return await getattr(server, tool)(arguments)
|
|
finally:
|
|
mcp_connection.unbind(bound)
|
|
server.get_upstream_client = original
|
|
await client.aclose()
|
|
|
|
return asyncio.run(run())
|
|
|
|
|
|
def test_every_query_is_scoped_by_the_connection():
|
|
azure = Azure([{"id": "/subscriptions/1/x"}])
|
|
result = call("azure_list_servers", {}, azure)
|
|
assert result["success"]
|
|
assert azure.queries[0]["managementGroups"] == ["mg-root"]
|
|
assert "microsoft.compute/virtualmachines" in azure.queries[0]["query"]
|
|
|
|
|
|
def test_a_caller_cannot_widen_the_scope():
|
|
azure = Azure([])
|
|
call("azure_query_resources", {"query": "Resources | limit 1", "managementGroups": ["other"]}, azure)
|
|
assert azure.queries[0]["managementGroups"] == ["mg-root"]
|
|
|
|
|
|
def test_query_must_start_with_a_known_table():
|
|
azure = Azure([])
|
|
result = call("azure_query_resources", {"query": "securityresources | limit 1"}, azure)
|
|
assert result["error"]["code"] == "invalid_argument" and not azure.queries
|
|
|
|
|
|
def test_resource_outside_scope_is_refused():
|
|
result = call("azure_get_resource_metadata", {"resource_id": "/subscriptions/other/resourceGroups/rg/x/y"}, Azure([]))
|
|
assert result["error"]["code"] == "outside_scope"
|
|
|
|
|
|
def test_resource_id_cannot_inject_kql():
|
|
azure = Azure([])
|
|
result = call("azure_get_resource_metadata", {"resource_id": "/subscriptions/1' or 1==1 //"}, azure)
|
|
assert result["error"]["code"] == "invalid_argument" and not azure.queries
|
|
|
|
|
|
def test_resource_type_cannot_inject_kql():
|
|
azure = Azure([])
|
|
result = call("azure_list_resources", {"resource_type": "x' or 1==1 //"}, azure)
|
|
assert result["error"]["code"] == "invalid_argument" and not azure.queries
|
|
|
|
|
|
def test_vm_secrets_and_owner_are_removed():
|
|
vm = {"id": "/subscriptions/1/vm", "tags": {"owner": "Ana"},
|
|
"properties": {"osProfile": {"customData": "c2VjcmV0", "adminUsername": "azureuser"}}}
|
|
text = json.dumps(call("azure_list_servers", {}, Azure([vm])))
|
|
assert "c2VjcmV0" not in text and '"Ana"' not in text
|
|
|
|
|
|
def test_key_vault_principals_are_anonymised():
|
|
vault = {"id": "/v", "properties": {"accessPolicies": [{"objectId": "person-guid", "permissions": {"secrets": ["get"]}}]}}
|
|
text = json.dumps(call("azure_list_resource_policies", {"kind": "key_vault_access"}, Azure([vault])))
|
|
assert "person-guid" not in text and '"get"' in text
|
|
|
|
|
|
def test_paging_passes_the_skip_token():
|
|
azure = Azure([{"id": "a"}], skip="more")
|
|
first = call("azure_list_servers", {}, azure)["data"]
|
|
call("azure_list_servers", {"page_token": first["next_page_token"]}, azure)
|
|
assert azure.queries[1]["options"]["$skipToken"] == "more"
|
|
|
|
|
|
def test_advisor_categories_map_to_pillars():
|
|
row = {"id": "r", "properties": {"category": "HighAvailability"}}
|
|
item = call("azure_list_recommendations", {}, Azure([row]))["data"]["items"][0]
|
|
assert item["pillar"] == "reliability" and item["provider_category"] == "HighAvailability"
|
|
|
|
|
|
def test_upstream_error_text_is_not_returned():
|
|
text = json.dumps(call("azure_list_servers", {}, Azure(status=403)))
|
|
assert "403" in text and "ana@" not in text
|
|
|
|
|
|
def test_check_connection_reports_failure():
|
|
assert call("azure_check_connection", {}, Azure(status=401))["ok"] is False
|
|
assert call("azure_check_connection", {}, Azure([]))["ok"] is True
|
|
|
|
|
|
@pytest.mark.parametrize("scope", ["", "tenants/x", "managementGroups/a/b"])
|
|
def test_bad_scope_root_fails_closed(scope):
|
|
result = call("azure_list_servers", {}, Azure([]), {**CONNECTION, "scope_root": scope})
|
|
assert result["success"] is False
|
|
|
|
|
|
def test_a_stored_access_token_is_used_without_signing_in():
|
|
def azure(request):
|
|
assert "login.microsoftonline.com" not in str(request.url)
|
|
assert request.headers["Authorization"] == "Bearer personal"
|
|
return httpx.Response(200, json={"data": [{"id": "/subscriptions/1/vm"}]})
|
|
|
|
connection = {"access_token": "personal", "scope_root": "subscriptions/sub-1"}
|
|
assert call("azure_list_servers", {}, azure, connection)["data"]["count"] == 1
|
|
|
|
|
|
def test_an_incomplete_connection_is_refused():
|
|
assert call("azure_list_servers", {}, Azure([]), {"scope_root": "subscriptions/sub-1"})["success"] is False
|
|
|
|
|
|
def throttled_then(ok, times=1, headers=None):
|
|
state = {"left": times}
|
|
|
|
def azure(request):
|
|
if "login.microsoftonline.com" in str(request.url):
|
|
return httpx.Response(200, json={"access_token": "tok", "expires_in": 3600})
|
|
if state["left"]:
|
|
state["left"] -= 1
|
|
return httpx.Response(429, headers=headers or {"retry-after": "0"}, json={})
|
|
return ok
|
|
|
|
return azure, state
|
|
|
|
|
|
def test_a_throttled_call_waits_and_succeeds():
|
|
azure, state = throttled_then(httpx.Response(200, json={"data": [{"id": "a"}]}), 2)
|
|
assert call("azure_list_servers", {}, azure)["data"]["count"] == 1 and state["left"] == 0
|
|
|
|
|
|
def test_resource_graph_quota_header_is_understood():
|
|
azure, _ = throttled_then(httpx.Response(200, json={"data": []}), 1, {"x-ms-user-quota-resets-after": "00:00:00"})
|
|
assert call("azure_list_servers", {}, azure)["success"] is True
|
|
|
|
|
|
def test_throttling_that_never_ends_fails_cleanly():
|
|
azure, _ = throttled_then(None, 99)
|
|
result = call("azure_list_servers", {}, azure)
|
|
assert result["success"] is False and "429" in result["error"]["message"]
|
|
|
|
|
|
def test_a_long_requested_wait_gives_up_instead_of_hanging():
|
|
azure, state = throttled_then(None, 99, {"retry-after": "3600"})
|
|
assert call("azure_list_servers", {}, azure)["success"] is False
|
|
assert state["left"] >= 90
|
|
|
|
|
|
LOCK_SUB = {**CONNECTION, "scope_root": "subscriptions/sub-1"}
|
|
|
|
|
|
def arm(value, urls):
|
|
def azure(request):
|
|
if "login.microsoftonline.com" in str(request.url):
|
|
return httpx.Response(200, json={"access_token": "tok", "expires_in": 3600})
|
|
urls.append(str(request.url))
|
|
if "ResourceGraph" in str(request.url):
|
|
return httpx.Response(200, json={"data": value})
|
|
return httpx.Response(200, json={"value": [{"name": "do-not-delete", "properties": {"level": "CanNotDelete"}}]})
|
|
|
|
return azure
|
|
|
|
|
|
def test_locks_come_from_arm_not_resource_graph():
|
|
urls = []
|
|
data = call("azure_list_resource_policies", {"kind": "resource_locks"}, arm([], urls), LOCK_SUB)["data"]
|
|
assert data["count"] == 1 and "/subscriptions/sub-1/providers/Microsoft.Authorization/locks" in urls[0]
|
|
|
|
|
|
def test_lock_subscription_outside_the_scope_is_refused():
|
|
result = call("azure_list_resource_policies", {"kind": "resource_locks", "subscription_id": "other"}, arm([], []), LOCK_SUB)
|
|
assert result["error"]["code"] == "outside_scope"
|
|
|
|
|
|
def test_management_group_needs_a_subscription_in_scope():
|
|
urls = []
|
|
assert call("azure_list_resource_policies", {"kind": "resource_locks"}, arm([], urls))["error"]["code"] == "outside_scope"
|
|
result = call("azure_list_resource_policies", {"kind": "resource_locks", "subscription_id": "sub-9"}, arm([], urls))
|
|
assert result["error"]["code"] == "outside_scope"
|
|
found = call("azure_list_resource_policies", {"kind": "resource_locks", "subscription_id": "sub-9"}, arm([{"id": "x"}], urls))
|
|
assert found["data"]["count"] == 1
|
|
|
|
|
|
def test_a_forged_lock_page_token_cannot_send_the_token_elsewhere():
|
|
urls = []
|
|
forged = server._write_page("https://attacker.example/steal", 0)
|
|
args = {"kind": "resource_locks", "page_token": forged}
|
|
assert call("azure_list_resource_policies", args, arm([], urls), LOCK_SUB)["error"]["code"] == "invalid_page_token"
|
|
assert not urls
|
|
|
|
|
|
RG = {**CONNECTION, "scope_root": "subscriptions/sub-1/resourceGroups/crucible-kab-dev"}
|
|
|
|
|
|
def test_a_resource_group_scope_filters_every_query():
|
|
azure = Azure([])
|
|
for tool, args in (("azure_list_servers", {}), ("azure_get_network_topology", {"kind": "network_security_groups"}),
|
|
("azure_list_recommendations", {}), ("azure_query_resources", {"query": "Resources | limit 5"})):
|
|
call(tool, args, azure, RG)
|
|
assert len(azure.queries) == 4
|
|
for sent in azure.queries:
|
|
assert sent["subscriptions"] == ["sub-1"]
|
|
assert "| where isempty(resourceGroup) or resourceGroup =~ 'crucible-kab-dev' |" in sent["query"]
|
|
assert azure.queries[3]["query"].startswith("Resources | where isempty(resourceGroup)")
|
|
|
|
|
|
def test_a_resource_group_scope_refuses_a_join():
|
|
azure = Azure([])
|
|
result = call("azure_query_resources", {"query": "Resources | join kind=inner (Resources) on id"}, azure, RG)
|
|
assert result["error"]["code"] == "outside_scope" and not azure.queries
|
|
|
|
|
|
@pytest.mark.parametrize("scope", ["subscriptions/s/resourceGroups/a'b", "subscriptions/s/resourceGroups/",
|
|
"subscriptions/s/resourceGroups/a/b", "managementGroups/m/resourceGroups/a",
|
|
"subscriptions/s/resourceGroups/ends."])
|
|
def test_a_bad_resource_group_fails_closed(scope):
|
|
azure = Azure([])
|
|
assert call("azure_list_servers", {}, azure, {**CONNECTION, "scope_root": scope})["success"] is False
|
|
assert not azure.queries
|
|
|
|
|
|
def test_resource_group_locks_are_read_from_the_group():
|
|
urls = []
|
|
call("azure_list_resource_policies", {"kind": "resource_locks"}, arm([], urls), RG)
|
|
assert "/subscriptions/sub-1/resourceGroups/crucible-kab-dev/providers/Microsoft.Authorization/locks" in urls[0]
|
|
|
|
|
|
def test_check_connection_needs_the_resource_group_to_exist():
|
|
assert call("azure_check_connection", {}, Azure([{"count_": 0}]), RG)["ok"] is False
|
|
assert call("azure_check_connection", {}, Azure([{"count_": 1}]), RG)["ok"] is True
|
|
|
|
|
|
def test_an_env_tag_keeps_its_value():
|
|
tags = {"Environment": "dev", "env": "dev", "criticality": "Low"}
|
|
assert server._scrub({"tags": tags}) == {"tags": tags}
|
|
assert server._scrub([{"Key": "env", "Value": "prod"}]) == [{"Key": "env", "Value": "prod"}]
|
|
|
|
|
|
def test_environment_variables_are_still_redacted():
|
|
container = {"env": [{"name": "DB_PASSWORD", "value": "s3cret"}], "envVars": {"TOKEN": "x"}, "password": "x"}
|
|
assert server._scrub(container) == {"env": "[redacted]", "envVars": "[redacted]", "password": "[redacted]"}
|