diff --git a/.gitea/workflows/bench-pages.yml b/.gitea/workflows/bench-pages.yml index 52c1bedf..86a31154 100644 --- a/.gitea/workflows/bench-pages.yml +++ b/.gitea/workflows/bench-pages.yml @@ -14,7 +14,7 @@ jobs: publish: runs-on: alkane-verify container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 permissions: contents: read steps: diff --git a/.gitea/workflows/ci-badge.yml b/.gitea/workflows/ci-badge.yml index c828ba78..3922b2d7 100644 --- a/.gitea/workflows/ci-badge.yml +++ b/.gitea/workflows/ci-badge.yml @@ -9,7 +9,7 @@ jobs: publish: runs-on: alkane-verify container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 permissions: actions: read contents: write diff --git a/.gitea/workflows/next.yml b/.gitea/workflows/next.yml index c133b135..6609be62 100644 --- a/.gitea/workflows/next.yml +++ b/.gitea/workflows/next.yml @@ -17,7 +17,7 @@ jobs: windows: runs-on: alkane-ci container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 env: CARGO_TARGET_DIR: target CARGO_BUILD_JOBS: 2 @@ -64,6 +64,14 @@ jobs: - name: Build Windows candidate if: steps.version.outputs.release == 'true' run: bash ci/windows-ci.sh build + - name: Sign Windows candidate (Azure Artifact Signing) + if: steps.version.outputs.release == 'true' + env: + AZURE_SIGNING_PROFILE: ${{ vars.AZURE_SIGNING_PROFILE }} + AZURE_SIGNING_TENANT_ID: ${{ secrets.AZURE_SIGNING_TENANT_ID }} + AZURE_SIGNING_CLIENT_ID: ${{ secrets.AZURE_SIGNING_CLIENT_ID }} + AZURE_SIGNING_CLIENT_SECRET: ${{ secrets.AZURE_SIGNING_CLIENT_SECRET }} + run: bash ci/windows-ci.sh sign - name: Package Windows candidate if: steps.version.outputs.release == 'true' run: bash ci/windows-ci.sh package "${{ steps.version.outputs.version }}" @@ -80,7 +88,7 @@ jobs: needs: windows runs-on: alkane-ci container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 env: CARGO_TARGET_DIR: target CARGO_BUILD_JOBS: 2 diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index e2967455..10df1e5c 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -23,7 +23,7 @@ jobs: windows: runs-on: alkane-ci container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 env: CARGO_TARGET_DIR: target CARGO_BUILD_JOBS: 2 @@ -91,6 +91,14 @@ jobs: - name: Headless UI self-test (Windows build, under Wine) run: bash ci/windows-ci.sh selftest + - name: Sign Windows executable (Azure Artifact Signing) + env: + AZURE_SIGNING_PROFILE: ${{ vars.AZURE_SIGNING_PROFILE }} + AZURE_SIGNING_TENANT_ID: ${{ secrets.AZURE_SIGNING_TENANT_ID }} + AZURE_SIGNING_CLIENT_ID: ${{ secrets.AZURE_SIGNING_CLIENT_ID }} + AZURE_SIGNING_CLIENT_SECRET: ${{ secrets.AZURE_SIGNING_CLIENT_SECRET }} + run: bash ci/windows-ci.sh sign + - name: Package Windows artifact if: steps.ver.outputs.release == 'true' run: bash ci/windows-ci.sh package "${{ steps.ver.outputs.version }}" @@ -120,7 +128,7 @@ jobs: needs: windows runs-on: alkane-ci container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 env: CARGO_TARGET_DIR: target CARGO_BUILD_JOBS: 2 diff --git a/.gitea/workflows/source.yml b/.gitea/workflows/source.yml index bb1e73fd..48401783 100644 --- a/.gitea/workflows/source.yml +++ b/.gitea/workflows/source.yml @@ -13,7 +13,7 @@ jobs: check: runs-on: alkane-fast container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 env: CARGO_BUILD_JOBS: 2 steps: diff --git a/.gitea/workflows/verify.yml b/.gitea/workflows/verify.yml index 883d49b4..efac5524 100644 --- a/.gitea/workflows/verify.yml +++ b/.gitea/workflows/verify.yml @@ -13,7 +13,7 @@ jobs: verify: runs-on: alkane-verify container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 outputs: surface: ${{ steps.surface.outputs.surface }} env: @@ -203,7 +203,7 @@ jobs: if: needs.verify.outputs.surface == 'full' runs-on: alkane-verify container: - image: gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 + image: gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 env: CARGO_TARGET_DIR: target CARGO_BUILD_JOBS: 2 diff --git a/.gitea/workflows/windows-native.yml b/.gitea/workflows/windows-native.yml index 64bbfa1b..8bd88b9d 100644 --- a/.gitea/workflows/windows-native.yml +++ b/.gitea/workflows/windows-native.yml @@ -63,6 +63,18 @@ jobs: $os = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' "Testing run $env:RUN_ID on Windows $($os.DisplayVersion) build $($os.CurrentBuild)" + - name: Authenticode signature (native Windows) + env: + SIGNING_ENABLED: ${{ vars.AZURE_SIGNING_PROFILE }} + run: | + # Windows itself judges the signature: chain to a trusted root, + # intact file hash, valid timestamp. Required once signing is on. + $sig = Get-AuthenticodeSignature "$env:ALKANE_NATIVE\alkane-toolkit.exe" + Write-Host "Signature: $($sig.Status) $($sig.StatusMessage)" + if ($sig.SignerCertificate) { Write-Host "Signer: $($sig.SignerCertificate.Subject)" } + if ($sig.TimeStamperCertificate) { Write-Host "Timestamp: $($sig.TimeStamperCertificate.Subject)" } + if ($env:SIGNING_ENABLED -and $sig.Status -ne 'Valid') { exit 1 } + - name: Unit tests (native Windows) run: | $failed = @() diff --git a/ci/ensure-build-deps.sh b/ci/ensure-build-deps.sh index c4a72d37..c60f5c0f 100644 --- a/ci/ensure-build-deps.sh +++ b/ci/ensure-build-deps.sh @@ -13,7 +13,8 @@ case "$mode" in # Windows cross build (mingw-w64, posix threads) + Wine for its tests. windows) packages=(libssl-dev pkg-config ca-certificates gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix - binutils-mingw-w64-x86-64 mingw-w64-x86-64-dev wine wine64 xvfb xauth) ;; + binutils-mingw-w64-x86-64 mingw-w64-x86-64-dev wine wine64 xvfb xauth + openjdk-21-jre-headless) ;; *) echo "unknown dependency set: $mode" >&2; exit 2 ;; esac diff --git a/ci/images/alkane-ci.Dockerfile b/ci/images/alkane-ci.Dockerfile index d27f9a13..8e71fd16 100644 --- a/ci/images/alkane-ci.Dockerfile +++ b/ci/images/alkane-ci.Dockerfile @@ -9,6 +9,8 @@ # scripts/build-wx-windows.sh and stamped with the source digest the same # way as the wxGTK prefix (ci/ensure-wx-prefix.sh rebuilds on a mismatch) # - Wine with an initialised prefix, to run the Windows tests and self-test +# - a Java runtime and jsign (checksum-pinned), to Authenticode-sign the .exe +# through Azure Artifact Signing (ci/sign-windows.sh) # # Build and push from the workstation with ci/images/build-image.sh. ARG BASE=gitea.bluefla.re/blueflare/alkane-ci@sha256:9d2a992bbcb91014af3c85d2e1ec8c5990701cecf83fd06c8d46f9ef13dbd948 @@ -18,9 +20,18 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update -qq \ && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \ gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix \ binutils-mingw-w64-x86-64 mingw-w64-x86-64-dev \ - wine wine64 xvfb xauth \ + wine wine64 xvfb xauth openjdk-21-jre-headless \ && rm -rf /var/lib/apt/lists/* +# jsign (Maven Central net.jsign:jsign; keep in step with ci/windows-ci.sh). +ARG JSIGN_VERSION=7.6 +ARG JSIGN_SHA256=6cf58aa080e7ff82656f320cff8d4a3bff30408df8766806dcba3740dd839fa5 +RUN mkdir -p /opt/jsign \ + && curl -sSfL -o /opt/jsign/jsign.jar \ + "https://repo1.maven.org/maven2/net/jsign/jsign/${JSIGN_VERSION}/jsign-${JSIGN_VERSION}.jar" \ + && echo "${JSIGN_SHA256} /opt/jsign/jsign.jar" | sha256sum -c - \ + && java -jar /opt/jsign/jsign.jar --help >/dev/null + RUN rustup target add x86_64-pc-windows-gnu # The build context is the repository root, filtered by @@ -44,4 +55,5 @@ RUN test -s /root/.cache/wx-3.3.3-mingw/lib/libwx_mswu-3.3-x86_64-w64-mingw32.a && test -s /root/.cache/wx-3.3.3-linux/lib/libwx_gtk3u-3.3.a \ && x86_64-w64-mingw32-g++-posix --version >/dev/null \ && wine --version \ + && test -s /opt/jsign/jsign.jar \ && rustup target list --installed | grep -qx x86_64-pc-windows-gnu diff --git a/ci/runner-image.txt b/ci/runner-image.txt index d79fd495..db3057f6 100644 --- a/ci/runner-image.txt +++ b/ci/runner-image.txt @@ -1 +1 @@ -gitea.bluefla.re/blueflare/alkane-ci@sha256:8f20439b38ae1c070b1a692a89c084f69134f4fa16a755d02783f621cd9ac041 +gitea.bluefla.re/blueflare/alkane-ci@sha256:4929b2979851f1f12fb8f92ef74236ae4c2ea735a41ead94a16af416a4f0a1a5 diff --git a/ci/sign-windows.sh b/ci/sign-windows.sh new file mode 100755 index 00000000..1e8eb5f1 --- /dev/null +++ b/ci/sign-windows.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MIT +# Authenticode-sign Windows executables with Azure Artifact Signing (formerly +# Trusted Signing), from Linux, with jsign. Every signature is RFC 3161 +# timestamped: the signing certificates live only a few days, the timestamp is +# what keeps a signature valid after that. +# +# ci/sign-windows.sh FILE.exe [FILE.exe ...] +# +# Configuration (environment): +# AZURE_SIGNING_PROFILE certificate profile name; EMPTY = signing not set +# up yet: the files are left unsigned (exit 0) +# AZURE_SIGNING_ACCOUNT signing account (default blueflaresigning) +# AZURE_SIGNING_ENDPOINT account endpoint (default https://wus2.codesigning.azure.net) +# Credentials, one of: +# AZURE_SIGNING_TENANT_ID + AZURE_SIGNING_CLIENT_ID + AZURE_SIGNING_CLIENT_SECRET +# (the CI service principal alkane-toolkit-ci-signer, role +# "Artifact Signing Certificate Profile Signer" on the account only) +# AZURE_SIGNING_TOKEN an access token for https://codesigning.azure.net, +# e.g. from `az account get-access-token --resource https://codesigning.azure.net` +# ALKANE_JSIGN_JAR jsign jar (default /opt/jsign/jsign.jar, from the CI image) +# +# Once a profile is configured, any failure fails the job: a release is never +# shipped half-signed or silently unsigned. +set -euo pipefail + +[ "$#" -gt 0 ] || { sed -n '3,25p' "$0" >&2; exit 2; } +profile="${AZURE_SIGNING_PROFILE:-}" +if [ -z "$profile" ]; then + echo "Windows code signing is not enabled (AZURE_SIGNING_PROFILE unset); leaving $* unsigned" + exit 0 +fi +account="${AZURE_SIGNING_ACCOUNT:-blueflaresigning}" +endpoint="${AZURE_SIGNING_ENDPOINT:-https://wus2.codesigning.azure.net}" +jar="${ALKANE_JSIGN_JAR:-/opt/jsign/jsign.jar}" +[ -f "$jar" ] || { echo "jsign not found at $jar" >&2; exit 1; } + +if [ -z "${AZURE_SIGNING_TOKEN:-}" ]; then + : "${AZURE_SIGNING_TENANT_ID:?signing credentials missing}" + : "${AZURE_SIGNING_CLIENT_ID:?signing credentials missing}" + : "${AZURE_SIGNING_CLIENT_SECRET:?signing credentials missing}" + # Client-credentials token for the signing service. The secret and token + # stay in this process's environment, never on a command line. + AZURE_SIGNING_TOKEN="$(python3 - <<'PY' +import json, os, sys, urllib.parse, urllib.request +body = urllib.parse.urlencode({ + "grant_type": "client_credentials", + "client_id": os.environ["AZURE_SIGNING_CLIENT_ID"], + "client_secret": os.environ["AZURE_SIGNING_CLIENT_SECRET"], + "scope": "https://codesigning.azure.net/.default", +}).encode() +url = f"https://login.microsoftonline.com/{os.environ['AZURE_SIGNING_TENANT_ID']}/oauth2/v2.0/token" +try: + with urllib.request.urlopen(url, body, timeout=60) as r: + print(json.load(r)["access_token"]) +except Exception as e: + sys.exit(f"could not get a signing token: {e}") +PY +)" +fi +export AZURE_SIGNING_TOKEN + +for file in "$@"; do + java -jar "$jar" \ + --storetype TRUSTEDSIGNING \ + --keystore "${endpoint#https://}" \ + --storepass env:AZURE_SIGNING_TOKEN \ + --alias "$account/$profile" \ + --alg SHA-256 \ + --tsaurl http://timestamp.acs.microsoft.com --tsmode RFC3161 \ + --tsretries 3 --tsretrywait 10 \ + --name "Alkane Toolkit" \ + --url https://gitea.bluefla.re/blueflare/alkane-toolkit \ + "$file" + # jsign fails on any error; also make sure a signature actually landed in + # the PE certificate table before the file goes anywhere. + python3 - "$file" <<'PY' +import struct, sys +data = open(sys.argv[1], "rb").read() +pe = struct.unpack_from(" None: "Clippy (Windows target, warnings are errors)", "Build release (Windows)", "Unit tests (Windows build, under Wine)", "Headless UI self-test (Windows build, under Wine)", + "Sign Windows executable (Azure Artifact Signing)", "Package Windows artifact", "Clean generated Toolkit build", )), ("next.yml", "next", "candidate", ( @@ -50,7 +51,8 @@ def check_workflow_contract() -> None: "Create private prerelease", "Clean generated Toolkit build", )), ("next.yml", "next", "windows", ( - "Build Windows candidate", "Package Windows candidate", + "Build Windows candidate", "Sign Windows candidate (Azure Artifact Signing)", + "Package Windows candidate", "Clean generated Toolkit build", )), ("verify.yml", "main", "verify", ( @@ -83,7 +85,8 @@ def check_workflow_contract() -> None: condition = steps[position].get("if") if gate in {"Create Gitea release + upload artifact", "Create private prerelease", "Package Windows artifact", - "Build Windows candidate", "Package Windows candidate"}: + "Build Windows candidate", "Package Windows candidate", + "Sign Windows candidate (Azure Artifact Signing)"}: assert condition is not None and "release == 'true'" in condition, path elif filename == "verify.yml" and gate in { "Build", "Unit tests", "Microbenchmarks", diff --git a/ci/windows-ci.sh b/ci/windows-ci.sh index d813616b..6aaa4e17 100644 --- a/ci/windows-ci.sh +++ b/ci/windows-ci.sh @@ -8,6 +8,9 @@ # windows-ci.sh build release alkane-toolkit.exe # windows-ci.sh test all unit tests, run under Wine # windows-ci.sh selftest headless UI self-test under Wine + Xvfb +# windows-ci.sh sign Authenticode-sign alkane-toolkit.exe through +# Azure Artifact Signing (ci/sign-windows.sh; +# a no-op until AZURE_SIGNING_PROFILE is set) # windows-ci.sh package VERSION dist/alkane-toolkit-vVERSION-windows-x86_64.zip # windows-ci.sh test-bundle dist/alkane-toolkit-windows-tests.zip (for # the native Windows job) @@ -30,6 +33,15 @@ case "${1:-}" in toolchain) bash ci/ensure-build-deps.sh windows rustup target list --installed | grep -qx "$target" || rustup target add "$target" + # jsign ships in the CI image; fetch the same pinned jar on older images. + jsign_version=7.6 + jsign_sha256=6cf58aa080e7ff82656f320cff8d4a3bff30408df8766806dcba3740dd839fa5 + if [ ! -s /opt/jsign/jsign.jar ]; then + mkdir -p /opt/jsign + curl -sSfL -o /opt/jsign/jsign.jar \ + "https://repo1.maven.org/maven2/net/jsign/jsign/$jsign_version/jsign-$jsign_version.jar" + echo "$jsign_sha256 /opt/jsign/jsign.jar" | sha256sum -c - + fi ;; clippy) rustup component add clippy >/dev/null 2>&1 || true @@ -53,6 +65,9 @@ case "${1:-}" in selftest) bash scripts/ui-selftest-windows.sh "$exe" ;; + sign) + bash ci/sign-windows.sh "$exe" + ;; package) python3 ci/package-windows.py release "${2:?usage: windows-ci.sh package VERSION}" "$exe" dist ;; @@ -64,7 +79,7 @@ case "${1:-}" in dist/alkane-toolkit-windows-tests.zip ;; *) - sed -n '3,16p' "$0" >&2 + sed -n '3,19p' "$0" >&2 exit 2 ;; esac diff --git a/docs/windows.md b/docs/windows.md index 248d0fc8..7ae3ade5 100644 --- a/docs/windows.md +++ b/docs/windows.md @@ -59,6 +59,40 @@ target and a prebuilt wxMSW prefix to the Linux image. Rebuild it with `ci/images/build-image.sh TAG` from the workstation that holds the registry credential; the script pins the new digest everywhere. +## Code signing + +Release and `-rc` executables are Authenticode-signed with Azure Artifact +Signing (formerly Trusted Signing), so Windows and SmartScreen show +"Blueflare" as the verified publisher instead of "Unknown publisher". + +| Piece | Where | +|-------|-------| +| Signing account | `blueflaresigning` (Basic), resource group `rg-blueflare-signing`, West US 2, endpoint `https://wus2.codesigning.azure.net` | +| CI identity | service principal `alkane-toolkit-ci-signer`, role "Artifact Signing Certificate Profile Signer" on the certificate profile only; Gitea secrets `AZURE_SIGNING_TENANT_ID`, `AZURE_SIGNING_CLIENT_ID`, `AZURE_SIGNING_CLIENT_SECRET` (secret expires 2027-11-03) | +| Switch | Gitea repository variable `AZURE_SIGNING_PROFILE` (certificate profile name); unset = builds stay unsigned | +| Signer | `ci/sign-windows.sh` (jsign from the CI image, RFC 3161 timestamp from `timestamp.acs.microsoft.com`) | +| Check | `windows-native.yml` requires `Get-AuthenticodeSignature` = Valid on real Windows once signing is on | + +Turning it on, once Microsoft approves the identity validation (portal: +signing account > Identity validations): + +```sh +scripts/enable-windows-signing.sh +``` + +It creates the PublicTrust certificate profile, narrows the CI identity to it, +and sets `AZURE_SIGNING_PROFILE`. From then on a signing failure fails the +release. Rotate the CI secret before it expires with +`az ad sp credential reset --id --years 1` and update the Gitea secret. + +Sign by hand (any machine with Java, `az login` and the Signer role): + +```sh +AZURE_SIGNING_PROFILE=alkane-toolkit ALKANE_JSIGN_JAR=jsign.jar \ +AZURE_SIGNING_TOKEN=$(az account get-access-token --resource https://codesigning.azure.net --query accessToken -o tsv) \ + ci/sign-windows.sh alkane-toolkit.exe +``` + ## Native Windows runner `windows-native.yml` runs on the self-hosted runner `win11-test-vm` diff --git a/scripts/enable-windows-signing.sh b/scripts/enable-windows-signing.sh new file mode 100755 index 00000000..a8fee891 --- /dev/null +++ b/scripts/enable-windows-signing.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# Turn on Authenticode signing of the Windows build, once Microsoft has +# approved the publisher identity validation for the Artifact Signing account. +# +# scripts/enable-windows-signing.sh IDENTITY_VALIDATION_ID [PROFILE] +# +# IDENTITY_VALIDATION_ID is shown in the Azure portal: signing account +# "blueflaresigning" > Objects > Identity validations > (approved entry). +# The script +# 1. creates the PublicTrust certificate profile PROFILE (default +# alkane-toolkit) bound to that identity, +# 2. narrows the CI signer's role from the whole account to that profile, +# 3. sets the Gitea repository variable AZURE_SIGNING_PROFILE, which makes +# ci/sign-windows.sh sign every release and candidate from then on. +# +# Run on the workstation: it needs `az login` as an account owner and the +# Gitea admin token (~/.config/gitea/token), neither of which CI ever sees. +# Re-running is safe. +set -eu +IVID=${1:?usage: enable-windows-signing.sh IDENTITY_VALIDATION_ID [PROFILE]} +PROFILE=${2:-alkane-toolkit} +RG=rg-blueflare-signing +ACCOUNT=blueflaresigning +SIGNER=alkane-toolkit-ci-signer +REPO=blueflare/alkane-toolkit +TOKEN_FILE=${GITEA_TOKEN_FILE:-$HOME/.config/gitea/token} + +if ! az trustedsigning certificate-profile show -g "$RG" --account-name "$ACCOUNT" \ + -n "$PROFILE" --only-show-errors -o none 2>/dev/null; then + az trustedsigning certificate-profile create -g "$RG" --account-name "$ACCOUNT" \ + -n "$PROFILE" --profile-type PublicTrust --identity-validation-id "$IVID" \ + --only-show-errors -o none +fi +az trustedsigning certificate-profile show -g "$RG" --account-name "$ACCOUNT" -n "$PROFILE" \ + --only-show-errors --query "{profile:name, state:provisioningState, status:status}" -o table + +# Least privilege: the CI identity may sign with this profile and nothing else. +profile_id=$(az trustedsigning certificate-profile show -g "$RG" --account-name "$ACCOUNT" \ + -n "$PROFILE" --only-show-errors --query id -o tsv) +account_id=${profile_id%/certificateProfiles/*} +sp=$(az ad sp list --display-name "$SIGNER" --query "[0].id" -o tsv) +role="Artifact Signing Certificate Profile Signer" +az role assignment create --assignee-object-id "$sp" --assignee-principal-type ServicePrincipal \ + --role "$role" --scope "$profile_id" -o none +az role assignment delete --assignee "$sp" --role "$role" --scope "$account_id" 2>/dev/null || true + +# Switch signing on for CI (repository variable, not a secret). +api=https://gitea.bluefla.re/api/v1/repos/$REPO/actions/variables/AZURE_SIGNING_PROFILE +body=$(printf '{"value":"%s"}' "$PROFILE") +auth() { printf 'header = "Authorization: token %s"\n' "$(cat "$TOKEN_FILE")"; } +code=$(auth | curl -sS --config - -X PUT -H 'Content-Type: application/json' -d "$body" \ + -o /dev/null -w '%{http_code}' "$api") +[ "$code" = 204 ] || [ "$code" = 201 ] || { + code=$(auth | curl -sS --config - -X POST -H 'Content-Type: application/json' -d "$body" \ + -o /dev/null -w '%{http_code}' "$api") +} +case "$code" in 201|204) echo "Gitea AZURE_SIGNING_PROFILE=$PROFILE: signing is on" ;; + *) echo "setting the Gitea variable failed (HTTP $code)" >&2; exit 1 ;; esac