|
| 1 | +name: Mint test |
| 2 | + |
| 3 | +# One-off: first live mint from the Cloudflare token issuer. Delete after it has run. |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [mint-test] |
| 7 | + |
| 8 | +permissions: |
| 9 | + id-token: write |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + mint: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + environment: release # must match the stage in config.ts |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 18 | + - uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0 |
| 19 | + with: |
| 20 | + role-to-assume: arn:aws:iam::604439173067:role/github/opencode-release |
| 21 | + aws-region: us-east-1 |
| 22 | + - name: Cloudflare token |
| 23 | + id: cloudflare-token |
| 24 | + env: |
| 25 | + CLOUDFLARE_TOKEN_URL: ${{ vars.CLOUDFLARE_TOKEN_URL }} |
| 26 | + CLOUDFLARE_ACCOUNT_ID: "15d29c8639fd3733b1b5486a2acfd968" |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | + set +x |
| 30 | + url="${CLOUDFLARE_TOKEN_URL:-https://kjivkui2evldapbok5fhrrl6oy0fctkp.lambda-url.us-east-1.on.aws/}" |
| 31 | + url_pattern='^https://[a-z0-9]+\.lambda-url\.us-east-1\.on\.aws/$' |
| 32 | + if ! [[ "$url" =~ $url_pattern ]]; then echo "::error::CLOUDFLARE_TOKEN_URL is not the token issuer's function URL"; exit 1; fi |
| 33 | + case "$CLOUDFLARE_ACCOUNT_ID" in *[!0-9a-f]*|"") echo "::error::bad account id"; exit 1 ;; esac |
| 34 | + if [ ${#CLOUDFLARE_ACCOUNT_ID} -ne 32 ]; then echo "::error::bad account id"; exit 1; fi |
| 35 | + for name in AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; do |
| 36 | + [ -n "${!name:-}" ] || { echo "::error::$name is not set"; exit 1; } |
| 37 | + done |
| 38 | + response=$(printf 'user = "%s:%s"\nheader = "x-amz-security-token: %s"\n' \ |
| 39 | + "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "$AWS_SESSION_TOKEN" | |
| 40 | + curl --config - --proto '=https' --proto-redir '=https' \ |
| 41 | + --aws-sigv4 "aws:amz:us-east-1:lambda" --request POST --url "$url" \ |
| 42 | + --fail-with-body --silent --show-error --max-time 20) |
| 43 | + token=$(printf '%s' "$response" | jq -er 'if (.token|type)=="string" then .token else error("token") end' 2>/dev/null) |
| 44 | + expires=$(printf '%s' "$response" | jq -er 'if (.expires_on|type)=="string" then .expires_on else error("expires_on") end' 2>/dev/null) |
| 45 | + case "$token" in ""|*[[:space:]]*) echo "::error::invalid token"; exit 1 ;; esac |
| 46 | + if [ ${#token} -lt 8 ] || [ ${#token} -gt 512 ]; then echo "::error::invalid token length"; exit 1; fi |
| 47 | + case "$expires" in ""|*[[:space:]]*) echo "::error::invalid expires_on"; exit 1 ;; esac |
| 48 | + iso_pattern='^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?Z$' |
| 49 | + if ! printf '%s' "$response" | jq -e --arg re "$iso_pattern" '(.expires_on | type) == "string" and (.expires_on | test($re))' > /dev/null; then |
| 50 | + echo "::error::invalid expires_on"; exit 1 |
| 51 | + fi |
| 52 | + if ! [[ "$expires" =~ $iso_pattern ]]; then |
| 53 | + echo "::error::invalid expires_on"; exit 1 |
| 54 | + fi |
| 55 | + printf '::add-mask::%s\n' "$token" |
| 56 | + if [ -n "${GITHUB_OUTPUT:-}" ]; then |
| 57 | + printf 'token=%s\nexpires_on=%s\n' "$token" "$expires" >> "$GITHUB_OUTPUT" |
| 58 | + elif [ -n "${GITHUB_ENV:-}" ]; then |
| 59 | + printf 'CLOUDFLARE_API_TOKEN=%s\n' "$token" >> "$GITHUB_ENV" |
| 60 | + fi |
| 61 | + if [ "${CLOUDFLARE_TOKEN_EXPORT:-step}" = "environment" ] && [ -n "${GITHUB_ENV:-}" ]; then |
| 62 | + printf 'CLOUDFLARE_API_TOKEN=%s\n' "$token" >> "$GITHUB_ENV" |
| 63 | + fi |
| 64 | + export CLOUDFLARE_API_TOKEN="$token" |
| 65 | + verify_url="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/tokens/verify" |
| 66 | + if ! verify=$(printf 'header = "Authorization: Bearer %s"\nurl = "%s"\n' "$token" "$verify_url" | |
| 67 | + curl --config - --proto '=https' --proto-redir '=https' --silent --show-error --fail-with-body --max-time 20); then |
| 68 | + echo "::error::Cloudflare rejected the freshly minted token. The runner's egress IP may differ from the IP Lambda saw (IPv4 vs IPv6 or a different NAT address)." >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + printf '%s' "$verify" | jq -e '.success == true and .result.status == "active"' > /dev/null |
| 72 | + printf "%s" "$verify" | jq -r "\"token id: \" + .result.id" |
| 73 | + echo "token verified active; expires $expires" |
0 commit comments