diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b6bf4ee..3f1e0bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,12 @@ env: permissions: contents: read +# On main, each commit gets its own group: a group holds only one pending run, so a shared one would let a later push +# cancel a queued version bump, which would then never be released. Elsewhere a newer push cancels the older run. +concurrency: + group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha || github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + jobs: check: name: Check @@ -67,8 +73,14 @@ jobs: - name: Run cargo clippy run: cargo clippy --all-targets --all-features + # The release is created as a DRAFT and only published once every asset -- + # binaries plus the generated DotSlash files -- has been uploaded. This + # ordering is required for immutable releases: once a release is published, + # GitHub rejects further asset uploads with + # "Cannot upload assets to an immutable release". + # See https://github.com/actions/attest-build-provenance/issues/734 release: - runs-on: macos-latest + runs-on: ubuntu-latest permissions: contents: write needs: @@ -87,13 +99,6 @@ jobs: # TLDR – By default this action fetches no history. # We need a bit of history to be able to check if we've recently updated the version in Cargo.toml fetch-depth: 2 - - name: Toolchain info - run: | - cargo --version --verbose - rustc --version - cargo clippy --version - - name: Build - run: cargo build --release --target aarch64-apple-darwin --target x86_64-apple-darwin - name: Check for version changes in Cargo.toml id: check_for_version_changes run: | @@ -109,10 +114,31 @@ jobs: echo "changed=false" >> $GITHUB_OUTPUT fi - - name: Create GitHub Release if current commit has updated the version in Cargo.toml + - name: Create draft GitHub Release if current commit has updated the version in Cargo.toml if: steps.check_for_version_changes.outputs.changed == 'true' run: | - gh release create "$NEW_VERSION" --target "$GITHUB_SHA" --generate-notes + # A draft release does not create a git tag, so GitHub does NOT enforce + # tag uniqueness for drafts -- `gh release create` would happily make a + # second draft with the same pending tag name, and the DotSlash + # generator resolves whichever one the API returns first. That is + # reachable via "Re-run all jobs" after a failed upload, so reuse an + # existing draft instead of creating a duplicate. + # + # gh prints exactly "release not found" when no release or draft has this tag. Any other failure is an error, + # since treating it as "no draft" would create the duplicate this step exists to avoid. + view_err=$(mktemp) + if is_draft=$(gh release view "$NEW_VERSION" --json isDraft --jq .isDraft 2>"$view_err"); then + if [[ "$is_draft" != "true" ]]; then + echo "::error::Release $NEW_VERSION is already published." + exit 1 + fi + echo "Draft $NEW_VERSION already exists; reusing it." + elif [[ "$(cat "$view_err")" == "release not found" ]]; then + gh release create "$NEW_VERSION" --target "$GITHUB_SHA" --generate-notes --draft + else + cat "$view_err" >&2 + exit 1 + fi env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NEW_VERSION: ${{ steps.check_for_version_changes.outputs.new_version }} @@ -121,29 +147,59 @@ jobs: runs-on: macos-latest permissions: contents: write + # Required by actions/attest-build-provenance: id-token to mint the OIDC + # token for the Sigstore signing certificate, attestations to persist the + # resulting attestation. + id-token: write + attestations: write if: ${{needs.release.outputs.new_version}} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Build - run: cargo build --release --target aarch64-apple-darwin --target x86_64-apple-darwin + run: cargo build --release --locked --target aarch64-apple-darwin --target x86_64-apple-darwin - - name: Upload mac universal binary + - name: Create universal binary run: | # This combines the intel and m1 binaries into a single binary lipo -create -output target/codeowners target/aarch64-apple-darwin/release/codeowners target/x86_64-apple-darwin/release/codeowners + # lipo carries over the per-architecture linker (ad-hoc) signatures, + # but Apple recommends re-signing the merged binary so it carries a + # single coherent signature. All code on Apple silicon must be at + # least ad-hoc signed or it is SIGKILLed on launch, so verify rather + # than assume. https://developer.apple.com/forums/thread/708552 + # + # `--sign -` is the ad-hoc identity, not a placeholder for a cert + # name: no certificate, no keychain, no Developer ID, nothing to + # provision in CI. It only makes the binary internally consistent and + # asserts nothing about who built it -- provenance comes from the + # attestation step below, not from codesign. + codesign --force --sign - target/codeowners + codesign --verify --verbose target/codeowners + # Notices that some dependencies' licenses require redistributions of the binary to carry dev/license-notices target/licenses # Creates artifact for homebrew. -C means run from `target` directory, which keeps the - # binary at the archive root, where dotslash-config.json looks for it + # binary at the archive root, where dotslash-config.json looks for it. + # NOTE: any mutation of the binary must happen BEFORE signing, and + # any repackaging must happen BEFORE attestation, or the digest the + # attestation covers will not match what users download. tar -czf target/codeowners-mac.tar.gz -C target codeowners licenses - # This tarball holds the executable binary and the license notices - gh release upload $NEW_VERSION target/codeowners-mac.tar.gz + - name: Attest build provenance + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-path: target/codeowners-mac.tar.gz + - name: Upload mac universal binary + run: | + # This tarball holds the executable binary and the license notices. + # --clobber so re-running after a partial failure replaces the asset + # instead of failing on "asset already exists". + gh release upload "$NEW_VERSION" target/codeowners-mac.tar.gz --clobber env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NEW_VERSION: ${{ needs.release.outputs.new_version }} @@ -154,18 +210,25 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + id-token: write + attestations: write steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Update local toolchain run: | - cargo install cross + # Pinned so a future cross release cannot change how release binaries + # are built without a reviewed commit. 0.2.5 is what an unpinned + # `cargo install cross` resolves to today, so this is not a version + # bump. Deliberately not --locked: cross 0.2.5's lockfile predates + # current rustc and this build cannot be exercised outside `main`. + cargo install cross --version 0.2.5 - name: Build linux binaries run: | - cross build --release --target x86_64-unknown-linux-gnu - cross build --release --target aarch64-unknown-linux-gnu - - name: Upload linux binaries + cross build --release --locked --target x86_64-unknown-linux-gnu + cross build --release --locked --target aarch64-unknown-linux-gnu + - name: Package linux binaries run: | # Notices that some dependencies' licenses require redistributions of the binary to carry. # Each -C keeps the binary at its archive's root, where dotslash-config.json looks for it. @@ -173,8 +236,20 @@ jobs: dev/license-notices target/aarch64-unknown-linux-gnu/release/licenses tar -czf target/x86_64-unknown-linux-gnu.tar.gz -C target/x86_64-unknown-linux-gnu/release codeowners licenses tar -czf target/aarch64-unknown-linux-gnu.tar.gz -C target/aarch64-unknown-linux-gnu/release codeowners licenses - gh release upload $NEW_VERSION target/x86_64-unknown-linux-gnu.tar.gz - gh release upload $NEW_VERSION target/aarch64-unknown-linux-gnu.tar.gz + + - name: Attest build provenance + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-path: | + target/x86_64-unknown-linux-gnu.tar.gz + target/aarch64-unknown-linux-gnu.tar.gz + + - name: Upload linux binaries + run: | + # --clobber so re-running after a partial failure replaces the assets + # instead of failing on "asset already exists". + gh release upload "$NEW_VERSION" target/x86_64-unknown-linux-gnu.tar.gz --clobber + gh release upload "$NEW_VERSION" target/aarch64-unknown-linux-gnu.tar.gz --clobber env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NEW_VERSION: ${{ needs.release.outputs.new_version }} @@ -191,6 +266,18 @@ jobs: contents: write steps: + - name: Remove a DotSlash file left by an earlier attempt + run: | + # The action uploads without --clobber, so on a re-run its upload would fail, and the uploads above may have + # replaced the archives that the old file's digests describe. + assets=$(gh release view "$NEW_VERSION" --repo "$GITHUB_REPOSITORY" --json assets --jq '.assets[].name') + if grep -qx codeowners <<<"$assets"; then + gh release delete-asset "$NEW_VERSION" codeowners --yes --repo "$GITHUB_REPOSITORY" + fi + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NEW_VERSION: ${{ needs.release.outputs.new_version }} + - uses: facebook/dotslash-publish-release@9c9ec027515c34db9282a09a25a9cab5880b2c52 # v2 # This is necessary because the action uses # `gh release upload` to publish the generated DotSlash file(s) @@ -203,3 +290,22 @@ jobs: config: .github/workflows/dotslash-config.json # Tag for the release to target. tag: ${{ needs.release.outputs.new_version }} + + # Publishing last is what makes immutable releases workable: every asset is + # in place before the release becomes visible and frozen. + publish-release: + name: Publish the release + needs: + - release + - generate-dotslash-files + if: needs.release.outputs.new_version != '' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Undraft the release + run: | + gh release edit "$NEW_VERSION" --draft=false --repo "$GITHUB_REPOSITORY" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NEW_VERSION: ${{ needs.release.outputs.new_version }} diff --git a/.github/workflows/dotslash-config.json b/.github/workflows/dotslash-config.json index 3fd237f..9d8db47 100644 --- a/.github/workflows/dotslash-config.json +++ b/.github/workflows/dotslash-config.json @@ -1,28 +1,33 @@ { - "outputs": { - "codeowners": { - "platforms": { - "macos-x86_64": { - "regex": "^codeowners-mac", - "path": "codeowners", - "format": "tar.gz" - }, - "macos-aarch64": { - "regex": "^codeowners-mac", - "path": "codeowners", - "format": "tar.gz" - }, - "linux-x86_64": { - "regex": "^x86_64-unknown-linux", - "path": "codeowners", - "format": "tar.gz" - }, - "linux-aarch64": { - "regex": "^aarch64-unknown-linux", - "path": "codeowners", - "format": "tar.gz" - } + "exclude-http-provider": true, + "outputs": { + "codeowners": { + "platforms": { + "macos-x86_64": { + "name": "codeowners-mac.tar.gz", + "path": "codeowners", + "format": "tar.gz", + "hash": "sha256" + }, + "macos-aarch64": { + "name": "codeowners-mac.tar.gz", + "path": "codeowners", + "format": "tar.gz", + "hash": "sha256" + }, + "linux-x86_64": { + "name": "x86_64-unknown-linux-gnu.tar.gz", + "path": "codeowners", + "format": "tar.gz", + "hash": "sha256" + }, + "linux-aarch64": { + "name": "aarch64-unknown-linux-gnu.tar.gz", + "path": "codeowners", + "format": "tar.gz", + "hash": "sha256" } } } } +} diff --git a/README.md b/README.md index 47269f8..1720287 100644 --- a/README.md +++ b/README.md @@ -43,12 +43,79 @@ You can run `codeowners` without installing a platform-specific binary by using ### Option A: DotSlash (recommended) -1. Install DotSlash: see [https://dotslash-cli.com/docs/installation/](https://dotslash-cli.com/docs/installation/) -2. Download the latest DotSlash text file from a release, for example [https://github.com/rubyatscale/codeowners-rs/releases](https://github.com/rubyatscale/codeowners-rs/releases). -3. Execute the downloaded file with DotSlash; it will fetch and run the correct binary. +1. Install DotSlash: see [https://dotslash-cli.com/docs/installation/](https://dotslash-cli.com/docs/installation/). + DotSlash files from releases after v0.6.0 fetch the binary by running + `gh release download`, so the [GitHub CLI](https://cli.github.com/) must be on your + `PATH` as well. +2. Download the `codeowners` DotSlash text file from the release you want to pin + to: [https://github.com/rubyatscale/codeowners-rs/releases](https://github.com/rubyatscale/codeowners-rs/releases) +3. **Commit that file into your repository** (for example at `bin/codeowners`) and + mark it executable with `chmod +x`. +4. Run it like any other executable — `./bin/codeowners --help`. DotSlash fetches, + verifies, and caches the correct binary for the current platform on first use. + +Upgrading is a deliberate act: download the DotSlash file from the newer release, +replace the committed one, and review the diff. The digests change, so the change +is visible in code review. Release archives after v0.5.0 also hold a `licenses` directory next to the `codeowners` binary. It has the license notices that the dependencies listed in `dev/license-notices` require redistributions of the binary to include. +#### Why commit the file rather than re-download it + +A DotSlash file records the expected `size` and `digest` of each platform's release +archive, and DotSlash refuses to unpack or execute an artifact that does not match. +That check is only worth something if the digest itself is trusted — and the digest +is trusted because the file lives in your repository and changes to it go through +review. If you re-download the DotSlash file alongside the binary every time, you +are trusting whatever the release currently claims, and the digest verifies nothing +you did not just fetch from the same place. + +This is also DotSlash's own guidance: "the provenance of a DotSlash file is +paramount", and DotSlash files "are meant to live in source control" +([motivation](https://github.com/facebook/dotslash/blob/main/website/docs/motivation.md)). + +#### Verifying a release independently (optional) + +DotSlash verifies digests but does not check signatures. Release binaries are built +by GitHub Actions and carry [SLSA build provenance](https://docs.github.com/en/actions/security-for-github-actions/using-artifact-attestations/using-artifact-attestations-to-establish-provenance-for-builds) +attestations, which you can verify with the GitHub CLI: + +```sh +gh attestation verify codeowners-mac.tar.gz --repo rubyatscale/codeowners-rs +``` + +To additionally require that the attestation came from this repository's release +workflow rather than any workflow in it: + +```sh +gh attestation verify codeowners-mac.tar.gz \ + --repo rubyatscale/codeowners-rs \ + --signer-workflow rubyatscale/codeowners-rs/.github/workflows/ci.yml +``` + +For releases after v0.6.0, the `digest` a DotSlash file records for each platform is +the SHA-256 of that platform's archive, which is also the digest the attestation +covers. So `gh attestation verify` on an archive checks the same bytes your committed +DotSlash file pins. Earlier DotSlash files record BLAKE3 digests instead. + +Releases up to and including v0.6.0 were published before attestation was added, so +`gh attestation verify` reports "no matching attestations found" for them. That is +expected for those versions and is not evidence of tampering. + +Note that macOS binaries are ad-hoc signed only. They are not signed with an Apple +Developer ID and are not notarized. Ad-hoc signing satisfies Apple +silicon's requirement that all code be signed, but it does not satisfy Gatekeeper. + +Whether that matters depends on how you fetch the binary. DotSlash and `curl` do not +set the `com.apple.quarantine` attribute, so Gatekeeper never evaluates the binary +and it runs normally. If you download the tarball from the releases page in a +browser, quarantine *is* set and Gatekeeper will refuse to run it until you clear +the attribute: + +```sh +xattr -d com.apple.quarantine codeowners +``` + ### Option B: From source with Cargo Requires Rust toolchain.