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 / status.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Mark each org audit stamp CURRENT, STALE, UNKNOWN or NOT_CHECKABLE. |
| 3 | |
| 4 | Usage: |
| 5 | status.py <stamps-dir> --out audit/status.json [--org aien-dev] [--heads FILE] |
| 6 | |
| 7 | For every public stamp the repository's default-branch HEAD is looked up with |
| 8 | `git ls-remote https://github.com/<org>/<repo> HEAD` (or read from --heads, a JSON |
| 9 | map repo -> sha, used by the tests). The newest stamp of a repository is |
| 10 | |
| 11 | CURRENT HEAD equals the stamped commit |
| 12 | STALE HEAD has moved past the stamped commit |
| 13 | UNKNOWN HEAD could not be read; never treated as CURRENT |
| 14 | NOT_CHECKABLE private stamp (self-reported; nobody outside can read the HEAD) |
| 15 | |
| 16 | Older stamps of the same repository are SUPERSEDED. The output carries no clock |
| 17 | time, so a run that sees the same HEADs writes the same bytes and the workflow |
| 18 | commits nothing. |
| 19 | """ |
| 20 | import argparse, json, os, re, subprocess, sys |
| 21 | |
| 22 | SHA40 = re.compile(r"^[0-9a-f]{40}$") |
| 23 | |
| 24 | |
| 25 | def ls_remote_head(org, repo): |
| 26 | try: |
| 27 | p = subprocess.run(["git", "ls-remote", "https://github.com/%s/%s" % (org, repo), "HEAD"], |
| 28 | capture_output=True, text=True, timeout=60, |
| 29 | env=dict(os.environ, GIT_TERMINAL_PROMPT="0")) |
| 30 | except (OSError, subprocess.TimeoutExpired) as e: |
| 31 | return None, "ls-remote failed: %s" % type(e).__name__ |
| 32 | sha = p.stdout.split("\t", 1)[0].strip() |
| 33 | if p.returncode != 0 or not SHA40.match(sha): |
| 34 | return None, "ls-remote exit %d" % p.returncode |
| 35 | return sha, "" |
| 36 | |
| 37 | |
| 38 | def collect(stamps_dir): |
| 39 | by_id = {} |
| 40 | if not os.path.isdir(stamps_dir): # no stamps published yet |
| 41 | return by_id |
| 42 | for ident in sorted(os.listdir(stamps_dir)): |
| 43 | d = os.path.join(stamps_dir, ident) |
| 44 | if not os.path.isdir(d): |
| 45 | continue |
| 46 | for sha in sorted(os.listdir(d)): |
| 47 | f = os.path.join(d, sha, "stamp.json") |
| 48 | if os.path.isfile(f): |
| 49 | with open(f) as fh: |
| 50 | st = json.load(fh) |
| 51 | by_id.setdefault(ident, []).append(st) |
| 52 | return by_id |
| 53 | |
| 54 | |
| 55 | def compute(stamps_dir, org, heads=None): |
| 56 | rows = [] |
| 57 | for ident, sts in collect(stamps_dir).items(): |
| 58 | # newest = latest stamped_at date, then commit for a total order |
| 59 | sts.sort(key=lambda s: (s.get("stamped_at", ""), s["subject"]["commit"])) |
| 60 | newest = sts[-1] |
| 61 | for st in sts[:-1]: |
| 62 | rows.append({"id": ident, "commit": st["subject"]["commit"], "verdict": st.get("verdict"), |
| 63 | "state": "SUPERSEDED", "head": None, "note": "a newer stamp exists"}) |
| 64 | sub = newest["subject"] |
| 65 | row = {"id": ident, "commit": sub["commit"], "verdict": newest.get("verdict")} |
| 66 | if sub.get("visibility") == "private": |
| 67 | row.update(state="NOT_CHECKABLE", head=None, note="private repository; stamp is self-reported") |
| 68 | else: |
| 69 | if heads is not None: |
| 70 | head, why = heads.get(ident), "no HEAD supplied" |
| 71 | else: |
| 72 | head, why = ls_remote_head(org, ident) |
| 73 | if not head: |
| 74 | row.update(state="UNKNOWN", head=None, note=why) |
| 75 | elif head == sub["commit"]: |
| 76 | row.update(state="CURRENT", head=head, note="") |
| 77 | else: |
| 78 | row.update(state="STALE", head=head, note="default-branch HEAD moved past the stamped commit") |
| 79 | rows.append(row) |
| 80 | rows.sort(key=lambda r: (r["id"], r["state"] == "SUPERSEDED", r["commit"])) |
| 81 | return {"schema": "aien-audit-status/1", "org": org, "stamps": rows} |
| 82 | |
| 83 | |
| 84 | def main(): |
| 85 | ap = argparse.ArgumentParser() |
| 86 | ap.add_argument("stamps_dir") |
| 87 | ap.add_argument("--out", required=True) |
| 88 | ap.add_argument("--org", default="aien-dev") |
| 89 | ap.add_argument("--heads", default="") |
| 90 | a = ap.parse_args() |
| 91 | heads = None |
| 92 | if a.heads: |
| 93 | with open(a.heads) as fh: |
| 94 | heads = json.load(fh) |
| 95 | doc = compute(a.stamps_dir, a.org, heads) |
| 96 | data = json.dumps(doc, indent=1, sort_keys=True) + "\n" |
| 97 | with open(a.out, "w") as fh: |
| 98 | fh.write(data) |
| 99 | unknown = sum(r["state"] == "UNKNOWN" for r in doc["stamps"]) |
| 100 | print("stamps=%d stale=%d unknown=%d" % (len(doc["stamps"]), sum(r["state"] == "STALE" for r in doc["stamps"]), unknown)) |
| 101 | sys.exit(3 if unknown else 0) |
| 102 | |
| 103 | |
| 104 | if __name__ == "__main__": |
| 105 | main() |