From 5c1560ab9634611fb9063600281db6336f92e9ab Mon Sep 17 00:00:00 2001 From: Geno Roupsky <185580+groupsky@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:47:56 +0200 Subject: [PATCH] ci: verify the draft release's files before publishing release.yml now uploads to the draft, then a read-only verify job checks the draft's files (names, SHA256SUMS, attestation, version, install.sh in a clean container), and publish runs only when verify passed and the draft still has the verified files. verify-release.yml has a dry run that can break a file on purpose. Fixes #200 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 68 +++++++++- .github/workflows/verify-release.yml | 167 ++++++++++++++++++++++++ AGENTS.md | 3 + THREAT-MODEL.md | 6 + docs/releasing.md | 67 ++++++++-- scripts/verify-release.sh | 186 +++++++++++++++++++++++++++ scripts_linux_test.go | 176 +++++++++++++++++++++++++ 8 files changed, 660 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/verify-release.yml create mode 100644 scripts/verify-release.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fcbfcb7..3ae23a1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,7 +86,7 @@ jobs: (cd contrib/pullgate && "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh -x -- *.sh) "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh contrib/ansible/build.sh contrib/ansible/test/ci.sh # The release build and the checks of the Release check workflow. - "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh scripts/build-release.sh scripts/release-check/*.sh + "$dir/shellcheck-${SHELLCHECK_VERSION}/shellcheck" --shell=sh scripts/build-release.sh scripts/verify-release.sh scripts/release-check/*.sh - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: go.mod diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5f312e2..60c3953 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -87,14 +87,18 @@ jobs: path: dist/ if-no-files-found: error - # Signs and publishes the files from the build job. It runs no code from - # the repository. - publish: + # Signs the files from the build job and adds them to the draft release. + # It runs no code from the repository. It does not publish: verify checks + # the draft's files first. + upload: needs: [release-please, build] if: needs.release-please.outputs.release_created == 'true' runs-on: ubuntu-latest + outputs: + # The SHA-256 and the name of each file of the draft that verify gets. + files: ${{ steps.draft.outputs.files }} permissions: - contents: write # upload the files and publish the draft release + contents: write # upload the files to the draft release id-token: write # sign the attestation (Sigstore) attestations: write # store the attestation env: @@ -154,6 +158,62 @@ jobs: env: GH_TOKEN: ${{ github.token }} run: gh release upload "$TAG" dist/*.tar.gz dist/SHA256SUMS "dist/nodkey_${VERSION}.sigstore.json" --clobber --repo "$GITHUB_REPOSITORY" + # The files of a draft can only be read with write access, so this + # job gets them back, and verify, which can only read, checks them. + - name: Download the files of the draft release + id: draft + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + gh release download "$TAG" --dir draft --repo "$GITHUB_REPOSITORY" + # publish compares this list with the assets of the draft, so it + # publishes only the files that verify checked. + echo "files=$(cd draft && sha256sum -- * | LC_ALL=C sort -k2 | sha256sum | cut -d' ' -f1)" >>"$GITHUB_OUTPUT" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: draft-files + path: draft/ + if-no-files-found: error + + # The verify gate: checks the files of the draft as a user would + # (verify-release.yml, docs/releasing.md "The verify gate"). It can only + # read. When it fails, the release stays a draft. + verify: + needs: [release-please, upload] + if: needs.release-please.outputs.release_created == 'true' + permissions: + contents: read + uses: ./.github/workflows/verify-release.yml + with: + version: ${{ needs.release-please.outputs.version }} + artifact: draft-files + + # Publishes the draft release, only after verify passed. It runs no code + # from the repository. + publish: + needs: [release-please, upload, verify] + if: needs.release-please.outputs.release_created == 'true' && needs.verify.outputs.verified == 'true' + runs-on: ubuntu-latest + permissions: + contents: write # publish the draft release + env: + TAG: ${{ needs.release-please.outputs.tag_name }} + steps: + - name: Check that the draft still has the verified files + env: + GH_TOKEN: ${{ github.token }} + FILES: ${{ needs.upload.outputs.files }} + run: | + set -euo pipefail + # The digest of each asset, in the form of sha256sum. An asset + # without a digest gives an empty line, and the check fails. + now="$(gh release view "$TAG" --repo "$GITHUB_REPOSITORY" --json assets \ + --jq '.assets[] | "\((.digest // "") | ltrimstr("sha256:")) \(.name)"' | LC_ALL=C sort -k2 | sha256sum | cut -d' ' -f1)" + if [ -z "$FILES" ] || [ "$now" != "$FILES" ]; then + echo "::error::The files of the draft changed after verify checked them. Nothing is published. See docs/releasing.md." + exit 1 + fi - name: Publish the release env: GH_TOKEN: ${{ github.token }} diff --git a/.github/workflows/verify-release.yml b/.github/workflows/verify-release.yml new file mode 100644 index 0000000..86fdfde --- /dev/null +++ b/.github/workflows/verify-release.yml @@ -0,0 +1,167 @@ +name: Verify release + +# The verify gate of a release (docs/releasing.md, "The verify gate"). +# release.yml calls it with the files of the draft release, between the +# upload of the files and the publish. It can only read. +# +# Run by hand, it is a dry run: it checks the files of a published release, +# with one file broken on purpose if you ask, and then shows whether the +# publish would run. It publishes nothing. + +on: + workflow_call: + inputs: + version: + description: The version X.Y.Z of the draft release + required: true + type: string + artifact: + description: The artifact with the files of the draft release + required: true + type: string + outputs: + verified: + description: true only when every check passed + value: ${{ jobs.verify.outputs.verified }} + workflow_dispatch: + inputs: + version: + description: A published release X.Y.Z to check (dry run) + required: true + type: string + break: + description: Break a file on purpose; the verify must then fail + required: true + default: none + type: choice + options: + - none + - tarball # one byte more in the pullgate tarball + - tarball-and-sums # the same, and SHA256SUMS made to match it + - extra-file # one more file in the release + +permissions: {} + +jobs: + # Dry run only: the files of a published release, maybe broken. + dry-run-files: + if: github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - name: Download the files of the release + env: + GH_TOKEN: ${{ github.token }} + VERSION: ${{ inputs.version }} + BREAK: ${{ inputs.break }} + run: | + set -euo pipefail + if ! printf '%s\n' "$VERSION" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+'; then + echo "::error::The version is not X.Y.Z." + exit 1 + fi + mkdir draft + gh release download "v$VERSION" --repo "$GITHUB_REPOSITORY" --dir draft + t="draft/nodkey_${VERSION}_pullgate.tar.gz" + case $BREAK in + none) ;; + tarball) printf x >>"$t" ;; + tarball-and-sums) + printf x >>"$t" + (cd draft && grep -v ' nodkey_.*_pullgate.tar.gz$' SHA256SUMS >sums && sha256sum "${t#draft/}" >>sums && mv sums SHA256SUMS) ;; + extra-file) echo "not a file of the release" >draft/extra.txt ;; + *) echo "::error::Unknown break '$BREAK'."; exit 1 ;; + esac + echo "broken on purpose: $BREAK" + ls -l draft + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dry-run-files + path: draft/ + if-no-files-found: error + + verify: + needs: dry-run-files + # On a call from release.yml, dry-run-files is skipped. + if: always() && (needs.dry-run-files.result == 'success' || (github.event_name != 'workflow_dispatch' && needs.dry-run-files.result == 'skipped')) + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + outputs: + verified: ${{ steps.verify.outputs.verified }} + env: + VERSION: ${{ inputs.version }} + ARTIFACT: ${{ github.event_name == 'workflow_dispatch' && 'dry-run-files' || inputs.artifact }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + # The tags: the dry run names the commit of an older release. + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + # Only to read the build info of the binaries (go version -m). + go-version: stable + cache: false + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ env.ARTIFACT }} + path: draft + - name: Get gh 2.102.0 for install.sh + # install.sh downloads this gh and checks it against the SHA-256 in + # install.sh. In the container, github.com is a local server, so + # the file is fetched here first. + run: | + set -euo pipefail + mkdir -p "${RUNNER_TEMP}/gh" + curl --proto '=https' --tlsv1.2 -fsSL --retry 3 -o "${RUNNER_TEMP}/gh/gh_2.102.0_linux_amd64.tar.gz" \ + https://github.com/cli/cli/releases/download/v2.102.0/gh_2.102.0_linux_amd64.tar.gz + - name: Verify the files of the draft + id: verify + env: + DRY_RUN: ${{ github.event_name == 'workflow_dispatch' }} + run: | + set -euo pipefail + if ! printf '%s\n' "$VERSION" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+'; then + echo "::error::The version is not X.Y.Z." + exit 1 + fi + if [ "$DRY_RUN" = true ]; then + # An older release: its own commit, and the kinds of files that + # it has. + export NODKEY_VERIFY_COMMIT NODKEY_VERIFY_KINDS + NODKEY_VERIFY_COMMIT="$(git rev-parse "refs/tags/v${VERSION}^{commit}")" + NODKEY_VERIFY_KINDS="linux_amd64 linux_arm64 pullgate" + if [ -n "$(git ls-tree "v${VERSION}" contrib/ansible/build.sh)" ]; then + NODKEY_VERIFY_KINDS="ansible $NODKEY_VERIFY_KINDS" + fi + fi + rc=0 + sh scripts/verify-release.sh draft "$VERSION" "${RUNNER_TEMP}/gh/gh_2.102.0_linux_amd64.tar.gz" 2>&1 | tee verify.txt || rc=$? + { + echo "## Verify ${VERSION}" + echo + echo '```text' + cat verify.txt + echo '```' + } >>"$GITHUB_STEP_SUMMARY" + if [ "$rc" -ne 0 ]; then + echo "::error::A check of the draft failed; see the summary. The release stays a draft (docs/releasing.md)." + exit 1 + fi + # publish needs this output: a skipped verify must not count as passed. + echo "verified=true" >>"$GITHUB_OUTPUT" + + # Dry run only: in release.yml, the publish job needs verify in the same + # way. This job does nothing; it shows whether the publish would run. + would-publish: + if: github.event_name == 'workflow_dispatch' && needs.verify.outputs.verified == 'true' + needs: verify + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: {} + steps: + - run: echo "The verify passed, so release.yml would publish the release now. This dry run publishes nothing." diff --git a/AGENTS.md b/AGENTS.md index 9485f85..5364f29 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -26,6 +26,9 @@ Planned. Each v1 issue adds its part. (`.github/workflows/release-check.yml`, docs/releasing.md "Check a release candidate"). Their tests are in `scripts_linux_test.go`, at the root of the module. CI checks them with shellcheck. + `scripts/verify-release.sh` is the verify gate of a release: release.yml + runs it through `.github/workflows/verify-release.yml` on the files of + the draft, before it publishes (docs/releasing.md "The verify gate"). - `cmd/nodkey/` - the one binary. Subcommands: `broker`, `try`, `run`, `ssh-agent`, `ssh-grant`, `preapprove`, `agent-browser-plugin`, `keygen`, `audit verify`, `update`, `version`, `doctor`. diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index 60a3f71..eee22f1 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -653,6 +653,12 @@ before they install anything (DESIGN.md section 18). What they trust: a file. Its update check uses a made-up bundle and a fake gh, so it proves nothing about the attestation; only the release workflow on `main` can make one. +- The release is published only after the `verify` job has checked the + files of the draft, with read access only: their names, `SHA256SUMS`, + the attestation with the production identity, the version in each + binary, and an install with `install.sh` as a user does it + (docs/releasing.md, "The verify gate"). So a broken or wrong file stops + the release while it is still a draft, before anybody can download it. - Both trust root on the host where they run, and the way the person got root. As root, the script runs only the programs of the system, removes the variables that change what they do (such as `TAR_OPTIONS`), and diff --git a/docs/releasing.md b/docs/releasing.md index fa56c2f..d03f32c 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -169,7 +169,7 @@ After every push to `main`, the **Release** workflow runs release-please. line of the changelog in it.** This text becomes the release notes. 5. When you merge it, release-please creates the tag (for example `v0.2.0`) and a **draft** GitHub release with the release notes. -6. Then two more jobs run in the same workflow: +6. Then four more jobs run in the same workflow: - `build` can only read the repository. It checks that the tag is the commit of this workflow run. Then it runs [`scripts/build-release.sh`](../scripts/build-release.sh), the one @@ -181,28 +181,75 @@ After every push to `main`, the **Release** workflow runs release-please. `nodkey__pullgate.tar.gz`. It makes the Ansible recipe with `contrib/ansible/build.sh` and packs it into `nodkey__ansible.tar.gz`, and writes `SHA256SUMS`. - - `publish` gets these files from `build`. It checks them against + - `upload` gets these files from `build`. It checks them against `SHA256SUMS` and checks the tag again. Then it makes a signed build provenance attestation for the tarballs. It saves the attestation as one more file, `nodkey_.sigstore.json` (a Sigstore bundle for all tarballs), and checks all tarballs with it, with no login, as in [Verify a download](#verify-a-download). Then it uploads the - files to the draft release and publishes it. + files to the draft release, and downloads them back from the draft. + - `verify` checks the files of the draft before anybody can see them + (see [The verify gate](#the-verify-gate)). It can only read. + - `publish` runs only when `verify` passed. It publishes the draft. The release is never public without its files. Immutable releases are on for this repository: after a release is published, nobody can change its files or move its tag. This is why the files are added while the release is still a draft. -### If `build` or `publish` fails +### The verify gate + +The `verify` job ([`verify-release.yml`](../.github/workflows/verify-release.yml), +with [`scripts/verify-release.sh`](../scripts/verify-release.sh)) checks +the files of the draft release, as a user gets them: + +1. The draft has exactly the expected files: the four tarballs, + `SHA256SUMS` and the bundle, and nothing else. +2. `SHA256SUMS` names each tarball once, and each tarball matches it. +3. The attestation in the bundle verifies each tarball, offline, with + the identity of [Verify a download](#verify-a-download). The bundle + names exactly the tarballs of `SHA256SUMS`. +4. Each binary is built for its CPU, from the commit of the release, as + version `v`, from a clean checkout (its build info). The + amd64 binary also runs and prints `nodkey `. +5. `install.sh` of the release installs the draft's tarball as root in a + clean `debian:trixie` container, with the same checks that a user + runs: the pinned gh and the real bundle. Only `github.com` and + `api.github.com` are a local HTTPS server there, because the files of + a draft are not public yet. + +`publish` needs `verify`. When `verify` fails, nothing is published: the +release stays a draft, and nobody but the maintainers can see it. + +**Dry run.** To see the gate work without a release, run the +**Verify release** workflow by hand (Actions, "Verify release", "Run +workflow"), with a published version and a way to break a file: -The release stays a draft. **Re-run failed jobs** uses the same commit and -the same workflow file as the first run. So a re-run only helps when the -failure was temporary, for example a network error. +```sh +gh workflow run verify-release.yml --repo code6x/nodkey -f version=0.7.0 -f break=tarball +``` -For any other problem, fix it in a new `fix:` pull request, then merge the -next release pull request. That release gets a new version. You can delete -the old draft release. Its tag stays, because tags cannot be deleted. +It downloads the files of that release, breaks one if you ask, and runs +the same checks. With `break=none` it passes, and the job +`would-publish` runs. With a broken file, `verify` fails, and +`would-publish` is skipped, as `publish` is in a release. A dry run +publishes nothing. + +### If `build`, `upload`, `verify` or `publish` fails + +The release stays a draft. Nothing is public. + +1. **Look.** Open the failed job. For `verify`, the summary of the run + lists each check with `ok` or `FAILED`. +2. **Re-run** only for a temporary failure, for example a network error. + **Re-run failed jobs** uses the same commit and the same workflow file + as the first run, so it does not help for any other problem. +3. **Fix** any other problem in a new `fix:` pull request. +4. **Delete the draft release** (Releases, the draft, Delete). The tag + stays: the ruleset for `v*` tags does not let anybody delete it (see + [Repository settings](#repository-settings)). +5. **Release again**: merge the next release pull request. That release + gets a new version. ## Check a release candidate diff --git a/scripts/verify-release.sh b/scripts/verify-release.sh new file mode 100644 index 0000000..9722a72 --- /dev/null +++ b/scripts/verify-release.sh @@ -0,0 +1,186 @@ +#!/bin/sh +# verify-release.sh checks the files of a draft release before it is +# published (the verify job of release.yml, docs/releasing.md "The +# verify gate"). The release stays a draft unless every check passes. +# +# sh scripts/verify-release.sh DIR VERSION GH_TARBALL +# +# Run it at the root of the repository, at the commit of the release. +# DIR holds the files of the draft. GH_TARBALL is +# gh_2.102.0_linux_amd64.tar.gz of cli/cli (install.sh checks its +# SHA-256). It needs Docker, openssl, jq, gh and go. The checks: +# 1. the names of the files are exactly the expected ones; +# 2. SHA256SUMS names each tarball once, and each matches; +# 3. the attestation in the bundle verifies, offline, with the identity +# of the release workflow on main, and names exactly these tarballs; +# 4. each binary is built for its CPU and says this version (the build +# info, and amd64 also runs "nodkey version --offline"); +# 5. install.sh of this commit installs the draft's tarball in a clean +# debian:trixie container, as root, with the same checks that a +# user runs: the real gh of install.sh and the real bundle. Only +# github.com and api.github.com are a local HTTPS server there, +# because the files of a draft are not public yet. +# +# NODKEY_VERIFY_COMMIT (default: HEAD) is the commit that the binaries +# must name. +# NODKEY_VERIFY_KINDS (default "ansible linux_amd64 linux_arm64 pullgate") +# names the tarballs that must be there; only the dry run sets it. +# +# It prints "ok: ..." or "FAILED: ..." for each check, and exits with 1 +# when one failed. + +set -eu + +die() { + echo "verify-release.sh: $*" >&2 + exit 2 +} + +[ "$#" -eq 3 ] || die "give DIR VERSION GH_TARBALL" +dir=$1 +version=$2 +gh_tarball=$3 +case $version in *' +'*) die "the version has a line break" ;; +esac +printf '%s\n' "$version" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+' || die "'$version' is not X.Y.Z" +version_re=$(printf '%s' "$version" | sed 's/\./\\./g') +[ -d "$dir" ] || die "$dir is not a folder" +[ -f "$gh_tarball" ] || die "$gh_tarball does not exist" +[ -f install.sh ] || die "run it at the root of the repository" +repo=${GITHUB_REPOSITORY:-code6x/nodkey} +# The commit of the release: this checkout, or NODKEY_VERIFY_COMMIT (only +# the dry run sets it, for an older release). +commit=${NODKEY_VERIFY_COMMIT:-$(git rev-parse HEAD)} +printf '%s\n' "$commit" | grep -Eqx '[0-9a-f]{40}' || die "the commit '$commit' is not a full SHA-1" +identity="https://github.com/${repo}/.github/workflows/release.yml@refs/heads/main" + +failed=0 +ok() { echo "ok: $*"; } +fail() { + echo "FAILED: $*" + failed=1 +} + +# The kinds of tarballs. Only the dry run of verify-release.yml sets +# NODKEY_VERIFY_KINDS, for an older release that has fewer kinds. +kinds=${NODKEY_VERIFY_KINDS:-ansible linux_amd64 linux_arm64 pullgate} +tarballs= +for k in $kinds; do + case $k in + ansible | linux_amd64 | linux_arm64 | pullgate) tarballs="$tarballs nodkey_${version}_${k}.tar.gz" ;; + *) die "NODKEY_VERIFY_KINDS: unknown kind '$k'" ;; + esac +done +case " $kinds " in *" linux_amd64 "*) ;; *) die "NODKEY_VERIFY_KINDS must have linux_amd64" ;; esac +bundle=nodkey_${version}.sigstore.json + +# 1. The names. +tarball_lines=$(for t in $tarballs; do echo "$t"; done | LC_ALL=C sort) +want=$(printf '%s\n%s\n%s\n' "$tarball_lines" SHA256SUMS "$bundle" | LC_ALL=C sort) +have=$(find "$dir" -mindepth 1 -maxdepth 1 -printf '%f\n' | LC_ALL=C sort) +if [ "$have" = "$want" ]; then + ok "1 the draft has exactly the expected files" +else + fail "1 the files of the draft are not the expected ones: have [$(printf '%s' "$have" | tr '\n' ' ')], want [$(printf '%s' "$want" | tr '\n' ' ')]" +fi + +# 2. SHA256SUMS. +names=$(awk '{ sub(/^\*/, "", $2); print $2 }' "$dir/SHA256SUMS" 2>/dev/null | LC_ALL=C sort) +if [ "$names" != "$tarball_lines" ]; then + fail "2 SHA256SUMS does not name each tarball exactly once" +elif (cd "$dir" && sha256sum --strict --quiet -c SHA256SUMS); then + ok "2 each tarball matches SHA256SUMS" +else + fail "2 a tarball does not match SHA256SUMS" +fi + +# 3. The attestation: each tarball, offline with the bundle, and the +# bundle names exactly the four tarballs with their SHA-256. +att=ok +for t in $tarballs; do + if [ ! -f "$dir/$t" ] || ! gh attestation verify "$dir/$t" --bundle "$dir/$bundle" --repo "$repo" \ + --cert-identity "$identity" --deny-self-hosted-runners --source-digest "$commit" >/dev/null 2>&1; then + fail "3 the attestation does not verify $t" + att= + fi +done +subjects=$(jq -r '.dsseEnvelope.payload' "$dir/$bundle" 2>/dev/null | base64 -d 2>/dev/null | + jq -r '.subject[] | "\(.digest.sha256) \(.name)"' 2>/dev/null | LC_ALL=C sort -k2) || subjects= +sums=$(LC_ALL=C sort -k2 "$dir/SHA256SUMS" 2>/dev/null | sed 's/ \*/ /') +if [ -z "$subjects" ] || [ "$subjects" != "$sums" ]; then + fail "3 the bundle does not name exactly the tarballs of SHA256SUMS" + att= +fi +[ -z "$att" ] || ok "3 the attestation verifies each tarball, with the identity $identity and the commit $commit" + +# 4. The version in each binary. +work=$(mktemp -d) +trap 'rm -rf -- "$work"' EXIT +for arch in amd64 arm64; do + t=nodkey_${version}_linux_${arch}.tar.gz + case " $kinds " in *" linux_$arch "*) ;; *) continue ;; esac + mkdir -p "$work/$arch" + if ! tar -xzf "$dir/$t" -C "$work/$arch" nodkey 2>/dev/null; then + fail "4 $t has no file nodkey" + continue + fi + # With -trimpath the build info has no -ldflags, but Go puts in the + # version of the tag of a clean checkout, and the commit. + info=$(go version -m "$work/$arch/nodkey" 2>/dev/null) || info= + if printf '%s\n' "$info" | grep -Eq "^[[:space:]]+mod[[:space:]]+github\.com/code6x/nodkey[[:space:]]+v${version_re}([[:space:]]|\$)" && + printf '%s\n' "$info" | grep -Eq "^[[:space:]]+build[[:space:]]+GOARCH=${arch}\$" && + printf '%s\n' "$info" | grep -Eq "^[[:space:]]+build[[:space:]]+vcs.revision=${commit}\$" && + printf '%s\n' "$info" | grep -Eq "^[[:space:]]+build[[:space:]]+vcs.modified=false\$"; then + ok "4 $t: the build info says v$version, $arch, commit $commit, a clean checkout" + else + fail "4 $t: the build info does not say v$version, $arch, commit $commit and a clean checkout" + fi +done +got=$("$work/amd64/nodkey" version --offline 2>&1) || got="(it does not run)" +if [ "$got" = "nodkey $version" ]; then + ok "4 the amd64 binary says: $got" +else + fail "4 the amd64 binary says '$got', want 'nodkey $version'" +fi + +# 5. install.sh in a clean container. +web=$work/web +mkdir -p "$web/code6x/nodkey/releases/download/v$version" "$web/repos/code6x/nodkey/releases" \ + "$web/cli/cli/releases/download/v2.102.0" +cp "$dir"/* "$web/code6x/nodkey/releases/download/v$version/" +cp "$gh_tarball" "$web/cli/cli/releases/download/v2.102.0/" +printf '{\n "tag_name": "v%s"\n}\n' "$version" >"$web/repos/code6x/nodkey/releases/latest" +cp install.sh "$work/install.sh" +openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -days 1 \ + -subj "/CN=nodkey verify CA" -keyout "$work/ca.key" -out "$work/ca.crt" 2>/dev/null +openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -subj "/CN=github.com" \ + -keyout "$work/server.key" -out "$work/server.csr" 2>/dev/null +printf 'subjectAltName=DNS:github.com,DNS:api.github.com\n' >"$work/ext" +openssl x509 -req -in "$work/server.csr" -CA "$work/ca.crt" -CAkey "$work/ca.key" -CAcreateserial \ + -days 1 -extfile "$work/ext" -out "$work/server.crt" 2>/dev/null +rm -f "$work/ca.key" +cat >"$work/inside.sh" <<'INSIDE' +set -eu +export DEBIAN_FRONTEND=noninteractive +apt-get update -qq >/dev/null +apt-get install -y -qq --no-install-recommends curl ca-certificates openssl >/dev/null +cp /work/ca.crt /usr/local/share/ca-certificates/verify.crt +update-ca-certificates >/dev/null 2>&1 +cp -r /work/web /srv/web +(cd /srv/web && openssl s_server -quiet -accept 443 -WWW -cert /work/server.crt -key /work/server.key >/dev/null 2>&1) & +sleep 1 +sh /work/install.sh --version "$1" +/usr/local/bin/nodkey version --offline +INSIDE +chmod -R go-w "$work" +# debian:trixie, pinned, as in scripts/release-check/update-path.sh. +image=debian:trixie@sha256:9c6e381421d0f48d1b8133e573f33c185ee16cbf505eda9c4a8a4791b25ea8a6 +if out=$(docker run --rm --add-host github.com:127.0.0.1 --add-host api.github.com:127.0.0.1 \ + -v "$work:/work:ro" "$image" sh /work/inside.sh "$version" 2>&1) && + [ "$(printf '%s\n' "$out" | tail -n 1)" = "nodkey $version" ]; then + ok "5 install.sh installs the draft's tarball in a clean container, as root" +else + fail "5 install.sh does not install the draft's tarball: $(printf '%s\n' "$out" | tail -n 5 | tr '\n' ' ')" +fi +exit "$failed" diff --git a/scripts_linux_test.go b/scripts_linux_test.go index 543c514..d0c3a2f 100644 --- a/scripts_linux_test.go +++ b/scripts_linux_test.go @@ -2,6 +2,9 @@ package nodkey import ( "bytes" + "crypto/sha256" + "encoding/base64" + "fmt" "os" "os/exec" "path/filepath" @@ -225,3 +228,176 @@ func TestFragmentRefusesAnUnknownResult(t *testing.T) { t.Errorf("exit %d, output %q", code, got) } } + +// verifyFixture makes the files of a release 1.2.3 in a new folder, and +// fake gh, go and docker programs in another, for verify-release.sh. The +// nodkey of each tarball is a script that prints "nodkey 1.2.3". +type verifyFixture struct { + dir, bin, commit string + head bool // run without NODKEY_VERIFY_COMMIT +} + +func newVerifyFixture(t *testing.T) *verifyFixture { + t.Helper() + f := &verifyFixture{dir: t.TempDir(), bin: t.TempDir(), commit: strings.Repeat("ab", 20)} + nodkey := []byte("#!/bin/sh\necho \"nodkey ${FAKE_VERSION:-1.2.3}\"\n") + var sums, subjects strings.Builder + for _, kind := range []string{"ansible", "linux_amd64", "linux_arm64", "pullgate"} { + name := "nodkey_1.2.3_" + kind + ".tar.gz" + data := tarGz(t, tarFile{name: "nodkey", mode: 0o755, data: nodkey}) + writeFile(t, filepath.Join(f.dir, name), data, 0o600) + sum := sha256.Sum256(data) + fmt.Fprintf(&sums, "%x %s\n", sum, name) + if subjects.Len() > 0 { + subjects.WriteString(",") + } + fmt.Fprintf(&subjects, `{"name":%q,"digest":{"sha256":"%x"}}`, name, sum) + } + writeFile(t, filepath.Join(f.dir, "SHA256SUMS"), []byte(sums.String()), 0o600) + payload := base64.StdEncoding.EncodeToString([]byte(`{"subject":[` + subjects.String() + `]}`)) + writeFile(t, filepath.Join(f.dir, "nodkey_1.2.3.sigstore.json"), []byte(`{"dsseEnvelope":{"payload":"`+payload+`"}}`), 0o600) + writeFile(t, filepath.Join(f.bin, "gh"), []byte("#!/bin/sh\n[ ! -e \"$FAKE_FAIL/gh\" ]\n"), 0o700) + // go version -m: the arch is in the name of the folder of the binary. + writeFile(t, filepath.Join(f.bin, "go"), []byte(`#!/bin/sh +arch=$(basename "$(dirname "$3")") +printf '%s\n\tmod\tgithub.com/code6x/nodkey\tv1.2.3\t\n\tbuild\tGOARCH=%s\n\tbuild\tvcs.revision=%s\n\tbuild\tvcs.modified=false\n' "$3" "$arch" "$(cat "$FAKE_FAIL/../commit")" +`), 0o700) + writeFile(t, filepath.Join(f.bin, "docker"), []byte("#!/bin/sh\n[ ! -e \"$FAKE_FAIL/docker\" ] || exit 1\necho \"nodkey ${FAKE_DOCKER_VERSION:-1.2.3}\"\n"), 0o700) + return f +} + +// run runs verify-release.sh with the fakes first in PATH. +func (f *verifyFixture) run(t *testing.T, fail ...string) (string, int) { + t.Helper() + state := t.TempDir() + failDir := filepath.Join(state, "fail") + mkdir(t, failDir, 0o700) + writeFile(t, filepath.Join(state, "commit"), []byte(f.commit), 0o600) + for _, name := range fail { + writeFile(t, filepath.Join(failDir, name), nil, 0o600) + } + gh := filepath.Join(t.TempDir(), "gh.tar.gz") + writeFile(t, gh, []byte("gh"), 0o600) + t.Setenv("PATH", f.bin+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("FAKE_FAIL", failDir) + t.Setenv("NODKEY_VERIFY_COMMIT", strings.Repeat("ab", 20)) + if f.head { + // The script then takes the commit of HEAD. + if err := os.Unsetenv("NODKEY_VERIFY_COMMIT"); err != nil { + t.Fatal(err) + } + } + return runScript(t, ".", "", "scripts/verify-release.sh", f.dir, "1.2.3", gh) +} + +func TestVerifyRelease(t *testing.T) { + for _, p := range []string{"jq", "openssl", "base64"} { + if _, err := exec.LookPath(p); err != nil { + skipOrFail(t, "%s is missing: %v", p, err) + } + } + t.Run("the default commit is HEAD", func(t *testing.T) { + head, err := exec.CommandContext(t.Context(), "git", "rev-parse", "HEAD").Output() + if err != nil { + skipOrFail(t, "git rev-parse HEAD: %v", err) + } + f := newVerifyFixture(t) + f.commit, f.head = strings.TrimSpace(string(head)), true + got, code := f.run(t) + if code != 0 || !strings.Contains(got, "commit "+f.commit) { + t.Errorf("exit %d, output:\n%s", code, got) + } + }) + t.Run("good files pass", func(t *testing.T) { + got, code := newVerifyFixture(t).run(t) + if code != 0 || strings.Contains(got, "FAILED") || strings.Count(got, "ok: ") != 7 { + t.Errorf("exit %d, output:\n%s", code, got) + } + }) + for _, c := range []struct { + name string + change func(t *testing.T, f *verifyFixture) []string // returns the fakes that fail + want string + }{ + {"an extra file", func(t *testing.T, f *verifyFixture) []string { + writeFile(t, filepath.Join(f.dir, "extra.txt"), nil, 0o600) + return nil + }, "FAILED: 1 the files of the draft are not the expected ones"}, + {"a missing file", func(t *testing.T, f *verifyFixture) []string { + if err := os.Remove(filepath.Join(f.dir, "nodkey_1.2.3_pullgate.tar.gz")); err != nil { + t.Fatal(err) + } + return nil + }, "FAILED: 1 the files"}, + {"a changed tarball", func(t *testing.T, f *verifyFixture) []string { + p := filepath.Join(f.dir, "nodkey_1.2.3_pullgate.tar.gz") + writeFile(t, p, append(readFile(t, p), 'x'), 0o600) + return nil + }, "FAILED: 2 a tarball does not match SHA256SUMS"}, + {"SHA256SUMS without a tarball", func(t *testing.T, f *verifyFixture) []string { + p := filepath.Join(f.dir, "SHA256SUMS") + lines := strings.SplitAfter(string(readFile(t, p)), "\n") + writeFile(t, p, []byte(strings.Join(lines[1:], "")), 0o600) + return nil + }, "FAILED: 2 SHA256SUMS does not name each tarball exactly once"}, + {"the attestation fails", func(*testing.T, *verifyFixture) []string { return []string{"gh"} }, + "FAILED: 3 the attestation does not verify"}, + {"the bundle names other files", func(t *testing.T, f *verifyFixture) []string { + payload := base64.StdEncoding.EncodeToString([]byte(`{"subject":[{"name":"other.tar.gz","digest":{"sha256":"00"}}]}`)) + writeFile(t, filepath.Join(f.dir, "nodkey_1.2.3.sigstore.json"), []byte(`{"dsseEnvelope":{"payload":"`+payload+`"}}`), 0o600) + return nil + }, "FAILED: 3 the bundle does not name exactly the tarballs"}, + {"a binary of another commit", func(_ *testing.T, f *verifyFixture) []string { + f.commit = strings.Repeat("cd", 20) + return nil + }, "FAILED: 4 nodkey_1.2.3_linux_amd64.tar.gz: the build info does not say"}, + {"the install fails", func(*testing.T, *verifyFixture) []string { return []string{"docker"} }, + "FAILED: 5 install.sh does not install"}, + {"the install gives another version", func(t *testing.T, _ *verifyFixture) []string { + t.Setenv("FAKE_DOCKER_VERSION", "1.2.4") + return nil + }, "FAILED: 5 install.sh does not install"}, + {"the amd64 binary says another version", func(t *testing.T, _ *verifyFixture) []string { + t.Setenv("FAKE_VERSION", "1.2.4") + return nil + }, "FAILED: 4 the amd64 binary says 'nodkey 1.2.4'"}, + } { + t.Run(c.name, func(t *testing.T) { + f := newVerifyFixture(t) + got, code := f.run(t, c.change(t, f)...) + if code != 1 || !strings.Contains(got, c.want) { + t.Errorf("exit %d, output:\n%s\nwant exit 1 and %q", code, got, c.want) + } + }) + } + for _, c := range []struct { + name string + env []string + args []string + want string + }{ + {"a bad version", nil, []string{"v1.2.3"}, "is not X.Y.Z"}, + {"a bad commit", []string{"NODKEY_VERIFY_COMMIT=abc"}, nil, "is not a full SHA-1"}, + {"an unknown kind", []string{"NODKEY_VERIFY_KINDS=linux_amd64 windows"}, nil, "unknown kind 'windows'"}, + {"no amd64", []string{"NODKEY_VERIFY_KINDS=pullgate"}, nil, "must have linux_amd64"}, + } { + t.Run(c.name, func(t *testing.T) { + f := newVerifyFixture(t) + gh := filepath.Join(t.TempDir(), "gh.tar.gz") + writeFile(t, gh, []byte("gh"), 0o600) + args := []string{f.dir, "1.2.3", gh} + if c.args != nil { + args[1] = c.args[0] + } + t.Setenv("NODKEY_VERIFY_COMMIT", strings.Repeat("ab", 20)) + for _, kv := range c.env { + k, v, _ := strings.Cut(kv, "=") + t.Setenv(k, v) + } + got, code := runScript(t, ".", "", "scripts/verify-release.sh", args...) + if code != 2 || !strings.Contains(got, c.want) { + t.Errorf("exit %d, output %q; want exit 2 and %q", code, got, c.want) + } + }) + } +}