diff --git a/.gitea/workflows/build-push.yaml b/.gitea/workflows/build-push.yaml index 407307d..e3d49f3 100644 --- a/.gitea/workflows/build-push.yaml +++ b/.gitea/workflows/build-push.yaml @@ -133,6 +133,29 @@ jobs: PKG_CACHE=${{ steps.pkgcache.outputs.url }} outputs: type=image,"name=repo.anhonesthost.net/cloud-hosting-platform/haproxy-manager-base,ghcr.io/shadowdao/haproxy-manager-base",push-by-digest=true,name-canonical=true,push=true + # Per-platform ELF check BEFORE anything is tagged (2026-11-19: an "arm64" + # entry held x86-64 binaries). amd64 mismatch fails the job and publishes + # nothing. An arm64 mismatch (or a failed check) keeps arm64 OUT of the + # tags (amd64-only publish, as for a failed arm64 build) and the job is + # failed afterwards so it is not mistaken for a good multi-arch publish. + - name: Verify amd64 image binaries are amd64 + env: + DIGEST: ${{ steps.amd64.outputs.digest }} + run: | + set -euo pipefail + python3 -I scripts/verify-image-arch.py "repo.anhonesthost.net/cloud-hosting-platform/haproxy-manager-base@${DIGEST}" amd64 + + - name: Verify arm64 image binaries are arm64 + id: arm64check + if: steps.arm64.outcome == 'success' + continue-on-error: true + timeout-minutes: 10 + env: + DIGEST: ${{ steps.arm64.outputs.digest }} + run: | + set -euo pipefail + python3 -I scripts/verify-image-arch.py "repo.anhonesthost.net/cloud-hosting-platform/haproxy-manager-base@${DIGEST}" arm64 + # imagetools needs a NEW buildx. Same pinned, sha256-verified binary as # mirror-base-image.yaml (kept identical on purpose). The assemble step # previously ran on whatever buildx the (possibly ephemeral) runner had @@ -163,7 +186,8 @@ jobs: BUILDKIT_PROGRESS: plain AMD64_DIGEST: ${{ steps.amd64.outputs.digest }} ARM64_DIGEST: ${{ steps.arm64.outputs.digest }} - ARM64_OUTCOME: ${{ steps.arm64.outcome }} + # success only if the build AND the ELF check succeeded (a skipped check = not verified = not attached) + ARM64_OUTCOME: ${{ steps.arm64check.outcome == 'success' && steps.arm64.outcome || 'unverified' }} VERSION: ${{ steps.ver.outputs.version }} TAGS: ${{ steps.tags.outputs.tags }} run: | @@ -209,3 +233,11 @@ jobs: --repo repo.anhonesthost.net/cloud-hosting-platform/haproxy-manager-base \ --repo ghcr.io/shadowdao/haproxy-manager-base \ "${tag_args[@]}" + + # Tags are already published (amd64-only) if arm64 failed verification; fail + # the job so that is visible instead of reading as a green multi-arch publish. + - name: Fail if arm64 was built but its binaries are not arm64 + if: always() && steps.arm64.outcome == 'success' && steps.arm64check.outcome != 'success' + run: | + echo "::error::arm64 image was built but failed the ELF architecture check; tags were published amd64-only (or not at all)" + exit 1 diff --git a/Dockerfile b/Dockerfile index 85732c2..ecfd9f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,17 @@ # Future improvement: a scheduled Gitea Action that does the above automatically. FROM repo.anhonesthost.net/cloud-hosting-platform/python:3.12-slim +# Base-architecture guard. If the mirrored base tag is a single-platform +# (flattened amd64) manifest instead of a multi-arch index, buildx silently uses +# that amd64 base for a linux/arm64 build, every RUN below executes natively as +# x86-64, and the result is x86-64 binaries labelled arm64 (2026-11-19: +# haproxy-manager-base:latest). TARGETARCH is set by buildx (empty on the legacy +# builder, where the check is skipped). On a correct build this is a no-op. +ARG TARGETARCH +RUN if [ -n "${TARGETARCH}" ] && [ "$(dpkg --print-architecture)" != "${TARGETARCH}" ]; then \ + echo "FATAL: base image is $(dpkg --print-architecture) but this build targets ${TARGETARCH}; the in-house python:3.12-slim mirror is not a multi-arch index (re-run mirror-base-image.yaml)" >&2; exit 1; \ + fi + # image.source is what ghcr.io uses to link the package to a GitHub repo # sidebar; pointing at the public GitHub mirror enables that linking. The # canonical source-of-truth git remote is still Gitea, but Gitea's registry diff --git a/scripts/test-verify-image-arch.py b/scripts/test-verify-image-arch.py new file mode 100755 index 0000000..410c9d1 --- /dev/null +++ b/scripts/test-verify-image-arch.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Tests scripts/verify-image-arch.py against a stub `docker` (no daemon needed).""" +import os, stat, subprocess, sys, tempfile, textwrap + +HERE = os.path.dirname(os.path.abspath(__file__)) +SCRIPT = os.path.join(HERE, "verify-image-arch.py") +fails = 0 + +def elf(machine): + return b"\x7fELF\x02\x01\x01" + b"\0" * 11 + machine.to_bytes(2, "little") + b"\0" * 40 + +def run(cfg_arch, file_arch, expect_rc, label, psutil_so=True): + global fails + with tempfile.TemporaryDirectory() as td: + stub = os.path.join(td, "docker") + with open(stub, "w") as f: + f.write(textwrap.dedent(f"""\ + #!/usr/bin/env python3 + import sys, os + a = sys.argv[1:] + E = {elf(file_arch)!r} + if a[0] == "image": print("{cfg_arch}") + elif a[0] == "create": print("cid1") + elif a[0] == "cp": + src, dst = a[-2], a[-1] + if src.endswith("/psutil"): + os.makedirs(dst) + if {psutil_so!r}: open(os.path.join(dst, "_psutil_linux.cpython-312.so"), "wb").write(E) + else: open(dst, "wb").write(E) + """)) + os.chmod(stub, os.stat(stub).st_mode | stat.S_IXUSR) + r = subprocess.run([sys.executable, "-I", SCRIPT, "ref@sha256:x", "arm64"], + env={"DOCKER": stub, "PATH": os.environ["PATH"]}, capture_output=True, text=True) + ok = r.returncode == expect_rc + print(("ok " if ok else "FAIL"), label, "rc=", r.returncode) + if not ok: + fails += 1; print(r.stdout, r.stderr) + +run("arm64", 183, 0, "genuine arm64 passes") +run("arm64", 62, 1, "arm64-labelled x86-64 binaries (the 2026-11-19 bug) fail") +run("amd64", 183, 1, "wrong config architecture fails") +run("arm64", 183, 1, "missing compiled extension fails closed", psutil_so=False) +sys.exit(1 if fails else 0) diff --git a/scripts/verify-image-arch.py b/scripts/verify-image-arch.py new file mode 100755 index 0000000..8d941d3 --- /dev/null +++ b/scripts/verify-image-arch.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Verify that a per-platform image really contains binaries for that platform. + +CI only (not copied into images' runtime use). Usage: + + verify-image-arch.py + +Pulls (normally @sha256: of ONE platform, pushed by +digest and not yet tagged) with --platform linux/, never runs it, and +checks: + * the image config's architecture equals ; + * the ELF e_machine of haproxy, bash, the python interpreter and every + compiled extension of the python stack (psutil *.so) equals . + +Why: 2026-11-19, haproxy-manager-base:latest carried an "arm64" entry whose +binaries were all x86-64 (the in-house python:3.12-slim base was a flattened +amd64-only manifest, so the arm64 build silently ran on an amd64 base). +Manifest/config metadata said arm64; only the ELF headers told the truth. + +Exit 0 = every checked file matches; non-zero = mismatch or could not check +(fail closed). Env DOCKER overrides the docker command (tests use a stub). +""" +import os +import shlex +import struct +import subprocess +import sys +import tempfile + +EM = {"amd64": 62, "arm64": 183} # EM_X86_64, EM_AARCH64 +EM_NAME = {62: "x86-64", 183: "aarch64", 40: "arm", 3: "i386"} +FILES = ["/usr/sbin/haproxy", "/bin/bash", "/usr/local/bin/python3.12"] +EXT_DIR = "/usr/local/lib/python3.12/site-packages/psutil" # compiled (_psutil_linux*.so) + + +def elf_machine(path): + """e_machine of an ELF file, or None if it is not a little-endian ELF.""" + with open(path, "rb") as f: + h = f.read(20) + if len(h) < 20 or h[:4] != b"\x7fELF": + return None + endian = "<" if h[5] == 1 else ">" + return struct.unpack(endian + "H", h[18:20])[0] + + +def docker(*args, check=True): + cmd = shlex.split(os.environ.get("DOCKER", "docker")) + list(args) + return subprocess.run(cmd, check=check, capture_output=True, text=True) + + +def main(argv): + if len(argv) != 3 or argv[2] not in EM: + print("usage: verify-image-arch.py ", file=sys.stderr) + return 2 + ref, arch = argv[1], argv[2] + want = EM[arch] + bad = [] + docker("pull", "--quiet", "--platform", f"linux/{arch}", ref) + cfg = docker("image", "inspect", ref, "--format", "{{.Architecture}}").stdout.strip() + print(f"{ref}: image config architecture={cfg} (expected {arch})") + if cfg != arch: + bad.append(f"config architecture {cfg!r}") + cid = docker("create", "--platform", f"linux/{arch}", ref).stdout.strip() + try: + with tempfile.TemporaryDirectory() as td: + for f in FILES: + dst = os.path.join(td, os.path.basename(f)) + docker("cp", "-L", f"{cid}:{f}", dst) + m = elf_machine(dst) + print(f" {f}: {EM_NAME.get(m, m)}") + if m != want: + bad.append(f"{f} is {EM_NAME.get(m, m)}") + extdir = os.path.join(td, "psutil") + docker("cp", f"{cid}:{EXT_DIR}", extdir) + sos = sorted(os.path.join(extdir, n) for n in os.listdir(extdir) if n.endswith(".so")) + if not sos: + bad.append(f"no compiled extension found in {EXT_DIR}") + for so in sos: + m = elf_machine(so) + print(f" psutil/{os.path.basename(so)}: {EM_NAME.get(m, m)}") + if m != want: + bad.append(f"psutil/{os.path.basename(so)} is {EM_NAME.get(m, m)}") + finally: + docker("rm", cid, check=False) + if bad: + print(f"::error::{ref} is labelled linux/{arch} but: " + "; ".join(bad), file=sys.stderr) + return 1 + print(f"OK: {ref} is genuinely {arch}") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv))