🏡 index : quine-global/quorum.git

#!/usr/bin/env bash
# A rebuilder's whole job, in one command (charter M5): build a release from
# source with Nix, check the build is your own, and publish a signed
# attestation of the resulting files' hashes to a feed.
#
#   rebuild --project hyper --release 4.0.1 \
#       --flake github:quine-global/hyper/v4.0.1#windows \
#       --name alice --key alice.key --feed ~/attestations
#
# writes <feed>/<project>/<release>/<name>/attestation.sigstore.json. Publish
# it (a pull request to the feed's repository), with your public key as
# <feed>/<project>/builder-keys/<name>.pem, once. docs/REBUILDER.md.
#
# With --log-key (the log operator's): also logs it in <feed>/<project>/log,
# with --witness-policy, cosigned by the witnesses first.
set -euo pipefail

die() { echo "rebuild: $*" >&2; exit 2; }
project='' release='' flake='' name='' key='' feed='' logkey='' policy='' files=()
while [ $# -gt 0 ]; do
  case $1 in
    --project) project=$2; shift 2 ;;
    --release) release=$2; shift 2 ;;
    --flake) flake=$2; shift 2 ;;
    --name) name=$2; shift 2 ;;
    --key) key=$2; shift 2 ;;
    --feed) feed=$2; shift 2 ;;
    --log-key) logkey=$2; shift 2 ;;
    --witness-policy) policy=$2; shift 2 ;;
    --file) files+=("$2"); shift 2 ;;
    *) die "unknown argument $1 (see the top of $0)" ;;
  esac
done
for v in project release flake name key feed; do
  [ -n "${!v}" ] || die "--$v is required"
done
[ -f "$key" ] || die "no key at $key (cosign generate-key-pair)"
case $flake in *#*) ;; *) die "--flake needs an attribute: <ref>#<package>" ;; esac
case $flake in
  path:*|.*|/*) echo "rebuild: warning: $flake is a local path; rebuild a commit or tag of the published repository, which others can rebuild too" >&2 ;;
esac

work=$(mktemp -d); trap 'rm -rf "$work"' EXIT
nixf=(--extra-experimental-features 'nix-command flakes')

echo "== building $flake"
out=$(nix "${nixf[@]}" build "$flake" --no-link --print-out-paths -L)
# Built here, not downloaded: --rebuild builds it again on this machine and
# fails unless the result is bit for bit the same (Nix would otherwise take
# it from a binary cache if one had it).
echo "== rebuilding it, to make sure the result is this machine's own"
nix "${nixf[@]}" build "$flake" --no-link --rebuild

# The files to vouch for: --file, else the output's files at its top and in
# bin/, without hash lists.
if [ ${#files[@]} -eq 0 ]; then
  while IFS= read -r f; do files+=("$f"); done < <(
    find "$out" "$out/bin" -maxdepth 1 -type f ! -name '*.sha256' ! -name SHA256SUMS 2>/dev/null | LC_ALL=C sort)
else
  files=("${files[@]/#/$out/}")
fi
[ ${#files[@]} -gt 0 ] || die "nothing to attest in $out"
echo "== attesting"
(cd "$(dirname "${files[0]}")" && sha256sum "${files[@]}")

dest=$feed/$project/$release/$name
mkdir -p "$dest"
quorum statement --release "$release" --out "$work/statement.json" "${files[@]}"
# No public log, no timestamp server: without this cosign would also publish
# the attestation to Sigstore's Rekor. Our own log is below.
cosign signing-config create --out "$work/offline.json" >/dev/null
cosign attest-blob --statement "$work/statement.json" --key "$key" \
  --signing-config "$work/offline.json" --bundle "$dest/attestation.sigstore.json" -y
echo "== wrote $dest/attestation.sigstore.json"

if [ -n "$logkey" ]; then
  mkdir -p "$work/entries"
  quorum log entry --rebuilder "$name" --release "$release" \
    "$dest/attestation.sigstore.json" --out "$work/entries/0"
  args=(--storage_dir="$feed/$project/log" --private_key="$logkey" --entries="$work/entries/*")
  [ -z "$policy" ] || args+=(--witness_policy_file="$policy")
  posix-oneshot "${args[@]}"
  echo "== logged in $feed/$project/log"
fi