chore(release): mint short-lived cloudflare token for r2 uploads (#63115)

This commit is contained in:
Dax
2026-11-10 08:24:58 -05:00
committed by GitHub
Unverified
parent 34f2b028e3
commit 783b7d646a
2 changed files with 123 additions and 10 deletions
+110
View File
@@ -0,0 +1,110 @@
#!/usr/bin/env bash
# Mints a short-lived Cloudflare token for the current release job from the token issuer
# (anomalyco/secrets). Run after aws-actions/configure-aws-credentials has assumed
# arn:aws:iam::604439173067:role/github/opencode-release: the Lambda identifies the role from the
# SigV4 signature and mints only the `opencode/release` token. No repo, stage, permission or TTL
# is sent in the request, and the token is pinned to the runner's IP for 30 minutes.
#
# Step-scoped by default: the token goes to $GITHUB_OUTPUT and only steps that list
# `CLOUDFLARE_API_TOKEN: ${{ steps.<id>.outputs.token }}` in their env receive it.
set -euo pipefail
set +x
url=${CLOUDFLARE_TOKEN_URL:-https://kjivkui2evldapbok5fhrrl6oy0fctkp.lambda-url.us-east-1.on.aws/}
account=${CLOUDFLARE_ACCOUNT_ID:-15d29c8639fd3733b1b5486a2acfd968}
# One argv value, never a line in curl's config: a newline in an override must not be able to
# inject curl options or move the AWS credentials into Basic auth.
url_pattern='^https://[a-z0-9]+\.lambda-url\.us-east-1\.on\.aws/$'
if ! [[ "$url" =~ $url_pattern ]]; then
echo "::error::CLOUDFLARE_TOKEN_URL is not the token issuer's function URL." >&2
exit 1
fi
case "$account" in
*[!0-9a-f]*|"")
echo "::error::CLOUDFLARE_ACCOUNT_ID must be a 32-character lowercase hex id." >&2
exit 1
;;
esac
if [ ${#account} -ne 32 ]; then
echo "::error::CLOUDFLARE_ACCOUNT_ID must be a 32-character lowercase hex id." >&2
exit 1
fi
for name in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; do
if [ -z "${!name:-}" ]; then
echo "::error::$name is not set; run aws-actions/configure-aws-credentials first." >&2
exit 1
fi
done
# The response is captured, never streamed: it contains the token. AWS credentials go to curl
# through stdin, not argv, so they stay out of `ps` and any trace of this script.
response=$(
printf 'user = "%s:%s"\nheader = "x-amz-security-token: %s"\n' \
"$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "$AWS_SESSION_TOKEN" |
curl --config - \
--proto '=https' --proto-redir '=https' \
--aws-sigv4 "aws:amz:us-east-1:lambda" \
--request POST --url "$url" \
--fail-with-body --silent --show-error --max-time 20
)
token=$(printf '%s' "$response" | jq -er 'if (.token | type) == "string" then .token else error("token") end' 2>/dev/null)
expires=$(printf '%s' "$response" | jq -er 'if (.expires_on | type) == "string" then .expires_on else error("expires_on") end' 2>/dev/null)
# Validate before any output. No error below includes the token value.
case "$token" in
""|*[[:space:]]*)
echo "::error::The token issuer returned an invalid token." >&2
exit 1
;;
esac
if [ ${#token} -lt 8 ] || [ ${#token} -gt 512 ]; then
echo "::error::The token issuer returned an invalid token length." >&2
exit 1
fi
case "$expires" in
""|*[[:space:]]*)
echo "::error::The token issuer returned an invalid expires_on." >&2
exit 1
;;
esac
iso_pattern='^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?Z$'
if ! printf '%s' "$response" | jq -e --arg re "$iso_pattern" '(.expires_on | type) == "string" and (.expires_on | test($re))' > /dev/null; then
echo "::error::The token issuer returned an invalid expires_on." >&2
exit 1
fi
if ! [[ "$expires" =~ $iso_pattern ]]; then
echo "::error::The token issuer returned an invalid expires_on." >&2
exit 1
fi
# GitHub masks the exact string everywhere after this, including accidental traces and our own
# error messages. This is the first output that contains it.
printf '::add-mask::%s\n' "$token"
if [ -n "${GITHUB_OUTPUT:-}" ]; then
{
printf 'token=%s\n' "$token"
printf 'expires_on=%s\n' "$expires"
} >> "$GITHUB_OUTPUT"
elif [ -n "${GITHUB_ENV:-}" ]; then
printf 'CLOUDFLARE_API_TOKEN=%s\n' "$token" >> "$GITHUB_ENV"
fi
# Verify immediately, with the token on curl's stdin rather than argv. A runner whose Cloudflare
# egress address differs from the address Lambda saw (IPv4 vs IPv6, or a different NAT address)
# fails here instead of part way through an upload.
verify_url="https://api.cloudflare.com/client/v4/accounts/$account/tokens/verify"
if ! verify=$(printf 'header = "Authorization: Bearer %s"\nurl = "%s"\n' "$token" "$verify_url" |
curl --config - --proto '=https' --proto-redir '=https' --silent --show-error --fail-with-body --max-time 20); then
echo "::error::Cloudflare rejected the freshly minted token. The runner's egress IP to Cloudflare may differ from the IP Lambda saw (IPv4 vs IPv6 or a different NAT address). Use a runner with stable egress." >&2
exit 1
fi
if ! printf '%s' "$verify" | jq -e '.success == true and .result.status == "active"' > /dev/null; then
echo "::error::Cloudflare did not report the freshly minted token as active." >&2
exit 1
fi
echo "::notice::Cloudflare token ready; expires $expires"
+13 -10
View File
@@ -8,6 +8,8 @@ run-name: "${{ format('release {0}{1}', inputs.bump, inputs.version) }}"
# - sign-* and publish-* jobs read their secrets from AWS Secrets Manager: they assume
# arn:aws:iam::604439173067:role/github/opencode-release over GitHub OIDC in the release environment. The Azure
# trusted signing ids are repository variables.
# - publish-cli and publish-desktop upload to R2 with a 30-minute token minted by the token issuer from that role
# (.github/release/cloudflare-token.sh), not with a long-lived Cloudflare token from Secrets Manager.
# - plan decides version, channel and npm dist-tag from the event, inputs and ref; only a dispatch with a bump or
# version on dev reaches latest.
# .github/release/ and this file are the security boundary for releases.
@@ -649,16 +651,17 @@ jobs:
role-to-assume: arn:aws:iam::604439173067:role/github/opencode-release
role-session-name: opencode-${{ github.run_id }}
aws-region: us-east-1
- name: Read publish secrets
- name: Cloudflare token
id: cloudflare-token
if: needs.plan.outputs.dry_run != 'true'
uses: aws-actions/aws-secretsmanager-get-secrets@2297f9a879480a9e3af9b293ed15c70caf8e1c88 # v3.0.2
with:
secret-ids: CLOUDFLARE_API_TOKEN,opencode/release/CLOUDFLARE_API_TOKEN
parse-json-secrets: false
run: bash .github/release/cloudflare-token.sh
env:
CLOUDFLARE_TOKEN_URL: ${{ vars.CLOUDFLARE_TOKEN_URL }}
- name: Publish
run: bash .github/release/publish-cli.sh "$RUNNER_TEMP/in/build-archives" "$RUNNER_TEMP/in/sign-macos" "$RUNNER_TEMP/in/sign-windows"
env: &cloudflare
CLOUDFLARE_ACCOUNT_ID: 15d29c8639fd3733b1b5486a2acfd968
CLOUDFLARE_API_TOKEN: ${{ steps.cloudflare-token.outputs.token }}
publish-desktop:
needs: [plan, sign-macos, sign-desktop-windows-installer]
@@ -683,12 +686,12 @@ jobs:
role-to-assume: arn:aws:iam::604439173067:role/github/opencode-release
role-session-name: opencode-${{ github.run_id }}
aws-region: us-east-1
- name: Read publish secrets
- name: Cloudflare token
id: cloudflare-token
if: needs.plan.outputs.dry_run != 'true'
uses: aws-actions/aws-secretsmanager-get-secrets@2297f9a879480a9e3af9b293ed15c70caf8e1c88 # v3.0.2
with:
secret-ids: CLOUDFLARE_API_TOKEN,opencode/release/CLOUDFLARE_API_TOKEN
parse-json-secrets: false
run: bash .github/release/cloudflare-token.sh
env:
CLOUDFLARE_TOKEN_URL: ${{ vars.CLOUDFLARE_TOKEN_URL }}
- name: Publish
run: |
bash .github/release/publish-desktop.sh "$RUNNER_TEMP/in/sign-macos" "$RUNNER_TEMP/in/sign-desktop-windows-installer" \