|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import jsonschema |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[1] |
| 11 | +RUNTIME_SCHEMA = ROOT / "schemas" / "runtime-effect-decision.v1.1.json" |
| 12 | +GRANT_SCHEMA = ROOT / "schemas" / "grant-state-decision.v1.1.json" |
| 13 | +RUNTIME_VALID = ROOT / "examples" / "runtime-effect-decision.valid.json" |
| 14 | +RUNTIME_INVALID_AUTHORITY = ROOT / "examples" / "runtime-effect-decision.authority-mutated.invalid.json" |
| 15 | +GRANT_VALID = ROOT / "examples" / "grant-state-decision.valid.json" |
| 16 | +GRANT_INVALID_MISSING_AUTH = ROOT / "examples" / "grant-state-decision.missing-authorization.invalid.json" |
| 17 | + |
| 18 | + |
| 19 | +class ValidationError(Exception): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +def load(path: Path) -> dict[str, Any]: |
| 24 | + payload = json.loads(path.read_text(encoding="utf-8")) |
| 25 | + if not isinstance(payload, dict): |
| 26 | + raise ValidationError(f"{path}: expected JSON object") |
| 27 | + return payload |
| 28 | + |
| 29 | + |
| 30 | +def validate_json_schema(schema_path: Path, instance_path: Path) -> dict[str, Any]: |
| 31 | + schema = load(schema_path) |
| 32 | + jsonschema.validators.validator_for(schema).check_schema(schema) |
| 33 | + instance = load(instance_path) |
| 34 | + jsonschema.validate(instance, schema) |
| 35 | + return instance |
| 36 | + |
| 37 | + |
| 38 | +def validate_runtime_effect(instance: dict[str, Any]) -> None: |
| 39 | + if instance.get("decision_kind") != "runtime-effect-decision": |
| 40 | + raise ValidationError("runtime effect decision_kind mismatch") |
| 41 | + if instance.get("authority_mutation_performed") is not False: |
| 42 | + raise ValidationError("runtime effect decisions must not mutate authority") |
| 43 | + if instance.get("ledger_write_performed") is not False: |
| 44 | + raise ValidationError("runtime effect decisions must not write ledger records") |
| 45 | + effect = instance.get("runtime_effect") |
| 46 | + status = instance.get("effect_status") |
| 47 | + scope = instance.get("effect_scope", {}) |
| 48 | + if effect in {"allow_dispatch", "export_ref_only"} and status not in {"admitted", "partial"}: |
| 49 | + raise ValidationError(f"{effect} requires admitted or partial status") |
| 50 | + if effect in {"block", "quarantine", "deny_dispatch"} and status == "admitted": |
| 51 | + raise ValidationError(f"{effect} cannot report admitted status") |
| 52 | + if scope.get("side_effecting") is True and effect in {"metadata_only", "export_ref_only", "noop"}: |
| 53 | + raise ValidationError("metadata/ref/noop runtime effects cannot be side-effecting") |
| 54 | + if instance.get("grant_state_decision_ref") and instance.get("authority_mutation_performed") is not False: |
| 55 | + raise ValidationError("grant_state_decision_ref is a reference, not inline authority mutation") |
| 56 | + |
| 57 | + |
| 58 | +def validate_grant_state(instance: dict[str, Any]) -> None: |
| 59 | + if instance.get("decision_kind") != "grant-state-decision": |
| 60 | + raise ValidationError("grant state decision_kind mismatch") |
| 61 | + if not instance.get("authorization_policy_ref"): |
| 62 | + raise ValidationError("grant state decisions require authorization_policy_ref") |
| 63 | + if not instance.get("authorization_evidence_refs"): |
| 64 | + raise ValidationError("grant state decisions require authorization_evidence_refs") |
| 65 | + decision = instance.get("authority_decision") |
| 66 | + effects = instance.get("authority_effects", {}) |
| 67 | + changed = any(value != "unchanged" for value in effects.values()) |
| 68 | + if decision == "unchanged" and changed: |
| 69 | + raise ValidationError("unchanged grant state decision requires unchanged authority_effects") |
| 70 | + if decision != "unchanged" and not changed: |
| 71 | + raise ValidationError("changed grant state decision requires changed authority_effects") |
| 72 | + if decision == "revoked" and any(value != "revoked" for value in effects.values()): |
| 73 | + raise ValidationError("revoked grant state decision requires all authority_effects revoked") |
| 74 | + if decision == "restored" and instance.get("restoration_allowed") is not True: |
| 75 | + raise ValidationError("restored grant state decision requires restoration_allowed=true") |
| 76 | + |
| 77 | + |
| 78 | +def expect_invalid(schema_path: Path, instance_path: Path, semantic_validator) -> None: |
| 79 | + try: |
| 80 | + instance = validate_json_schema(schema_path, instance_path) |
| 81 | + semantic_validator(instance) |
| 82 | + except Exception: |
| 83 | + return |
| 84 | + raise ValidationError(f"invalid fixture unexpectedly validated: {instance_path.relative_to(ROOT)}") |
| 85 | + |
| 86 | + |
| 87 | +def main() -> int: |
| 88 | + runtime = validate_json_schema(RUNTIME_SCHEMA, RUNTIME_VALID) |
| 89 | + validate_runtime_effect(runtime) |
| 90 | + grant = validate_json_schema(GRANT_SCHEMA, GRANT_VALID) |
| 91 | + validate_grant_state(grant) |
| 92 | + expect_invalid(RUNTIME_SCHEMA, RUNTIME_INVALID_AUTHORITY, validate_runtime_effect) |
| 93 | + expect_invalid(GRANT_SCHEMA, GRANT_INVALID_MISSING_AUTH, validate_grant_state) |
| 94 | + print(json.dumps({"ok": True, "checks": [ |
| 95 | + str(RUNTIME_VALID.relative_to(ROOT)), |
| 96 | + str(GRANT_VALID.relative_to(ROOT)), |
| 97 | + str(RUNTIME_INVALID_AUTHORITY.relative_to(ROOT)), |
| 98 | + str(GRANT_INVALID_MISSING_AUTH.relative_to(ROOT)), |
| 99 | + ]}, indent=2, sort_keys=True)) |
| 100 | + return 0 |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + raise SystemExit(main()) |
0 commit comments