aien-receipts
Append-only AIEN verification receipts, indexed by repository and exact source commit. Read verdicts, signatures and retained logs before making a claim.
Brought by drakestapleton
aien-receipts / audit / tools / verify.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Check an org audit stamp from outside. |
| 3 | |
| 4 | Usage: |
| 5 | verify.py <stamp-dir> [--rerun GITDIR] [--worktree DIR] [--tools DIR] [--salts FILE] |
| 6 | |
| 7 | Step 1 always runs: stamp.py's validation (schema, verdict ceiling, file hashes). |
| 8 | Step 2 runs with --rerun: checks.py is run again at the stamped commit from GITDIR |
| 9 | (a clone of the stamped repository), and every check's output_sha256 must equal the |
| 10 | stamped value. A different checker or tools lock cannot reproduce the stamp, so that |
| 11 | case is reported as NOT_RUN rather than as a match or a mismatch. |
| 12 | |
| 13 | Exit 0 verified, 1 invalid or mismatched, 2 NOT_RUN (could not reproduce). |
| 14 | """ |
| 15 | import argparse, json, os, sys, tempfile |
| 16 | |
| 17 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 18 | sys.path.insert(0, HERE) |
| 19 | import checks # noqa: E402 |
| 20 | import stamp # noqa: E402 |
| 21 | |
| 22 | |
| 23 | def rerun(stamp_dir, gitdir, worktree, tools, lock_path=checks.LOCK_PATH): |
| 24 | with open(os.path.join(stamp_dir, "stamp.json")) as fh: |
| 25 | st = json.load(fh) |
| 26 | dc = st["deterministic_checked"] |
| 27 | _, lock_sha = checks.load_lock(lock_path) |
| 28 | if dc.get("checker", {}).get("sha256") != checks.checker_sha256(): |
| 29 | return 2, ["stamp was made by checker %s; this checkout is %s" % (dc.get("checker", {}).get("sha256"), |
| 30 | checks.checker_sha256())] |
| 31 | if dc.get("tools_lock_sha256") != lock_sha: |
| 32 | return 2, ["stamp used tools lock %s; this checkout has %s" % (dc.get("tools_lock_sha256"), lock_sha)] |
| 33 | repo = st["subject"].get("repository") or st["subject"]["anonymous_id"] |
| 34 | with tempfile.TemporaryDirectory(prefix="aien-verify-") as out: |
| 35 | doc = checks.run_all(gitdir, st["subject"]["commit"], repo, out, dc.get("fork_base") or "", tools, worktree, |
| 36 | lock_path=lock_path) |
| 37 | got = {c["check"]: c for c in doc["checks"]} |
| 38 | errs = [] |
| 39 | skipped = [] |
| 40 | for c in dc["checks"]: |
| 41 | g = got.get(c["check"]) |
| 42 | if c.get("reproducible") is False: |
| 43 | skipped.append(c["check"]) # e.g. OSV advisories: an online database queried at stamp time |
| 44 | continue |
| 45 | if g is None: |
| 46 | errs.append("%s: not produced by the rerun" % c["check"]) |
| 47 | elif g["status"] == "NOT_RUN" and c["status"] != "NOT_RUN": |
| 48 | return 2, ["%s: tool unavailable here (%s)" % (c["check"], g.get("note", ""))] |
| 49 | elif g["output_sha256"] != c["output_sha256"]: |
| 50 | errs.append("%s: output %s, stamp says %s" % (c["check"], g["output_sha256"], c["output_sha256"])) |
| 51 | if not errs and skipped: |
| 52 | print("not reproducible by design, not compared: " + ", ".join(skipped), file=sys.stderr) |
| 53 | return (1 if errs else 0), errs |
| 54 | |
| 55 | |
| 56 | def main(): |
| 57 | ap = argparse.ArgumentParser() |
| 58 | ap.add_argument("stamp_dir") |
| 59 | ap.add_argument("--rerun", default="", help="git dir of the stamped repository") |
| 60 | ap.add_argument("--worktree", default="") |
| 61 | ap.add_argument("--tools", default=os.environ.get("AUDIT_TOOLS", HERE)) |
| 62 | ap.add_argument("--salts", default="") |
| 63 | a = ap.parse_args() |
| 64 | errs = stamp.validate(a.stamp_dir, a.salts or None) |
| 65 | if errs: |
| 66 | for e in errs: |
| 67 | print("INVALID: " + e, file=sys.stderr) |
| 68 | sys.exit(1) |
| 69 | if not a.rerun: |
| 70 | print("VALID (hashes and verdict ceiling); rerun NOT_RUN: pass --rerun GITDIR to reproduce the checks") |
| 71 | sys.exit(0) |
| 72 | code, msgs = rerun(a.stamp_dir, a.rerun, a.worktree, a.tools) |
| 73 | for m in msgs: |
| 74 | print(("MISMATCH: " if code == 1 else "NOT_RUN: ") + m, file=sys.stderr) |
| 75 | if code == 0: |
| 76 | print("VERIFIED " + a.stamp_dir) |
| 77 | sys.exit(code) |
| 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | main() |