Files
cp1-agents/mcp-servers/_common/test_contract.py
Jonathan Boniface c14a94f33b
Some checks failed
check agents / check (push) Failing after 12s
Add hyperscaler discovery MCP servers and Well-Architected guidance
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>
2026-10-30 20:11:57 +00:00

126 lines
5.4 KiB
Python

"""The contract every hyperscaler server must meet. Run once per server: SERVER_DIR=<folder> pytest _common/test_contract.py"""
import asyncio
import json
import os
import re
import sys
from pathlib import Path
import pytest
SERVER_DIR = Path(os.environ["SERVER_DIR"]).resolve()
sys.path.insert(0, str(SERVER_DIR))
import server # noqa: E402
CONNECTIONS = {
"azure-discovery": {"tenant_id": "t", "client_id": "c", "client_secret": "s", "scope_root": "subscriptions/sub-1"},
"aws-discovery": {"access_key_id": "a", "secret_access_key": "b", "role_arn": "arn:aws:iam::111111111111:role/r",
"external_id": "e", "regions": "eu-west-2"},
"gcp-discovery": {"scope_root": "projects/p1", "service_account_json": "{}"},
"cloud-knowledge": {"google_api_key": "k"},
}
def test_every_tool_has_a_handler():
names = [t["name"] for t in server.TOOLS_REGISTRY]
assert "verify_connection" in names
assert len(names) == len(set(names))
for name in names:
assert callable(getattr(server, name))
def test_verify_connection_is_implemented():
assert server.VERIFY_TOOL == "verify_connection"
assert "does not check it against" not in Path(SERVER_DIR / "server.py").read_text()
def test_connection_is_required_by_default():
assert server.config.REQUIRE_VAULT_CONNECTION is True
with pytest.raises(ValueError):
server.check_connection(None)
def test_no_credential_is_read_from_the_environment():
code = Path(SERVER_DIR / "server.py").read_text()
for field in CONNECTIONS[SERVER_DIR.name]:
assert not re.search(rf"(getenv|environ)[^\n]*{field}", code)
def test_only_read_operations_are_named():
"""No create, update, delete or put anywhere in the upstream calls."""
if SERVER_DIR.name != "aws-discovery":
pytest.skip("only AWS names operations; Azure and Google Cloud are tested on the URLs they reach")
code = Path(SERVER_DIR / "server.py").read_text()
operations = re.findall(r'_aws_call\([^,]+,\s*"[a-z0-9]+",\s*"([a-z_]+)"', code)
operations += re.findall(r'\("[a-z0-9]+",\s*"([a-z_]+)",\s*"[A-Za-z]+",\s*"[A-Za-z]+",\s*"[A-Za-z]+",', code)
assert all(op.split("_")[0] in ("list", "describe", "get", "select") for op in operations), operations
@pytest.mark.parametrize("value,expected", [
("user:ana@customer.example", "[user]"),
("group:admins@customer.example", "[group]"),
("domain:customer.example", "[domain]"),
("deleted:user:ana@customer.example?uid=1", "[user]"),
("serviceAccount:scan@p1.iam.gserviceaccount.com", "serviceAccount:scan@p1.iam.gserviceaccount.com"),
])
def test_members_are_anonymised(value, expected):
assert server._scrub({"members": [value]})["members"] == [expected]
def test_people_never_survive_scrubbing():
record = {
"tags": {"owner": "Ana Silva", "environment": "prod"},
"Tags": [{"Key": "Owner", "Value": "Ana Silva"}, {"Key": "Name", "Value": "web-01"}],
"systemData": {"createdBy": "ana@customer.example"},
"description": "Contact ana@customer.example for access",
"Principal": {"AWS": "arn:aws:iam::111111111111:user/ana"},
"session": "arn:aws:sts::111111111111:assumed-role/ops/ana@customer.example",
"objectId": "00000000-0000-0000-0000-000000000000",
}
text = json.dumps(server._scrub(record))
assert "ana" not in text.lower().replace("[", "") or "Ana" not in text
assert "ana@" not in text and "Ana Silva" not in text and ":user/ana" not in text
assert "00000000-0000" not in text
assert "web-01" in text and "prod" in text and "assumed-role/ops/[session]" in text
def test_secrets_never_survive_scrubbing():
record = {"osProfile": {"customData": "c2VjcmV0", "adminPassword": "x"}, "Environment": {"Variables": {"DB": "y"}},
"properties": {"connectionStrings": ["z"], "envVars": [{"name": "A"}]}}
text = json.dumps(server._scrub(record))
assert "c2VjcmV0" not in text and '"DB"' not in text and '"z"' not in text and '"A"' not in text
def test_page_is_cut_and_resumes_at_the_cut():
items = [{"id": i, "pad": "x" * 60000} for i in range(10)]
first = server._page_result("t", "tool", {}, items, "up", "next")["data"]
assert 0 < first["count"] < 10 and first["next_page_token"]
upstream, skip = server._read_page({"page_token": first["next_page_token"]})
assert (upstream, skip) == ("up", first["count"])
last = server._page_result("t", "tool", {}, items, upstream, "next", skip)["data"]
assert last["items"][0]["id"] == first["count"]
def test_failures_do_not_quote_the_upstream():
class Boom(Exception):
pass
result = server._upstream_failed("azure", Boom("token=abc user ana@customer.example"))
assert "abc" not in json.dumps(result) and "ana@" not in json.dumps(result)
def test_evidence_changes_with_the_content():
assert server._evidence_ref("t", {}, [1]) != server._evidence_ref("t", {}, [2])
def test_tools_refuse_a_forged_page_token():
if SERVER_DIR.name == "cloud-knowledge":
pytest.skip("this server has no paged tools")
prefix = SERVER_DIR.name.split("-")[0]
token = server.mcp_connection.bind(CONNECTIONS[SERVER_DIR.name])
try:
result = asyncio.run(getattr(server, f"{prefix}_list_servers")({"page_token": "not-a-token"}))
finally:
server.mcp_connection.unbind(token)
assert result["success"] is False and result["error"]["code"] == "invalid_page_token"