test_trusted_delivery_controls_service.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from __future__ import annotations
  2. import uuid
  3. import pytest
  4. from app.core.system.trusted_delivery_controls import TrustedDeliveryControlError
  5. from app.core.system.trusted_delivery_controls_service import (
  6. TrustedDeliveryControlsService,
  7. )
  8. ACTOR = "01900000-0000-7000-8000-000000068801"
  9. class Repository:
  10. def __init__(self):
  11. self.profiles = set()
  12. self.records = []
  13. self.hold = False
  14. def users_available(self, values):
  15. return set(values)
  16. def profile_exists(self, profile_id, version):
  17. return (profile_id, version) in self.profiles
  18. def create_profile(self, record):
  19. self.profiles.add((record["profile_id"], record["version"]))
  20. return record
  21. def activate_profile(self, profile_id, version, idempotency_key, request_digest, actor_uid, approval_ref, approval_digest, operation):
  22. return {"profile_id": profile_id, "version": version, "status": "active", "operation": operation}
  23. def active_profile_summary(self, profile_id):
  24. return None
  25. def approve_capability(self, record):
  26. self.records.append(record)
  27. return {"status": "approved", "config_digest": record["config_digest"]}
  28. def record_release_gate(self, record):
  29. self.records.append(record)
  30. return {"uid": "01900000-0000-7000-8000-000000068802", **record}
  31. def create_evidence(self, record):
  32. self.records.append(record)
  33. return record
  34. def evidence_summary(self):
  35. return []
  36. def approve_destruction(self, record):
  37. if self.hold:
  38. raise PermissionError("destruction blocked by active legal hold")
  39. self.records.append(record)
  40. return {"status": "approved"}
  41. def _digest(value: str) -> str:
  42. return value * 64
  43. def test_controls_service_binds_catalog_capability_and_release_gate_to_safe_digests_only():
  44. repository = Repository()
  45. service = TrustedDeliveryControlsService(repository)
  46. profile = service.activate_profile({"profile_id": "finance", "version": "1.0", "approval_ref": "approval:profile", "approval_digest": _digest("a"), "idempotency_key": "profile-activate"}, actor_uid=ACTOR)
  47. assert profile["status"] == "active"
  48. approved = service.approve_capability({"provider": "kms", "capability": "kms-key-wrap", "version": "v1", "config_digest": _digest("b"), "approval_ref": "approval:capability-1", "approval_digest": _digest("c"), "idempotency_key": "capability-1"}, actor_uid=ACTOR)
  49. assert approved["status"] == "approved"
  50. gate = service.evaluate_release_gate({"artifact_digest": _digest("a"), "sbom_digest": _digest("b"), "license_policy_digest": _digest("c"), "vulnerability_scan_digest": _digest("d"), "approval_id": "release-approval-1", "approval_version": "v1", "scan_decision": "passed", "evidence_expires_at": "2099-01-01T00:00:00+00:00", "approval_ref": "approval:gate-1", "approval_digest": _digest("e"), "idempotency_key": "gate-1"}, actor_uid=ACTOR)
  51. assert gate["decision_code"] == "RELEASE_GATE_APPROVED"
  52. assert "raw" not in str(repository.records).lower()
  53. def test_controls_service_rejects_provider_mismatch_and_legal_hold_destruction():
  54. repository = Repository()
  55. service = TrustedDeliveryControlsService(repository)
  56. with pytest.raises(TrustedDeliveryControlError, match="unsupported"):
  57. service.approve_capability({"provider": "kms", "capability": "dlp-export-check", "version": "v1", "config_digest": _digest("b"), "idempotency_key": "capability-2"}, actor_uid=ACTOR)
  58. repository.hold = True
  59. with pytest.raises(PermissionError, match="active legal hold"):
  60. service.approve_destruction({"asset_uid": str(uuid.uuid4()), "approval_refs": ["destroy:one", "destroy:two"], "evidence_digest": _digest("c"), "idempotency_key": "destroy-1"}, actor_uid=ACTOR)