diff --git a/.github/workflows/release-trust.yml b/.github/workflows/release-trust.yml new file mode 100644 index 000000000..3f16bd019 --- /dev/null +++ b/.github/workflows/release-trust.yml @@ -0,0 +1,193 @@ +name: Release trust + +# Installed copies of LPM check every update with code they can never +# change, against keys and records that Apple and Sigstore keep changing. +# These checks watch the published releases from the outside every day: +# +# - Apple still vouches for the current stable and nightly macOS apps: the +# notarization ticket stands and the Developer ID certificate isn't +# revoked. Clients check the stapled ticket offline and can't see either. +# - Sigstore still serves the trust root built into LPM, so releases ship +# Sigstore's current keys. +# - The latest stable updates itself to the newest nightly, so a signing or +# packaging change on main that installed updaters would refuse fails +# here before a stable release carries it. Until the first nightly after a +# stable release, the stable before it updates to it instead. +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + paths: + - 'crates/lpm-cli/assets/sigstore_*' + - 'crates/lpm-cli/src/sigstore_verify/**' + - 'scripts/ci/previous-stable-update-gate.sh' + - 'scripts/ci/semver-newer.py' + - 'scripts/ci/with-apple-service-retry.sh' + - '.github/workflows/release-trust.yml' + schedule: + - cron: '17 6 * * *' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: release-trust-${{ github.ref }} + cancel-in-progress: true + +env: + CARGO_INCREMENTAL: '0' + CARGO_PROFILE_DEV_DEBUG: '0' + CARGO_PROFILE_TEST_DEBUG: '0' + +jobs: + apple: + name: Apple vouches for the current macOS apps + if: ${{ !github.event.pull_request.draft }} + runs-on: macos-15 + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Check each current release with Apple + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + tags=$(curl -fsSL --retry 3 https://registry.npmjs.org/-/package/@lpm-registry/cli/dist-tags) + failed=0 + for channel in latest nightly; do + version=$(jq -er --arg channel "$channel" '.[$channel]' <<<"$tags") + release="$RUNNER_TEMP/v$version" + mkdir -p "$release" + gh release download "v$version" --repo lpm-dev/rust-client --dir "$release" \ + --pattern SHA256SUMS.txt --pattern 'lpm-darwin-*.zip' + for asset in lpm-darwin-arm64.zip lpm-darwin-x64.zip; do + archive="$release/$asset" + if [ ! -f "$archive" ]; then + echo "::error::v$version has no $asset" + failed=1 + continue + fi + (cd "$release" && awk -v asset="$asset" '$2 == asset' SHA256SUMS.txt | shasum -a 256 --check --strict) + unpacked="$RUNNER_TEMP/unpacked/v$version/${asset%.zip}" + mkdir -p "$unpacked" + ditto -x -k "$archive" "$unpacked" + app="$unpacked/LPM CLI.app" + echo "::group::v$version $asset" + if /usr/bin/codesign --verify --strict --verbose=2 "$app" && + scripts/ci/with-apple-service-retry.sh stapler validate "$app" && + /usr/sbin/spctl --assess --type execute --verbose=4 "$app"; then + echo "Apple vouches for v$version $asset" + else + echo "::error::Apple no longer vouches for v$version $asset. Installed copies that update to it, and the installer, will refuse it." + failed=1 + fi + echo "::endgroup::" + done + done + exit "$failed" + + sigstore: + name: Sigstore serves the trust root built into LPM + if: ${{ !github.event.pull_request.draft }} + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master + with: + toolchain: '1.99.1' + - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: release-trust-sigstore-1.99.1-${{ hashFiles('Cargo.lock') }} + restore-keys: release-trust-sigstore-1.99.1- + + - name: Install Linux build dependencies + run: bash scripts/ci/install-apt-packages.sh pkg-config libdbus-1-dev + + - name: Refresh from Sigstore's live repository + run: | + set -euo pipefail + cargo test --locked -p lpm-cli --bin lpm-rs -- --ignored --exact \ + sigstore_verify::trust_store::tests::sigstores_live_repository_serves_the_built_in_trust_root \ + | tee "$RUNNER_TEMP/sigstore-live.log" + # A renamed test would match nothing, and cargo passes an empty run. + grep -q '^test result: ok. 1 passed' "$RUNNER_TEMP/sigstore-live.log" + + previous-stable-updates-to-newest: + name: Latest stable updates itself to the newest release (${{ matrix.runner }}) + if: ${{ !github.event.pull_request.draft }} + strategy: + fail-fast: false + matrix: + include: + - runner: ubuntu-latest + asset: lpm-linux-x64 + - runner: ubuntu-24.04-arm + asset: lpm-linux-arm64 + - runner: macos-15 + asset: lpm-darwin-arm64.zip + - runner: macos-15-intel + asset: lpm-darwin-x64.zip + runs-on: ${{ matrix.runner }} + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Download the release to update to + env: + ASSET: ${{ matrix.asset }} + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + tags=$(curl -fsSL --retry 3 https://registry.npmjs.org/-/package/@lpm-registry/cli/dist-tags) + stable=$(jq -er .latest <<<"$tags") + nightly=$(jq -er .nightly <<<"$tags") + nightly_is_newer=$(python3 -I scripts/ci/semver-newer.py "$nightly" "$stable") + if [ "$nightly_is_newer" = yes ]; then + target=$nightly + channel=nightly + previous=$stable + else + # Until the next nightly, the newest one predates the latest + # stable, so check the update to that stable from the one before. + target=$stable + channel=stable + previous=$(gh release list --repo lpm-dev/rust-client --exclude-drafts --exclude-pre-releases \ + --limit 2 --json tagName --jq '.[1].tagName') + previous=${previous#v} + fi + release="$RUNNER_TEMP/release" + gh release download "v$target" --repo lpm-dev/rust-client --dir "$release" \ + --pattern SHA256SUMS.txt --pattern SHA256SUMS.txt.sigstore --pattern "$ASSET" + gh release view "v$target" --repo lpm-dev/rust-client --json targetCommitish,publishedAt \ + > "$RUNNER_TEMP/release.json" + { + echo "TARGET_VERSION=$target" + echo "TARGET_CHANNEL=$channel" + echo "TARGET_COMMIT=$(jq -er .targetCommitish "$RUNNER_TEMP/release.json")" + echo "TARGET_PUBLISHED_AT=$(jq -er .publishedAt "$RUNNER_TEMP/release.json")" + echo "TARGET_RELEASE=$release" + echo "PREVIOUS_VERSION=$previous" + } >> "$GITHUB_ENV" + + - name: Update the previous stable to it through its own updater + run: >- + scripts/ci/previous-stable-update-gate.sh + --previous "$PREVIOUS_VERSION" + --published-at "$TARGET_PUBLISHED_AT" + "$TARGET_RELEASE" "$TARGET_VERSION" "$TARGET_COMMIT" "$TARGET_CHANNEL" + + - name: Restore developer tools after the update gate + if: always() && runner.os == 'macOS' + run: bash scripts/ci/macos-developer-tools.sh restore "$RUNNER_TEMP/previous-stable-gate-developer-tools" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 198a541bc..aa5ca7c97 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1791,7 +1791,7 @@ jobs: needs.release-metadata.outputs.should_release == 'true' && (needs.release-metadata.outputs.mode == 'candidate' || needs.release-metadata.outputs.mode == 'full') runs-on: ${{ matrix.os }} - timeout-minutes: 15 + timeout-minutes: 30 permissions: actions: read contents: read @@ -1967,6 +1967,21 @@ jobs: if: always() && matrix.platform == 'macos' run: bash scripts/ci/macos-developer-tools.sh restore "$RUNNER_TEMP/developer-tools" + # Installed copies update with the updater they shipped. If the latest + # stable's updater refuses this release, users on it are stranded, so + # the release must not be published. + - name: Update the latest stable to this release through its own updater + env: + RELEASE_CHANNEL: ${{ needs.release-metadata.outputs.channel }} + RELEASE_VERSION: ${{ needs.release-metadata.outputs.version }} + run: >- + scripts/ci/previous-stable-update-gate.sh + staged-release "$RELEASE_VERSION" "$GITHUB_SHA" "$RELEASE_CHANNEL" + + - name: Restore developer tools after the update gate + if: always() && matrix.platform == 'macos' + run: bash scripts/ci/macos-developer-tools.sh restore "$RUNNER_TEMP/previous-stable-gate-developer-tools" + seal-candidate: name: Seal stable release candidate needs: @@ -2166,7 +2181,7 @@ jobs: if: needs.prepare-promotion.result == 'success' && needs.release-metadata.outputs.mode == 'promote' runs-on: ubuntu-latest environment: release - timeout-minutes: 10 + timeout-minutes: 20 permissions: actions: read attestations: write @@ -2224,6 +2239,16 @@ jobs: release-candidate/github-release/SHA256SUMS.txt.sigstore "$SOURCE_SHA" + # Promotion signs the release again, and users get this signature, not + # the candidate's. The candidate's macOS bundles already passed the + # gate and are unchanged. + - name: Update the latest stable to the promoted release through its own updater + env: + RELEASE_VERSION: ${{ needs.release-metadata.outputs.version }} + run: >- + scripts/ci/previous-stable-update-gate.sh + release-candidate/github-release "$RELEASE_VERSION" "$SOURCE_SHA" stable + - name: Upload promotion-time GitHub assets id: github-assets uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 diff --git a/scripts/ci/previous-stable-update-gate.sh b/scripts/ci/previous-stable-update-gate.sh new file mode 100755 index 000000000..59b6a6486 --- /dev/null +++ b/scripts/ci/previous-stable-update-gate.sh @@ -0,0 +1,268 @@ +#!/bin/bash +# Updates a published stable LPM to a release served from this machine, +# through that stable's own `lpm self-update`. +# +# Every installed copy updates with the code it shipped, and that code can't +# change afterwards. A release whose signature, manifest or macOS bundle an +# installed updater would refuse strands those users, so the release workflow +# runs this on each candidate before promoting it, and the release trust +# monitor runs it daily on the newest release. +# +# The release directory holds the release's GitHub assets: SHA256SUMS.txt, +# SHA256SUMS.txt.sigstore and the platform assets. +# +# --previous names the stable to update from; the default is npm's latest. +# --published-at gives a published release's real publication time; an +# unpublished candidate is served as published when Rekor recorded its +# signature. +# +# The updater finds the account's home directory from the system, not from +# $HOME, and on macOS updates only an app in that home. So this runs only on +# a disposable CI runner, where it sets the account's ~/.lpm aside and puts +# it back afterwards. Sigstore's trust root is fetched as installed copies +# fetch it. + +set -Eeuo pipefail +trap 'echo "::error::$(basename "$0") failed at line $LINENO: $BASH_COMMAND" >&2' ERR + +usage() { + echo "usage: $0 [--previous VERSION] [--published-at RFC3339] " >&2 + exit 2 +} + +previous= +published_at= +while [ "$#" -gt 0 ]; do + case "$1" in + --previous) + [ "$#" -ge 2 ] || usage + previous=$2 + shift 2 + ;; + --published-at) + [ "$#" -ge 2 ] || usage + published_at=$2 + shift 2 + ;; + --*) usage ;; + *) break ;; + esac +done +[ "$#" -eq 4 ] || usage + +if [ "${GITHUB_ACTIONS:-}" != true ]; then + echo "$0 replaces this account's ~/.lpm while it runs, so it runs only in GitHub Actions" >&2 + exit 2 +fi + +release_dir=$(cd "$1" && pwd) +version=$2 +source_commit=$3 +channel=$4 +repo_root=$(cd "$(dirname "$0")/../.." && pwd) + +# The first stable whose updater needs neither Xcode nor the Command Line +# Tools on macOS. Earlier ones run /usr/bin/stapler, which only Xcode +# provides. +first_version_without_developer_tools=0.89.0 + +case "$channel" in + stable|nightly) ;; + *) usage ;; +esac +if [[ ! "$source_commit" =~ ^[0-9a-f]{40}$ ]]; then + echo "source commit must be a 40-character lowercase Git object ID: $source_commit" >&2 + exit 2 +fi +for asset in SHA256SUMS.txt SHA256SUMS.txt.sigstore; do + if [ ! -f "$release_dir/$asset" ]; then + echo "$release_dir has no $asset" >&2 + exit 2 + fi +done + +newer_than() { + python3 -I "$repo_root/scripts/ci/semver-newer.py" "$1" "$2" +} + +if [ -z "$previous" ]; then + previous=$(curl -fsSL --retry 3 https://registry.npmjs.org/@lpm-registry/cli/latest | + python3 -I -c 'import json, sys; print(json.load(sys.stdin)["version"])') +fi +if [ "$(newer_than "$version" "$previous")" != yes ]; then + echo "::error::$version is not newer than $previous, so LPM $previous has nothing to update to" >&2 + exit 1 +fi + +account_home=$(python3 -I -c 'import os, pwd; print(pwd.getpwuid(os.geteuid()).pw_dir)') +account_lpm="$account_home/.lpm" +work=$(mktemp -d) +# Beside ~/.lpm, so setting it aside is a rename on the same volume. +set_aside="$account_home/.lpm-set-aside-by-update-gate" +# Under RUNNER_TEMP, so a workflow step can restore the developer tools even +# if this script is killed. +developer_tools="${RUNNER_TEMP:-$work}/previous-stable-gate-developer-tools" +server_pid= +account_lpm_claimed= +developer_tools_hidden= +cleanup() { + local status=$? + if [ -n "$server_pid" ]; then + kill "$server_pid" 2>/dev/null || true + fi + if [ -n "$developer_tools_hidden" ] && + ! bash "$repo_root/scripts/ci/macos-developer-tools.sh" restore "$developer_tools"; then + echo "::error::developer tools could not be restored; they remain under $developer_tools" >&2 + status=1 + fi + if [ -n "$account_lpm_claimed" ]; then + rm -rf "$account_lpm" + if [ -e "$set_aside" ] && ! mv "$set_aside" "$account_lpm"; then + echo "::error::could not put the account's ~/.lpm back; it is at $set_aside" >&2 + status=1 + fi + fi + rm -rf "$work" + exit "$status" +} +trap cleanup EXIT +trap 'exit 130' INT TERM + +if [ -e "$set_aside" ]; then + echo "$set_aside already exists: an earlier run didn't put the account's ~/.lpm back" >&2 + exit 1 +fi +if [ -e "$account_lpm" ]; then + mv "$account_lpm" "$set_aside" +fi +account_lpm_claimed=1 + +serve="$work/serve" +mkdir -p "$serve/download/$version" "$serve/release" +cp -p "$release_dir"/* "$serve/download/$version/" + +# The updater checks that the release was published close to when Rekor +# recorded its signature, and from the commit the signature names. +if [ -z "$published_at" ]; then + published_at=$(python3 -I - "$release_dir/SHA256SUMS.txt.sigstore" <<'PY' +import datetime, json, sys + +def find(value): + if isinstance(value, dict): + if "integratedTime" in value: + return value["integratedTime"] + values = value.values() + elif isinstance(value, list): + values = value + else: + return None + for item in values: + found = find(item) + if found is not None: + return found + return None + +integrated = int(find(json.load(open(sys.argv[1])))) +print(datetime.datetime.fromtimestamp(integrated, datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")) +PY +) +fi +printf '{"name":"@lpm-registry/cli","version":"%s"}\n' "$version" > "$serve/npm.json" +printf '{"tag_name":"v%s","published_at":"%s","target_commitish":"%s"}\n' \ + "$version" "$published_at" "$source_commit" > "$serve/release/$version.json" + +if [ "$(uname -s)" = Darwin ]; then + # The server keeps running while the developer tools are hidden. + python_prefix=$(python3 -I -c 'import sys; print(sys.base_prefix)') + case "$python_prefix" in + /Library/Developer/*|/Applications/Xcode*) + echo "::error::python3 comes from developer tools ($python_prefix), which this gate hides" >&2 + exit 1 + ;; + esac +fi +# The server binds its own port and writes it to a file, so nothing else can +# take the port between choosing and binding it. +cat >"$work/serve.py" <<'PY' +import functools, http.server, os, sys + +handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=sys.argv[1]) +server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler) +with open(sys.argv[2] + ".partial", "w") as port_file: + port_file.write(str(server.server_address[1])) +os.replace(sys.argv[2] + ".partial", sys.argv[2]) +server.serve_forever() +PY +python3 -I "$work/serve.py" "$serve" "$work/port" "$work/server.log" 2>&1 & +server_pid=$! +port= +for attempt in $(seq 1 300); do + if [ -s "$work/port" ]; then + port=$(cat "$work/port") + if curl -fsS -o /dev/null "http://127.0.0.1:$port/npm.json" 2>/dev/null; then + break + fi + fi + if [ "$attempt" -eq 300 ] || ! kill -0 "$server_pid" 2>/dev/null; then + echo "::error::the release server didn't start (port: ${port:-none})" >&2 + { + echo "python3: $(command -v python3) ($(python3 --version 2>&1))" + ps -o pid,stat,etime,command -p "$server_pid" || echo "the server process has exited" + echo "server log:" + cat "$work/server.log" + } >&2 + exit 1 + fi + sleep 0.1 +done +echo "Serving v$version on 127.0.0.1:$port" +base="http://127.0.0.1:$port" + +run_isolated() { + env -u LPM_HOME -u LPM_INSTALL_DOWNLOAD_BASE -u LPM_INSTALL_TEST_DOWNLOAD_BASE -u DEVELOPER_DIR \ + HOME="$account_home" LPM_NO_UPDATE_CHECK=1 "$@" +} + +echo "Installing LPM $previous through the official installer" +run_isolated LPM_INSTALL_VERSION="v$previous" LPM_INSTALL_CHANNEL=stable sh "$repo_root/install.sh" +lpm="$account_lpm/bin/lpm" +actual=$(run_isolated "$lpm" --version) +if [ "$actual" != "lpm $previous" ]; then + echo "the installer installed '$actual', not lpm $previous" >&2 + exit 1 +fi + +if [ "$(uname -s)" = Darwin ] && + [ "$(newer_than "$first_version_without_developer_tools" "$previous")" = no ]; then + # Most users' Macs have neither Xcode nor the Command Line Tools. Set + # before hiding, so a hide that fails partway is still undone. + developer_tools_hidden=1 + bash "$repo_root/scripts/ci/macos-developer-tools.sh" hide none "$developer_tools" +fi + +echo "Updating LPM $previous to $version through its own updater" +update_status=0 +run_isolated \ + LPM_NPM_REGISTRY_URL_OVERRIDE="$base/npm.json" \ + LPM_NPM_NIGHTLY_REGISTRY_URL_OVERRIDE="$base/npm.json" \ + LPM_GITHUB_RELEASES_URL_OVERRIDE="$base/no-release-list" \ + LPM_GITHUB_NIGHTLY_RELEASES_URL_OVERRIDE="$base/no-release-list" \ + LPM_GITHUB_RELEASE_BY_TAG_URL_OVERRIDE="$base/release/{tag}.json" \ + LPM_GITHUB_RELEASE_DOWNLOAD_URL_OVERRIDE="$base/download/{tag}/{file}" \ + "$lpm" self-update --channel "$channel" --refresh || update_status=$? + +if [ -n "$developer_tools_hidden" ]; then + bash "$repo_root/scripts/ci/macos-developer-tools.sh" restore "$developer_tools" + developer_tools_hidden= +fi +if [ "$update_status" -ne 0 ]; then + echo "::error::LPM $previous refused to update to $version (exit $update_status). Installed copies would be stranded on $previous." >&2 + exit 1 +fi + +actual=$(run_isolated "$lpm" --version) +if [ "$actual" != "lpm $version" ]; then + echo "::error::LPM $previous reported success, but lpm --version says '$actual', not lpm $version" >&2 + exit 1 +fi +echo "LPM $previous updated itself to $version" diff --git a/scripts/ci/semver-newer.py b/scripts/ci/semver-newer.py new file mode 100644 index 000000000..172ce0fd5 --- /dev/null +++ b/scripts/ci/semver-newer.py @@ -0,0 +1,25 @@ +"""Print yes when the first semantic version is newer than the second, by +SemVer 2.0.0 precedence, else no.""" + +import re +import sys + + +def precedence(version): + match = re.fullmatch(r"(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?", version) + if not match: + sys.exit(f"not a semantic version: {version}") + major, minor, patch, prerelease = match.groups() + if prerelease is None: + return (int(major), int(minor), int(patch), 1, ()) + # Numeric identifiers compare as numbers and rank below alphanumeric + # ones; a longer list of equal identifiers ranks higher. + identifiers = tuple( + (0, int(part), "") if part.isdigit() else (1, 0, part) for part in prerelease.split(".") + ) + return (int(major), int(minor), int(patch), 0, identifiers) + + +if len(sys.argv) != 3: + sys.exit("usage: semver-newer.py ") +print("yes" if precedence(sys.argv[1]) > precedence(sys.argv[2]) else "no")