diff --git a/README.md b/README.md index fdb1a7b..17cc706 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,8 @@ processes, so stop them first: Ctrl-C in each terminal, the agent first. scripts/install-agent ``` -It builds NeonWatch's wheel and downloads the nats-py wheel `uv.lock` pins (checked against its +It builds NeonWatch's wheel, with a build backend pinned by hash in `build-constraints.txt` (its +header says how to move it), and downloads the nats-py wheel `uv.lock` pins (checked against its hash) into `dist/agent` as you, then asks sudo to run `tools/services.py install-agent --operator ` on the system Python, isolated. That copies the bundle somewhere only root can write and checks every hash. It builds `/opt/neonwatch.new`, diff --git a/build-constraints.txt b/build-constraints.txt new file mode 100644 index 0000000..5c3db9a --- /dev/null +++ b/build-constraints.txt @@ -0,0 +1,35 @@ +# The build backend and everything it imports, pinned by hash. `tools/services.py bundle` +# builds the wheel root installs with `uv build --require-hashes --build-constraints` on this +# file, so the code that writes the wheel is as fixed as uv.lock fixes what goes into it: a new +# release of any of these cannot reach /opt until this file names it. uv refuses a hash that does +# not match, and any build requirement not pinned here. +# +# To move it, pick versions at least two weeks old and regenerate: +# echo hatchling > /tmp/build.in +# uv pip compile --universal --generate-hashes --no-header --python-version 3.12 \ +# --exclude-newer /tmp/build.in -o build-constraints.txt +# then put this header back. +hatchling==1.32.4 \ + --hash=sha256:08ecf7548fb48205e7f213d70c71e67b8271b7242093dc3f1da578b42c734a2c \ + --hash=sha256:c4468f73144c054d2aab4ef0f0378c43b9878bf07f8ffd6b79690e970d375f07 + # via -r build.in +packaging==26.3 \ + --hash=sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79 \ + --hash=sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c + # via hatchling +pathspec==1.1.1 \ + --hash=sha256:17db5ecd524104a120e173814c90367a96a98d07c45b2e10c2f3919fff91bf5a \ + --hash=sha256:a00ce642f577bf7f473932318056212bc4f8bfdf53128c78bbd5af0b9b20b189 + # via hatchling +pluggy==1.7.0 \ + --hash=sha256:7dd7b0d8832ba3cb632c306926ded123429211b83641b35dc5c41ad2d34f9bec \ + --hash=sha256:d1eaa46ebb595891b860ab086b4d09c8588af65ebd4361b8e8f4bb8920b90ba8 + # via hatchling +tomlkit==0.15.2 \ + --hash=sha256:05562412163a3037b94497385fb7f9204c637bc605af7a48199e41cf2272f437 \ + --hash=sha256:83e32755ba1d20d9340bd069ee97e9f6232ba2346b63ffcba425de01f7e327a6 + # via hatchling +trove-classifiers==2026.9.21.13 \ + --hash=sha256:0a9ebc8d4e2f3e8a22848c5258033035bec17a3012ac3fea16dbaa764489eb71 \ + --hash=sha256:8b1ff4f9c191b1040b71c37f1e445ab99732911e3cd91de52838453a854d7a17 + # via hatchling diff --git a/tools/services.py b/tools/services.py index 1572931..b3542cb 100644 --- a/tools/services.py +++ b/tools/services.py @@ -19,7 +19,9 @@ What this does not close: the bundle is built by your account, so installing trusts your account at that moment, and so does running this file under sudo. What it closes is the standing -exposure: from then on the unit runs nothing your account can modify. +exposure: from then on the unit runs nothing your account can modify. The build itself runs only +pinned code: nats-py comes from uv.lock by hash, and the build backend that writes our wheel from +build-constraints.txt by hash, so a new release of either cannot reach /opt unannounced. `--root DIR` installs under DIR as a staging prefix (the unit still names /opt/neonwatch) and expects files owned by whoever runs it; with `--no-systemctl` that is how the tests run it. @@ -53,6 +55,8 @@ PYTHON = "/usr/bin/python3" NATS = "nats-py" MANIFEST = "manifest.json" +#: The build backend and its imports, pinned by hash: what writes the wheel root installs. +BUILD_CONSTRAINTS = "build-constraints.txt" #: What `neonwatch.launch agent` imports once it runs; importing them proves the bundle is whole. AGENT_MODULES = ("neonwatch.launch", "neonwatch.bus", "neonwatch.collector", "neonwatch.executor", "neonwatch.probes") @@ -205,7 +209,8 @@ def bundle(out: Path = BUNDLE, *, repo: Path = REPO, run: Run = subprocess.run, if out.exists(): shutil.rmtree(out) out.mkdir(parents=True) - run(["uv", "build", "--wheel", "--out-dir", str(out), str(repo)], check=True) + run(["uv", "build", "--wheel", "--require-hashes", "--build-constraints", + str(repo / BUILD_CONSTRAINTS), "--out-dir", str(out), str(repo)], check=True) built = sorted(out.glob("*.whl")) if len(built) != 1: raise Refused(f"uv build left {len(built)} wheels in {out}, not one") diff --git a/tools/services_test.py b/tools/services_test.py index 82355f2..22ed3bb 100644 --- a/tools/services_test.py +++ b/tools/services_test.py @@ -11,6 +11,7 @@ import hashlib import json import os +import re import shutil import subprocess import sys @@ -176,8 +177,21 @@ def fake_repo(tmp_path: Path, nats: bytes) -> Path: def fake_uv(argv, *, check): - assert argv[:4] == ["uv", "build", "--wheel", "--out-dir"] and check - (Path(argv[4]) / "neonwatch-0.1.0-py3-none-any.whl").write_bytes(b"ours") + assert argv[:5] == ["uv", "build", "--wheel", "--require-hashes", "--build-constraints"] + assert Path(argv[5]).name == services.BUILD_CONSTRAINTS and argv[6] == "--out-dir" and check + (Path(argv[7]) / "neonwatch-0.1.0-py3-none-any.whl").write_bytes(b"ours") + + +def test_the_build_backend_and_all_it_imports_are_pinned_by_hash(): + """uv refuses an unpinned build requirement under --require-hashes; this says so before a + bundle is attempted, and that the backend pyproject names is among them.""" + text = (REPO / services.BUILD_CONSTRAINTS).read_text() + pins = re.findall(r"^([a-z0-9][a-z0-9._-]*)==\S+((?:\s*\\\n\s+--hash=sha256:[0-9a-f]{64})+)", + text, re.M) + names = [name for name, _ in pins] + requirements = [line for line in text.splitlines() if line and line[0].isalnum()] + assert len(names) == len(requirements) and "hatchling" in names + assert 'requires = ["hatchling"]' in (REPO / "pyproject.toml").read_text() def test_bundle_builds_our_wheel_fetches_the_pinned_one_and_writes_their_hashes(tmp_path):