GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — chore(release): mint short-lived cloudflare token for r2 uploads (#63… · jerrylee/opencode@783b7d6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .github
    • release
      • cloudflare-token.sh
    • workflows
      • release.yml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.github/release/cloudflare-token.sh‎

Copy file name to clipboard
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env bash
2+
# Mints a short-lived Cloudflare token for the current release job from the token issuer
3+
# (anomalyco/secrets). Run after aws-actions/configure-aws-credentials has assumed
4+
# arn:aws:iam::604439173067:role/github/opencode-release: the Lambda identifies the role from the
5+
# SigV4 signature and mints only the `opencode/release` token. No repo, stage, permission or TTL
6+
# is sent in the request, and the token is pinned to the runner's IP for 30 minutes.
7+
#
8+
# Step-scoped by default: the token goes to $GITHUB_OUTPUT and only steps that list
9+
# `CLOUDFLARE_API_TOKEN: ${{ steps.<id>.outputs.token }}` in their env receive it.
10+
set -euo pipefail
11+
set +x
12+
13+
url=${CLOUDFLARE_TOKEN_URL:-https://kjivkui2evldapbok5fhrrl6oy0fctkp.lambda-url.us-east-1.on.aws/}
14+
account=${CLOUDFLARE_ACCOUNT_ID:-15d29c8639fd3733b1b5486a2acfd968}
15+
16+
# One argv value, never a line in curl's config: a newline in an override must not be able to
17+
# inject curl options or move the AWS credentials into Basic auth.
18+
url_pattern='^https://[a-z0-9]+\.lambda-url\.us-east-1\.on\.aws/$'
19+
if ! [[ "$url" =~ $url_pattern ]]; then
20+
echo "::error::CLOUDFLARE_TOKEN_URL is not the token issuer's function URL." >&2
21+
exit 1
22+
fi
23+
case "$account" in
24+
*[!0-9a-f]*|"")
25+
echo "::error::CLOUDFLARE_ACCOUNT_ID must be a 32-character lowercase hex id." >&2
26+
exit 1
27+
;;
28+
esac
29+
if [ ${#account} -ne 32 ]; then
30+
echo "::error::CLOUDFLARE_ACCOUNT_ID must be a 32-character lowercase hex id." >&2
31+
exit 1
32+
fi
33+
for name in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; do
34+
if [ -z "${!name:-}" ]; then
35+
echo "::error::$name is not set; run aws-actions/configure-aws-credentials first." >&2
36+
exit 1
37+
fi
38+
done
39+
40+
# The response is captured, never streamed: it contains the token. AWS credentials go to curl
41+
# through stdin, not argv, so they stay out of `ps` and any trace of this script.
42+
response=$(
43+
printf 'user = "%s:%s"\nheader = "x-amz-security-token: %s"\n' \
44+
"$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "$AWS_SESSION_TOKEN" |
45+
curl --config - \
46+
--proto '=https' --proto-redir '=https' \
47+
--aws-sigv4 "aws:amz:us-east-1:lambda" \
48+
--request POST --url "$url" \
49+
--fail-with-body --silent --show-error --max-time 20
50+
)
51+
52+
token=$(printf '%s' "$response" | jq -er 'if (.token | type) == "string" then .token else error("token") end' 2>/dev/null)
53+
expires=$(printf '%s' "$response" | jq -er 'if (.expires_on | type) == "string" then .expires_on else error("expires_on") end' 2>/dev/null)
54+
55+
# Validate before any output. No error below includes the token value.
56+
case "$token" in
57+
""|*[[:space:]]*)
58+
echo "::error::The token issuer returned an invalid token." >&2
59+
exit 1
60+
;;
61+
esac
62+
if [ ${#token} -lt 8 ] || [ ${#token} -gt 512 ]; then
63+
echo "::error::The token issuer returned an invalid token length." >&2
64+
exit 1
65+
fi
66+
case "$expires" in
67+
""|*[[:space:]]*)
68+
echo "::error::The token issuer returned an invalid expires_on." >&2
69+
exit 1
70+
;;
71+
esac
72+
iso_pattern='^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?Z$'
73+
if ! printf '%s' "$response" | jq -e --arg re "$iso_pattern" '(.expires_on | type) == "string" and (.expires_on | test($re))' > /dev/null; then
74+
echo "::error::The token issuer returned an invalid expires_on." >&2
75+
exit 1
76+
fi
77+
if ! [[ "$expires" =~ $iso_pattern ]]; then
78+
echo "::error::The token issuer returned an invalid expires_on." >&2
79+
exit 1
80+
fi
81+
82+
# GitHub masks the exact string everywhere after this, including accidental traces and our own
83+
# error messages. This is the first output that contains it.
84+
printf '::add-mask::%s\n' "$token"
85+
86+
if [ -n "${GITHUB_OUTPUT:-}" ]; then
87+
{
88+
printf 'token=%s\n' "$token"
89+
printf 'expires_on=%s\n' "$expires"
90+
} >> "$GITHUB_OUTPUT"
91+
elif [ -n "${GITHUB_ENV:-}" ]; then
92+
printf 'CLOUDFLARE_API_TOKEN=%s\n' "$token" >> "$GITHUB_ENV"
93+
fi
94+
95+
# Verify immediately, with the token on curl's stdin rather than argv. A runner whose Cloudflare
96+
# egress address differs from the address Lambda saw (IPv4 vs IPv6, or a different NAT address)
97+
# fails here instead of part way through an upload.
98+
verify_url="https://api.cloudflare.com/client/v4/accounts/$account/tokens/verify"
99+
if ! verify=$(printf 'header = "Authorization: Bearer %s"\nurl = "%s"\n' "$token" "$verify_url" |
100+
curl --config - --proto '=https' --proto-redir '=https' --silent --show-error --fail-with-body --max-time 20); then
101+
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
102+
exit 1
103+
fi
104+
105+
if ! printf '%s' "$verify" | jq -e '.success == true and .result.status == "active"' > /dev/null; then
106+
echo "::error::Cloudflare did not report the freshly minted token as active." >&2
107+
exit 1
108+
fi
109+
110+
echo "::notice::Cloudflare token ready; expires $expires"
Collapse file

‎.github/workflows/release.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/release.yml
+13-10Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ run-name: "${{ format('release {0}{1}', inputs.bump, inputs.version) }}"
88
# - sign-* and publish-* jobs read their secrets from AWS Secrets Manager: they assume
99
# arn:aws:iam::604439173067:role/github/opencode-release over GitHub OIDC in the release environment. The Azure
1010
# trusted signing ids are repository variables.
11+
# - publish-cli and publish-desktop upload to R2 with a 30-minute token minted by the token issuer from that role
12+
# (.github/release/cloudflare-token.sh), not with a long-lived Cloudflare token from Secrets Manager.
1113
# - plan decides version, channel and npm dist-tag from the event, inputs and ref; only a dispatch with a bump or
1214
# version on dev reaches latest.
1315
# .github/release/ and this file are the security boundary for releases.
@@ -649,16 +651,17 @@ jobs:
649651
role-to-assume: arn:aws:iam::604439173067:role/github/opencode-release
650652
role-session-name: opencode-${{ github.run_id }}
651653
aws-region: us-east-1
652-
- name: Read publish secrets
654+
- name: Cloudflare token
655+
id: cloudflare-token
653656
if: needs.plan.outputs.dry_run != 'true'
654-
uses: aws-actions/aws-secretsmanager-get-secrets@2297f9a879480a9e3af9b293ed15c70caf8e1c88 # v3.0.2
655-
with:
656-
secret-ids: CLOUDFLARE_API_TOKEN,opencode/release/CLOUDFLARE_API_TOKEN
657-
parse-json-secrets: false
657+
run: bash .github/release/cloudflare-token.sh
658+
env:
659+
CLOUDFLARE_TOKEN_URL: ${{ vars.CLOUDFLARE_TOKEN_URL }}
658660
- name: Publish
659661
run: bash .github/release/publish-cli.sh "$RUNNER_TEMP/in/build-archives" "$RUNNER_TEMP/in/sign-macos" "$RUNNER_TEMP/in/sign-windows"
660662
env: &cloudflare
661663
CLOUDFLARE_ACCOUNT_ID: 15d29c8639fd3733b1b5486a2acfd968
664+
CLOUDFLARE_API_TOKEN: ${{ steps.cloudflare-token.outputs.token }}
662665

663666
publish-desktop:
664667
needs: [plan, sign-macos, sign-desktop-windows-installer]
@@ -683,12 +686,12 @@ jobs:
683686
role-to-assume: arn:aws:iam::604439173067:role/github/opencode-release
684687
role-session-name: opencode-${{ github.run_id }}
685688
aws-region: us-east-1
686-
- name: Read publish secrets
689+
- name: Cloudflare token
690+
id: cloudflare-token
687691
if: needs.plan.outputs.dry_run != 'true'
688-
uses: aws-actions/aws-secretsmanager-get-secrets@2297f9a879480a9e3af9b293ed15c70caf8e1c88 # v3.0.2
689-
with:
690-
secret-ids: CLOUDFLARE_API_TOKEN,opencode/release/CLOUDFLARE_API_TOKEN
691-
parse-json-secrets: false
692+
run: bash .github/release/cloudflare-token.sh
693+
env:
694+
CLOUDFLARE_TOKEN_URL: ${{ vars.CLOUDFLARE_TOKEN_URL }}
692695
- name: Publish
693696
run: |
694697
bash .github/release/publish-desktop.sh "$RUNNER_TEMP/in/sign-macos" "$RUNNER_TEMP/in/sign-desktop-windows-installer" \

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.