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 / install_tools.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Install the pinned audit tools into DIR, refusing anything whose sha256 differs from tools.lock.json. |
| 3 | |
| 4 | Usage: install_tools.py DIR [--arch linux_amd64|linux_arm64] |
| 5 | |
| 6 | Downloads each binary named in the lock and checks the publisher's sha256 before |
| 7 | unpacking; installs semgrep and zizmor into DIR/venv with exact == version pins |
| 8 | (pip does not hash-check these; checks.py re-checks the printed versions); fetches semgrep-rules at the locked commit and rebuilds |
| 9 | semgrep-config.yaml (semgrep_config.py checks its sha256); fetches crumb.c at the |
| 10 | locked commit, checks its sha256 and builds DIR/crumb-arch. Exit 1 on any mismatch. |
| 11 | """ |
| 12 | import argparse, hashlib, io, json, os, platform, shutil, subprocess, sys, tarfile, urllib.request |
| 13 | |
| 14 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 15 | |
| 16 | |
| 17 | def fetch(url): |
| 18 | with urllib.request.urlopen(url, timeout=120) as r: |
| 19 | return r.read() |
| 20 | |
| 21 | |
| 22 | def run(*cmd, **kw): |
| 23 | subprocess.run(cmd, check=True, **kw) |
| 24 | |
| 25 | |
| 26 | def main(): |
| 27 | ap = argparse.ArgumentParser() |
| 28 | ap.add_argument("dir") |
| 29 | ap.add_argument("--arch", default={"x86_64": "linux_amd64", "aarch64": "linux_arm64"}.get(platform.machine(), "")) |
| 30 | ap.add_argument("--only", default="", help="comma-separated binary names; skips pip, semgrep-rules and crumb") |
| 31 | a = ap.parse_args() |
| 32 | with open(os.path.join(HERE, "tools.lock.json")) as fh: |
| 33 | lock = json.load(fh) |
| 34 | os.makedirs(a.dir, exist_ok=True) |
| 35 | only = set(filter(None, a.only.split(","))) |
| 36 | for name, t in lock["tools"].items(): |
| 37 | if only and name not in only: |
| 38 | continue |
| 39 | if "downloads" in t: |
| 40 | d = t["downloads"].get(a.arch) |
| 41 | if not d: |
| 42 | sys.exit("%s: no pinned download for %s" % (name, a.arch)) |
| 43 | data = fetch(d["url"]) |
| 44 | got = hashlib.sha256(data).hexdigest() |
| 45 | if got != d["sha256"]: |
| 46 | sys.exit("%s: sha256 %s != lock %s" % (name, got, d["sha256"])) |
| 47 | dest = os.path.join(a.dir, name) |
| 48 | if "member" in d: |
| 49 | with tarfile.open(fileobj=io.BytesIO(data)) as tf: |
| 50 | m = tf.getmember(d["member"]) |
| 51 | with tf.extractfile(m) as src, open(dest, "wb") as out: |
| 52 | shutil.copyfileobj(src, out) |
| 53 | else: |
| 54 | with open(dest, "wb") as out: |
| 55 | out.write(data) |
| 56 | os.chmod(dest, 0o755) |
| 57 | print("%s %s %s" % (got, name, t["version"])) |
| 58 | if only: |
| 59 | return |
| 60 | venv = os.path.join(a.dir, "venv") |
| 61 | run(sys.executable, "-m", "venv", venv) |
| 62 | pins = [t["pip"] for t in lock["tools"].values() if "pip" in t] + ["pyyaml==" + lock["semgrep_rules"]["pyyaml"]] |
| 63 | run(os.path.join(venv, "bin", "pip"), "install", "--quiet", *pins) |
| 64 | sr = lock["semgrep_rules"] |
| 65 | rules = os.path.join(a.dir, "semgrep-rules") |
| 66 | run("git", "init", "-q", rules) |
| 67 | run("git", "-C", rules, "fetch", "-q", "--depth", "1", sr["repository"], sr["commit"]) |
| 68 | run("git", "-C", rules, "checkout", "-q", "FETCH_HEAD") |
| 69 | run(os.path.join(venv, "bin", "python"), os.path.join(HERE, "semgrep_config.py"), rules, |
| 70 | os.path.join(a.dir, "semgrep-config.yaml")) |
| 71 | cr = lock["crumb"] |
| 72 | url = cr["repository"].replace("https://github.com/", "https://raw.githubusercontent.com/") + "/%s/%s" % (cr["commit"], cr["source"]) |
| 73 | src = fetch(url) |
| 74 | got = hashlib.sha256(src).hexdigest() |
| 75 | if got != cr["source_sha256"]: |
| 76 | sys.exit("crumb.c sha256 %s != lock %s" % (got, cr["source_sha256"])) |
| 77 | with open(os.path.join(a.dir, "crumb.c"), "wb") as fh: |
| 78 | fh.write(src) |
| 79 | run("cc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-o", os.path.join(a.dir, "crumb-arch"), |
| 80 | os.path.join(a.dir, "crumb.c")) |
| 81 | print("%s crumb.c" % got) |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | main() |