test_trusted_delivery_openapi_contract.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from pathlib import Path
  2. import yaml
  3. ROOT = Path(__file__).resolve().parents[1]
  4. def _operation(openapi, path):
  5. methods = openapi["paths"][path]
  6. return methods.get("post") or methods.get("get")
  7. def _assert_no_store(section):
  8. for response in section["responses"].values():
  9. assert response["headers"]["Cache-Control"]["schema"]["const"] == "no-store"
  10. def test_trusted_delivery_openapi_permissions_no_store_and_deployment_byte_parity():
  11. openapi = yaml.safe_load((ROOT / "docs/architecture/OPENAPI.yaml").read_text())
  12. for path, permission in (
  13. ("/api/system/trusted-delivery/policy-versions", "security-governance:operate"),
  14. ("/api/system/trusted-delivery/policy-versions/activate", "security-governance:operate"),
  15. ("/api/system/trusted-delivery/policy-versions/rollback", "security-governance:operate"),
  16. ("/api/system/trusted-delivery/provisions", "security-governance:operate"),
  17. ("/api/system/trusted-delivery/decisions", "security-governance:operate"),
  18. ("/api/system/trusted-delivery/legal-holds", "security-governance:manage"),
  19. ("/api/system/trusted-delivery/legal-holds/release", "security-governance:manage"),
  20. ("/api/system/trusted-delivery/reclaim/preview", "security-governance:manage"),
  21. ("/api/system/trusted-delivery/reclaim/execute", "security-governance:manage"),
  22. ("/api/system/trusted-delivery/subscriptions", "security-governance:operate"),
  23. ("/api/system/trusted-delivery/subscriptions/{subscription_uid}/activate", "security-governance:operate"),
  24. ("/api/system/trusted-delivery/subscriptions/{subscription_uid}/pause", "security-governance:operate"),
  25. ("/api/system/trusted-delivery/subscriptions/{subscription_uid}/resume", "security-governance:operate"),
  26. ("/api/system/trusted-delivery/subscriptions/{subscription_uid}/terminate", "security-governance:operate"),
  27. ("/api/system/trusted-delivery/subscriptions/anomalies", "security-governance:operate"),
  28. ("/api/system/trusted-delivery/subscriptions/deliveries/{delivery_uid}/compensate", "security-governance:manage"),
  29. ):
  30. section = _operation(openapi, path)
  31. assert section["requestBody"]["content"]["application/json"]["schema"]["additionalProperties"] is False
  32. assert section["x-required-permission"] == permission
  33. _assert_no_store(section)
  34. for relative in (
  35. "app/core/system/trusted_delivery.py", "app/core/system/trusted_delivery_repository.py",
  36. "app/core/system/trusted_delivery_controls.py", "app/core/system/trusted_delivery_controls_repository.py",
  37. "app/core/system/trusted_delivery_controls_service.py", "app/api/system/trusted_delivery.py",
  38. "migrations/versions/20260811_481_trusted_delivery.py", "migrations/versions/20260811_482_trusted_delivery_lifecycle.py",
  39. "migrations/versions/20260811_483_trusted_delivery_subscriptions.py", "migrations/versions/20260811_484_trusted_delivery_controls.py",
  40. "migrations/versions/20260811_485_trusted_delivery_hardening.py",
  41. "migrations/versions/20260811_486_trusted_delivery_database_boundary.py",
  42. "migrations/versions/20260811_487_trusted_delivery_runtime_write_gateway.py",
  43. "migrations/versions/20260811_488_trusted_delivery_approval_and_incident_gate.py",
  44. "migrations/versions/20260811_491_trusted_delivery_control_fact_actor_fix.py",
  45. ):
  46. assert (ROOT / relative).read_bytes() == (ROOT / "deployment" / relative).read_bytes()
  47. def test_trusted_delivery_controls_openapi_permissions_and_safe_no_store_contract():
  48. raw = (ROOT / "docs/architecture/OPENAPI.yaml").read_text()
  49. openapi = yaml.safe_load(raw)
  50. for path, permission in (
  51. ("/api/system/trusted-delivery/controls/profiles/{profile_id}/active", "security-governance:read"),
  52. ("/api/system/trusted-delivery/controls/release-gates/evaluate", "security-governance:operate"),
  53. ("/api/system/trusted-delivery/controls/profiles/activate", "security-governance:manage"),
  54. ("/api/system/trusted-delivery/controls/profiles/rollback", "security-governance:manage"),
  55. ("/api/system/trusted-delivery/controls/capability-approvals", "security-governance:manage"),
  56. ("/api/system/trusted-delivery/controls/evidence", "security-governance:read"),
  57. ("/api/system/trusted-delivery/controls/destruction-approvals", "security-governance:manage"),
  58. ):
  59. section = _operation(openapi, path)
  60. assert section["x-required-permission"] == permission
  61. _assert_no_store(section)
  62. assert "raw_scan" not in raw