File size: 2,725 Bytes
45c2a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""Verify that released synthetic observations resolve only to local evidence."""
from __future__ import annotations

import hashlib
import json
import sys
from pathlib import Path


ROOT = Path(sys.argv[1]).resolve() if len(sys.argv) == 2 else Path(__file__).resolve().parents[1]
INDEX_PATH = ROOT / "evidence" / "evidence-index-v0.2.0.json"


def digest(path: Path) -> str:
    return hashlib.sha256(path.read_bytes()).hexdigest()


def fail(message: str) -> None:
    raise SystemExit(f"invalid evidence binding: {message}")


def local_path(relative: str) -> Path:
    candidate = (ROOT / relative).resolve()
    if not candidate.is_relative_to(ROOT) or not candidate.is_file():
        fail(f"unreleased or external reference: {relative}")
    return candidate


def main() -> None:
    index = json.loads(INDEX_PATH.read_text())
    run = json.loads((ROOT / "examples" / "synthetic-characterization.json").read_text())
    if index["contract_version"] != "0.2.0" or index["classification"] != "synthetic-only":
        fail("contract metadata")
    configuration = index["target_configuration"]
    config_path = local_path(configuration["path"])
    if digest(config_path) != configuration["sha256"] or config_path.stat().st_size != configuration["bytes"]:
        fail("target configuration manifest")
    receipts = {receipt["case_id"]: receipt for receipt in index["receipts"]}
    if len(receipts) != len(index["receipts"]):
        fail("duplicate receipt case")
    for observation in run["observations"]:
        case_id = observation["case"]["id"]
        receipt = receipts.get(case_id)
        if receipt is None:
            fail(f"missing receipt: {case_id}")
        payload = local_path(receipt["path"])
        if receipt["media_type"] != "application/json":
            fail(f"media type: {case_id}")
        if digest(payload) != receipt["sha256"] or payload.stat().st_size != receipt["bytes"]:
            fail(f"payload bytes or digest: {case_id}")
        payload_data = json.loads(payload.read_text())
        if payload_data.get("classification") != "synthetic-only" or payload_data.get("case_id") != case_id:
            fail(f"payload binding: {case_id}")
        if observation["evidence"]["digest"] != receipt["sha256"]:
            fail(f"observation evidence digest: {case_id}")
        if observation["target_configuration_digest"] != configuration["sha256"]:
            fail(f"observation target digest: {case_id}")
    if set(receipts) != {item["case"]["id"] for item in run["observations"]}:
        fail("unbound receipt")
    print(f"valid: {len(receipts)} local synthetic evidence receipts and configuration manifest")


if __name__ == "__main__":
    main()