diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 526392d7..a9a87f75 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -135,10 +135,17 @@ jobs: # FSDK's kernel is most of a cold build # (docs/skills/ci-tooling.md#build-time-and-caches), and no upstream cache # holds it (our module certificate and kernel config patches change its - # key). This job builds it (and Go) with the committed release module certificate and no - # signing secrets, and pushes the key-free artifacts to the project CAS unless - # a remote already holds them; every build pulls them through project.conf - # (scripts/kernel-cache.sh). + # key). These jobs build it (and Go) with no signing secrets, each against + # one committed module certificate, and push the key-free artifacts to the + # project CAS unless a remote already holds them; every build pulls them + # through project.conf (scripts/kernel-cache.sh): + # - kernel-cache: the release certificate, for release builds; + # - kernel-cache-dev: the public INSECURE dev certificate that + # `just gen-dev-keys` uses (files/dev-keys/), for pull requests, the + # nightly build and local builds. No job needs it, so it never delays + # a release. + # The cache only saves time: a failed seed must not hold back the release, + # which checks build's and boot-test's own results. kernel-cache: needs: changes if: needs.changes.outputs.release == 'true' @@ -162,20 +169,13 @@ jobs: - name: Set up runner disk space run: bash scripts/ci-runner-disk.sh - - name: Stage the release module certificate - run: | - mkdir -p files/boot-keys/modules - cp files/release-keys/linux-module-cert.crt files/boot-keys/modules/ - - # The cache only saves time: a failure here must not hold back the - # release, which checks build's and boot-test's own results. - name: Build and push the kernel cache unless it exists id: seed continue-on-error: true env: CASD_CLIENT_CERT: ${{ vars.CASD_CLIENT_CERT }} CASD_CLIENT_KEY: ${{ secrets.CASD_CLIENT_KEY }} - run: bash scripts/kernel-cache.sh seed + run: bash scripts/kernel-cache.sh seed release # continue-on-error turns the job green; say so, or a cache that never # seeds goes unnoticed while every build compiles the kernel. @@ -186,6 +186,46 @@ jobs: echo "::warning title=Kernel cache not seeded::${msg}" echo "${msg}" >> "$GITHUB_STEP_SUMMARY" + kernel-cache-dev: + needs: changes + if: needs.changes.outputs.release == 'true' + # CASD_CLIENT_KEY lives in this environment, which only main may use. + environment: bst-cache + runs-on: ubuntu-26.04 + timeout-minutes: 240 + permissions: + contents: read + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Set up just + uses: taiki-e/install-action@e407f7bafb71fd004bc5c2da3032e5470cbb6ef0 # v2 + with: + tool: just@1.58.0 + + - name: Set up runner disk space + run: bash scripts/ci-runner-disk.sh + + - name: Build and push the dev kernel cache unless it exists + id: seed + continue-on-error: true + env: + CASD_CLIENT_CERT: ${{ vars.CASD_CLIENT_CERT }} + CASD_CLIENT_KEY: ${{ secrets.CASD_CLIENT_KEY }} + run: bash scripts/kernel-cache.sh seed dev + + # continue-on-error turns the job green; say so, or a cache that never + # seeds goes unnoticed while every build compiles the kernel. + - name: Report a failed seed + if: steps.seed.outcome == 'failure' + run: | + msg="The dev kernel cache seed failed; pull requests and the nightly build compile the kernel until a release seeds it. See the step above." + echo "::warning title=Kernel cache not seeded::${msg}" + echo "${msg}" >> "$GITHUB_STEP_SUMMARY" + build: needs: [changes, kernel-cache] # Runs when kernel-cache is skipped (non-release builds) or failed: the @@ -255,6 +295,8 @@ jobs: exit 1 fi cp files/release-keys/linux-module-cert.crt files/boot-keys/modules/linux-module-cert.crt + # Never the public INSECURE dev module pair (files/dev-keys/). + bash scripts/check-release-keys.sh printf '%s\n' "${SYSUPDATE_SIGNING_KEY}" > files/boot-keys/sysupdate-signing.asc cp files/os/sysupdate-keys/import-pubring.gpg files/boot-keys/import-pubring.pgp else diff --git a/AGENTS.md b/AGENTS.md index 2039b391..2f1fc164 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -51,7 +51,7 @@ All local `just` targets run BuildStream inside the FSDK `bst2` container via `j |---|---| | `just validate` | Merge-contract graph check — run this on every change. | | `just test-unit` | Unit tests (pytest + bats). | -| `just gen-dev-keys` | Generate throwaway Secure Boot, module, and image (`SHA256SUMS`) signing keys in `files/boot-keys/` (gitignored). | +| `just gen-dev-keys` | Generate throwaway Secure Boot and image (`SHA256SUMS`) signing keys in `files/boot-keys/` (gitignored), plus the committed public INSECURE dev module pair from `files/dev-keys/` (`--private-module-key` for a fresh one). | | `just set-version V` | Set `image-version` in `include/image.yml` (≤17 chars, increasing under strverscmp). | | `just build-image` / `just export-image` | Build and export the release image set to `dist/diskless/`. | | `just dogfood` / `just dogfood-check` | Boot `dist/diskless/` diskless in QEMU with Secure Boot (interactive / headless probe). | diff --git a/README.md b/README.md index f011b3c9..0add527a 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ See [`CONTRIBUTING.md`](CONTRIBUTING.md) for the contributor checklist, Conventi ## Security and release trust -- **Signed boot chain**: Secure Boot keys enroll from the ESP on first boot (`secure-boot-enroll if-safe` in VMs; on bare metal in firmware Setup Mode, from the USB installer's prompt or the systemd-boot menu); the USB installer refuses to install without Secure Boot on with these keys unless told explicitly. Local builds use throwaway keys from `just gen-dev-keys`. +- **Signed boot chain**: Secure Boot keys enroll from the ESP on first boot (`secure-boot-enroll if-safe` in VMs; on bare metal in firmware Setup Mode, from the USB installer's prompt or the systemd-boot menu); the USB installer refuses to install without Secure Boot on with these keys unless told explicitly. Local builds use throwaway Secure Boot keys from `just gen-dev-keys`, with the published INSECURE dev module signing key from `files/dev-keys/` (never in a release; see [`docs/skills/secure-boot-keys.md`](docs/skills/secure-boot-keys.md)). - **Signed manifests**: the build signs one combined `SHA256SUMS` over the whole image set (OS images, UKIs, sysexts) inside `oci/bluefin-server-image.bst`; a release publishes `dist/diskless/` as-is to GitHub Releases and as an OCI artifact. - **Sysupdate verification**: installed nodes verify updates against the signed manifest (`Verify=yes`), and the diskless pull checks the same signature in the initrd; see [`docs/skills/systemd-sysupdate-verification.md`](docs/skills/systemd-sysupdate-verification.md) for details. - **Provenance and SBOM**: releases carry SLSA provenance and SPDX SBOM attestations; see [`docs/skills/systemd-sysupdate-verification.md`](docs/skills/systemd-sysupdate-verification.md) for verification. diff --git a/docs/skills/ci-tooling.md b/docs/skills/ci-tooling.md index 67b438da..3db32414 100644 --- a/docs/skills/ci-tooling.md +++ b/docs/skills/ci-tooling.md @@ -170,7 +170,7 @@ one of these environments is refused before its first step. | Environment | Secrets | Jobs | |---|---|---| | `release` | `BOOT_KEYS_TARBALL`, `SYSUPDATE_SIGNING_KEY` | `build`, only when `changes` says `release=true` | -| `bst-cache` | `CASD_CLIENT_KEY` (`CASD_CLIENT_CERT` is a repository variable) | `kernel-cache` (releases only) | +| `bst-cache` | `CASD_CLIENT_KEY` (`CASD_CLIENT_CERT` is a repository variable) | `kernel-cache`, `kernel-cache-dev` (releases only) | | `trackers` | `MERGERAPTOR_APP_ID`, `MERGERAPTOR_PRIVATE_KEY` | `track-junctions`, `track-binaries`' `propose` | `build` serves releases and every other build, so it names the environment @@ -350,11 +350,19 @@ version) and the NVIDIA Container Toolkit (Go); the driver sysext adds about so they can be made required without `paths-ignore` leaving them pending. The unit, docs and lint workflows have no `paths:` filter for the same reason. -- **Kernel cache in the project CAS, key-free by construction.** The - `kernel-cache` job (releases only) runs `scripts/kernel-cache.sh seed`: with - no signing secrets and the committed release module certificate - (`files/release-keys/linux-module-cert.crt`) staged as - `files/boot-keys/modules/linux-module-cert.crt`, it builds FSDK's +- **Kernel cache in the project CAS, key-free by construction.** Two jobs, + on releases only, run `scripts/kernel-cache.sh seed`, one per module + certificate the kernel can trust (it is part of the kernel's cache key): + - `kernel-cache` runs `seed release` with the committed release certificate + (`files/release-keys/linux-module-cert.crt`); `build` waits for it. + - `kernel-cache-dev` runs `seed dev` with the public INSECURE dev + certificate (`files/dev-keys/INSECURE-dev-module-key.crt`) that + `just gen-dev-keys` uses for every non-release build. No job waits for + it, so it never delays a release. + + With no signing secrets and that one certificate staged as the only file of + `files/boot-keys/modules/` (`seed` refuses anything else there: that + directory's import is pushed with the kernel), each builds FSDK's `components/linux.bst` and `components/go.bst` with a BuildStream config that pushes artifacts and sources to `cache.projectbluefin.io:11002`, unless `bst artifact show` reports both `available` on a remote. The push @@ -369,7 +377,9 @@ version) and the NVIDIA Container Toolkit (Go); the driver sysext adds about `patches/freedesktop-sdk/0001-project.conf-Add-GNOME-CAS-servers.patch`. Release builds normalize `BOOT_KEYS_TARBALL`'s module certificate to the committed bytes after checking it is the same certificate (and stop if not), so the keys - match. The CAS is publicly readable: `seed` refuses if + match, and run `scripts/check-release-keys.sh`, which fails the build if + the module certificate or key is the dev pair; `seed release` runs it too. + The CAS is publicly readable: `seed` refuses if `bluefin-server/keys/boot-keys.bst` is anywhere in the graph it builds, and the job holds no signing secret. A content grep is no guard here: FSDK sources (Go's TLS test data and others) carry 307 PEM private keys. The @@ -391,9 +401,12 @@ version) and the NVIDIA Container Toolkit (Go); the driver sysext adds about `patches/` (an untested FSDK bump would otherwise merge unbuilt); the rest run `validate`. A regression outside those paths shows up in the next `main` build or the nightly build, both of which block nothing but the - release that contains it. When a pull request does build, it still builds - the kernel: `just gen-dev-keys` makes a new module certificate on every - run, so the PR kernel's cache key never matches anything cached. + release that contains it. When a pull request or the nightly build does + build, it pulls the kernel that `kernel-cache-dev` seeded: `just + gen-dev-keys` always stages the same public dev module certificate, so + the dev kernel's cache key is the same on every run. It compiles the + kernel (1 h 43 min) only until the next release after a kernel change + seeds it. - **No `actions/cache`.** A full build's cache holds `boot-keys.bst`, and pull requests can restore caches saved on `main`; the key-free kernel cache lives in the project CAS instead. diff --git a/docs/skills/ddi-installer-build.md b/docs/skills/ddi-installer-build.md index f9020a7e..bd299c7a 100644 --- a/docs/skills/ddi-installer-build.md +++ b/docs/skills/ddi-installer-build.md @@ -22,7 +22,7 @@ inside the FSDK `bst2` container (`just bst`); it is not installed locally. ```bash just validate # version invariants + resolve the shipped element graphs just test-unit # pytest + bats -just gen-dev-keys # throwaway Secure Boot + module keys in files/boot-keys/ +just gen-dev-keys # throwaway Secure Boot keys + the public dev module key in files/boot-keys/ just set-version V # set image-version in include/image.yml (<=17 chars, # increasing under strverscmp) just build-image # build oci/bluefin-server-image.bst @@ -59,7 +59,9 @@ Every image build signs: the UKIs and systemd-boot with DB, kernel modules with the module signing certificate, and the release `SHA256SUMS` with the image signing key. What each key is, where it lives, what `just gen-dev-keys` generates (throwaway keys in the gitignored `files/boot-keys/`, kept unless -`--force`, a partial set is an error), and how CI supplies the real keys: +`--force`, a partial set is an error, the module pair is the public +INSECURE dev pair so the kernel comes from the cache), and how CI supplies +the real keys: [secure-boot-keys.md](secure-boot-keys.md). The release-signing half (`sysupdate-signing.asc` / `import-pubring.pgp`, the committed `files/os/sysupdate-keys/import-pubring.gpg`, and rotation): @@ -243,7 +245,8 @@ nodes may already trust. (the build fails the check in `bluefin-server-boot.bst`). - A new release asset that is not added to `SHA256SUMS` in `oci/bluefin-server-image.bst`. -- Keys committed anywhere outside the gitignored `files/boot-keys/`. +- Keys committed anywhere outside the gitignored `files/boot-keys/`, other + than the public INSECURE dev module pair in `files/dev-keys/`. ## Verification diff --git a/docs/skills/secure-boot-keys.md b/docs/skills/secure-boot-keys.md index 25471b16..ddc312f3 100644 --- a/docs/skills/secure-boot-keys.md +++ b/docs/skills/secure-boot-keys.md @@ -4,7 +4,7 @@ description: Secure Boot and image signing key management for Bluefin Server. Lo metadata: type: reference status: stable - last_updated: "2026-09-29" + last_updated: "2026-10-04" --- # Secure Boot Key Management @@ -32,12 +32,45 @@ built against it, and release builds stop if it differs from the one in ```bash just gen-dev-keys # throwaway keys in files/boot-keys/ (gitignored) -just gen-dev-keys --force # rotate (rebuilds the kernel) +just gen-dev-keys --force # rotate the Secure Boot and image signing keys +just gen-dev-keys --force --private-module-key # also a fresh module key ``` `just validate` and `just build-image` depend on `gen-dev-keys`, so a fresh clone builds out of the box. +### The INSECURE dev module key + +The module signing pair of a `gen-dev-keys` key set is not generated: it is +the committed, public pair in `files/dev-keys/` +(`INSECURE-dev-module-key.{pem,crt}`; see its `README.md`). The module +certificate is part of the kernel's cache key, so with one fixed dev +certificate every non-release build (pull requests, the nightly build, local +builds) pulls the kernel that `scripts/kernel-cache.sh seed dev` pushed on +the last release instead of compiling it (see "Build time and caches" in +[ci-tooling.md](ci-tooling.md)). + +A key set generated before the dev key existed keeps its own module +certificate, so the kernel still compiles; `just gen-dev-keys` says so, and +`just gen-dev-keys --force` switches it to the dev key. + +- **Public on purpose.** Anyone can sign a module that a dev kernel loads + under lockdown. Dev images are also signed with throwaway Secure Boot keys + and never published. For an image you boot on hardware you care about, + use `--private-module-key` (the kernel then builds locally). +- **Never in a release.** Release builds install `BOOT_KEYS_TARBALL` and run + `scripts/check-release-keys.sh`, which fails when the module certificate or + key is the dev pair (compared by public key); `kernel-cache.sh seed + release` runs it too. The release kernel trusts only + `files/release-keys/linux-module-cert.crt`, so dev-signed modules do not + load on it. +- **Never in an artifact.** FSDK's kernel reads only + `/keys/linux-module-cert.crt` (`SYSTEM_TRUSTED_KEYS`, `MODULE_SIG_KEY ""`, + no `MODULE_SIG_ALL`): the private key is used only by + `bluefin-server/kernel-modules.bst` and the other signing elements through + `bluefin-server/keys/boot-keys.bst`, which is never pushed. No element + stages `files/dev-keys/` (`tests/unit/test_dev_module_key.py`). + ## CI secrets On `main`, the workflow unpacks two secrets from the `release` environment, @@ -49,13 +82,15 @@ which only `main` may use (see "Environments and secrets" in - `SYSUPDATE_SIGNING_KEY` — the ASCII-armored secret key written to `files/boot-keys/sysupdate-signing.asc`. -Pull requests get throwaway keys from `just gen-dev-keys`; they never see the -real secrets. +Pull requests get throwaway keys from `just gen-dev-keys` (with the public +dev module pair); they never see the real secrets. ## Key rotation -1. Generate new keys (`just gen-dev-keys --force` locally, or the equivalent - in a secure environment for production). +1. Generate new keys (`just gen-dev-keys --force --private-module-key` + locally, or the equivalent in a secure environment for production). Plain + `--force` installs the public INSECURE dev module key, which + `scripts/check-release-keys.sh` rejects for release builds. 2. Update the CI secrets (`BOOT_KEYS_TARBALL`, `SYSUPDATE_SIGNING_KEY`), and commit the new public module certificate as `files/release-keys/linux-module-cert.crt` in the same change. @@ -72,7 +107,9 @@ new kernel build is mandatory. Plan kernel rebuilds into the rotation window. - [ ] `just validate` passes after key generation. - [ ] `sbverify --cert files/boot-keys/DB.crt ` passes on built UKIs. - [ ] `gpgv --keyring files/boot-keys/import-pubring.pgp SHA256SUMS.gpg SHA256SUMS` passes. -- [ ] No private keys are committed outside the gitignored `files/boot-keys/`. +- [ ] No private keys are committed outside the gitignored `files/boot-keys/`, + except the public INSECURE dev module key in `files/dev-keys/`. +- [ ] `bash scripts/check-release-keys.sh` passes on a release key set. ## See also diff --git a/files/dev-keys/INSECURE-dev-module-key.crt b/files/dev-keys/INSECURE-dev-module-key.crt new file mode 100644 index 00000000..9b2013fd --- /dev/null +++ b/files/dev-keys/INSECURE-dev-module-key.crt @@ -0,0 +1,32 @@ +-----BEGIN CERTIFICATE----- +MIIFgzCCA2ugAwIBAgIUeo+QMr5qJbMHs5lR0LBgutFsWR0wDQYJKoZIhvcNAQEN +BQAwPzE9MDsGA1UEAww0Qmx1ZWZpbiBTZXJ2ZXIgSU5TRUNVUkUgcHVibGljIGRl +diBrZXJuZWwgbW9kdWxlIGtleTAgFw0yNjEwMDUwMTEwMDFaGA8yMTI2MDkxMTAx +MTAwMVowPzE9MDsGA1UEAww0Qmx1ZWZpbiBTZXJ2ZXIgSU5TRUNVUkUgcHVibGlj +IGRldiBrZXJuZWwgbW9kdWxlIGtleTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCC +AgoCggIBALNIdFHp9VUhjTxW0PmxC8rXN6Q3qw3UScQmIoOX8aUEJJeWQi9BDNol +VMRbc24bPyMnYk2BvvGSlPkFVRhcLb9tje/Xq5ApdV3BEzvm6hEPrDA3oH7TAgI6 +D+pXaJ7eY1ikTixdD0+zGYx+v2aCBdgCtiMpRpYDrh7lb4KRMYWqra1KDCG9RO9w +GKKprhgVcP1jdJE9gW/hF24o7rXkyWS0M5W3MGsrcVDcvQp3jAiX5NTh4nI8ypIk +GF0NXq40z57iu6GytqWDimEUVgTWfB8G05TkNtj1pr7326RTKSFyygwD/m4R6eT5 +Epbqjl1DijkmmUCgDzWkab8yjB+tYLmsBi/W8rXk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DGKPKg0hwUsenQiI42XI65qz49dRxs81D+34N9s/RjK2Jsi/KbUq +QxWjvVzPLHEYCuOH78cBHvugV8HRU1ax08tQxk/JmoWRi02NguAas1bQYukZQLB2 +0zwcO8VeO1QxYIFng3lnbDERj3YkFl0= +-----END CERTIFICATE----- diff --git a/files/dev-keys/INSECURE-dev-module-key.pem b/files/dev-keys/INSECURE-dev-module-key.pem new file mode 100644 index 00000000..5ef87c47 --- /dev/null +++ b/files/dev-keys/INSECURE-dev-module-key.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCzSHRR6fVVIY08 +VtD5sQvK1zekN6sN1EnEJiKDl/GlBCSXlkIvQQzaJVTEW3NuGz8jJ2JNgb7xkpT5 +BVUYXC2/bY3v16uQKXVdwRM75uoRD6wwN6B+0wICOg/qV2ie3mNYpE4sXQ9PsxmM +fr9mggXYArYjKUaWA64e5W+CkTGFqq2tSgwhvUTvcBiiqa4YFXD9Y3SRPYFv4Rdu +KO615MlktDOVtzBrK3FQ3L0Kd4wIl+TU4eJyPMqSJBhdDV6uNM+e4ruhsralg4ph +FFYE1nwfBtOU5DbY9aa+99ukUykhcsoMA/5uEenk+RKW6o5dQ4o5JplAoA81pGm/ +MowfrWC5rAYv1vK15K1XTCmZwUKK5d0fT0SftOZuPI9Cuo2tXP8eOyESILekTES9 +g/gCicsgT6pWmushoWAXRV+UwGlXFS2Qqon0fD0qRauiu7scz2E8QbUA2YiraeRn +Y6QWv31K8OP+xCXKgQueoDZKbEC2MX5tGDMZA/cMPoAQJsxPas6FpK7nxtklHjoG +K6QZhnbqBCDWjSNFFR4vcD18uRlllUpRS02JAtPFLOxjvrGUtdWvkjJdaRBngOX+ +4k3KSGYtqXFk8gkFp6ykaqcTavNyDe5h1y0E4LkhBFtyJiYbjTHHQSrZImyoD9W8 +Zl1b4LleoPyRc0AslbkZiv3OIGJtFQIDAQABAoIB/0qTl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Y9uOwFGJ8e6yzCaBXfXhpUWBNoaPXsu0 +VGRnTTtKRyYjfg/+y/fGhSRqe672QGAhjNPcUvus2RHYEvk3vbqk5CBK0GCyHeQq +ECGQKg7PLz9i1kNzgmWopU1daSpmOvpdIQr9dFDWSXhVZeTlgH1OTKmaS8ZoMN5m +hnhvzbSsh2wXSWZl79mr58T1+ftDQ108YQ+MgNw/OU+Am+TQm+Ge/lxdJdmx0QJJ +wyzrbZ6+2ruUS0pGSiiCzrBpYHCraKSGGxgtiL5di1VJ0ezAF6o9gJUVI0sMBEpz +GJb2YfKGUjFqnqZ73Mnru4DQAPClEA== +-----END PRIVATE KEY----- diff --git a/files/dev-keys/README.md b/files/dev-keys/README.md new file mode 100644 index 00000000..11f3cad0 --- /dev/null +++ b/files/dev-keys/README.md @@ -0,0 +1,33 @@ +# INSECURE dev kernel module key — public, non-release builds only + +`INSECURE-dev-module-key.pem` is a **published private key**. Anyone can sign +a kernel module with it. Never trust it on a machine you care about. + +What it is for: `just gen-dev-keys` (`scripts/gen-dev-keys.sh`) copies this +pair into `files/boot-keys/` as the module signing key of throwaway key sets +(pull requests, the nightly build, local builds). The kernel's cache key +depends on the module certificate it trusts, so a fixed dev certificate lets +those builds pull a kernel that `scripts/kernel-cache.sh seed dev` built once, +instead of compiling FSDK's kernel on every run. + +What keeps it out of releases: + +- Release builds install the project keys from `BOOT_KEYS_TARBALL` and run + `scripts/check-release-keys.sh`, which fails the build when the staged + module certificate or module key is this pair. +- `scripts/kernel-cache.sh seed release` runs the same check. +- The release kernel trusts only `files/release-keys/linux-module-cert.crt`, + so modules signed with this key do not load on it under lockdown. + +What a dev image trusts: its kernel trusts this certificate, so under +lockdown anyone with root on a dev image can load a module signed with this +key. Dev images are also signed with throwaway Secure Boot keys and are never +published. For a local image you boot on real hardware, generate a private +module key: `just gen-dev-keys --force --private-module-key` (that kernel is +then built locally). + +Only the certificate reaches the kernel build (through +`bluefin-server/keys/linux-module-cert.bst`, which imports +`files/boot-keys/modules/`). The key reaches only the signing elements +through `bluefin-server/keys/boot-keys.bst`, which no pushed artifact +contains. Details: `docs/skills/secure-boot-keys.md`. diff --git a/scripts/check-release-keys.sh b/scripts/check-release-keys.sh new file mode 100755 index 00000000..c60f2ff5 --- /dev/null +++ b/scripts/check-release-keys.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Refuse a release key set that carries the committed INSECURE dev module key +# (files/dev-keys/, published so that non-release builds share one kernel +# cache key). A release kernel trusting that certificate would load any module +# anyone signs with the public key; a release signed with that key would ship +# modules only dev kernels accept. +# +# check-release-keys.sh [KEY_DIR] (default: files/boot-keys) +# +# A relative KEY_DIR is resolved against the repository root, not the +# caller's working directory. +# +# Compares public keys, so a certificate re-issued for the dev key is caught +# too. The module key is checked when KEY_DIR has one (release builds); the +# kernel cache seed stages only the certificate. +set -euo pipefail + +cd "$(dirname "$0")/.." +dir="${1:-files/boot-keys}" +dev_cert=files/dev-keys/INSECURE-dev-module-key.crt + +die() { echo "check-release-keys: $*" >&2; exit 1; } + +dev_pub="$(openssl x509 -in "${dev_cert}" -noout -pubkey)" || die "cannot read ${dev_cert}" +cert="${dir}/modules/linux-module-cert.crt" +[ -s "${cert}" ] || die "missing ${cert}" +cert_pub="$(openssl x509 -in "${cert}" -noout -pubkey 2>/dev/null)" || die "${cert} is not a certificate" +[ "${cert_pub}" != "${dev_pub}" ] \ + || die "${cert} is the INSECURE dev module certificate; release builds must never trust it" + +key="${dir}/linux-module-cert.key" +if [ -e "${key}" ]; then + key_pub="$(openssl pkey -in "${key}" -pubout 2>/dev/null)" || die "${key} is not a private key" + [ "${key_pub}" != "${dev_pub}" ] \ + || die "${key} is the INSECURE dev module key; release builds must never sign with it" +fi +echo "check-release-keys: the module certificate and key in ${dir} are not the dev pair" diff --git a/scripts/gen-dev-keys.sh b/scripts/gen-dev-keys.sh index eecb8376..9a39400c 100644 --- a/scripts/gen-dev-keys.sh +++ b/scripts/gen-dev-keys.sh @@ -4,6 +4,15 @@ # systemd-boot with DB and kernel modules with linux-module-cert; the # firmware enrolls PK/KEK/DB from the ESP on first boot. # +# The module signing pair is the committed, public INSECURE dev pair in +# files/dev-keys/ (see its README.md): every non-release build then trusts +# the same certificate, so the kernel's cache key is stable and builds pull +# the kernel that `scripts/kernel-cache.sh seed dev` pushed instead of +# compiling it. --private-module-key generates a fresh pair instead, for an +# image you boot on hardware you care about (the kernel then builds locally). +# Release builds never run this: CI installs the project keys, and +# scripts/check-release-keys.sh refuses the dev pair. +# # Layout (matches freedesktop-sdk's files/boot-keys convention): # files/boot-keys/{PK,KEK,DB}.{key,crt} # files/boot-keys/linux-module-cert.key private, never staged into the kernel build @@ -15,15 +24,24 @@ # Existing keys are never overwritten without --force: every key is baked # into or signs the image, so replacing one needs a new image-version (see # "Keys" in docs/skills/ddi-installer-build.md), and changing -# modules/linux-module-cert.crt also forces a kernel rebuild. A partial set +# modules/linux-module-cert.crt changes the kernel's cache key. A partial set # (some files of a pair or of the boot set missing) is an error rather than # something to fill in, since filling it in would replace the files that do # exist. set -euo pipefail -dir="$(cd "$(dirname "$0")/.." && pwd)/files/boot-keys" +root="$(cd "$(dirname "$0")/.." && pwd)" +dir="${root}/files/boot-keys" +dev_keys="${root}/files/dev-keys" force=0 -[ "${1:-}" = "--force" ] && force=1 +private_module_key=0 +for arg in "$@"; do + case "${arg}" in + --force) force=1 ;; + --private-module-key) private_module_key=1 ;; + *) echo "usage: $0 [--force] [--private-module-key]" >&2; exit 2 ;; + esac +done mkdir -p "${dir}/modules" chmod 0700 "${dir}" @@ -67,6 +85,12 @@ if [ "${force}" = 0 ]; then exit 1 fi done + if [ "${private_module_key}" = 1 ] && [ "${boot_state}" = all ] \ + && cmp -s "${dir}/modules/linux-module-cert.crt" "${dev_keys}/INSECURE-dev-module-key.crt"; then + echo "ERROR: ${dir} already holds the INSECURE dev module key; --private-module-key only replaces it with --force." >&2 + echo " Pass --force --private-module-key to regenerate every key with a private module key (then bump image-version)." >&2 + exit 1 + fi fi if [ "${signing_state}" != all ] || [ "${force}" = 1 ]; then @@ -74,7 +98,11 @@ if [ "${signing_state}" != all ] || [ "${force}" = 1 ]; then fi if [ "${boot_state}" = all ] && [ "${force}" = 0 ]; then - echo "Keys already exist in ${dir}; pass --force to regenerate (rebuilds the kernel)." + echo "Keys already exist in ${dir}; pass --force to regenerate." + if ! cmp -s "${dir}/modules/linux-module-cert.crt" "${dev_keys}/INSECURE-dev-module-key.crt"; then + echo " The module certificate is not the INSECURE dev one, so the kernel builds locally;" + echo " pass --force to switch to the dev key and pull the cached dev kernel." + fi exit 0 fi @@ -84,11 +112,19 @@ for name in PK KEK DB; do -keyout "${dir}/${name}.key" -out "${dir}/${name}.crt" 2>/dev/null done -openssl req -new -x509 -newkey rsa:4096 -nodes -sha512 -days 3650 \ - -subj "/CN=Bluefin Server dev kernel modules (${owner})/" \ - -addext "keyUsage=digitalSignature" \ - -addext "extendedKeyUsage=codeSigning" \ - -keyout "${dir}/linux-module-cert.key" -out "${dir}/modules/linux-module-cert.crt" 2>/dev/null +if [ "${private_module_key}" = 1 ]; then + openssl req -new -x509 -newkey rsa:4096 -nodes -sha512 -days 3650 \ + -subj "/CN=Bluefin Server dev kernel modules (${owner})/" \ + -addext "keyUsage=digitalSignature" \ + -addext "extendedKeyUsage=codeSigning" \ + -keyout "${dir}/linux-module-cert.key" -out "${dir}/modules/linux-module-cert.crt" 2>/dev/null + echo "Generated a private module signing key (the kernel builds locally)" +else + install -m 0600 "${dev_keys}/INSECURE-dev-module-key.pem" "${dir}/linux-module-cert.key" + install -m 0644 "${dev_keys}/INSECURE-dev-module-key.crt" "${dir}/modules/linux-module-cert.crt" + echo "Module signing key: the public INSECURE dev key (files/dev-keys/README.md);" + echo " pass --force --private-module-key for an image you boot on real hardware." +fi chmod 0644 "${dir}"/*.crt "${dir}/modules/linux-module-cert.crt" echo "Generated dev keys in ${dir}" diff --git a/scripts/kernel-cache.sh b/scripts/kernel-cache.sh index a8daed57..e35d6d2f 100755 --- a/scripts/kernel-cache.sh +++ b/scripts/kernel-cache.sh @@ -1,16 +1,23 @@ #!/usr/bin/env bash # Key-free BuildStream cache of FSDK's kernel (and Go), in the project CAS. # -# CASD_CLIENT_CERT= CASD_CLIENT_KEY= kernel-cache.sh seed +# CASD_CLIENT_CERT= CASD_CLIENT_KEY= kernel-cache.sh seed release|dev # # The kernel is most of a cold build (figures: docs/skills/ci-tooling.md, # "Build time and caches") and no upstream cache holds it: its cache key # depends on the module certificate it trusts (the # linux-module-cert junction override) and on the patches in # patches/freedesktop-sdk/ that edit its config (0006 kubenet, 0007 watchdog). -# Both the seed job and release builds stage the committed release -# certificate files/release-keys/linux-module-cert.crt as -# files/boot-keys/modules/linux-module-cert.crt, so the keys match. +# `seed` stages one committed certificate as +# files/boot-keys/modules/linux-module-cert.crt, the only file the kernel's +# linux-module-cert override imports: +# - release: files/release-keys/linux-module-cert.crt, which release builds +# also stage, so their kernel keys match. scripts/check-release-keys.sh +# must pass. +# - dev: the public files/dev-keys/INSECURE-dev-module-key.crt, which +# `just gen-dev-keys` uses for pull requests, the nightly build and local +# builds, so they pull the kernel instead of compiling it. Only the +# certificate is staged; its private key never enters this graph. # # `seed` builds ELEMENTS with a BuildStream config that pushes artifacts and # sources to the CAS's mTLS endpoint; every build reads them back through the @@ -21,9 +28,11 @@ # through `bst build`, which pushes it). # # The CAS is publicly readable. What `seed` pushes is key-free by -# construction: the seed job holds no signing secrets, and `seed` refuses to +# construction: the seed job holds no signing secrets, `seed` refuses to # build if bluefin-server/keys/boot-keys.bst (the only element that stages -# private keys) is anywhere in the graph of ELEMENTS. +# private keys) is anywhere in the graph of ELEMENTS, and it refuses a +# files/boot-keys/modules/ that holds anything but the one certificate (that +# directory's import is pushed with the kernel). set -euo pipefail ELEMENTS=(freedesktop-sdk.bst:components/linux.bst freedesktop-sdk.bst:components/go.bst) @@ -60,14 +69,35 @@ EOF done } +# Stage the certificate the kernel of VARIANT trusts, and nothing else. +stage_cert() { + local variant="$1" src modules=files/boot-keys/modules extra + case "${variant}" in + release) src=files/release-keys/linux-module-cert.crt ;; + dev) src=files/dev-keys/INSECURE-dev-module-key.crt ;; + esac + mkdir -p "${modules}" + install -m 0644 "${src}" "${modules}/linux-module-cert.crt" + extra="$(find "${modules}" -mindepth 1 ! -path "${modules}/linux-module-cert.crt")" + if [ -n "${extra}" ] || grep -q 'PRIVATE KEY' "${modules}/linux-module-cert.crt"; then + echo "kernel-cache: ${modules} must hold only the certificate; refusing to publish:" >&2 + printf '%s\n' "${extra}" >&2 + exit 1 + fi + if [ "${variant}" = release ]; then + bash scripts/check-release-keys.sh + fi +} + # The credentials directory; global, as the EXIT trap removes it after seed # returns. auth="" seed() { - local states available + local variant="$1" states available : "${CASD_CLIENT_CERT:?kernel-cache: CASD_CLIENT_CERT is not set}" : "${CASD_CLIENT_KEY:?kernel-cache: CASD_CLIENT_KEY is not set}" + stage_cert "${variant}" if bst_show show --deps all --format "'%{name}'" "${ELEMENTS[@]}" | grep -qxF "${KEYS_ELEMENT}"; then echo "kernel-cache: ${KEYS_ELEMENT} is in the graph of ${ELEMENTS[*]}; refusing to publish" >&2 exit 1 @@ -91,14 +121,14 @@ seed() { fi available="$(grep -cE '^ *available ' <<< "${states}" || true)" if [ "${available}" -eq "${#ELEMENTS[@]}" ]; then - echo "kernel-cache: ${ELEMENTS[*]} already on a remote" + echo "kernel-cache: ${variant}: ${ELEMENTS[*]} already on a remote" return 0 fi just bst build "${ELEMENTS[@]}" - echo "kernel-cache: pushed ${ELEMENTS[*]} to ${PUSH_URL}" + echo "kernel-cache: ${variant}: pushed ${ELEMENTS[*]} to ${PUSH_URL}" } -case "${1:-}" in - seed) seed ;; - *) echo "usage: $0 seed" >&2; exit 2 ;; +case "${1:-} ${2:-}" in + "seed release" | "seed dev") seed "$2" ;; + *) echo "usage: $0 seed release|dev" >&2; exit 2 ;; esac diff --git a/tests/unit/test_build_modes.py b/tests/unit/test_build_modes.py index ee42aa3e..f95f5dca 100644 --- a/tests/unit/test_build_modes.py +++ b/tests/unit/test_build_modes.py @@ -86,7 +86,7 @@ def test_signing_secrets_and_publishing_are_release_only() -> None: for step in job.get("steps", []): for key, value in (step.get("env") or {}).items(): if "secrets." in str(value) and "GITHUB_TOKEN" not in str(value): - if name == "kernel-cache": + if name in ("kernel-cache", "kernel-cache-dev"): # The CAS push key, in a job that only runs for # releases and holds no signing secret. assert (key, value) == ("CASD_CLIENT_KEY", "${{ secrets.CASD_CLIENT_KEY }}") diff --git a/tests/unit/test_ci_runner_disk.py b/tests/unit/test_ci_runner_disk.py index 70205e03..3f4f6e9e 100644 --- a/tests/unit/test_ci_runner_disk.py +++ b/tests/unit/test_ci_runner_disk.py @@ -21,7 +21,7 @@ def test_runner_disk_setup_is_not_inlined(workflow: str) -> None: def test_every_build_job_sets_up_the_runner_disk() -> None: - for workflow, job in (("build.yml", "kernel-cache"), ("build.yml", "build"), + for workflow, job in (("build.yml", "kernel-cache"), ("build.yml", "kernel-cache-dev"), ("build.yml", "build"), ("reproducibility.yml", "reproducibility")): steps = yaml.safe_load((WORKFLOWS / workflow).read_text())["jobs"][job]["steps"] assert any(s.get("run") == "bash scripts/ci-runner-disk.sh" for s in steps), f"{workflow}:{job}" diff --git a/tests/unit/test_dev_module_key.py b/tests/unit/test_dev_module_key.py new file mode 100644 index 00000000..76eb8f29 --- /dev/null +++ b/tests/unit/test_dev_module_key.py @@ -0,0 +1,148 @@ +"""The committed INSECURE dev module key never reaches a release. + +files/dev-keys/ publishes a module signing pair so that every non-release +build trusts one certificate and pulls one cached kernel. Release builds must +refuse it: scripts/check-release-keys.sh fails on the dev certificate, on the +dev private key and on a certificate re-issued for that key; build.yml runs it +on every release key set; the committed release certificate is not the dev +one; and no element stages files/dev-keys/, so the published private key ships +in no artifact. +""" + +from __future__ import annotations + +import shutil +import subprocess +from pathlib import Path + +import pytest +import yaml + +ROOT = Path(__file__).resolve().parents[2] +DEV_KEYS = ROOT / "files" / "dev-keys" +DEV_CERT = DEV_KEYS / "INSECURE-dev-module-key.crt" +DEV_KEY = DEV_KEYS / "INSECURE-dev-module-key.pem" +RELEASE_CERT = ROOT / "files" / "release-keys" / "linux-module-cert.crt" +WORKFLOW = yaml.safe_load((ROOT / ".github" / "workflows" / "build.yml").read_text()) + + +def pubkey(*args: str) -> str: + return subprocess.run(["openssl", *args], check=True, capture_output=True, text=True).stdout + + +def new_pair(directory: Path, key: Path | None = None) -> None: + """A module pair in directory; with `key`, a new certificate for that key.""" + (directory / "modules").mkdir(parents=True, exist_ok=True) + key_args = ["-key", str(key)] if key else ["-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256", "-nodes", + "-keyout", str(directory / "linux-module-cert.key")] + subprocess.run( + ["openssl", "req", "-new", "-x509", "-days", "1", "-subj", "/CN=test/", *key_args, + "-out", str(directory / "modules" / "linux-module-cert.crt")], + check=True, capture_output=True, + ) + + +@pytest.fixture +def checkout(tmp_path: Path) -> Path: + (tmp_path / "scripts").mkdir() + shutil.copy(ROOT / "scripts" / "check-release-keys.sh", tmp_path / "scripts") + shutil.copytree(DEV_KEYS, tmp_path / "files" / "dev-keys") + return tmp_path + + +def check(checkout: Path, key_dir: str = "files/boot-keys") -> subprocess.CompletedProcess: + return subprocess.run(["bash", str(checkout / "scripts" / "check-release-keys.sh"), key_dir], + capture_output=True, text=True) + + +def test_a_project_key_set_passes(checkout: Path) -> None: + new_pair(checkout / "files" / "boot-keys") + result = check(checkout) + assert result.returncode == 0, result.stderr + + +def test_the_release_certificate_alone_passes(checkout: Path) -> None: + # The kernel cache seed stages only the certificate. + modules = checkout / "files" / "boot-keys" / "modules" + modules.mkdir(parents=True) + shutil.copy(RELEASE_CERT, modules / "linux-module-cert.crt") + result = check(checkout) + assert result.returncode == 0, result.stderr + + +def test_the_dev_certificate_fails(checkout: Path) -> None: + keys = checkout / "files" / "boot-keys" + new_pair(keys) + shutil.copy(DEV_CERT, keys / "modules" / "linux-module-cert.crt") + result = check(checkout) + assert result.returncode != 0 + assert "INSECURE dev module certificate" in result.stderr + + +def test_the_dev_private_key_fails(checkout: Path) -> None: + keys = checkout / "files" / "boot-keys" + new_pair(keys) + shutil.copy(DEV_KEY, keys / "linux-module-cert.key") + result = check(checkout) + assert result.returncode != 0 + assert "INSECURE dev module key" in result.stderr + + +def test_a_certificate_reissued_for_the_dev_key_fails(checkout: Path) -> None: + keys = checkout / "files" / "boot-keys" + new_pair(keys, key=DEV_KEY) + assert (keys / "modules" / "linux-module-cert.crt").read_bytes() != DEV_CERT.read_bytes() + (keys / "linux-module-cert.key").unlink(missing_ok=True) + result = check(checkout) + assert result.returncode != 0 + assert "INSECURE dev module certificate" in result.stderr + + +@pytest.mark.parametrize("content", [None, b"", b"not a certificate\n"]) +def test_a_missing_or_broken_certificate_fails(checkout: Path, content: bytes | None) -> None: + modules = checkout / "files" / "boot-keys" / "modules" + modules.mkdir(parents=True) + if content is not None: + (modules / "linux-module-cert.crt").write_bytes(content) + assert check(checkout).returncode != 0 + + +def test_the_committed_pairs() -> None: + dev = pubkey("x509", "-in", str(DEV_CERT), "-noout", "-pubkey") + assert dev == pubkey("pkey", "-in", str(DEV_KEY), "-pubout"), "the dev certificate is for the dev key" + assert dev != pubkey("x509", "-in", str(RELEASE_CERT), "-noout", "-pubkey"), "release must not be the dev key" + readme = (DEV_KEYS / "README.md").read_text() + assert "INSECURE" in readme and "published private key" in readme + assert sorted(p.name for p in DEV_KEYS.iterdir()) == [ + "INSECURE-dev-module-key.crt", "INSECURE-dev-module-key.pem", "README.md" + ] + + +def test_no_element_stages_the_dev_keys() -> None: + for element in (ROOT / "elements").rglob("*.bst"): + doc = yaml.safe_load(element.read_text()) or {} + for source in doc.get("sources") or []: + path = str(source.get("path", "")).rstrip("/") if isinstance(source, dict) else "" + if path: + assert not ("files/dev-keys/".startswith(path + "/") or path.startswith("files/dev-keys")), ( + f"{element.relative_to(ROOT)} stages {path}, which holds the published dev private key" + ) + + +def test_release_builds_check_their_keys_after_staging_them() -> None: + (step,) = [s for s in WORKFLOW["jobs"]["build"]["steps"] if s.get("name") == "Install signing keys"] + release_branch = step["run"].split("else", 1)[0] + staged = release_branch.index("cp files/release-keys/linux-module-cert.crt") + assert release_branch.index("bash scripts/check-release-keys.sh") > staged + + +def test_both_kernel_variants_are_seeded_on_releases_and_only_release_gates_the_build() -> None: + jobs = WORKFLOW["jobs"] + for job, variant in (("kernel-cache", "release"), ("kernel-cache-dev", "dev")): + assert jobs[job]["if"] == "needs.changes.outputs.release == 'true'" + runs = [s.get("run", "") for s in jobs[job]["steps"]] + assert f"bash scripts/kernel-cache.sh seed {variant}" in runs + assert "BOOT_KEYS_TARBALL" not in str(jobs[job]) and "SYSUPDATE_SIGNING_KEY" not in str(jobs[job]) + assert "gen-dev-keys" not in str(jobs[job]) + assert "kernel-cache" in jobs["build"]["needs"] + assert not any("kernel-cache-dev" in str(j.get("needs", "")) for j in jobs.values()) diff --git a/tests/unit/test_files_payload_reachability.py b/tests/unit/test_files_payload_reachability.py index 1be89c40..7d19b207 100644 --- a/tests/unit/test_files_payload_reachability.py +++ b/tests/unit/test_files_payload_reachability.py @@ -49,6 +49,11 @@ # (scripts/kernel-cache.sh), from where the kernel's # linux-module-cert override stages it. "files/release-keys/linux-module-cert.crt", + # The public INSECURE dev module signing pair and its README; + # scripts/gen-dev-keys.sh copies the pair to files/boot-keys/ for + # non-release builds. Staging it in any element would ship a published + # private key. + "files/dev-keys", } # Gitignored signing material, absent from every fresh checkout: diff --git a/tests/unit/test_gen_dev_keys.py b/tests/unit/test_gen_dev_keys.py index d341022a..df9d8df0 100644 --- a/tests/unit/test_gen_dev_keys.py +++ b/tests/unit/test_gen_dev_keys.py @@ -17,6 +17,7 @@ REPO_ROOT = Path(__file__).resolve().parents[2] SCRIPT = REPO_ROOT / "scripts" / "gen-dev-keys.sh" +DEV_KEYS = REPO_ROOT / "files" / "dev-keys" SIGNING = ["sysupdate-signing.asc", "import-pubring.pgp"] BOOT = [ @@ -35,6 +36,7 @@ def tree(tmp_path: Path) -> Path: (tmp_path / "scripts").mkdir() shutil.copy(SCRIPT, tmp_path / "scripts" / SCRIPT.name) + shutil.copytree(DEV_KEYS, tmp_path / "files" / "dev-keys") stubs = tmp_path / "stubs" stubs.mkdir() for tool in ("openssl", "gpg"): @@ -61,8 +63,10 @@ def snapshot(tree: Path) -> dict[str, str]: } -def run(tree: Path, *args: str) -> subprocess.CompletedProcess[str]: +def run(tree: Path, *args: str, real_openssl: bool = False) -> subprocess.CompletedProcess[str]: env = dict(os.environ) + if real_openssl: + (tree / "stubs" / "openssl").unlink(missing_ok=True) env["PATH"] = f"{tree / 'stubs'}:{env['PATH']}" env["STUB_LOG"] = str(tree / "stub.log") return subprocess.run( @@ -117,3 +121,52 @@ def test_force_regenerates_an_existing_set(tree: Path) -> None: result = run(tree, "--force") assert result.returncode != 0, "the failing gpg stub must have been reached" assert stub_calls(tree).splitlines()[0] == "gpg" + + +def module_pair(tree: Path) -> tuple[bytes, bytes]: + keys = tree / "files" / "boot-keys" + return (keys / "linux-module-cert.key").read_bytes(), (keys / "modules" / "linux-module-cert.crt").read_bytes() + + +def test_module_key_is_the_committed_dev_pair(tree: Path) -> None: + # A fixed module certificate keeps the kernel's cache key stable across + # runs, so non-release builds pull the kernel instead of compiling it. + seed(tree, SIGNING) + result = run(tree, real_openssl=True) + assert result.returncode == 0, result.stderr + assert "INSECURE dev key" in result.stdout + assert module_pair(tree) == ( + (DEV_KEYS / "INSECURE-dev-module-key.pem").read_bytes(), + (DEV_KEYS / "INSECURE-dev-module-key.crt").read_bytes(), + ) + keys = tree / "files" / "boot-keys" + assert (keys / "linux-module-cert.key").stat().st_mode & 0o777 == 0o600 + assert not (keys / "modules" / "linux-module-cert.key").exists() + assert sorted(p.name for p in (keys / "modules").iterdir()) == ["linux-module-cert.crt"] + + +def test_regenerating_keeps_the_module_certificate(tree: Path) -> None: + (tree / "stubs" / "gpg").write_text("#!/bin/sh\necho key\n") + assert run(tree, real_openssl=True).returncode == 0 + first_db, first_module = (tree / "files" / "boot-keys" / "DB.crt").read_bytes(), module_pair(tree) + result = run(tree, "--force", real_openssl=True) + assert result.returncode == 0, result.stderr + assert (tree / "files" / "boot-keys" / "DB.crt").read_bytes() != first_db, "boot keys are fresh" + assert module_pair(tree) == first_module, "the module pair is not" + + +def test_private_module_key_is_fresh_and_not_the_dev_pair(tree: Path) -> None: + seed(tree, SIGNING) + result = run(tree, "--private-module-key", real_openssl=True) + assert result.returncode == 0, result.stderr + key, cert = module_pair(tree) + assert cert != (DEV_KEYS / "INSECURE-dev-module-key.crt").read_bytes() + assert key != (DEV_KEYS / "INSECURE-dev-module-key.pem").read_bytes() + assert "private module signing key" in result.stdout + + +def test_unknown_argument_is_refused(tree: Path) -> None: + result = run(tree, "--frce") + assert result.returncode == 2 + assert "usage" in result.stderr + assert not (tree / "files" / "boot-keys").exists() diff --git a/tests/unit/test_kernel_cache.py b/tests/unit/test_kernel_cache.py index 2fda52ea..dbe9bcdf 100644 --- a/tests/unit/test_kernel_cache.py +++ b/tests/unit/test_kernel_cache.py @@ -5,9 +5,15 @@ client credentials anywhere; it must build nothing when a remote already holds every element; and the push config it hands BuildStream must authenticate with the client certificate and leave no credential behind. `just` is stubbed. + +Each variant stages exactly one committed certificate as the only file of +files/boot-keys/modules/ (whose import is pushed with the kernel): release +the release certificate, which must not be the dev one; dev the public +INSECURE dev certificate, never its private key. """ import os +import shutil import subprocess from pathlib import Path @@ -34,9 +40,13 @@ @pytest.fixture def checkout(tmp_path: Path) -> Path: - # A copy of the script in an empty checkout, which it cds into. + # A copy of the scripts and committed certificates in an otherwise empty + # checkout, which the script cds into. (tmp_path / "scripts").mkdir() - (tmp_path / "scripts" / "kernel-cache.sh").write_text(SCRIPT.read_text(encoding="utf-8"), encoding="utf-8") + for name in ("kernel-cache.sh", "check-release-keys.sh"): + shutil.copy(ROOT / "scripts" / name, tmp_path / "scripts" / name) + for d in ("release-keys", "dev-keys"): + shutil.copytree(ROOT / "files" / d, tmp_path / "files" / d) return tmp_path @@ -60,12 +70,18 @@ def env(tmp_path: Path) -> dict[str, str]: } -def run(checkout: Path, env: dict[str, str]) -> subprocess.CompletedProcess: +def run(checkout: Path, env: dict[str, str], variant: str = "release") -> subprocess.CompletedProcess: return subprocess.run( - ["bash", str(checkout / "scripts" / "kernel-cache.sh"), "seed"], env=env, capture_output=True, text=True + ["bash", str(checkout / "scripts" / "kernel-cache.sh"), "seed", variant], + env=env, capture_output=True, text=True, ) +def staged(checkout: Path) -> dict[str, bytes]: + modules = checkout / "files" / "boot-keys" / "modules" + return {str(f.relative_to(modules)): f.read_bytes() for f in modules.rglob("*") if f.is_file()} + + def leftovers(checkout: Path) -> list[Path]: return list(checkout.glob(".casd.*")) @@ -142,3 +158,50 @@ def test_fsdk_junction_patch_includes_bluefin_cas() -> None: patch = (ROOT / "patches" / "freedesktop-sdk" / "0001-project.conf-Add-GNOME-CAS-servers.patch").read_text() assert "+- url: https://cache.projectbluefin.io:11001" in patch assert patch.count("https://cache.projectbluefin.io:11001") == 2 + + +@pytest.mark.parametrize( + ("variant", "cert"), + [("release", "release-keys/linux-module-cert.crt"), ("dev", "dev-keys/INSECURE-dev-module-key.crt")], +) +def test_seed_stages_only_the_variant_certificate(checkout: Path, env: dict[str, str], variant: str, cert: str) -> None: + result = run(checkout, env, variant) + assert result.returncode == 0, result.stderr + assert staged(checkout) == {"linux-module-cert.crt": (ROOT / "files" / cert).read_bytes()} + assert b"PRIVATE KEY" not in staged(checkout)["linux-module-cert.crt"] + assert f"kernel-cache: {variant}: pushed" in result.stdout + + +@pytest.mark.parametrize("variant", ["release", "dev"]) +def test_seed_refuses_anything_else_in_the_pushed_modules_import( + checkout: Path, env: dict[str, str], variant: str +) -> None: + modules = checkout / "files" / "boot-keys" / "modules" + modules.mkdir(parents=True) + shutil.copy(ROOT / "files" / "dev-keys" / "INSECURE-dev-module-key.pem", modules / "linux-module-cert.key") + result = run(checkout, env, variant) + assert result.returncode != 0 + assert "must hold only the certificate; refusing to publish" in result.stderr + assert not (Path(env["LOG"]) / "built").exists() + assert not leftovers(checkout) + + +def test_seed_release_refuses_the_dev_certificate(checkout: Path, env: dict[str, str]) -> None: + shutil.copy( + checkout / "files" / "dev-keys" / "INSECURE-dev-module-key.crt", + checkout / "files" / "release-keys" / "linux-module-cert.crt", + ) + result = run(checkout, env, "release") + assert result.returncode != 0 + assert "INSECURE dev module certificate" in result.stderr + assert not (Path(env["LOG"]) / "built").exists() + + +@pytest.mark.parametrize("args", [["seed"], ["seed", "prod"], ["build", "dev"], []]) +def test_seed_needs_a_known_variant(checkout: Path, env: dict[str, str], args: list[str]) -> None: + result = subprocess.run( + ["bash", str(checkout / "scripts" / "kernel-cache.sh"), *args], env=env, capture_output=True, text=True + ) + assert result.returncode == 2 + assert "usage" in result.stderr + assert not (Path(env["LOG"]) / "built").exists() diff --git a/tests/unit/test_release_workflow.py b/tests/unit/test_release_workflow.py index 4f2b52fd..2eec490e 100644 --- a/tests/unit/test_release_workflow.py +++ b/tests/unit/test_release_workflow.py @@ -5,6 +5,7 @@ import json from pathlib import Path +import pytest import yaml ROOT = Path(__file__).resolve().parents[2] @@ -69,10 +70,11 @@ def test_signing_secrets_only_reach_main_only_environments() -> None: install = next(s for s in build["steps"] if s.get("name") == "Install signing keys") for secret in ("BOOT_KEYS_TARBALL", "SYSUPDATE_SIGNING_KEY"): assert install["env"][secret] == f"${{{{ needs.changes.outputs.release == 'true' && secrets.{secret} || '' }}}}" - assert JOBS["kernel-cache"]["environment"] == "bst-cache" - assert JOBS["kernel-cache"]["if"] == "needs.changes.outputs.release == 'true'" + for seeder in ("kernel-cache", "kernel-cache-dev"): + assert JOBS[seeder]["environment"] == "bst-cache" + assert JOBS[seeder]["if"] == "needs.changes.outputs.release == 'true'" for name, job in JOBS.items(): - if name not in ("build", "kernel-cache"): + if name not in ("build", "kernel-cache", "kernel-cache-dev"): assert "secrets." not in json.dumps(job), name assert "environment" not in job, name @@ -146,10 +148,11 @@ def test_release_publishes_only_a_built_and_boot_tested_set() -> None: assert RELEASE["needs"] == ["changes", "build", "boot-test"] -def test_a_failed_kernel_cache_seed_is_reported() -> None: - seed = next(s for s in JOBS["kernel-cache"]["steps"] if s.get("id") == "seed") +@pytest.mark.parametrize("seeder", ["kernel-cache", "kernel-cache-dev"]) +def test_a_failed_kernel_cache_seed_is_reported(seeder: str) -> None: + seed = next(s for s in JOBS[seeder]["steps"] if s.get("id") == "seed") assert seed["continue-on-error"] is True - report = next(s for s in JOBS["kernel-cache"]["steps"] if s.get("if") == "steps.seed.outcome == 'failure'") + report = next(s for s in JOBS[seeder]["steps"] if s.get("if") == "steps.seed.outcome == 'failure'") assert "::warning" in report["run"] and "GITHUB_STEP_SUMMARY" in report["run"]