From 5271576996294b65c6f35c2cf08214bb706f2e6f Mon Sep 17 00:00:00 2001 From: "David E. Weekly" Date: Mon, 2 Nov 2026 11:32:46 -0800 Subject: [PATCH 1/2] Publish over R2's S3 API; require the CDN to serve the uploaded pair The repo's R2 credential is an S3 access key pair, which Wrangler cannot use. publish-database.sh now uploads with the aws CLI when R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY and R2_ENDPOINT are set, and with Wrangler otherwise. Post-upload verification checked only that the CDN pair was internally consistent, so a failed upload would pass on the previous pair. It now requires the CDN to serve the exact uploaded bytes, retrying past the 300 s cache. Publishing moves to publish-database.yml (workflow_call from the weekly update, or workflow_dispatch with a ref to publish or roll back). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01A8mvWkYd1YBhFyvbFx7UF7 --- .github/workflows/publish-database.yml | 47 ++++++++++++++++ .github/workflows/update-database.yml | 24 ++------- CHANGELOG.md | 8 ++- CONTRIBUTING.md | 22 +++++--- Scripts/publish-database.sh | 74 ++++++++++++++++++++------ 5 files changed, 131 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/publish-database.yml diff --git a/.github/workflows/publish-database.yml b/.github/workflows/publish-database.yml new file mode 100644 index 0000000..5a56a56 --- /dev/null +++ b/.github/workflows/publish-database.yml @@ -0,0 +1,47 @@ +name: Publish IP2ASN Database + +# Publishes a commit's bundled database and manifest to R2 and verifies them +# through pkgs.networkweather.com. Called by update-database.yml after it +# merges; dispatch it by hand to publish main or to roll back to an earlier +# commit's pair. +on: + workflow_dispatch: + inputs: + ref: + description: Commit, branch, or tag whose database to publish + required: true + default: main + type: string + workflow_call: + inputs: + ref: + required: true + type: string + +permissions: + contents: read + +concurrency: + group: publish-database + cancel-in-progress: false + +jobs: + publish: + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + ref: ${{ inputs.ref }} + + - name: Publish to R2 and verify through the CDN + env: + R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} + R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} + R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }} + run: | + if [[ -z "$R2_ACCESS_KEY_ID" || -z "$R2_SECRET_ACCESS_KEY" || -z "$R2_ENDPOINT" ]]; then + echo "::error::R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY and R2_ENDPOINT secrets are required; nothing was published." + exit 1 + fi + Scripts/publish-database.sh publish diff --git a/.github/workflows/update-database.yml b/.github/workflows/update-database.yml index 0eaaebc..4c824f0 100644 --- a/.github/workflows/update-database.yml +++ b/.github/workflows/update-database.yml @@ -192,25 +192,9 @@ jobs: publish-database: needs: update-database if: needs.update-database.outputs.merge_sha != '' - runs-on: macos-latest permissions: contents: read - steps: - - name: Checkout merged database - uses: actions/checkout@v6 - with: - ref: ${{ needs.update-database.outputs.merge_sha }} - - - name: Install Wrangler - run: npm install --global wrangler@4 - - - name: Publish to R2 and verify through the CDN - env: - CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_R2_PUBLISH_TOKEN }} - CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - run: | - if [[ -z "$CLOUDFLARE_API_TOKEN" || -z "$CLOUDFLARE_ACCOUNT_ID" ]]; then - echo "::error::CLOUDFLARE_R2_PUBLISH_TOKEN and CLOUDFLARE_ACCOUNT_ID secrets are required to publish; the merged database is not on the CDN." - exit 1 - fi - Scripts/publish-database.sh publish + uses: ./.github/workflows/publish-database.yml + with: + ref: ${{ needs.update-database.outputs.merge_sha }} + secrets: inherit diff --git a/CHANGELOG.md b/CHANGELOG.md index 85e482c..58ac5e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,13 @@ All notable changes to this project will be documented in this file. ### Changed - The weekly database update now runs CI on its pull request, merges it when CI passes, and publishes the merged database to the CDN without a manual - step. + step. Publishing lives in `publish-database.yml`, which can also be + dispatched by hand to publish or roll back any commit's database. +- `Scripts/publish-database.sh` uploads over R2's S3 API when + `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY` and `R2_ENDPOINT` are set, and + now fails unless the CDN serves exactly the uploaded pair. Previously a + failed upload could pass, because only the served pair's internal + consistency was checked. ### Removed - `RandomIPVerificationTest`, which was unconditionally skipped and asserted diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 25643a3..4d61f64 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -197,11 +197,16 @@ dispatches CI on that pull request, merges it when CI passes, and publishes the merged pair to R2. Any failed gate stops the run before merge or publication and marks the workflow failed. -Publication from CI needs two repository secrets: `CLOUDFLARE_ACCOUNT_ID` and -`CLOUDFLARE_R2_PUBLISH_TOKEN`, an API token limited to object writes on the -`networkweather-pkgs` bucket. +Publication runs in `publish-database.yml`, which uploads over R2's S3 API and +needs three repository secrets from an R2 API token with object read and write +on the `networkweather-pkgs` bucket: `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, +and `R2_ENDPOINT` (`https://.r2.cloudflarestorage.com`). The +publish step fails until the CDN serves exactly the uploaded pair, allowing +for its 300-second cache. -To roll back a bad publication, publish the previous commit's pair: +To roll back a bad publication, dispatch `publish-database.yml` with `ref` set +to the last good commit, or publish that commit's pair from a maintainer +machine: ```bash git show :Sources/SwiftIP2ASN/Resources/ip2asn.ultra > /path/to/rollback/ip2asn.ultra @@ -217,9 +222,9 @@ To publish by hand, verify the committed pair locally: Scripts/publish-database.sh verify ``` -Publishing requires an authenticated Wrangler 4.x session with object-write -access to the production `networkweather-pkgs` R2 bucket. Publication is an -explicit operation: +With the `R2_*` variables above set, the script uploads with the `aws` CLI; +otherwise it uses an authenticated Wrangler 4.x session with object-write +access to the production `networkweather-pkgs` R2 bucket: ```bash Scripts/publish-database.sh publish @@ -227,7 +232,8 @@ Scripts/publish-database.sh publish The script validates the local digest and byte count before upload, publishes the database before its manifest, downloads both through -`pkgs.networkweather.com`, validates them again, and requires a conditional +`pkgs.networkweather.com` until they match the uploaded bytes exactly, +validates them again, and requires a conditional database request to return `304 Not Modified`. ## License diff --git a/Scripts/publish-database.sh b/Scripts/publish-database.sh index 5d6a79d..94171fe 100755 --- a/Scripts/publish-database.sh +++ b/Scripts/publish-database.sh @@ -102,30 +102,74 @@ if [[ "$mode" == "verify" ]]; then exit 0 fi -require_command wrangler +# Upload with R2's S3 API when its keys are set (CI), otherwise with the +# caller's Wrangler login (a maintainer's machine). +if [[ -n "${R2_ACCESS_KEY_ID:-}" || -n "${R2_SECRET_ACCESS_KEY:-}" || -n "${R2_ENDPOINT:-}" ]]; then + if [[ -z "${R2_ACCESS_KEY_ID:-}" || -z "${R2_SECRET_ACCESS_KEY:-}" || -z "${R2_ENDPOINT:-}" ]]; then + echo "R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY and R2_ENDPOINT must all be set." >&2 + exit 64 + fi + require_command aws + put_object() { + AWS_ACCESS_KEY_ID="$R2_ACCESS_KEY_ID" \ + AWS_SECRET_ACCESS_KEY="$R2_SECRET_ACCESS_KEY" \ + AWS_REGION=auto \ + AWS_REQUEST_CHECKSUM_CALCULATION=when_required \ + AWS_RESPONSE_CHECKSUM_VALIDATION=when_required \ + aws s3api put-object \ + --endpoint-url "$R2_ENDPOINT" \ + --bucket "$bucket" \ + --key "$1" \ + --body "$2" \ + --content-type "$3" \ + --cache-control 'public, max-age=300, must-revalidate' \ + --output text --query ETag + } +else + require_command wrangler + put_object() { + wrangler r2 object put "$bucket/$1" \ + --remote \ + --file "$2" \ + --content-type "$3" \ + --cache-control 'public, max-age=300, must-revalidate' + } +fi echo "Publishing database to r2://$bucket/$database_key" -wrangler r2 object put "$bucket/$database_key" \ - --remote \ - --file "$database_path" \ - --content-type application/octet-stream \ - --cache-control 'public, max-age=300, must-revalidate' +put_object "$database_key" "$database_path" application/octet-stream echo "Publishing manifest to r2://$bucket/$manifest_key" -wrangler r2 object put "$bucket/$manifest_key" \ - --remote \ - --file "$manifest_path" \ - --content-type application/json \ - --cache-control 'public, max-age=300, must-revalidate' +put_object "$manifest_key" "$manifest_path" application/json downloaded_database=$(mktemp /tmp/swift-ip2asn-database.XXXXXX) downloaded_manifest=$(mktemp /tmp/swift-ip2asn-manifest.XXXXXX) trap 'rm -f "$downloaded_database" "$downloaded_manifest"' EXIT -curl --fail --silent --show-error --location \ - "$public_database_url" --output "$downloaded_database" -curl --fail --silent --show-error --location \ - "$public_manifest_url" --output "$downloaded_manifest" +# The CDN must serve exactly what was uploaded, not merely a self-consistent +# pair (an earlier one would pass that check). Objects are cached for up to +# 300 seconds, so allow a little longer than that for the new pair to appear. +expected_sha=$(sha256_file "$database_path") +expected_manifest_sha=$(sha256_file "$manifest_path") +attempts="${IP2ASN_PUBLISH_VERIFY_ATTEMPTS:-13}" +for attempt in $(seq 1 "$attempts"); do + curl --fail --silent --show-error --location \ + "$public_database_url" --output "$downloaded_database" + curl --fail --silent --show-error --location \ + "$public_manifest_url" --output "$downloaded_manifest" + if [[ "$(sha256_file "$downloaded_database")" == "$expected_sha" && + "$(sha256_file "$downloaded_manifest")" == "$expected_manifest_sha" ]]; then + break + fi + if ((attempt == attempts)); then + echo "CDN still serves a different database or manifest than was uploaded." >&2 + echo "Expected database SHA-256: $expected_sha" >&2 + echo "Served database SHA-256: $(sha256_file "$downloaded_database")" >&2 + exit 65 + fi + echo "CDN does not serve the new pair yet (attempt $attempt of $attempts); retrying in 30 s." + sleep 30 +done verify_pair "$downloaded_database" "$downloaded_manifest" etag=$(curl --fail --silent --show-error --head "$public_database_url" \ From 197b39b2da85d8edd81bfed47d551ddc025e97a9 Mon Sep 17 00:00:00 2001 From: "David E. Weekly" Date: Mon, 2 Nov 2026 11:35:24 -0800 Subject: [PATCH 2/2] Run the current publisher against the requested ref's artifacts Rollback dispatches with an older ref checked out that commit's publish script, which predates S3 support and fails (Codex P1). Check out the publisher from the workflow's own revision and only the Resources directory from ref, and verify the pair before publishing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01A8mvWkYd1YBhFyvbFx7UF7 --- .github/workflows/publish-database.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish-database.yml b/.github/workflows/publish-database.yml index 5a56a56..7148762 100644 --- a/.github/workflows/publish-database.yml +++ b/.github/workflows/publish-database.yml @@ -29,13 +29,30 @@ jobs: publish: runs-on: macos-latest steps: - - name: Checkout + # The publisher comes from this workflow's own revision; only the + # database and manifest come from `ref`, so rolling back to a commit + # that predates the current script still uses the current script. + - name: Checkout publisher + uses: actions/checkout@v6 + + - name: Checkout artifacts to publish uses: actions/checkout@v6 with: ref: ${{ inputs.ref }} + path: artifact-source + sparse-checkout: Sources/SwiftIP2ASN/Resources + sparse-checkout-cone-mode: true + + - name: Verify artifacts + env: + IP2ASN_DATABASE_PATH: artifact-source/Sources/SwiftIP2ASN/Resources/ip2asn.ultra + IP2ASN_MANIFEST_PATH: artifact-source/Sources/SwiftIP2ASN/Resources/ip2asn.manifest.json + run: Scripts/publish-database.sh verify - name: Publish to R2 and verify through the CDN env: + IP2ASN_DATABASE_PATH: artifact-source/Sources/SwiftIP2ASN/Resources/ip2asn.ultra + IP2ASN_MANIFEST_PATH: artifact-source/Sources/SwiftIP2ASN/Resources/ip2asn.manifest.json R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }}