Some checks failed
check agents / check (push) Failing after 12s
azure-discovery, aws-discovery and gcp-discovery collect read-only metadata on servers, workloads, Kubernetes clusters, networks, data stores, policies and provider recommendations. Credentials arrive per call from the vault. Replies are paged, scrubbed of secrets and anonymised, and throttled calls back off. cloud-knowledge searches each provider's Well-Architected guidance from fixed lists only, so no customer detail can leave the platform. Adds shared skills, the render and test tooling, a public Python Dockerfile until the hardened base image is reachable, the customer access guide, the implementation plan and the Vault Service platform issue. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
224 lines
9.0 KiB
Python
224 lines
9.0 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
|