|
| 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" |