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

123 lines
5.2 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": "{}"},
}
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():
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"