Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/publish-database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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:
# 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 }}
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
24 changes: 4 additions & 20 deletions .github/workflows/update-database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<account-id>.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 <previous-commit>:Sources/SwiftIP2ASN/Resources/ip2asn.ultra > /path/to/rollback/ip2asn.ultra
Expand All @@ -217,17 +222,18 @@ 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
```

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
Expand Down
74 changes: 59 additions & 15 deletions Scripts/publish-database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
Loading