diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 16a6423..16b99bd 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -10,7 +10,16 @@ on: permissions: {} jobs: + # Signs the macOS agents in mutagen-agents.tar.gz. + sign-mutagen-agents: + permissions: + contents: read + id-token: write + uses: coder/coder-desktop-windows/.github/workflows/sign-mutagen-agents.yml@main + secrets: inherit + release: + needs: sign-mutagen-agents # The EV signing service account (coder-ci) only trusts jobs in this # environment. It holds the signing configuration, the appcast key and # cdrci's token for winget, and a desktop release manager approves each run. @@ -82,12 +91,27 @@ jobs: if ($LASTEXITCODE -ne 0) { throw "Failed to add wix extension $ext" } } + - name: Download mutagen agents + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: mutagen-agents + path: ${{ runner.temp }}/mutagen-agents + - name: scripts/Release.ps1 id: release shell: pwsh run: | $ErrorActionPreference = "Stop" + $mutagenAgentsPath = Join-Path $env:RUNNER_TEMP "mutagen-agents\mutagen-agents.tar.gz" + if ($env:MUTAGEN_AGENTS_SHA256 -notmatch '^[0-9a-f]{64}$') { + throw "No checksum from the sign-mutagen-agents job" + } + $mutagenAgentsHash = (Get-FileHash -Algorithm SHA256 -Path $mutagenAgentsPath).Hash + if ($mutagenAgentsHash -ne $env:MUTAGEN_AGENTS_SHA256) { + throw "mutagen-agents.tar.gz checksum mismatch: $mutagenAgentsHash" + } + $env:EV_CERTIFICATE_PATH = Join-Path $env:TEMP "ev_cert.pem" Set-Content -Path $env:EV_CERTIFICATE_PATH -Value $env:EV_SIGNING_CERT $env:JSIGN_PATH = Join-Path $env:TEMP "jsign-6.0.jar" @@ -100,9 +124,11 @@ jobs: & ./scripts/Release.ps1 ` -version ${{ steps.version.outputs.VERSION }} ` - -assemblyVersion ${{ steps.version.outputs.ASSEMBLY_VERSION }} + -assemblyVersion ${{ steps.version.outputs.ASSEMBLY_VERSION }} ` + -mutagenAgentsPath $mutagenAgentsPath if ($LASTEXITCODE -ne 0) { throw "Failed to publish" } env: + MUTAGEN_AGENTS_SHA256: ${{ needs.sign-mutagen-agents.outputs.sha256 }} EV_SIGNING_CERT: ${{ secrets.EV_SIGNING_CERT }} EV_KEYSTORE: ${{ secrets.EV_KEYSTORE }} EV_KEY: ${{ secrets.EV_KEY }} diff --git a/.github/workflows/sign-mutagen-agents.yml b/.github/workflows/sign-mutagen-agents.yml new file mode 100644 index 0000000..64dcbf4 --- /dev/null +++ b/.github/workflows/sign-mutagen-agents.yml @@ -0,0 +1,213 @@ +name: sign-mutagen-agents + +# Signs the macOS agents in mutagen-agents.tar.gz. + +on: + workflow_call: + outputs: + sha256: + description: SHA-256 of mutagen-agents.tar.gz in the mutagen-agents artifact. + value: ${{ jobs.sign.outputs.sha256 }} + +permissions: {} + +jobs: + rcodesign: + name: Build rcodesign + runs-on: ubuntu-22.04 + timeout-minutes: 30 + outputs: + sha256: ${{ steps.checksum.outputs.sha256 }} + steps: + - name: Build rcodesign + uses: coder/coder-desktop-macos/.github/actions/install-rcodesign@9c16b2e1ba534feb32cec0a7db25975da92e98b5 + + - name: Record checksum + id: checksum + run: | + set -euo pipefail + sha256="$(sha256sum /tmp/rcodesign-bin/rcodesign | cut -d ' ' -f 1)" + echo "sha256=$sha256" >> "$GITHUB_OUTPUT" + + - name: Upload rcodesign + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: rcodesign + path: /tmp/rcodesign-bin/rcodesign + retention-days: 3 + + sign: + name: Sign the macOS agents + needs: rcodesign + runs-on: ubuntu-22.04 + timeout-minutes: 15 + environment: code-signing + permissions: + contents: read + id-token: write + outputs: + sha256: ${{ steps.checksum.outputs.sha256 }} + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.sha }} + fetch-depth: 0 + persist-credentials: false + + - name: Check the run + run: | + set -euo pipefail + fail() { echo "::error::$*"; exit 1; } + [[ "$GITHUB_EVENT_NAME" == "push" ]] || fail "This workflow only runs for a tag push, not for $GITHUB_EVENT_NAME." + [[ "$GITHUB_REF" =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$ ]] || fail "The tag must look like v1.2.3, got $GITHUB_REF." + [[ "$(git rev-parse HEAD)" == "$GITHUB_SHA" ]] || fail "The checkout is not at $GITHUB_SHA." + git merge-base --is-ancestor "$GITHUB_SHA" origin/main || fail "$GITHUB_SHA is not on main." + + - name: Check the configuration + env: + AC_CERTIFICATE_PEM: ${{ secrets.AC_CERTIFICATE_PEM }} + AC_KMS_KEY_RING: ${{ secrets.AC_KMS_KEY_RING }} + AC_WORKLOAD_ID_PROVIDER: ${{ secrets.AC_WORKLOAD_ID_PROVIDER }} + AC_SERVICE_ACCOUNT: ${{ secrets.AC_SERVICE_ACCOUNT }} + run: | + set -euo pipefail + missing=0 + for name in AC_CERTIFICATE_PEM AC_KMS_KEY_RING AC_WORKLOAD_ID_PROVIDER AC_SERVICE_ACCOUNT; do + if [[ -z "${!name}" ]]; then + echo "::error::The $name secret is not set in the code-signing environment." + missing=1 + fi + done + exit "$missing" + + - name: Download mutagen agents + run: | + set -euo pipefail + fail() { echo "::error::$*"; exit 1; } + version="$(jq -er '.version' scripts/mutagen.json)" + sha256="$(jq -er '.sha256["mutagen-agents.tar.gz"]' scripts/mutagen.json)" + [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$ ]] || fail "Unexpected mutagen version '$version'." + [[ "$sha256" =~ ^[0-9a-f]{64}$ ]] || fail "Unexpected mutagen-agents.tar.gz checksum '$sha256'." + mkdir "$RUNNER_TEMP/mutagen" + curl --fail --silent --show-error --location \ + --output "$RUNNER_TEMP/mutagen/mutagen-agents.tar.gz" \ + "https://storage.googleapis.com/coder-desktop/mutagen/$version/mutagen-agents.tar.gz" + sha256sum -c <<<"$sha256 $RUNNER_TEMP/mutagen/mutagen-agents.tar.gz" + + - name: Download rcodesign + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: rcodesign + path: ${{ runner.temp }}/rcodesign + + - name: Check rcodesign + env: + SHA256: ${{ needs.rcodesign.outputs.sha256 }} + run: | + set -euo pipefail + [[ "$SHA256" =~ ^[0-9a-f]{64}$ ]] || { echo "::error::No checksum from the rcodesign job."; exit 1; } + sha256sum -c <<<"$SHA256 $RUNNER_TEMP/rcodesign/rcodesign" + + - name: Install rcodesign + run: | + set -euo pipefail + sudo install -m 0755 "$RUNNER_TEMP/rcodesign/rcodesign" /usr/bin/rcodesign + rcodesign --version + + - name: Setup Cloud KMS PKCS#11 library + env: + AC_KMS_KEY_RING: ${{ secrets.AC_KMS_KEY_RING }} + run: | + set -euo pipefail + curl --fail --silent --show-error --location \ + --output /tmp/libkmsp11.tar.gz \ + https://github.com/GoogleCloudPlatform/kms-integrations/releases/download/pkcs11-v1.9/libkmsp11-1.9-linux-amd64.tar.gz + echo "5397f965073cd5d1e2c43e5148f102f31a7c835902f97eb41c7c9c0f1b876a40 /tmp/libkmsp11.tar.gz" | sha256sum -c + sudo tar -xzf /tmp/libkmsp11.tar.gz \ + -C /usr/local/lib \ + --strip-components=1 \ + libkmsp11-1.9-linux-amd64/libkmsp11.so + rm /tmp/libkmsp11.tar.gz + + touch /tmp/kmsp11_config.yaml + chmod 600 /tmp/kmsp11_config.yaml + cat > /tmp/kmsp11_config.yaml < /tmp/apple_cert.pem + if ! openssl x509 -in /tmp/apple_cert.pem -noout -subject -serial -enddate; then + echo "::error::The AC_CERTIFICATE_PEM secret is not a PEM certificate." + exit 1 + fi + + - name: Authenticate to Google Cloud + id: gcloud_auth + uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3.0.0 + with: + workload_identity_provider: ${{ secrets.AC_WORKLOAD_ID_PROVIDER }} + service_account: ${{ secrets.AC_SERVICE_ACCOUNT }} + + - name: Sign + env: + KMS_PKCS11_CONFIG: /tmp/kmsp11_config.yaml + GOOGLE_APPLICATION_CREDENTIALS: ${{ steps.gcloud_auth.outputs.credentials_file_path }} + run: | + set -euo pipefail + fail() { echo "::error::$*"; exit 1; } + work="$RUNNER_TEMP/mutagen" + archive="$work/mutagen-agents.tar.gz" + signed="$work/signed/mutagen-agents.tar.gz" + mkdir "$work/original" "$work/agents" "$work/signed" "$work/check" + + mapfile -t members < <(tar -tzf "$archive") + tar -xpzf "$archive" -C "$work/original" + tar -xpzf "$archive" -C "$work/agents" + for agent in darwin_amd64 darwin_arm64; do + rcodesign sign \ + --pkcs11-library /usr/local/lib/libkmsp11.so \ + --pkcs11-key-label developer-id-application \ + --pkcs11-certificate-file /tmp/apple_cert.pem \ + --binary-identifier "$agent" \ + --code-signature-flags runtime \ + "$work/agents/$agent" + done + tar -czf "$signed" -C "$work/agents" -- "${members[@]}" + + mapfile -t signed_members < <(tar -tzf "$signed") + [[ "${signed_members[*]}" == "${members[*]}" ]] || fail "The members of the signed archive do not match: ${signed_members[*]}" + tar -xpzf "$signed" -C "$work/check" + for member in "${members[@]}"; do + case "$member" in + darwin_amd64 | darwin_arm64) ;; + *) cmp "$work/original/$member" "$work/check/$member" || fail "$member changed." ;; + esac + done + + - name: Record checksum + id: checksum + run: | + set -euo pipefail + sha256="$(sha256sum "$RUNNER_TEMP/mutagen/signed/mutagen-agents.tar.gz" | cut -d ' ' -f 1)" + echo "sha256=$sha256" >> "$GITHUB_OUTPUT" + + - name: Upload mutagen agents + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: mutagen-agents + path: ${{ runner.temp }}/mutagen/signed/mutagen-agents.tar.gz + retention-days: 3 + + - name: Delete certificate and configuration + if: always() + run: rm -f /tmp/apple_cert.pem /tmp/kmsp11_config.yaml diff --git a/scripts/Get-Mutagen.ps1 b/scripts/Get-Mutagen.ps1 index 2f64416..afec1c6 100644 --- a/scripts/Get-Mutagen.ps1 +++ b/scripts/Get-Mutagen.ps1 @@ -7,7 +7,7 @@ param ( $ErrorActionPreference = "Stop" -function Download-File([string] $url, [string] $outputPath, [string] $etagFile) { +function Download-File([string] $url, [string] $outputPath, [string] $etagFile, [string] $checksum) { Write-Host "Downloading '$url' to '$outputPath'" # We use `curl.exe` here because `Invoke-WebRequest` is notoriously slow. & curl.exe ` @@ -23,6 +23,14 @@ function Download-File([string] $url, [string] $outputPath, [string] $etagFile) if (!(Test-Path $outputPath) -or (Get-Item $outputPath).Length -eq 0) { throw "Failed to download '$url', output file '$outputPath' is missing or empty" } + $bytes = [System.IO.File]::ReadAllBytes($outputPath) + $hash = [System.Security.Cryptography.SHA256]::Create().ComputeHash($bytes) + $actualChecksum = [System.BitConverter]::ToString($hash).Replace("-", "") + if ($actualChecksum -ne $checksum) { + if (Test-Path $etagFile) { Remove-Item -Force $etagFile } + Remove-Item -Force $outputPath + throw "Checksum mismatch for '$outputPath': expected '$checksum', got '$actualChecksum'" + } } $goArch = switch ($arch) { @@ -33,14 +41,17 @@ $goArch = switch ($arch) { # Download the mutagen binary from our bucket for this platform if we don't have # it yet (or it's different). -$mutagenVersion = "v0.18.5" +$mutagen = Get-Content -Raw (Join-Path $PSScriptRoot "mutagen.json") | ConvertFrom-Json +$mutagenVersion = $mutagen.version $mutagenPath = Join-Path $PSScriptRoot "files\mutagen-windows-$($arch).exe" $mutagenUrl = "https://storage.googleapis.com/coder-desktop/mutagen/$($mutagenVersion)/mutagen-windows-$($goArch).exe" $mutagenEtagFile = $mutagenPath + ".etag" -Download-File $mutagenUrl $mutagenPath $mutagenEtagFile +$mutagenChecksum = $mutagen.sha256."mutagen-windows-$($goArch).exe" +Download-File $mutagenUrl $mutagenPath $mutagenEtagFile $mutagenChecksum # Download mutagen agents tarball. $mutagenAgentsPath = Join-Path $PSScriptRoot "files\mutagen-agents.tar.gz" $mutagenAgentsUrl = "https://storage.googleapis.com/coder-desktop/mutagen/$($mutagenVersion)/mutagen-agents.tar.gz" $mutagenAgentsEtagFile = $mutagenAgentsPath + ".etag" -Download-File $mutagenAgentsUrl $mutagenAgentsPath $mutagenAgentsEtagFile +$mutagenAgentsChecksum = $mutagen.sha256."mutagen-agents.tar.gz" +Download-File $mutagenAgentsUrl $mutagenAgentsPath $mutagenAgentsEtagFile $mutagenAgentsChecksum diff --git a/scripts/Publish.ps1 b/scripts/Publish.ps1 index 385018d..e5d13fb 100644 --- a/scripts/Publish.ps1 +++ b/scripts/Publish.ps1 @@ -1,4 +1,4 @@ -# Usage: Publish.ps1 -arch -version [-msiOutputPath ] [-outputPath ] [-sign] +# Usage: Publish.ps1 -arch -version [-msiOutputPath ] [-outputPath ] [-mutagenAgentsPath ] [-sign] param ( [ValidateSet("x64", "arm64")] [Parameter(Mandatory = $true)] @@ -14,6 +14,9 @@ param ( [Parameter(Mandatory = $false)] [string] $outputPath = "", # defaults to "publish\CoderDesktop-$version-$arch.exe" + [Parameter(Mandatory = $false)] + [string] $mutagenAgentsPath = "", # defaults to "scripts\files\mutagen-agents.tar.gz" + [Parameter(Mandatory = $false)] [switch] $keepBuildTemp = $false, @@ -56,15 +59,20 @@ if ($sign) { Write-Host "Signing is enabled" Find-Dependencies java Find-EnvironmentVariables @("JSIGN_PATH", "EV_KEYSTORE", "EV_KEY", "EV_CERTIFICATE_PATH", "EV_TSA_URL", "GCLOUD_ACCESS_TOKEN") + if ($mutagenAgentsPath -eq "") { + throw "Missing parameter: mutagenAgentsPath" + } } -function Add-CoderSignature([string] $path) { +function Add-CoderSignature([string] $path, [switch] $replaceExisting) { if (!$sign) { Write-Host "Skipping signing $path" return } Write-Host "Signing $path" + $extraArgs = @() + if ($replaceExisting) { $extraArgs += "--replace" } & java.exe -jar $env:JSIGN_PATH ` --storetype GOOGLECLOUD ` --storepass $env:GCLOUD_ACCESS_TOKEN ` @@ -73,6 +81,7 @@ function Add-CoderSignature([string] $path) { --certfile $env:EV_CERTIFICATE_PATH ` --tsmode RFC3161 ` --tsaurl $env:EV_TSA_URL ` + @extraArgs ` $path if ($LASTEXITCODE -ne 0) { throw "Failed to sign $path" } @@ -148,10 +157,45 @@ $getMutagen = Join-Path $scriptRoot "Get-Mutagen.ps1" $mutagenSrcPath = Join-Path $scriptRoot "files\mutagen-windows-$($arch).exe" $mutagenDestPath = Join-Path $vpnFilesPath "mutagen.exe" Copy-Item $mutagenSrcPath $mutagenDestPath +Add-CoderSignature $mutagenDestPath -replaceExisting $mutagenAgentsSrcPath = Join-Path $scriptRoot "files\mutagen-agents.tar.gz" +if ($mutagenAgentsPath -ne "") { + $mutagenAgentsSrcPath = $mutagenAgentsPath +} $mutagenAgentsDestPath = Join-Path $vpnFilesPath "mutagen-agents.tar.gz" -Copy-Item $mutagenAgentsSrcPath $mutagenAgentsDestPath +if ($sign) { + # Unpack the mutagen agents, sign the Windows agents and repack them in the + # same order. + $tar = Join-Path ([Environment]::SystemDirectory) "tar.exe" + $mutagenAgentsDir = Join-Path $publishDir "mutagen-agents-$($version)-$($arch)" + if (Test-Path $mutagenAgentsDir) { + Remove-Item -Recurse -Force $mutagenAgentsDir + } + New-Item -ItemType Directory -Path $mutagenAgentsDir -Force + $mutagenAgents = @(& $tar -tf $mutagenAgentsSrcPath) + if ($LASTEXITCODE -ne 0) { throw "Failed to list mutagen agents" } + & $tar -xf $mutagenAgentsSrcPath -C $mutagenAgentsDir + if ($LASTEXITCODE -ne 0) { throw "Failed to unpack mutagen agents" } + foreach ($agent in @("windows_amd64", "windows_arm64")) { + $agentPath = Join-Path $mutagenAgentsDir $agent + $agentExePath = $agentPath + ".exe" + Move-Item $agentPath $agentExePath + Add-CoderSignature $agentExePath -replaceExisting + Move-Item $agentExePath $agentPath + } + & $tar -czf $mutagenAgentsDestPath -C $mutagenAgentsDir @mutagenAgents + if ($LASTEXITCODE -ne 0) { throw "Failed to repack mutagen agents" } + $repackedAgents = @(& $tar -tf $mutagenAgentsDestPath) + if ($LASTEXITCODE -ne 0) { throw "Failed to list repacked mutagen agents" } + if (($repackedAgents -join " ") -cne ($mutagenAgents -join " ")) { + throw "Repacked mutagen agents '$repackedAgents' do not match '$mutagenAgents'" + } + Remove-Item -Recurse -Force $mutagenAgentsDir +} +else { + Copy-Item $mutagenAgentsSrcPath $mutagenAgentsDestPath +} # Build the MSI installer & dotnet.exe run --project .\Installer\Installer.csproj -c Release -- ` diff --git a/scripts/Release.ps1 b/scripts/Release.ps1 index 1654f9a..98a05ef 100644 --- a/scripts/Release.ps1 +++ b/scripts/Release.ps1 @@ -1,4 +1,4 @@ -# Usage: Release.ps1 -version +# Usage: Release.ps1 -version -assemblyVersion -mutagenAgentsPath param ( [Parameter(Mandatory = $true)] [ValidatePattern("^\d+\.\d+\.\d+$")] @@ -6,7 +6,10 @@ param ( [Parameter(Mandatory = $true)] [ValidatePattern("^\d+\.\d+\.\d+\.\d+$")] - [string] $assemblyVersion + [string] $assemblyVersion, + + [Parameter(Mandatory = $true)] + [string] $mutagenAgentsPath ) $ErrorActionPreference = "Stop" @@ -30,6 +33,7 @@ foreach ($arch in @("x64", "arm64")) { -arch $arch ` -msiOutputPath $msiOutputPath ` -outputPath $outputPath ` + -mutagenAgentsPath $mutagenAgentsPath ` -sign if ($LASTEXITCODE -ne 0) { throw "Failed to publish" } diff --git a/scripts/mutagen.json b/scripts/mutagen.json new file mode 100644 index 0000000..778e96b --- /dev/null +++ b/scripts/mutagen.json @@ -0,0 +1,8 @@ +{ + "version": "v0.18.5", + "sha256": { + "mutagen-windows-amd64.exe": "51415c027c3e63010fc1578fefd64998e92abbb8aa2cc40df36081261ef61c1d", + "mutagen-windows-arm64.exe": "bea242b472d1700b3d33dc07a187c615cc3cd13465fbddc397060e949d48afb9", + "mutagen-agents.tar.gz": "02e7458b9c6d1436c1fda8151bac86bfb46783b75a10d0fd9202100eb2a16f1d" + } +}