diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index eefb2f75e1..3d10f73bed 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,7 +1,6 @@ # Release and signing paths need the release owner's review. The branch ruleset on main and 0.22 # requires code-owner review and no other approvals, so other changes merge as before. -# Target: jobs that hold release secrets run only these paths. Until the Android helper signing -# split lands, the Build job in release.yml is the known exception. See docs/agents/pull-requests.md. +# Jobs that hold release secrets run only these paths. See docs/agents/pull-requests.md. /.github/ @thymikee /scripts/release-* @thymikee /scripts/*android-helper* @thymikee diff --git a/.github/workflows/android-helper-signing-rehearsal.yml b/.github/workflows/android-helper-signing-rehearsal.yml index ea6fe5d80c..2edd675519 100644 --- a/.github/workflows/android-helper-signing-rehearsal.yml +++ b/.github/workflows/android-helper-signing-rehearsal.yml @@ -11,7 +11,7 @@ on: - 'scripts/build-android-helper.sh' - 'scripts/package-android-helper.sh' - 'scripts/check-unsigned-android-helper.mjs' - - 'scripts/rehearse-android-helper-signing.sh' + - 'scripts/sign-android-helpers.sh' - 'scripts/release-version.mjs' permissions: {} @@ -59,6 +59,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 permissions: + actions: read contents: read outputs: artifact_id: ${{ steps.upload.outputs.artifact-id }} @@ -72,19 +73,21 @@ jobs: sparse-checkout: | .github/actions/setup-android-sdk/action.yml scripts/check-unsigned-android-helper.mjs - scripts/rehearse-android-helper-signing.sh + scripts/sign-android-helpers.sh scripts/release-version.mjs sparse-checkout-cone-mode: false - - name: Require the compiler artifact ID + # Fetched packed: actions/download-artifact extracts without a path containment check, and + # the compiler controls the entries. The signer unpacks only the expected APK names. + - name: Fetch unsigned helpers env: + GH_TOKEN: ${{ github.token }} ARTIFACT_ID: ${{ needs.compile-helpers.outputs.artifact_id }} - run: '[[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]]' - - name: Download unsigned helpers - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 - with: - artifact-ids: ${{ needs.compile-helpers.outputs.artifact_id }} - path: ${{ runner.temp }}/unsigned-helpers - merge-multiple: true + run: | + set -euo pipefail + [[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]] + artifact="repos/$GITHUB_REPOSITORY/actions/artifacts/$ARTIFACT_ID" + [ "$(gh api "$artifact" --jq .workflow_run.id)" = "$GITHUB_RUN_ID" ] + gh api "$artifact/zip" > "$RUNNER_TEMP/unsigned-helpers.zip" - uses: ./.github/actions/setup-android-sdk - name: Setup Node.js uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 @@ -95,10 +98,11 @@ jobs: id: sign env: VERSION: ${{ needs.compile-helpers.outputs.version }} + AGENT_DEVICE_ANDROID_HELPER_DISPOSABLE_KEY: '1' run: | set -euo pipefail - fingerprint="$(sh scripts/rehearse-android-helper-signing.sh "$VERSION" \ - "$RUNNER_TEMP/unsigned-helpers" "$RUNNER_TEMP/signed-helpers")" + fingerprint="$(sh scripts/sign-android-helpers.sh "$VERSION" \ + "$RUNNER_TEMP/unsigned-helpers.zip" "$RUNNER_TEMP/signed-helpers" snapshot ime)" printf 'certificate=%s\n' "$fingerprint" >> "$GITHUB_OUTPUT" - name: Upload signed helpers id: upload diff --git a/.github/workflows/release-android-snapshot-helper.yml b/.github/workflows/release-android-snapshot-helper.yml index c6d1437579..703cbc4b00 100644 --- a/.github/workflows/release-android-snapshot-helper.yml +++ b/.github/workflows/release-android-snapshot-helper.yml @@ -19,23 +19,28 @@ permissions: contents: write jobs: - publish-android-snapshot-helper: - name: Publish Android Snapshot Helper - runs-on: blacksmith-4vcpu-ubuntu-2404 - timeout-minutes: 30 - # The helper keystore secrets live only in the protected `release` environment. Its deployment - # rule gates github.ref, so the job builds exactly that ref. - environment: release - env: - AGENT_DEVICE_ANDROID_HELPER_RELEASE: '1' + # Compiles from the release ref with no secret. The release environment's deployment policy + # admits only main and v* tags, so the signer below never runs for another ref. + compile-helper: + name: Compile unsigned Android snapshot helper + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + # No Actions cache token: tag-source code cannot write the default-branch cache. + cache-mode: none + outputs: + artifact_id: ${{ steps.upload.outputs.artifact-id }} + version: ${{ steps.meta.outputs.version }} steps: - name: Checkout - uses: useblacksmith/checkout@2b9fe87d796feea153b455ec2f3125b471a7b0e7 # v1.8.2 + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: ref: ${{ github.ref }} + persist-credentials: false - name: Checkout release validator - uses: useblacksmith/checkout@2b9fe87d796feea153b455ec2f3125b471a7b0e7 # v1.8.2 + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: repository: ${{ job.workflow_repository }} ref: ${{ job.workflow_sha }} @@ -48,6 +53,7 @@ jobs: uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 with: node-version: '24.13' + package-manager-cache: false - name: Resolve release metadata id: meta @@ -65,25 +71,145 @@ jobs: - name: Setup Android SDK uses: ./.github/actions/setup-android-sdk + - name: Compile unsigned helper + env: + VERSION: ${{ steps.meta.outputs.version }} + AGENT_DEVICE_ANDROID_HELPER_UNSIGNED: '1' + run: sh scripts/build-android-helper.sh snapshot "$VERSION" "$RUNNER_TEMP/unsigned-helpers" + + - name: Upload unsigned helper + id: upload + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: unsigned-snapshot-helper-${{ inputs.release_tag }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/unsigned-helpers/*.apk + if-no-files-found: error + # Covers the environment approval window and reruns of later jobs. + retention-days: 30 + + # The only job that holds the helper keystore. It runs code from the workflow commit alone. + sign-helper: + name: Sign Android snapshot helper + needs: compile-helper + runs-on: ubuntu-latest + timeout-minutes: 10 + environment: release + permissions: + actions: read + contents: read + outputs: + artifact_id: ${{ steps.upload.outputs.artifact-id }} + certificate: ${{ steps.sign.outputs.certificate }} + steps: + - name: Checkout signing tools + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + repository: ${{ job.workflow_repository }} + ref: ${{ job.workflow_sha }} + persist-credentials: false + sparse-checkout: | + .github/actions/setup-android-sdk/action.yml + .github/actions/setup-android-helper-signing/action.yml + scripts/check-unsigned-android-helper.mjs + scripts/release-version.mjs + scripts/sign-android-helpers.sh + sparse-checkout-cone-mode: false + + # Fetched packed: actions/download-artifact extracts without a path containment check, and + # the compiler controls the entries. The signer unpacks only the expected APK names. + - name: Fetch unsigned helpers + env: + GH_TOKEN: ${{ github.token }} + ARTIFACT_ID: ${{ needs.compile-helper.outputs.artifact_id }} + run: | + set -euo pipefail + [[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]] + artifact="repos/$GITHUB_REPOSITORY/actions/artifacts/$ARTIFACT_ID" + [ "$(gh api "$artifact" --jq .workflow_run.id)" = "$GITHUB_RUN_ID" ] + gh api "$artifact/zip" > "$RUNNER_TEMP/unsigned-helpers.zip" + + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk + + - name: Setup Node.js + uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 + with: + node-version: '24.13' + package-manager-cache: false + - name: Setup Android helper signing uses: ./.github/actions/setup-android-helper-signing with: keystore-base64: ${{ secrets.ANDROID_HELPER_KEYSTORE_BASE64 }} keystore-password: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} - - name: Check Java toolchain + - name: Check and sign helper + id: sign + env: + VERSION: ${{ needs.compile-helper.outputs.version }} + AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} run: | - javac --version - java --version + set -euo pipefail + fingerprint="$(sh scripts/sign-android-helpers.sh "$VERSION" \ + "$RUNNER_TEMP/unsigned-helpers.zip" "$RUNNER_TEMP/signed-helpers" snapshot)" + printf 'certificate=%s\n' "$fingerprint" >> "$GITHUB_OUTPUT" + + - name: Upload signed helper + id: upload + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: signed-snapshot-helper-${{ inputs.release_tag }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/signed-helpers/*.apk + if-no-files-found: error + # Covers the environment approval window and reruns of later jobs. + retention-days: 30 + + publish-android-snapshot-helper: + name: Publish Android Snapshot Helper + needs: [compile-helper, sign-helper] + runs-on: blacksmith-4vcpu-ubuntu-2404 + timeout-minutes: 15 + permissions: + contents: write + env: + AGENT_DEVICE_ANDROID_HELPER_RELEASE: '1' + AGENT_DEVICE_ANDROID_HELPER_CERTIFICATE_SHA256: ${{ needs.sign-helper.outputs.certificate }} + steps: + - name: Checkout + uses: useblacksmith/checkout@2b9fe87d796feea153b455ec2f3125b471a7b0e7 # v1.8.2 + with: + ref: ${{ github.ref }} + persist-credentials: false + + - name: Require the signed artifact ID + env: + ARTIFACT_ID: ${{ needs.sign-helper.outputs.artifact_id }} + run: '[[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]]' + + - name: Download signed helper + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + artifact-ids: ${{ needs.sign-helper.outputs.artifact_id }} + path: ${{ runner.temp }}/signed-helpers + merge-multiple: true + + - name: Setup Node.js + uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 + with: + node-version: '24.13' + + # Exports AGENT_DEVICE_ANDROID_BUILD_TOOLS, which the helper packaging requires on CI. + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk - name: Package Android snapshot helper id: package env: RELEASE_ASSET_DIR: ${{ github.workspace }}/.tmp/release-assets - RELEASE_VERSION: ${{ steps.meta.outputs.version }} - RELEASE_TAG: ${{ steps.meta.outputs.tag }} + RELEASE_VERSION: ${{ needs.compile-helper.outputs.version }} + RELEASE_TAG: ${{ inputs.release_tag }} AGENT_DEVICE_ANDROID_HELPER: snapshot - AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} + AGENT_DEVICE_ANDROID_SIGNED_HELPERS_DIR: ${{ runner.temp }}/signed-helpers run: | set -euo pipefail mkdir -p "${RELEASE_ASSET_DIR}" @@ -99,7 +225,7 @@ jobs: CHECKSUM_PATH: ${{ steps.package.outputs.checksum_path }} MANIFEST_PATH: ${{ steps.package.outputs.manifest_path }} GH_TOKEN: ${{ github.token }} - RELEASE_TAG: ${{ steps.meta.outputs.tag }} + RELEASE_TAG: ${{ inputs.release_tag }} run: | set -euo pipefail gh release upload "$RELEASE_TAG" \ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d4f2d515ad..4ebe2d8199 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,6 +12,7 @@ on: - '.github/workflows/release.yml' - 'scripts/release-*.mjs' - 'scripts/build-package-xcuitest.mjs' + - 'scripts/*android-helper*' - 'scripts/check-package.ts' - 'scripts/prepare-publish-assets.mjs' push: @@ -76,22 +77,145 @@ jobs: GITHUB_TOKEN: ${{ github.token }} run: node scripts/release-plan.mjs - build: - name: Build ${{ needs.plan.outputs.version }} + # Compiles the helper APKs from the planned source. It holds no secret, so planned-source code + # can change the APK bytes, as it can change any npm package byte, but never reads the key. + compile-helpers: + name: Compile unsigned Android helpers needs: plan if: contains(fromJSON('["dry-run", "nightly", "stable"]'), needs.plan.outputs.mode) + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + # No Actions cache token: planned-source code cannot write the default-branch cache. + cache-mode: none + outputs: + artifact_id: ${{ steps.upload.outputs.artifact-id }} + steps: + - name: Checkout planned source + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + persist-credentials: false + ref: ${{ needs.plan.outputs.commit }} + + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk + + - name: Compile unsigned helpers + env: + VERSION: ${{ needs.plan.outputs.version }} + AGENT_DEVICE_ANDROID_HELPER_UNSIGNED: '1' + run: | + set -euo pipefail + for helper in snapshot ime; do + sh scripts/build-android-helper.sh "$helper" "$VERSION" "$RUNNER_TEMP/unsigned-helpers" + done + + - name: Upload unsigned helpers + id: upload + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: unsigned-helpers-${{ needs.plan.outputs.commit }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/unsigned-helpers/*.apk + if-no-files-found: error + # Covers the environment approval window and reruns of later jobs. + retention-days: 30 + + # The only job that holds the helper keystore. It runs code from the workflow commit alone: a + # sparse checkout of the signer, its checks, and the actions they need. It restores no cache and + # installs no dependency. PR dry runs sign with a disposable key outside the environment. + sign-helpers: + name: Sign Android helpers + needs: [plan, compile-helpers] + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + actions: read + contents: read + environment: ${{ needs.plan.outputs.mode != 'dry-run' && 'release' || '' }} + outputs: + artifact_id: ${{ steps.upload.outputs.artifact-id }} + certificate: ${{ steps.sign.outputs.certificate }} + steps: + - name: Checkout signing tools + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + persist-credentials: false + ref: ${{ github.workflow_sha }} + sparse-checkout: | + .github/actions/setup-android-sdk/action.yml + .github/actions/setup-android-helper-signing/action.yml + scripts/check-unsigned-android-helper.mjs + scripts/release-version.mjs + scripts/sign-android-helpers.sh + sparse-checkout-cone-mode: false + + # Fetched packed: actions/download-artifact extracts without a path containment check, and + # the compiler controls the entries. The signer unpacks only the expected APK names. + - name: Fetch unsigned helpers + env: + GH_TOKEN: ${{ github.token }} + ARTIFACT_ID: ${{ needs.compile-helpers.outputs.artifact_id }} + run: | + set -euo pipefail + [[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]] + artifact="repos/$GITHUB_REPOSITORY/actions/artifacts/$ARTIFACT_ID" + [ "$(gh api "$artifact" --jq .workflow_run.id)" = "$GITHUB_RUN_ID" ] + gh api "$artifact/zip" > "$RUNNER_TEMP/unsigned-helpers.zip" + + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk + + - name: Setup Node.js + uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 + with: + node-version: '24.13' + package-manager-cache: false + + - name: Setup Android helper signing + if: needs.plan.outputs.mode != 'dry-run' + uses: ./.github/actions/setup-android-helper-signing + with: + keystore-base64: ${{ secrets.ANDROID_HELPER_KEYSTORE_BASE64 }} + keystore-password: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} + + - name: Check and sign helpers + id: sign + env: + VERSION: ${{ needs.plan.outputs.version }} + AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} + AGENT_DEVICE_ANDROID_HELPER_DISPOSABLE_KEY: ${{ needs.plan.outputs.mode == 'dry-run' && '1' || '' }} + run: | + set -euo pipefail + fingerprint="$(sh scripts/sign-android-helpers.sh "$VERSION" \ + "$RUNNER_TEMP/unsigned-helpers.zip" "$RUNNER_TEMP/signed-helpers" snapshot ime)" + printf 'certificate=%s\n' "$fingerprint" >> "$GITHUB_OUTPUT" + + - name: Upload signed helpers + id: upload + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: signed-helpers-${{ needs.plan.outputs.commit }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/signed-helpers/*.apk + if-no-files-found: error + # Covers the environment approval window and reruns of later jobs. + retention-days: 30 + + build: + name: Build ${{ needs.plan.outputs.version }} + needs: [plan, sign-helpers] runs-on: macos-26 timeout-minutes: 120 permissions: contents: read cache-mode: none - # The helper keystore secrets live only in the protected `release` environment, so PR dry - # runs never receive them and sign with a throwaway key. - environment: ${{ needs.plan.outputs.mode != 'dry-run' && 'release' || '' }} env: VERSION: ${{ needs.plan.outputs.version }} - # scripts/build-android-helper.sh fails instead of using a throwaway key when this is set. - AGENT_DEVICE_ANDROID_HELPER_RELEASE: ${{ needs.plan.outputs.mode != 'dry-run' && '1' || '' }} + # scripts/build-android-helper.sh copies the signed helpers and fails if one is missing or + # carries a different certificate. It never signs here. + AGENT_DEVICE_ANDROID_HELPER_RELEASE: '1' + AGENT_DEVICE_ANDROID_SIGNED_HELPERS_DIR: ${{ github.workspace }}/.tmp/signed-helpers + AGENT_DEVICE_ANDROID_HELPER_CERTIFICATE_SHA256: ${{ needs.sign-helpers.outputs.certificate }} steps: - name: Checkout uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 @@ -105,11 +229,21 @@ jobs: persist-credentials: false ref: ${{ github.workflow_sha }} path: .release-tools - sparse-checkout: | - scripts/release-version.mjs - .github/actions/setup-android-helper-signing/action.yml + sparse-checkout: scripts/release-version.mjs sparse-checkout-cone-mode: false + - name: Require the signed artifact ID + env: + ARTIFACT_ID: ${{ needs.sign-helpers.outputs.artifact_id }} + run: '[[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]]' + + - name: Download signed helpers + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + artifact-ids: ${{ needs.sign-helpers.outputs.artifact_id }} + path: ${{ env.AGENT_DEVICE_ANDROID_SIGNED_HELPERS_DIR }} + merge-multiple: true + # No dependency cache: published bytes come from a clean install. - name: Setup toolchain uses: ./.github/actions/setup-node-pnpm @@ -120,20 +254,10 @@ jobs: - name: Setup Android SDK uses: ./.github/actions/setup-android-sdk - - name: Setup Android helper signing - if: needs.plan.outputs.mode != 'dry-run' - # From the workflow commit: the planned nightly source may predate this action. - uses: ./.release-tools/.github/actions/setup-android-helper-signing - with: - keystore-base64: ${{ secrets.ANDROID_HELPER_KEYSTORE_BASE64 }} - keystore-password: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} - - name: Stamp release version run: node .release-tools/scripts/release-version.mjs stamp "$VERSION" - name: Build and verify every public package - env: - AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_HELPER_KEYSTORE_PASSWORD }} run: pnpm release:prepare - name: Upload packages @@ -265,6 +389,7 @@ jobs: name: Android helper assets needs: [plan, github-release] permissions: + actions: read contents: write uses: ./.github/workflows/release-android-snapshot-helper.yml with: diff --git a/scripts/rehearse-android-helper-signing.sh b/scripts/rehearse-android-helper-signing.sh deleted file mode 100644 index 2d67c5180d..0000000000 --- a/scripts/rehearse-android-helper-signing.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/sh -set -eu - -if [ "$#" -ne 3 ]; then - echo "Usage: $0 " >&2 - exit 1 -fi - -VERSION="$1" -UNSIGNED_DIR="$2" -SIGNED_DIR="$3" -SCRIPTS="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)" -SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:?Android SDK is required}}" -TOOLS="$SDK_ROOT/build-tools/${AGENT_DEVICE_ANDROID_BUILD_TOOLS:?Android build-tools version is required}" - -node "$SCRIPTS/release-version.mjs" asset-tag "v$VERSION" -VERSION_CODE="$(printf '%s\n' "${VERSION%%+*}" | awk -F. '{ print $1 * 1000000 + $2 * 1000 + $3 }')" -if [ "$(find "$UNSIGNED_DIR" -mindepth 1 -maxdepth 1 | wc -l)" -ne 2 ]; then - echo "Expected exactly two unsigned Android helper APKs." >&2 - exit 1 -fi - -for helper in snapshot ime; do - apk="$UNSIGNED_DIR/agent-device-android-$helper-helper-$VERSION.apk" - node "$SCRIPTS/check-unsigned-android-helper.mjs" "$apk" >&2 - "$TOOLS/zipalign" -c 4 "$apk" - badging="$("$TOOLS/aapt2" dump badging "$apk")" - identity="$(printf '%s\n' "$badging" | sed -n '/^package: /p')" - case "$identity" in - "package: name='com.callstack.agentdevice.${helper}helper' versionCode='$VERSION_CODE' versionName='$VERSION' "*) ;; - *) - echo "Unsigned Android helper identity mismatch: $helper" >&2 - exit 1 - ;; - esac -done - -umask 077 -key_dir="$(mktemp -d)" -trap 'rm -rf "$key_dir"' EXIT -key="$key_dir/rehearsal.p12" -keytool -genkeypair -keystore "$key" -storepass rehearsal -keypass rehearsal \ - -alias helper -keyalg RSA -keysize 2048 -validity 1 -dname 'CN=agent-device signing rehearsal' >&2 -fingerprint="$(keytool -exportcert -keystore "$key" -storepass rehearsal -alias helper | - shasum -a 256 | awk '{ print $1 }')" -mkdir -p "$SIGNED_DIR" -for helper in snapshot ime; do - name="agent-device-android-$helper-helper-$VERSION.apk" - "$TOOLS/apksigner" sign --ks "$key" --ks-pass pass:rehearsal \ - --out "$SIGNED_DIR/$name" "$UNSIGNED_DIR/$name" - if ! result="$("$TOOLS/apksigner" verify --min-sdk-version 23 --verbose --print-certs "$SIGNED_DIR/$name")"; then - echo "Signed Android helper verification failed: $helper" >&2 - exit 1 - fi - cert="$(printf '%s\n' "$result" | awk '/^Signer #[0-9]+ certificate SHA-256 digest: / { print $NF }')" - if [ "$cert" != "$fingerprint" ]; then - echo "Signed Android helper certificate mismatch: $helper" >&2 - exit 1 - fi - if ! printf '%s\n' "$result" | grep -Fx 'Verified using v1 scheme (JAR signing): true' >&2; then - echo "Signed Android helper is missing a verified v1 signature: $helper" >&2 - exit 1 - fi - if ! printf '%s\n' "$result" | grep -Fx 'Verified using v2 scheme (APK Signature Scheme v2): true' >&2; then - echo "Signed Android helper is missing a verified v2 signature: $helper" >&2 - exit 1 - fi - "$TOOLS/zipalign" -c 4 "$SIGNED_DIR/$name" -done -printf '%s\n' "$fingerprint" diff --git a/scripts/sign-android-helpers.sh b/scripts/sign-android-helpers.sh new file mode 100644 index 0000000000..5dfc88b208 --- /dev/null +++ b/scripts/sign-android-helpers.sh @@ -0,0 +1,135 @@ +#!/bin/sh +set -eu + +# Signs compiled, unsigned Android helper APKs and prints the signing certificate's SHA-256. +# The input is the compiler job's artifact ZIP, still packed. The compiler controls its entries, so +# this script writes only the exact expected APK names, as regular files, into a fresh directory. +# Release jobs run this script from the workflow commit, never from the planned source, and give +# it the keystore through AGENT_DEVICE_ANDROID_KEYSTORE and AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD. +# A keystore must sign with the release certificate below, unless +# AGENT_DEVICE_ANDROID_HELPER_CERTIFICATE_SHA256 names another one (local tests). +# AGENT_DEVICE_ANDROID_HELPER_DISPOSABLE_KEY=1 signs with a new one-day key instead (rehearsals). + +if [ "$#" -lt 4 ]; then + echo "Usage: $0 ..." >&2 + exit 1 +fi + +VERSION="$1" +ARTIFACT_ZIP="$2" +SIGNED_DIR="$3" +shift 3 +SCRIPTS="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)" +SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:?Android SDK is required}}" +TOOLS="$SDK_ROOT/build-tools/${AGENT_DEVICE_ANDROID_BUILD_TOOLS:?Android build-tools version is required}" +EXPECTED_CERTIFICATE="${AGENT_DEVICE_ANDROID_HELPER_CERTIFICATE_SHA256:-}" + +node "$SCRIPTS/release-version.mjs" asset-tag "v$VERSION" +VERSION_CODE="$(printf '%s\n' "${VERSION%%+*}" | awk -F. '{ print $1 * 1000000 + $2 * 1000 + $3 }')" +for helper in "$@"; do + case "$helper" in + snapshot | ime) ;; + *) + echo "Unknown Android helper: $helper" >&2 + exit 1 + ;; + esac +done + +umask 077 +work_dir="$(mktemp -d)" +trap 'rm -rf "$work_dir"' EXIT +UNSIGNED_DIR="$work_dir/unsigned" +mkdir "$UNSIGNED_DIR" +expected="" +for helper in "$@"; do + expected="$expected agent-device-android-$helper-helper-$VERSION.apk" +done +# shellcheck disable=SC2086 # Each expected name is one word: the version passed asset-tag. +python3 -I - "$ARTIFACT_ZIP" "$UNSIGNED_DIR" $expected <<'PY' +import os +import stat +import sys +import zipfile + +archive, output, *names = sys.argv[1:] +with zipfile.ZipFile(archive) as packed: + entries = packed.infolist() + if sorted(entry.filename for entry in entries) != sorted(names): + sys.exit("The unsigned helper artifact must hold exactly the expected APKs.") + for entry in entries: + mode = entry.external_attr >> 16 + if entry.is_dir() or (mode and not stat.S_ISREG(mode)) or entry.file_size > 1024 * 1024: + sys.exit(f"Unexpected unsigned helper artifact entry: {entry.filename}") + data = packed.read(entry) + descriptor = os.open(os.path.join(output, entry.filename), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(descriptor, "wb") as target: + target.write(data) +PY + +for helper in "$@"; do + apk="$UNSIGNED_DIR/agent-device-android-$helper-helper-$VERSION.apk" + node "$SCRIPTS/check-unsigned-android-helper.mjs" "$apk" >&2 + "$TOOLS/zipalign" -c 4 "$apk" + badging="$("$TOOLS/aapt2" dump badging "$apk")" + identity="$(printf '%s\n' "$badging" | sed -n '/^package: /p')" + case "$identity" in + "package: name='com.callstack.agentdevice.${helper}helper' versionCode='$VERSION_CODE' versionName='$VERSION' "*) ;; + *) + echo "Unsigned Android helper identity mismatch: $helper" >&2 + exit 1 + ;; + esac +done + +key_dir="$work_dir/key" +mkdir "$key_dir" +if [ "${AGENT_DEVICE_ANDROID_HELPER_DISPOSABLE_KEY:-}" = "1" ]; then + if [ -n "${AGENT_DEVICE_ANDROID_KEYSTORE:-}" ] || [ -n "$EXPECTED_CERTIFICATE" ]; then + echo "A disposable signing key cannot be combined with a keystore or a certificate pin." >&2 + exit 1 + fi + KEYSTORE="$key_dir/disposable.p12" + AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD=disposable + keytool -genkeypair -keystore "$KEYSTORE" -storepass "$AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD" \ + -keypass "$AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD" -alias helper -keyalg RSA -keysize 2048 \ + -validity 1 -dname 'CN=agent-device signing rehearsal' >&2 +else + KEYSTORE="${AGENT_DEVICE_ANDROID_KEYSTORE:?AGENT_DEVICE_ANDROID_KEYSTORE is required}" + EXPECTED_CERTIFICATE="${EXPECTED_CERTIFICATE:-734335265cd3d3231160896dc7675977c6a96db43a25ab09e9f225051d2639e1}" + : "${AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD:?AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD is required}" +fi +export AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD + +mkdir -p "$SIGNED_DIR" +fingerprint="" +for helper in "$@"; do + name="agent-device-android-$helper-helper-$VERSION.apk" + "$TOOLS/apksigner" sign --ks "$KEYSTORE" --ks-pass env:AGENT_DEVICE_ANDROID_KEYSTORE_PASSWORD \ + --out "$SIGNED_DIR/$name" "$UNSIGNED_DIR/$name" + if ! result="$("$TOOLS/apksigner" verify --min-sdk-version 23 --verbose --print-certs "$SIGNED_DIR/$name")"; then + echo "Signed Android helper verification failed: $helper" >&2 + exit 1 + fi + certs="$(printf '%s\n' "$result" | awk '/^Signer #[0-9]+ certificate SHA-256 digest: / { print $NF }')" + if [ "$(printf '%s\n' "$certs" | wc -l)" -ne 1 ] || [ -z "$certs" ]; then + echo "Signed Android helper must have exactly one signer: $helper" >&2 + exit 1 + fi + [ -n "$fingerprint" ] || fingerprint="$certs" + if [ "$certs" != "$fingerprint" ] || + { [ -n "$EXPECTED_CERTIFICATE" ] && [ "$certs" != "$EXPECTED_CERTIFICATE" ]; }; then + echo "Signed Android helper certificate mismatch: $helper" >&2 + exit 1 + fi + if ! printf '%s\n' "$result" | grep -Fx 'Verified using v1 scheme (JAR signing): true' >&2; then + echo "Signed Android helper is missing a verified v1 signature: $helper" >&2 + exit 1 + fi + if ! printf '%s\n' "$result" | grep -Fx 'Verified using v2 scheme (APK Signature Scheme v2): true' >&2; then + echo "Signed Android helper is missing a verified v2 signature: $helper" >&2 + exit 1 + fi + "$TOOLS/zipalign" -c 4 "$SIGNED_DIR/$name" +done +printf '%s\n' "$fingerprint" diff --git a/test/ci/release-workflow.test.ts b/test/ci/release-workflow.test.ts index 369aed807f..451c49fea1 100644 --- a/test/ci/release-workflow.test.ts +++ b/test/ci/release-workflow.test.ts @@ -17,6 +17,7 @@ type Job = { id?: string; name?: string; if?: string; + run?: string; uses?: string; with?: Record; }>; @@ -50,21 +51,18 @@ const secretsOf = (job: Job) => [ test('the publisher authenticates through OIDC alone', () => { expect(source).not.toMatch(/NPM_TOKEN|NODE_AUTH_TOKEN/); - // No secret reaches the publisher. The build reads only the Android helper keystore, and only - // in the protected `release` environment that PR dry runs never enter. The site's deploy hook - // is read only by the redeploy job after a stable release. + // No secret reaches the publisher. Only the helper signer reads the keystore, and only in the + // protected `release` environment that PR dry runs never enter. The site's deploy hook is read + // only by the redeploy job after a stable release. expect(secretsOf(workflow.jobs.publish ?? {})).toEqual([]); - expect(workflow.jobs.build?.environment).toBe( + expect(workflow.jobs['sign-helpers']?.environment).toBe( "${{ needs.plan.outputs.mode != 'dry-run' && 'release' || '' }}", ); - expect(workflow.jobs.build?.env?.AGENT_DEVICE_ANDROID_HELPER_RELEASE).toBe( - "${{ needs.plan.outputs.mode != 'dry-run' && '1' || '' }}", - ); const withSecrets = Object.entries(workflow.jobs) .map(([name, job]) => [name, secretsOf(job)] as const) .filter(([, names]) => names.length > 0); expect(withSecrets).toEqual([ - ['build', ['ANDROID_HELPER_KEYSTORE_BASE64', 'ANDROID_HELPER_KEYSTORE_PASSWORD']], + ['sign-helpers', ['ANDROID_HELPER_KEYSTORE_BASE64', 'ANDROID_HELPER_KEYSTORE_PASSWORD']], ['redeploy-site', ['VERCEL_SITE_DEPLOY_HOOK']], ['mark-released', ['RELEASE_PUSH_DEPLOY_KEY']], ]); @@ -89,11 +87,12 @@ test('the Android signing rehearsal validates version changes without release cr expect(rehearsal.permissions).toEqual({}); for (const job of Object.values(rehearsal.jobs)) { expect(job.environment).toBeUndefined(); - expect(job.permissions).toEqual({ contents: 'read' }); + expect(job.permissions?.contents).toBe('read'); + expect(Object.values(job.permissions ?? {}).every((level) => level === 'read')).toBe(true); expect(secretsOf(job)).toEqual([]); } - for (const name of ['sign-helpers', 'package-helpers']) { + for (const name of ['package-helpers']) { const download = rehearsal.jobs[name]?.steps?.find((step) => step.uses?.startsWith('actions/download-artifact@'), ); @@ -119,8 +118,110 @@ test('the release push key reaches only a job that runs no repository code', () expect(secretsOf(workflow.jobs['prepare-next-version'] ?? {})).toEqual([]); }); +const readWorkflow = (file: string) => + parse(fs.readFileSync(path.join(repoRoot, '.github/workflows', file), 'utf8')) as { + jobs: Record; + }; + +// Planned-source code runs in the compile and package jobs, which hold no secret or environment. +// Each signer runs only the workflow commit's sparse checkout. It fetches the compiler's artifact +// packed, by ID, and never through actions/download-artifact, which extracts without a path +// containment check. Planned-source code can change APK bytes but cannot read the key. +test.each([ + ['release.yml', 'sign-helpers', ['compile-helpers', 'build'], true], + [ + 'release-android-snapshot-helper.yml', + 'sign-helper', + ['compile-helper', 'publish-android-snapshot-helper'], + true, + ], + [ + 'android-helper-signing-rehearsal.yml', + 'sign-helpers', + ['compile-helpers', 'package-helpers'], + false, + ], +])( + '%s: %s is the only signer and runs workflow-commit code', + (file, signerName, unkeyed, holdsKeystore) => { + const { jobs } = readWorkflow(file); + for (const name of unkeyed) { + expect(jobs[name]?.environment).toBeUndefined(); + expect(secretsOf(jobs[name] ?? {})).toEqual([]); + } + const keyed = Object.entries(jobs) + .filter(([, job]) => + secretsOf(job).some((secret) => secret?.startsWith('ANDROID_HELPER_KEYSTORE')), + ) + .map(([name]) => name); + expect(keyed).toEqual(holdsKeystore ? [signerName] : []); + const steps = jobs[signerName]?.steps ?? []; + const checkouts = steps.filter((step) => step.uses?.startsWith('actions/checkout@')); + expect(checkouts).toHaveLength(1); + expect(checkouts[0]?.with?.ref).toMatch(/^\$\{\{ (github|job)\.workflow_sha \}\}$/); + const sparse = String(checkouts[0]?.with?.['sparse-checkout'] ?? '') + .trim() + .split('\n'); + for (const step of steps.filter((entry) => entry.uses?.startsWith('./'))) { + expect(sparse).toContain(`${step.uses?.slice(2)}/action.yml`); + } + for (const step of steps) { + expect(step.uses ?? '').not.toMatch(/^actions\/(cache|download-artifact)@/); + expect(step.run ?? '').not.toMatch(/\bpnpm\b|\bnpm (ci|install)\b/); + if (step.uses?.startsWith('actions/setup-node@')) { + expect(step.with?.['package-manager-cache']).toBe(false); + expect(step.with?.cache).toBeUndefined(); + } + } + const fetch = steps.find((step) => step.name === 'Fetch unsigned helpers'); + expect(fetch?.run).toContain('/zip'); + }, +); + +// A called workflow's jobs can never get more than its caller grants. GitHub rejects the whole run +// at startup when a nested job asks for more, so a dry run shows no job at all. +const LEVELS = ['none', 'read', 'write']; +test.each( + Object.entries(workflow.jobs) + .filter(([, job]) => job.uses?.startsWith('./.github/workflows/')) + .map(([name, job]) => [name, job] as const), +)('%s grants every permission its called workflow needs', (_name, caller) => { + const called = readWorkflow(caller.uses!.slice('./.github/workflows/'.length)) as ReturnType< + typeof readWorkflow + > & { permissions?: Record }; + const granted = caller.permissions ?? {}; + const requests = [ + called.permissions ?? {}, + ...Object.values(called.jobs).map((job) => job.permissions ?? {}), + ]; + for (const request of requests) { + for (const [scope, level] of Object.entries(request)) { + expect(LEVELS.indexOf(granted[scope] ?? 'none'), scope).toBeGreaterThanOrEqual( + LEVELS.indexOf(level), + ); + } + } +}); + +test('the release build packages the signer output by artifact ID', () => { + const buildDownload = workflow.jobs.build?.steps?.find((step) => + step.uses?.startsWith('actions/download-artifact@'), + ); + expect(buildDownload?.with?.['artifact-ids']).toBe( + '${{ needs.sign-helpers.outputs.artifact_id }}', + ); + expect(workflow.jobs.build?.env?.AGENT_DEVICE_ANDROID_HELPER_RELEASE).toBe('1'); +}); + +test.each([ + ['release.yml', 'compile-helpers'], + ['release-android-snapshot-helper.yml', 'compile-helper'], +])('%s: %s runs release source without an Actions cache token', (file, name) => { + expect(readWorkflow(file).jobs[name]?.['cache-mode']).toBe('none'); +}); + test('release publication follows the build directly', () => { - expect(workflow.jobs.build?.needs).toBe('plan'); + expect(workflow.jobs.build?.needs).toEqual(['plan', 'sign-helpers']); expect(workflow.jobs.publish?.needs).toEqual(['plan', 'build']); }); @@ -153,9 +254,6 @@ test('the version stamping entrypoint is pinned to the workflow commit', () => { (step) => step.with?.path === '.release-tools', ); expect(entrypoint?.with?.ref).toBe('${{ github.workflow_sha }}'); - expect( - workflow.jobs.build?.steps?.find((step) => step.name === 'Setup Android helper signing')?.uses, - ).toBe('./.release-tools/.github/actions/setup-android-helper-signing'); }); test('both nightly schedules keep separate concurrency groups for cron and manual selection', () => {