diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc3bf68..69949ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,12 +18,16 @@ # (restore, build, unit, integration, smoke) in mcr.microsoft.com/dotnet/sdk:10.0 # — Ubuntu 24.04.5, SDK 10.0.401 resolving the pinned global.json, 54/54 + 28/28 # + 19/19, first try, 1:02.81 wall cold: evidence that the 25-minute timeout -# below is not near. Compose gate in docker:cli against the host daemon: 27/27 on +# below is not near. Compose gate in docker:cli against the host daemon: 28/28 on # Linux. Console gate in mcr.microsoft.com/playwright:v1.63.0-noble — the stack # seeded by docker:cli, the same console-check.cjs driving it in a real Linux # Chromium: 26/26. So every gate this file runs now has a Linux number. What it # does not have is a runner: the repository has no remote, so this file has # never executed on GitHub. +# +# One gate is not in this file: geo database freshness. It is a schedule, not a +# push gate (the pin ages on the vendor's calendar, not on ours), so it lives in +# .github/workflows/geo-freshness.yml and calls scripts/geo-db-freshness.sh. name: ci @@ -121,7 +125,8 @@ jobs: # # Honest status: this job has never run on a runner, like the rest of the # file. What was verified is the gate it calls — scripts/ci-gates.sh console - # passed locally (26 assertions, playwright-core 1.61.1), and its negative + # passed locally (26 assertions, playwright-core 1.63.0 — the version pinned + # right above, and rehearsed on Linux in the Playwright image), and its # control (a wrong token) exits 1 rather than reporting success. The step # calls the gate by name rather than retyping the script, so the CI definition # and the operator command cannot drift. diff --git a/.github/workflows/geo-freshness.yml b/.github/workflows/geo-freshness.yml new file mode 100644 index 0000000..eeb9c83 --- /dev/null +++ b/.github/workflows/geo-freshness.yml @@ -0,0 +1,56 @@ +# The geo database the image bakes is pinned by a build ARG (Dockerfile +# ARG DBIP_VERSION=YYYY-MM) and dbip publishes a new lite database every month. +# A pin with nothing attached to it is a reminder someone has to keep: this one +# was moved by hand on 2026-10-01, and the month before that it sat at 2026-09. +# +# This job asks the vendor what the newest release is and goes red when the pin +# is behind, printing the command that moves it (scripts/geo-db-bump.sh). It is +# the only job here that talks to the internet on purpose. +# +# It deliberately does NOT open a pull request. A bot that cannot run the geo +# assertions bumping a data dependency unreviewed is a worse default than a red +# job a human reads; and the bump is not the whole change — an existing +# deployment keeps the old database until its named volume is removed, which is +# exactly the thing a green PR would hide. +# +# Honest status, same as every other file in this directory: it has never run, +# because the repository has no remote. One more thing worth knowing about +# GitHub rather than about this project: schedules are disabled on a repository +# with no push for 60 days, so a cron is a nudge with a failure mode, not a +# guarantee. + +name: geo-freshness + +on: + schedule: + # dbip has published the month's release on the 1st so far. Check on the 1st + # and again a week later, so a late publish or a single failed run is still + # caught inside the same month. + - cron: '17 3 1,8 * *' + workflow_dispatch: + push: + paths: + - Dockerfile + - scripts/geo-db-freshness.sh + - scripts/geo-db-bump.sh + - .github/workflows/geo-freshness.yml + +permissions: + contents: read + +jobs: + freshness: + name: the pinned geo database vs the vendor + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + + # Three answers, not two: 0 the pin is current, 1 the vendor serves + # something newer, 2 could not tell (no network, or a pin the vendor has + # never served — which would make the image build fail on a 404). The exit + # code is passed through untouched, because a freshness check that reports + # success having checked nothing is the exact failure this job exists to + # prevent. + - name: pin vs vendor + run: ./scripts/geo-db-freshness.sh diff --git a/Dockerfile b/Dockerfile index 3f7cb70..7166849 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,10 +56,17 @@ RUN mkdir -p /publish/data /publish/geoip # never the name, so either edition below works without a code change. # # DBIP_VERSION is pinned, which means the database ages with the image. /healthz -# reports the loaded database's build date and a geoStale flag (90 days) so a -# stale one is visible at runtime, not only at build time. Bumping this argument -# on a schedule from CI is NOT implemented — the pin was moved by hand to -# 2026-10 on 2026-10-01, and the month before that it was 2026-09. +# reports the loaded database's build date and a geoStale flag (90 days), but the +# 90 days is an alarm, not the policy: measured 2026-10-01, a container running a +# one-month-old database reported geoBuiltAt 2026-09-01 and geoStale:false while +# this ARG asked for 2026-10. A month behind is invisible at runtime. +# +# So the build-time half exists: scripts/geo-db-freshness.sh compares this pin +# with the newest release the vendor serves (exit 0 current, 1 behind, 2 cannot +# tell), scripts/geo-db-bump.sh rewrites it and refuses to write anything when +# the check could not run, and .github/workflows/geo-freshness.yml runs the check +# on a schedule. The pin was moved by hand to 2026-10 on 2026-10-01; the check +# says 2026-10 is what the vendor serves today. # # One trap with the bump: docker-compose.yml mounts a named volume at # /app/geoip, and Docker seeds a named volume from the image only the first diff --git a/README.md b/README.md index c8c5100..3f482fb 100644 --- a/README.md +++ b/README.md @@ -131,10 +131,37 @@ Database: `Database:Provider` = `sqlite` (default) or `postgres`, ### The geo database ages on purpose, and a rebuild does not replace it The image bakes a dbip-country-lite MMDB pinned by `ARG DBIP_VERSION` -(currently `2026-10`, moved by hand on 2026-10-01; the bump is not automated in -CI). `/healthz` reports `geoBuiltAt`, `geoDatabase` and `geoStale` (over 90 -days), and `/admin-api/geo` reports the age in days, so a stale database is -visible at runtime rather than only at build time. +(currently `2026-10`, moved by hand on 2026-10-01). `/healthz` reports +`geoBuiltAt`, `geoDatabase` and `geoStale` (over 90 days), and `/admin-api/geo` +reports the age in days. + +The 90-day flag is an alarm, not the policy, and the gap is measurable: with the +named volume holding `dbip-country-lite-2026-09` while the Dockerfile pinned +`2026-10`, `/healthz` said `geoBuiltAt: 2026-09-01T01:32:45+00:00` and +`geoStale: false`, and every geo assertion in the smoke test still passed — the +country deny list still refused 8.8.8.8 with 451. A month behind is a policy +nobody chose, reported as healthy. + +Three things close it: + +* `scripts/geo-db-freshness.sh` compares the pin with the newest release the + vendor actually serves and answers with three codes, not two: 0 current, 1 the + vendor has something newer (it prints the bump), 2 could not tell — no + network, or a pin the vendor has never served, which would make the image build + fail on a 404. "Could not check" is never printed as ok. +* `scripts/geo-db-bump.sh` rewrites the ARG and prints the two commands that make + the new database actually land. It refuses to write anything when the check + could not run: a pin moved on the strength of a check that did not run is worse + than a pin left alone. +* `.github/workflows/geo-freshness.yml` runs the check on the 1st and the 8th, + and deliberately does not open a pull request — a bot that cannot run the geo + assertions bumping a data dependency is a worse default than a red job. + +`scripts/compose-smoke.sh` asserts the pin against the running container +(`the loaded geo database is the pinned one`). Its teeth were checked, not +assumed: against the stale volume above it printed `Dockerfile pins 2026-10, the +container loaded 2026-09` plus the `docker volume rm` line, **27 passed, 1 +failed**, and after removing the volume **28 passed, 0 failed**. `docker-compose.yml` mounts a named volume at `/app/geoip` so a redeploy does not re-download 8 MB. Docker seeds a named volume from the image only the first @@ -263,8 +290,12 @@ also the answer to whether `setup-dotnet` with `10.0.x` satisfies the pin), unit **54/54**, integration **28/28**, smoke **19/19**, first try, nothing to fix; wall clock with a cold container and a cold NuGet cache **1:02.81**, so the source job's 25-minute timeout is not close even on a slower CPU. Compose gate on -Linux: **27 passed, 0 failed**, enforcement lag 12 ms after a ban was written. -Console gate on Linux: **26 assertions, 0 failed** — described below. +Linux: **28 passed, 0 failed** (27 before the geo-pin assertion was added), +enforcement lag 12 ms and 36 ms in two runs. Console gate on Linux: **26 +assertions, 0 failed** — described below. `scripts/geo-db-freshness.sh` was run +in `mcr.microsoft.com/dotnet/sdk:10.0` too: current pin answers 0, and a forced +`PIN=2026-05` answers 1 with the bump command, so the gate that decides whether +the geo database is current behaves the same on the runner's OS. Getting the compose stage green surfaced two things about the harness rather than the product, both now written into the script. The gate asserts over JSON with @@ -327,7 +358,7 @@ dotnet/ OpenWaf.Tests.Unit/ 36 tests over the decision logic OpenWaf.Tests.Integration/ 19 tests driving the real pipeline against a fake upstream scripts/smoke.sh 19 assertions against a running edge + a real upstream -scripts/compose-smoke.sh 27 assertions against the built image (compose + nginx) +scripts/compose-smoke.sh 28 assertions against the built image (compose + nginx) .github/workflows/ci.yml runs all four gates on every push docs/ARCHITECTURE.md the design authority and its rulings ``` diff --git a/scripts/ci-gates.sh b/scripts/ci-gates.sh index 9deb3b6..54eb1cb 100755 --- a/scripts/ci-gates.sh +++ b/scripts/ci-gates.sh @@ -7,6 +7,11 @@ # scripts/ci-gates.sh restore|build|unit|integration|smoke|image|compose-smoke|all # scripts/ci-gates.sh console (needs a real Chromium; not part of `all`) # +# Not here on purpose: scripts/geo-db-freshness.sh. It asks the vendor what the +# newest geo database is, so it is the one gate that talks to the internet, and +# putting it behind `all` would make the offline claim below false. It has its own +# scheduled job (.github/workflows/geo-freshness.yml). +# # OPENWAF_ADMIN_TOKEN defaults below to the same throwaway CI uses; set it to # override. Nothing here talks to anything outside the run. diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index 39395ba..267a068 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -68,6 +68,26 @@ check 'image HEALTHCHECK reports healthy' 'healthy' "$(docker inspect --format ' check 'healthz says ok' 'ok' "$(curl -s "$EDGE/healthz" | sed -n 's/.*"status":"\([^"]*\)".*/\1/p')" check 'geo database is loaded' 'true' "$(curl -s "$EDGE/healthz" | sed -n 's/.*"geoLoaded":\([^,]*\).*/\1/p')" check 'healthz names the geo database' 'yes' "$(curl -s "$EDGE/healthz" | grep -q '"geoDatabase":"DBIP' && echo yes || echo no)" + +# The geo pin is a build ARG, and this compose file mounts a named volume at +# /app/geoip that Docker seeds from the image only when the volume is first +# created. So a rebuilt image can be running last month's database and every +# geo assertion below still passes — the policy is enforced on a database nobody +# chose. Compare what the container loaded with what the Dockerfile asked for. +# The month prefix is enough because dbip stamps the release with the 1st of its +# own month (observed: 2026-09-01T01:32:45 and 2026-10-01). If that ever changes, +# this assertion goes red first, which is the safer direction for a gate. +PINNED=$(awk -F= '/^ARG DBIP_VERSION=/{print $2; exit}' Dockerfile) +BUILT=$(curl -s "$EDGE/healthz" | sed -n 's/.*"geoBuiltAt":"\([0-9]\{4\}-[0-9]\{2\}\)[^"]*".*/\1/p') +if [ -n "$BUILT" ] && [ "$BUILT" = "$PINNED" ]; then + PASS=$((PASS+1)); printf ' ok %-54s %s\n' 'the loaded geo database is the pinned one' "$BUILT" +else + FAIL=$((FAIL+1)) + printf ' FAIL %-54s Dockerfile pins %s, the container loaded %s\n' \ + 'the loaded geo database is the pinned one' "$PINNED" "${BUILT:-no build date}" + printf ' a named volume is seeded once: docker volume rm %s\n' \ + "$(docker compose config --name 2>/dev/null || basename "$PWD")_openwaf-geoip" +fi check 'admin api rejects no token' '401' "$(code "$EDGE/admin-api/blocks")" # Start from a known state. Engine bans only — an operator's manual list is diff --git a/scripts/geo-db-bump.sh b/scripts/geo-db-bump.sh new file mode 100755 index 0000000..1132604 --- /dev/null +++ b/scripts/geo-db-bump.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +# Move the geo database pin to the newest release the vendor serves. +# +# The pin (Dockerfile ARG DBIP_VERSION) used to be moved by hand, once a month, +# by whoever remembered. This is the half that makes the freshness check +# actionable instead of just noisy: it rewrites the ARG and prints the two +# commands that actually make the new database land. +# +# scripts/geo-db-bump.sh # rewrite the pin, print the follow-ups +# DRY_RUN=1 scripts/geo-db-bump.sh # say what it would change, change nothing +# +# It refuses rather than guessing. If the vendor could not be reached, or the +# newest release could not be determined, nothing is written: a pin moved on the +# strength of a check that did not run is worse than a pin left alone. + +set -euo pipefail +cd "$(dirname "$0")/.." + +DOCKERFILE=${DOCKERFILE:-Dockerfile} +DRY_RUN=${DRY_RUN:-0} + +# `state=$?` on the next line would read the exit status of the assignment, and +# under set -e a non-zero command substitution ends the script before it gets +# there — the behind case would exit with the check's code instead of bumping. +state=0 +line=$(MACHINE=1 ./scripts/geo-db-freshness.sh) || state=$? +echo "geo bump: freshness says: $line (exit $state)" + +case "$state" in + 0) + echo 'geo bump: nothing to do — the pin is already the newest release.' + exit 0 ;; + 1) : ;; # behind: the case this script exists for + *) + echo 'geo bump: REFUSED — the freshness check could not name a newer release.' >&2 + echo ' Nothing was written. Fix the check (network, or the pin) and re-run.' >&2 + exit 2 ;; +esac + +newest=$(printf '%s' "$line" | awk '{print $2}') +pin=$(printf '%s' "$line" | awk '{print $1}') +[ -n "$newest" ] && [ "$newest" != unknown ] || { + echo 'geo bump: REFUSED — no release to bump to.' >&2; exit 2; } + +if [ "$DRY_RUN" = 1 ]; then + echo "geo bump: DRY_RUN — would rewrite ARG DBIP_VERSION=$pin to $newest in $DOCKERFILE" + exit 0 +fi + +before=$(grep -n "^ARG DBIP_VERSION=" "$DOCKERFILE") +tmp=$(mktemp) +awk -v v="$newest" 'BEGIN{done=0} /^ARG DBIP_VERSION=/{ if(!done){sub(/^ARG DBIP_VERSION=.*/,"ARG DBIP_VERSION=" v); done=1} } {print}' "$DOCKERFILE" > "$tmp" +mv "$tmp" "$DOCKERFILE" + +echo "geo bump: $DOCKERFILE" +echo " before: $before" +echo " after: $(grep -n '^ARG DBIP_VERSION=' "$DOCKERFILE")" + +# The part that is easy to forget and costs an afternoon: the compose file mounts +# a named volume at /app/geoip, and Docker seeds a named volume from the image +# only when the volume is first created. Rebuilding does not replace the +# database an existing deployment runs on. +project=$(basename "$PWD") +if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then + project=$(docker compose config --name 2>/dev/null || printf '%s' "$project") +fi + +cat < +# +# Exit codes, deliberately three: +# 0 the pin is the newest release the vendor serves +# 1 the vendor serves something newer — the pin has to move (prints how) +# 2 cannot tell: no network, no reachable release, or a pin the vendor has +# never served (which would make the image build fail, so it is worth +# knowing before CI builds it). "Could not check" is never reported as ok. + +set -euo pipefail +cd "$(dirname "$0")/.." + +DOCKERFILE=${DOCKERFILE:-Dockerfile} +PIN=${PIN:-$(awk -F= '/^ARG DBIP_VERSION=/{print $2; exit}' "$DOCKERFILE")} +EDITION=${DBIP_EDITION:-$(awk -F= '/^ARG DBIP_EDITION=/{print $2; exit}' "$DOCKERFILE")} +LOOKBACK=${LOOKBACK:-12} +MAX_TIME=${MAX_TIME:-20} +[ -n "$EDITION" ] || EDITION=country + +[ -n "$PIN" ] || { echo "geo freshness: cannot read ARG DBIP_VERSION from $DOCKERFILE" >&2; exit 2; } +MACHINE=${MACHINE:-0} +[ "$MACHINE" = 1 ] || echo "geo freshness: pin $PIN (edition $EDITION, read from $DOCKERFILE)" + +# Month arithmetic without GNU date: BSD date has -v, GNU has -d, and the runner +# has the one while a laptop usually has the other. Counting months as an +# integer works everywhere. +month_index() { printf '%s' $(( 10#$1 * 12 + 10#$2 )); } +pin_idx=$(month_index "${PIN%-*}" "${PIN##*-}") + +probe() { # -> an HTTP status, or 000 when the question was never answered + # curl prints 000 itself when the transfer fails AND exits non-zero, so a + # naive `|| echo 000` yields "000000" and a dead network looks like a server + # that answered. Everything that is not a real status is normalised here. + out=$(curl -sIL --max-time "$MAX_TIME" -o /dev/null -w '%{http_code}' \ + "https://download.db-ip.com/free/dbip-${EDITION}-lite-$1.mmdb.gz" 2>/dev/null) || true + case "${out:0:3}" in + 200|404) printf '%s' "${out:0:3}" ;; + *) printf '000' ;; + esac +} + +# Today's month, then LOOKBACK months back, plus one month forward: dbip has +# published the month's release on the 1st before now, and probing forward +# catches a release that lands early rather than a month late. +now_idx=$(month_index "$(date -u +%Y)" "$(date -u +%m)") +newest='' +probed=0; reachable=0 +for ((i = 1; i <= LOOKBACK + 2; i++)); do + idx=$(( now_idx + 2 - i )) + y=$(( (idx - 1) / 12 )); m=$(( (idx - 1) % 12 + 1 )) + v=$(printf '%04d-%02d' "$y" "$m") + code=$(probe "$v") + probed=$(( probed + 1 )) + [ "$code" = 000 ] && continue + reachable=1 + if [ "$code" = 200 ]; then + newest=$v + break # probing newest-first: the first 200 is the newest + fi +done + +if [ "$reachable" = 0 ]; then + if [ "$MACHINE" = 1 ]; then echo "$PIN unknown unreachable"; else + echo "geo freshness: CANNOT CHECK — $probed probes, no answer from download.db-ip.com" >&2 + fi + exit 2 +fi + +if [ -z "$newest" ]; then + if [ "$MACHINE" = 1 ]; then echo "$PIN none no_release"; else + echo "geo freshness: CANNOT CHECK — the vendor served no release in $probed candidate months" >&2 + fi + exit 2 +fi + +newest_idx=$(month_index "${newest%-*}" "${newest##*-}") +[ "$MACHINE" = 1 ] || echo "geo freshness: newest release the vendor serves: $newest" + +if [ "$pin_idx" -eq "$newest_idx" ]; then + if [ "$MACHINE" = 1 ]; then echo "$PIN $newest current"; else + echo "geo freshness: the pin is current." + fi + exit 0 +fi + +if [ "$pin_idx" -gt "$newest_idx" ]; then + if [ "$MACHINE" = 1 ]; then echo "$PIN $newest ahead_of_vendor"; else + echo "geo freshness: the pin names a release the vendor does not serve within the last $LOOKBACK months." >&2 + echo " The image build would fail on a 404. Check the ARG, or raise LOOKBACK." >&2 + fi + exit 2 +fi + +behind=$(( newest_idx - pin_idx )) +if [ "$MACHINE" = 1 ]; then echo "$PIN $newest behind_$behind"; else + echo "geo freshness: the pin is $behind month(s) behind. Bump it:" >&2 +echo " scripts/geo-db-bump.sh # rewrites the ARG, then rebuild" >&2 +echo " and remember: an existing deployment keeps the OLD database until" >&2 +echo " 'docker volume rm _openwaf-geoip' — the volume is seeded once." >&2 +fi +exit 1