diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 776b6e0..aa9a7b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -390,11 +390,32 @@ jobs: --notes-file notes.md "$kind" "${files[@]}" echo "created the GitHub release for $TAG with ${#files[@]} file(s)" + # ------------------------------------------------------------ sign ------ + # Signs the image keyless, attests its SBOMs and how it was built, and + # attaches the SBOMs to the release (sign.yml). It runs once the image, + # its index and the release exist. The forum announcement goes out when + # the release is published, before this; a failure here fails the tag's + # check, and is put right by running sign.yml by hand with the tag. + sign: + if: ${{ !cancelled() && vars.BUILD_ON == 'github' && github.ref_type == 'tag' && needs.publish.result == 'success' }} + needs: [version, publish, ghcr] + permissions: + contents: read + id-token: write + attestations: write + packages: write + uses: ./.github/workflows/sign.yml + with: + tag: ${{ github.ref_name }} + built-here: true + ghcr: ${{ needs.ghcr.result == 'success' }} + secrets: inherit + # One commit status on Gitea for the whole run: what Gitea's ci.yml waits # for, and what a Gitea PR shows. report: if: ${{ always() && vars.BUILD_ON == 'github' }} - needs: [pending, node, docker-build, version, build, publish, files] + needs: [pending, node, docker-build, version, build, publish, files, sign] runs-on: ubuntu-latest steps: - env: diff --git a/.github/workflows/sign.yml b/.github/workflows/sign.yml new file mode 100644 index 0000000..6998716 --- /dev/null +++ b/.github/workflows/sign.yml @@ -0,0 +1,237 @@ +# Signs a release's image, and attaches what it is made of and how it was +# built, so anyone can check an image came from this repository's workflow +# before running it (K-W6, as inbuxa-server's sign.yml does for the server). +# +# Keyless: cosign signs with a short-lived certificate for this workflow's +# GitHub identity, and the signature is recorded in Sigstore's public log. +# There is no key to keep. A signature names the workflow that made it, so +# it checks against this repository, not against a key: +# +# cosign verify @ \ +# --certificate-oidc-issuer https://token.actions.githubusercontent.com \ +# --certificate-identity-regexp \ +# '^https://github.com/inbuxa/inbuxa-webmail/\.github/workflows/sign\.yml@refs/(tags/inbuxa-v.+|heads/main)$' +# +# gh attestation verify oci://@ --owner inbuxa +# +# What it does: +# * signs the multi-arch index by digest, in the registry the image is +# published to (vars.REGISTRY) and in the GHCR copy, and checks both; +# * writes two kinds of SPDX SBOM (syft), attested in GitHub's store and +# attached to the release on Gitea: +# - one per architecture, of the image as shipped, attested against +# that architecture's image; +# - one of the app, from package-lock.json at the tagged commit, +# attested against the index. The image's +# node_modules are the server's; the browser app's packages are +# bundled into web/dist, where an image scan can't see them. +# * when called by ci.yml from the release's own run, attests the build +# (SLSA provenance) against the index: that run built it. A signing run +# started by hand built nothing, so it attests no build. +# +# Two ways in: +# * workflow_call, from ci.yml on a release tag, once the image, its index +# and the release exist. +# * workflow_dispatch, from main, with an existing release's tag: signs a +# version that was published before this workflow existed, or whose +# signing failed. Signing again is harmless: an image can carry more than +# one signature. +# +# The attestations are kept by GitHub, keyed by digest, rather than pushed to +# the registry. The signatures go to the registry with the image, where +# cosign looks. +# +# cosign and syft are downloaded at pinned versions and checked against +# pinned SHA-256 sums; actions are pinned to full commit SHAs. Only GitHub's +# own actions and the docker/* ones are used, as in ci.yml. +name: sign + +on: + workflow_call: + inputs: + tag: + description: The release's tag (inbuxa-v27.1-rc.11) + required: true + type: string + built-here: + description: The calling run built this image, so it attests the build + required: true + type: boolean + ghcr: + description: The GHCR copy exists and is signed too + required: true + type: boolean + workflow_dispatch: + inputs: + tag: + description: An existing release's tag (inbuxa-v27.1-rc.11) + required: true + type: string + +permissions: + contents: read + +env: + COSIGN_VERSION: v3.1.4 + COSIGN_SHA256: 424e2c97d0b45fd7a6b608a87f090f35633f4c23ece5f092d48bba11aaf4bbfa + SYFT_VERSION: 1.55.0 + SYFT_SHA256: bb65249542db5fedeeb4d3f9b018b75e7264dbfbbb10486f044581a92f90490d + +jobs: + sign: + if: ${{ vars.BUILD_ON == 'github' }} + runs-on: ubuntu-latest + permissions: + contents: read + # The certificate's identity, from GitHub's OIDC token + id-token: write + # GitHub's attestation store + attestations: write + # The signature on the GHCR copy + packages: write + env: + TAG: ${{ inputs.tag }} + GITEA_URL: ${{ vars.GITEA_URL }} + REGISTRY: ${{ vars.REGISTRY }} + GHCR: ${{ github.event_name == 'workflow_dispatch' || inputs.ghcr }} + steps: + - id: v + run: | + set -euo pipefail + # inbuxa-v27.1-rc.11 -> 27.1-rc.11, the image tag. Only a release's tag. + [[ "$TAG" =~ ^inbuxa-v[0-9]{2,4}\.[0-9]+(\.[0-9]+){0,2}(-rc\.[0-9]+)?$ ]] || { echo "not a release tag: $TAG" >&2; exit 1; } + version="${TAG#inbuxa-v}" + echo "version=$version" >> "$GITHUB_OUTPUT" + echo "image=$REGISTRY/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT" + echo "image_ghcr=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT" + # The tagged commit, for the app's SBOM + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/tags/${{ inputs.tag }} + path: src + persist-credentials: false + - name: cosign and syft, checked against their pinned sums + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/bin"; cd "$RUNNER_TEMP" + curl -fsSL -o cosign "https://github.com/sigstore/cosign/releases/download/$COSIGN_VERSION/cosign-linux-amd64" + echo "$COSIGN_SHA256 cosign" | sha256sum -c - + install -m 0755 cosign bin/cosign + curl -fsSL -o syft.tar.gz "https://github.com/anchore/syft/releases/download/v$SYFT_VERSION/syft_${SYFT_VERSION}_linux_amd64.tar.gz" + echo "$SYFT_SHA256 syft.tar.gz" | sha256sum -c - + tar -xzf syft.tar.gz syft && install -m 0755 syft bin/syft + echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" + - run: | + cosign version | grep -i gitversion + syft version | grep -i '^version' + - uses: docker/setup-buildx-action@594f3bf4285d9ea8dc53c9a0c9c4092420091003 # v4.4.0 + - uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 + with: + registry: ${{ vars.REGISTRY }} + username: jcoffey-dev + password: ${{ secrets.GITEA_TOKEN }} + - if: ${{ env.GHCR == 'true' }} + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + + # Digests, read once: everything below is by digest, never by tag, + # so a tag moving mid-run can't change what is signed. + - id: d + env: + VERSION: ${{ steps.v.outputs.version }} + IMAGE: ${{ steps.v.outputs.image }} + run: | + set -euo pipefail + i="$(docker buildx imagetools inspect "$IMAGE:$VERSION" --format '{{json .Manifest.Digest}}' | tr -d '"')" + arch() { docker buildx imagetools inspect "$IMAGE@$i" --raw | + jq -r --arg a "$1" '.manifests[] | select(.platform.os == "linux" and .platform.architecture == $a) | .digest'; } + echo "index=$i" >> "$GITHUB_OUTPUT" + echo "amd64=$(arch amd64)" >> "$GITHUB_OUTPUT" + echo "arm64=$(arch arm64)" >> "$GITHUB_OUTPUT" + cat "$GITHUB_OUTPUT" + + - name: sign the index + env: + IMAGE: ${{ steps.v.outputs.image }}@${{ steps.d.outputs.index }} + IMAGE_GHCR: ${{ steps.v.outputs.image_ghcr }}@${{ steps.d.outputs.index }} + run: | + set -euo pipefail + signed=("$IMAGE") + cosign sign --yes "$IMAGE" + if [ "$GHCR" = true ]; then + # The GHCR copy is the same digest; a version GHCR never got is + # skipped, not failed + if docker buildx imagetools inspect "$IMAGE_GHCR" >/dev/null 2>&1; then + cosign sign --yes "$IMAGE_GHCR"; signed+=("$IMAGE_GHCR") + else echo "::warning::$IMAGE_GHCR isn't on GHCR; not signed there"; fi + fi + # Check at once, as a user would + for ref in "${signed[@]}"; do + cosign verify "$ref" \ + --certificate-oidc-issuer https://token.actions.githubusercontent.com \ + --certificate-identity-regexp "^https://github.com/$GITHUB_REPOSITORY/\.github/workflows/sign\.yml@refs/(tags/inbuxa-v.+|heads/main)$" \ + >/dev/null + echo "verified $ref" + done + + - name: SBOMs + env: + VERSION: ${{ steps.v.outputs.version }} + IMAGE: ${{ steps.v.outputs.image }} + AMD64: ${{ steps.d.outputs.amd64 }} + ARM64: ${{ steps.d.outputs.arm64 }} + run: | + set -euo pipefail + mkdir -p sbom + count() { echo "$1: $(jq '.packages | length' "$1") packages"; } + syft "registry:$IMAGE@$AMD64" --quiet -o "spdx-json=sbom/inbuxa-webmail-$VERSION-linux-amd64.spdx.json" + syft "registry:$IMAGE@$ARM64" --quiet -o "spdx-json=sbom/inbuxa-webmail-$VERSION-linux-arm64.spdx.json" + # The lockfile only: what npm ci installs for the build, the code + # bundled into the app included + syft "file:src/package-lock.json" --quiet --source-name inbuxa-webmail --source-version "$VERSION" \ + -o "spdx-json=sbom/inbuxa-webmail-$VERSION-app.spdx.json" + for f in sbom/*.spdx.json; do count "$f"; done + - uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4.2.2 + with: + subject-name: ${{ steps.v.outputs.image }} + subject-digest: ${{ steps.d.outputs.amd64 }} + sbom-path: sbom/inbuxa-webmail-${{ steps.v.outputs.version }}-linux-amd64.spdx.json + - uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4.2.2 + with: + subject-name: ${{ steps.v.outputs.image }} + subject-digest: ${{ steps.d.outputs.arm64 }} + sbom-path: sbom/inbuxa-webmail-${{ steps.v.outputs.version }}-linux-arm64.spdx.json + - uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4.2.2 + with: + subject-name: ${{ steps.v.outputs.image }} + subject-digest: ${{ steps.d.outputs.index }} + sbom-path: sbom/inbuxa-webmail-${{ steps.v.outputs.version }}-app.spdx.json + + # How it was built: only from the run that built it + - if: ${{ inputs.built-here == true }} + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-name: ${{ steps.v.outputs.image }} + subject-digest: ${{ steps.d.outputs.index }} + + - name: attach the SBOMs to the release on Gitea + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + run: | + set -euo pipefail + api="$GITEA_URL/api/v1/repos/$GITHUB_REPOSITORY" + rel="$(curl -fsS -H "Authorization: token $GITEA_TOKEN" "$api/releases/tags/$TAG")" + id="$(jq -r .id <<<"$rel")" + for f in sbom/*.spdx.json; do + name="$(basename "$f")" + if jq -e --arg n "$name" '.assets[]? | select(.name == $n)' <<<"$rel" >/dev/null; then + echo "$name is already on the release"; continue + fi + curl -fsS --http1.1 --retry 5 --retry-all-errors --retry-delay 15 -o /dev/null \ + -H "Authorization: token $GITEA_TOKEN" \ + -F "attachment=@$f;type=application/json" "$api/releases/$id/assets?name=$name" + echo "attached $name" + done