Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ jobs:
- name: Install stable Rust
uses: dtolnay/rust-toolchain@stable

- name: Build the release binary
run: cargo build --release --locked --bin rust-panosmcp

- name: Stage the package
- name: Build and stage the package
run: |
set -euo pipefail
mkdir -p staging/bin
cp target/release/rust-panosmcp staging/bin/
cp -r packaging staging/packaging
PANOSMCP_OUTPUT_DIR="$RUNNER_TEMP/pkg" scripts/build-release.sh
mkdir staging
tar -xzf "$RUNNER_TEMP"/pkg/rust-panosmcp-v*.tar.gz -C staging --strip-components=1
cp packaging/conformance.toml staging/

- name: Skip-build provenance stays honest
run: packaging/tests/skip-build-provenance.sh

- name: Set up Buildx
uses: docker/setup-buildx-action@v4

Expand Down
22 changes: 22 additions & 0 deletions docs/OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ tar -xzf rust-panosmcp-v0.16.2-x86_64-unknown-linux-gnu.tar.gz
cat rust-panosmcp-v0.16.2/BUILD-INFO
```

`BUILD-INFO` is `key=value` lines: `version`, `git_commit` (the commit that
built the binary), `packaging_commit` (the checkout that produced the
archive; equal unless the binary was supplied prebuilt), `source_date_epoch`,
`target`, `binary_sha256` (of `bin/rust-panosmcp`) and a single `rustc=` line.

### Packaging a CI-built binary

A binary rebuilt on a workstation links a newer glibc than the LXCs have and
will not start there. Package the binary CI built instead: extract it from the
release image to `target/release/rust-panosmcp`, then

```bash
PANOSMCP_PACKAGE_SKIP_BUILD=1 \
PANOSMCP_BINARY_SOURCE_COMMIT=<40-hex commit that built the image> \
scripts/build-release.sh
```

The commit must exist in the clone (`git fetch origin <sha>`). `rustc=` is then
recorded as `unknown (... not compiled by this script)`, never a toolchain
version, and `target` defaults to `x86_64-unknown-linux-gnu`
(`PANOSMCP_TARGET_TRIPLE` overrides). Never hand-write `BUILD-INFO`.

The build uses `Cargo.lock`, a fixed Rust MSRV, path remapping, a fixed source
date, deterministic tar ordering/ownership/timestamps, and no incremental
compilation. `scripts/verify-reproducible-build.sh` compiles twice in isolated
Expand Down
10 changes: 6 additions & 4 deletions packaging/conformance.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ service = "rust-panosmcp"
config_dir = "/etc/rust-panosmcp"
tokens = "/var/lib/rust-panosmcp/tokens.json"

# No supported skip-build path yet, so no honest BUILD-INFO is possible.
# R3 warns until both change together. See mecmcp's spec, "Provenance ordering".
build_info = false
skip_build_env = false
# rust-panosmcp has a supported skip-build path (PANOSMCP_PACKAGE_SKIP_BUILD)
# and scripts/build-release.sh emits an honest BUILD-INFO either way -- a real
# rustc -vV when it compiled the binary, "unknown (...)" when it did not. R3 is
# therefore fatal rather than a warning. See mecmcp#355.
build_info = true
skip_build_env = "PANOSMCP_PACKAGE_SKIP_BUILD"

units = ["packaging/systemd/rust-panosmcp.service"]

Expand Down
69 changes: 69 additions & 0 deletions packaging/tests/skip-build-provenance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# Regression guard for mecmcp#355: BUILD-INFO from the skip-build path must not
# name a toolchain that never compiled the binary. Needs target/release/rust-panosmcp.
set -euo pipefail

repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"
[[ -x target/release/rust-panosmcp ]] || {
echo 'target/release/rust-panosmcp must already be built before this test runs' >&2
exit 1
}

expected_sha=$(sha256sum target/release/rust-panosmcp | cut -d' ' -f1)
commit=$(git rev-parse HEAD)
work=$(mktemp -d)
trap 'rm -rf -- "$work"' EXIT

# Negative: a missing or malformed source commit must be refused.
for bad in '' 'abc123' "${commit^^}"; do
if PANOSMCP_ALLOW_DIRTY=1 PANOSMCP_PACKAGE_SKIP_BUILD=1 \
PANOSMCP_BINARY_SOURCE_COMMIT="$bad" PANOSMCP_OUTPUT_DIR="$work/neg" \
scripts/build-release.sh >/dev/null 2>&1; then
echo "skip-build accepted a bad PANOSMCP_BINARY_SOURCE_COMMIT: '$bad'" >&2
exit 1
fi
done

# Negative: a binary whose --version disagrees with the source commit's Cargo.toml
# must be refused, or an old binary ships under a new version's name.
mkdir -p "$work/stub/release"
printf '#!/bin/sh\necho "rust-panosmcp 0.0.0-stub"\n' >"$work/stub/release/rust-panosmcp"
chmod +x "$work/stub/release/rust-panosmcp"
if PANOSMCP_ALLOW_DIRTY=1 PANOSMCP_PACKAGE_SKIP_BUILD=1 PANOSMCP_TARGET_DIR="$work/stub" \
PANOSMCP_BINARY_SOURCE_COMMIT="$commit" PANOSMCP_OUTPUT_DIR="$work/neg" \
scripts/build-release.sh >/dev/null 2>&1; then
echo 'skip-build accepted a binary whose --version disagrees with the source commit' >&2
exit 1
fi

PANOSMCP_ALLOW_DIRTY=1 PANOSMCP_PACKAGE_SKIP_BUILD=1 \
PANOSMCP_BINARY_SOURCE_COMMIT="$commit" PANOSMCP_OUTPUT_DIR="$work/out" \
scripts/build-release.sh >/dev/null

mapfile -t archives < <(find "$work/out" -maxdepth 1 -name 'rust-panosmcp-v*.tar.gz')
[[ ${#archives[@]} -eq 1 ]] || { echo "expected one archive, found ${#archives[@]}" >&2; exit 1; }
mkdir "$work/x"
tar -xzf "${archives[0]}" -C "$work/x"
build_info=$(find "$work/x" -maxdepth 2 -name BUILD-INFO)

recorded_rustc=$(sed -n 's/^rustc=//p' "$build_info")
case "$recorded_rustc" in
unknown\ \(*) ;;
*) echo "skip-build rustc= must start with 'unknown ('; got: $recorded_rustc" >&2; exit 1 ;;
esac
if grep -Eq 'rustc [0-9]+\.[0-9]+\.[0-9]+' <<<"$recorded_rustc"; then
echo "skip-build BUILD-INFO names a compiler version: $recorded_rustc" >&2
exit 1
fi
recorded_sha=$(sed -n 's/^binary_sha256=//p' "$build_info")
[[ "$recorded_sha" == "$expected_sha" ]] || {
echo "binary_sha256 ($recorded_sha) != packaged binary ($expected_sha)" >&2
exit 1
}
recorded_commit=$(sed -n 's/^git_commit=//p' "$build_info")
[[ "$recorded_commit" == "$commit" ]] || {
echo "git_commit ($recorded_commit) != supplied commit ($commit)" >&2
exit 1
}
echo 'skip-build provenance is honest'
59 changes: 54 additions & 5 deletions scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,56 @@ if [[ -z "$VERSION" ]]; then
echo "workspace version not found" >&2
exit 1
fi
COMMIT="$(git rev-parse --verify HEAD)"
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(git show -s --format=%ct HEAD)}"
TARGET_TRIPLE="${PANOSMCP_TARGET_TRIPLE:-$(rustc -vV | sed -n 's/^host: //p')}"
PACKAGING_COMMIT="$(git rev-parse --verify HEAD)"
SKIP_BUILD="${PANOSMCP_PACKAGE_SKIP_BUILD:-0}"
TARGET_DIR="${PANOSMCP_TARGET_DIR:-$ROOT/target}"
OUTPUT_DIR="${PANOSMCP_OUTPUT_DIR:-$ROOT/dist}"
if [[ "$SKIP_BUILD" == "1" ]]; then
# Packaging a CI-built binary: the binary is not ours to compile, so the
# local rustc host says nothing about it, and the release image is x86_64.
TARGET_TRIPLE="${PANOSMCP_TARGET_TRIPLE:-x86_64-unknown-linux-gnu}"
# Workstation glibc is newer than the LXCs', so a rebuilt binary will not
# start there; package the release image's binary instead.
if [[ ! -x "$TARGET_DIR/release/rust-panosmcp" ]]; then
echo "PANOSMCP_PACKAGE_SKIP_BUILD=1 but $TARGET_DIR/release/rust-panosmcp is missing or not executable." >&2
echo "Extract it from the release image, e.g.:" >&2
echo " docker create --name pm ghcr.io/mechubsec/rust-panosmcp:<version>" >&2
echo " docker cp pm:/usr/local/bin/rust-panosmcp $TARGET_DIR/release/rust-panosmcp; docker rm pm" >&2
exit 1
fi
if [[ ! "${PANOSMCP_BINARY_SOURCE_COMMIT:-}" =~ ^[0-9a-f]{40}$ ]]; then
echo "PANOSMCP_PACKAGE_SKIP_BUILD=1 requires PANOSMCP_BINARY_SOURCE_COMMIT: the full lowercase 40-hex commit that built the binary" >&2
exit 1
fi
if ! COMMIT="$(git rev-parse "${PANOSMCP_BINARY_SOURCE_COMMIT}^{commit}" 2>/dev/null)"; then
echo "commit $PANOSMCP_BINARY_SOURCE_COMMIT is not in this clone; run: git fetch origin $PANOSMCP_BINARY_SOURCE_COMMIT" >&2
exit 1
fi
# Version and toolchain pin describe the binary, so read them at its source
# commit, not from this checkout, and refuse a binary that disagrees.
VERSION="$(git show "$COMMIT:Cargo.toml" | sed -n 's/^version = "\([^"]*\)"/\1/p' | head -n 1)"
# shortcut: executes the binary, so packaging must run on a host that can run it
# (the fleet and packaging hosts are x86_64 Linux); fails closed otherwise.
BINARY_VERSION="$("$TARGET_DIR/release/rust-panosmcp" --version | awk '{print $2}')" || {
echo "cannot run $TARGET_DIR/release/rust-panosmcp to verify its version; package on a host that can execute it" >&2
exit 1
}
if [[ -z "$VERSION" || "$BINARY_VERSION" != "$VERSION" ]]; then
echo "binary reports version '$BINARY_VERSION' but ${COMMIT:0:12} declares '$VERSION'; wrong binary or wrong PANOSMCP_BINARY_SOURCE_COMMIT" >&2
exit 1
fi
TOOLCHAIN_CHANNEL="$(git show "$COMMIT:rust-toolchain.toml" 2>/dev/null | sed -n 's/^channel *= *"\([^"]*\)".*/\1/p' | head -n 1)"
if [[ -z "$TOOLCHAIN_CHANNEL" ]]; then
echo "no toolchain channel in rust-toolchain.toml at ${COMMIT:0:12}" >&2
exit 1
fi
RUSTC_LINE="unknown (binary supplied prebuilt via PANOSMCP_PACKAGE_SKIP_BUILD; pinned toolchain $TOOLCHAIN_CHANNEL at ${COMMIT:0:12}; not compiled by this script)"
else
COMMIT="$PACKAGING_COMMIT"
TARGET_TRIPLE="${PANOSMCP_TARGET_TRIPLE:-$(rustc -vV | sed -n 's/^host: //p')}"
RUSTC_LINE="$(rustc -vV | tr '\n' ' ' | sed 's/ *$//')"
fi
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(git show -s --format=%ct "$COMMIT")}"
ARCHIVE="rust-panosmcp-v${VERSION}-${TARGET_TRIPLE}.tar.gz"
BUILD_ARGS=(--release --locked --bin rust-panosmcp)
if [[ "${PANOSMCP_OFFLINE:-0}" == "1" ]]; then
Expand All @@ -29,7 +74,9 @@ fi

export SOURCE_DATE_EPOCH CARGO_INCREMENTAL=0 CARGO_TARGET_DIR="$TARGET_DIR"
export RUSTFLAGS="${RUSTFLAGS:+$RUSTFLAGS }--remap-path-prefix=$ROOT=/usr/src/rust-panosmcp"
cargo build "${BUILD_ARGS[@]}"
if [[ "$SKIP_BUILD" != "1" ]]; then
cargo build "${BUILD_ARGS[@]}"
fi

STAGING="$(mktemp -d)"
trap 'rm -rf "$STAGING"' EXIT
Expand All @@ -50,9 +97,11 @@ install -m 0644 config/devices.example.json \
{
printf 'version=%s\n' "$VERSION"
printf 'git_commit=%s\n' "$COMMIT"
printf 'packaging_commit=%s\n' "$PACKAGING_COMMIT"
printf 'source_date_epoch=%s\n' "$SOURCE_DATE_EPOCH"
printf 'target=%s\n' "$TARGET_TRIPLE"
rustc -vV
printf 'binary_sha256=%s\n' "$(sha256sum "$PKG/bin/rust-panosmcp" | cut -d' ' -f1)"
printf 'rustc=%s\n' "$RUSTC_LINE"
} >"$PKG/BUILD-INFO"

mkdir -p "$OUTPUT_DIR"
Expand Down
Loading