diff --git a/.github/workflows/measurements.yml b/.github/workflows/measurements.yml new file mode 100644 index 0000000..3ffaade --- /dev/null +++ b/.github/workflows/measurements.yml @@ -0,0 +1,80 @@ +name: Measurements + +# measurements.txt publishes the PCRs of the enclave image this repo builds, so +# third parties can check what code an enclave runs. This job builds the image +# from the checkout, measures it, and fails when the PCRs differ from the +# checked-in file. Any change that alters the image (code, lockfile, +# Dockerfile, a pinned base image or Debian snapshot) must update +# measurements.txt in the same PR: run `make measurements`, or copy the JSON +# this job prints. + +on: + pull_request: + # Inputs to the image (see the Dockerfile and .dockerignore) plus this + # check itself. If you make this a required check, drop the filter: + # a required check that never runs blocks the PR. + paths: + - Dockerfile + - .dockerignore + - Makefile + - pyproject.toml + - uv.lock + - scripts/start.sh + - scripts/measure-enclave.sh + - tee_gateway/** + - measurements.txt + - .github/workflows/measurements.yml + push: + branches: [main] + workflow_dispatch: + +jobs: + measurements: + runs-on: ubuntu-latest + permissions: + contents: read + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + + - name: Build image + run: make image + + - name: Measure image + run: make measurements MEASUREMENTS="$RUNNER_TEMP/computed-measurements.txt" + + - name: Keep the computed measurements + if: always() + uses: actions/upload-artifact@v4 + with: + name: computed-measurements + path: ${{ runner.temp }}/computed-measurements.txt + if-no-files-found: ignore + + - name: Compare with measurements.txt + run: | + computed="$RUNNER_TEMP/computed-measurements.txt" + pcrs='.Measurements | {PCR0, PCR1, PCR2}' + if [ "$(jq -S "$pcrs" measurements.txt)" = "$(jq -S "$pcrs" "$computed")" ]; then + echo "measurements.txt matches the image this commit builds." + exit 0 + fi + { + echo "### measurements.txt does not match this commit" + echo + echo "Replace measurements.txt with:" + echo + echo '```json' + cat "$computed" + echo '```' + echo + echo "Checked in:" + echo + echo '```json' + cat measurements.txt + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + echo "::error file=measurements.txt::measurements.txt does not match the image this commit builds. Run 'make measurements' and commit the result, or copy it from this job's summary." + echo "Computed:"; cat "$computed" + echo "Checked in:"; cat measurements.txt + exit 1 diff --git a/CLAUDE.md b/CLAUDE.md index 02cf409..119518b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -54,6 +54,8 @@ make mypy # Run mypy type checker only # Build enclave image make image # Build Docker image as TAR using Kaniko +make measurements # Build it and write its PCR0-2 to measurements.txt + # (needs only Docker; CI fails if the file is stale) # Build EIF and run in Nitro Enclave make run # or: make all @@ -126,6 +128,26 @@ Server configuration: - Endpoints `/enclave/ready` and `/enclave/hash` used for nitriding registration - PCR measurements in `measurements.txt` fingerprint the exact enclave image +### Measurements + +`measurements.txt` must always hold the PCRs of the image the checked-in code +builds. The `Measurements` workflow builds the image on every PR that touches +an image input, measures it (`scripts/measure-enclave.sh`: nitro-cli +`build-enclave` in a pinned Amazon Linux container, no Nitro hardware needed), +and fails on a mismatch, printing the JSON to commit. So a PR that changes +code, `uv.lock` or the Dockerfile also updates `measurements.txt`, via +`make measurements` or by copying the job's output. + +That only works because every build input is pinned in the `Dockerfile`: base +images by digest, Debian packages from a fixed `snapshot.debian.org` date +(`DEBIAN_SNAPSHOT`), nitriding by commit, Python packages by `uv.lock`. Never +use a floating tag or the live apt mirror: the same commit would then measure +differently from week to week. To take upstream updates (e.g. Debian security +fixes), bump the digest or snapshot date in a PR and re-record the +measurements. PCR1 depends only on nitro-cli's enclave kernel, which is the +same in every release from 1.3.4 to 1.5.3, so the hosts' nitro-cli version +does not affect the measurements. + ### Supported Providers `model_registry.py` is the source of truth; this list mirrors it. Model name diff --git a/Dockerfile b/Dockerfile index 8369b50..c34e564 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,12 @@ -# Build nitriding from source -FROM golang:1.24 as builder +# Every input to this image is pinned, so the same commit always builds the +# same image and the same PCRs (measurements.txt, checked in CI): base images +# by digest, Debian packages from a fixed snapshot.debian.org date. A floating +# tag or a live apt mirror would change the measurements with no code change. +# To update one, bump it here and re-record measurements.txt +# (`make measurements`). + +# Build nitriding from source (Go 1.24.13) +FROM golang:1.24@sha256:d2d2bc1c84f7e60d7d2438a3836ae7d0c847f4888464e7ec9ba3a1339a1ee804 as builder WORKDIR / @@ -16,19 +23,35 @@ RUN chown root:root /bin/start.sh RUN chmod 0755 /bin/start.sh # ---------- Final image ---------- -FROM python:3.12.10-slim-bookworm +FROM python:3.12.10-slim-bookworm@sha256:fd95fa221297a88e1cf49c55ec1828edd7c5a428187e67b5d1805692d11588db # API keys are NOT set here — they are injected at runtime via POST /v1/keys # after the enclave starts, keeping PCR measurements stable across deployments. -# Install necessary tools -RUN echo 'Dir::Log "/dev/null";' > /etc/apt/apt.conf.d/00no-log \ +# Install necessary tools, from the Debian archive as it was at a fixed moment +# (see the top of this file). Check-Valid-Until is off because a snapshot's +# Release files expire by design. +ARG DEBIAN_SNAPSHOT=20261124T000000Z +RUN printf '%s\n' \ + 'Types: deb' \ + "URIs: http://snapshot.debian.org/archive/debian/${DEBIAN_SNAPSHOT}" \ + 'Suites: bookworm bookworm-updates' \ + 'Components: main' \ + 'Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg' \ + '' \ + 'Types: deb' \ + "URIs: http://snapshot.debian.org/archive/debian-security/${DEBIAN_SNAPSHOT}" \ + 'Suites: bookworm-security' \ + 'Components: main' \ + 'Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg' \ + > /etc/apt/sources.list.d/debian.sources \ + && echo 'Dir::Log "/dev/null";' > /etc/apt/apt.conf.d/00no-log \ && echo 'Dir::Log::Terminal "";' >> /etc/apt/apt.conf.d/00no-log \ && echo 'Dir::Log::History "";' >> /etc/apt/apt.conf.d/00no-log \ && ln -sf /dev/null /var/log/dpkg.log \ && ln -sf /dev/null /var/log/alternatives.log \ && rm -rf /var/lib/apt/lists/* \ - && apt-get -o Acquire::Retries=5 update -qq \ + && apt-get -o Acquire::Retries=5 -o Acquire::Check-Valid-Until=false update -qq \ && apt-get install -y --no-install-recommends \ wget \ tar \ @@ -47,7 +70,7 @@ COPY --from=builder /nitriding-daemon/nitriding /bin/nitriding COPY --from=builder /bin/start.sh /bin/start.sh # Install uv for deterministic dependency installation from lockfile -COPY --from=ghcr.io/astral-sh/uv:0.7.3 /uv /usr/local/bin/uv +COPY --from=ghcr.io/astral-sh/uv:0.7.3@sha256:87a04222b228501907f487b338ca6fc1514a93369bfce6930eb06c8d576e58a4 /uv /usr/local/bin/uv # Install Python dependencies from lockfile (exact versions + hashes) COPY pyproject.toml uv.lock /app/ diff --git a/Makefile b/Makefile index 13567b8..d22d9fa 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,11 @@ all: run .PHONY: image image: $(image_tar) -$(image_tar): Dockerfile scripts/start.sh pyproject.toml uv.lock +# Everything the Dockerfile copies in, so an edited source file rebuilds. +image_inputs := Dockerfile .dockerignore scripts/start.sh pyproject.toml uv.lock \ + $(shell find tee_gateway -type f -not -path '*/__pycache__/*') + +$(image_tar): $(image_inputs) find tee_gateway -exec touch -t 202311150000 {} \; touch -t 202311150000 scripts/start.sh Dockerfile pyproject.toml uv.lock SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) docker run \ @@ -42,6 +46,18 @@ $(image_eif): $(image_tar) --docker-uri $(image_tag) \ --output-file $(image_eif) +# PCR0-2 of the image this checkout builds. CI runs the same target and fails +# when the result differs from the checked-in measurements.txt; write it +# elsewhere with `make measurements MEASUREMENTS=/path`. +MEASUREMENTS ?= measurements.txt + +.PHONY: measurements +measurements: $(image_tar) + docker load -i $(image_tar) + ./scripts/measure-enclave.sh $(image_tag) > $(MEASUREMENTS).tmp + mv $(MEASUREMENTS).tmp $(MEASUREMENTS) + cat $(MEASUREMENTS) + .PHONY: run run: $(image_eif) nitro-cli terminate-enclave --all @@ -107,6 +123,7 @@ help: @echo "Available targets:" @echo " make run - Build image, build EIF, and run in Nitro Enclave" @echo " make image - Build reproducible Docker image TAR (Kaniko)" + @echo " make measurements - Build the image and write its PCRs to measurements.txt" @echo " make clean - Remove build artifacts (.tar, .eif)" @echo "" @echo " make health - Check enclave health endpoint" diff --git a/README.md b/README.md index 1e75dbe..0238776 100644 --- a/README.md +++ b/README.md @@ -153,9 +153,16 @@ The enclave runs with: ### PCR Measurements -PCR (Platform Configuration Register) measurements uniquely fingerprint the enclave image — they change whenever the code or build environment changes. They are automatically written to `measurements.txt` by `scripts/run-enclave.sh` when the enclave starts. +PCR (Platform Configuration Register) measurements uniquely fingerprint the enclave image. The build is reproducible: every input is pinned (base images by digest, Debian packages from a fixed snapshot, Python packages by `uv.lock`), so a given commit always produces the same PCRs, on any machine. -The `measurements.txt` checked into this repository reflects the OpenGradient-operated deployment. **If you build and run your own enclave image, your PCR values will differ.** After running `make run`, your `measurements.txt` will be updated with your enclave's measurements. Share this file with your clients so they can verify attestation documents match your specific build. +`measurements.txt` holds the PCRs of the image the checked-in code builds, and CI fails any change that would make it stale. To check it yourself, you only need Docker (no Nitro hardware): + +```bash +make measurements MEASUREMENTS=/tmp/my-measurements.txt +diff measurements.txt /tmp/my-measurements.txt +``` + +An operator running an unmodified checkout gets these same PCRs, which is what lets third-party enclaves join the network. `scripts/run-enclave.sh` also writes the running enclave's measurements to `measurements.txt` when it starts. On an unmodified checkout that leaves the file unchanged; a diff there means the enclave is not running the checked-in code. ## API Reference diff --git a/measurements.txt b/measurements.txt index 38132ac..7fdc0af 100644 --- a/measurements.txt +++ b/measurements.txt @@ -1,8 +1,8 @@ { "Measurements": { "HashAlgorithm": "Sha384 { ... }", - "PCR0": "f2945c350e80c9431649dd691cb4ecebc11174c0201e22c151b0b11c0aa41074174b57cc17343444380f212baaf5752c", + "PCR0": "552bba1727641d72a12305304fb2c70666190701e5d76b1622acb8f86ce94dda4f83ed2912636931c15f3700da0055d1", "PCR1": "4b4d5b3661b3efc12920900c80e126e4ce783c522de6c02a2a5bf7af3a2b9327b86776f188e4be1c1c404a129dbda493", - "PCR2": "01d8b4615e5dc0417d57713b6d8d2c3b0ab3ec3e1848658c116a2986da35b47c04001c8acbea57858446d0bf0a35de68" + "PCR2": "ac6e4cc684050c05ef7a76523c6515775863e5a641768e0223facb0ad283102e321eff38a504c29c49515aed955692fa" } } diff --git a/scripts/measure-enclave.sh b/scripts/measure-enclave.sh new file mode 100755 index 0000000..4e524e6 --- /dev/null +++ b/scripts/measure-enclave.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# Print the PCR measurements of an enclave image, in the same JSON shape as +# measurements.txt (which run-enclave.sh writes from `nitro-cli +# describe-enclaves`). +# +# scripts/measure-enclave.sh [eif-output-path] +# +# The image must already be in the local Docker daemon (`make measurements` +# loads it). nitro-cli runs in a pinned Amazon Linux container against the +# host's Docker socket, so this works on any machine with Docker, Nitro +# hardware or not, including CI. +# +# Every nitro-cli release from 1.3.4 to 1.5.3 ships the same enclave kernel +# and init, so the version pinned here gives the same PCR0/PCR1 as the hosts' +# own nitro-cli; it is pinned so the measurement itself cannot drift. +# +# Extra `docker run` arguments (e.g. proxy settings) can be passed in +# MEASURE_DOCKER_ARGS. +set -euo pipefail + +image="${1:?usage: measure-enclave.sh [eif-output-path]}" +eif_out="${2:-}" + +NITRO_CLI_VERSION="1.5.3" +NITRO_CLI_IMAGE="amazonlinux:2023@sha256:edd3297df7d444ab9a7896b763d5305d4bab57d2fb3943e831c1713b97d32d9d" + +workdir="$(mktemp -d)" +trap 'rm -rf "$workdir"' EXIT + +# shellcheck disable=SC2086 # MEASURE_DOCKER_ARGS is a list of arguments +docker run --rm \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v "$workdir:/out" \ + -e NITRO_CLI_VERSION="$NITRO_CLI_VERSION" \ + -e IMAGE="$image" \ + ${MEASURE_DOCKER_ARGS:-} \ + "$NITRO_CLI_IMAGE" \ + bash -euo pipefail -c ' + if command -v update-ca-trust >/dev/null; then update-ca-trust; fi + dnf -q install -y \ + "aws-nitro-enclaves-cli-${NITRO_CLI_VERSION}" \ + "aws-nitro-enclaves-cli-devel-${NITRO_CLI_VERSION}" >/dev/null + mkdir -p /var/log/nitro_enclaves + nitro-cli build-enclave --docker-uri "$IMAGE" \ + --output-file /out/enclave.eif > /out/build.json + chmod a+r /out/enclave.eif /out/build.json + ' >&2 + +# build-enclave labels the algorithm "Sha512_48 { .. }", describe-enclaves +# "Sha384 { ... }"; both are SHA-384. Use the label measurements.txt has +# always carried so the file reads the same whichever command wrote it. +jq '{Measurements: (.Measurements | .HashAlgorithm = "Sha384 { ... }")}' \ + "$workdir/build.json" + +if [ -n "$eif_out" ]; then + cp "$workdir/enclave.eif" "$eif_out" +fi