The Dockerfile pins `ARG DBIP_VERSION=YYYY-MM`, and moving that pin was a habit
someone kept by hand: 2026-10 on 2026-10-01, 2026-09 the month before that. The
runtime answer was not better. `/healthz`'s `geoStale` is a 90-day alarm, and a
month behind is not 90 days. Measured on this tree: a container whose named
volume held `dbip-country-lite-2026-09` while the Dockerfile asked for `2026-10`
reported `geoBuiltAt: 2026-09-01T01:32:45+00:00` and `geoStale: false` — and
every geo assertion in `compose-smoke.sh` stayed green, the country deny list
still refusing 8.8.8.8 with 451. The policy was being enforced on a database
nobody chose, reported as healthy.
Three pieces:
- `scripts/geo-db-freshness.sh` compares the pin with the newest release the
vendor actually serves (14 HEAD probes, newest first). Three exit codes, not
two: 0 current, 1 the vendor has 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. `MACHINE=1` prints one line for the bump script.
- `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
(dbip publishes on the 1st; the second date catches a late publish or one
failed run). It deliberately does not open a PR — a bot that cannot run the
geo assertions bumping a data dependency is a worse default than a red job.
`scripts/compose-smoke.sh` grew the assertion that ties the pin to the running
container: `the loaded geo database is the pinned one`. Its teeth were checked,
not assumed — the stale volume above produced exactly one failure:
FAIL the loaded geo database is the pinned one Dockerfile pins 2026-10, the container loaded 2026-09
a named volume is seeded once: docker volume rm openwaf_openwaf-geoip
compose smoke: 27 passed, 1 failed
and after `docker volume rm`, **28 passed, 0 failed**.
Numbers on this tree. Host, `scripts/ci-gates.sh all`, EXIT=0: build 0 warnings,
unit 54/54, integration 28/28, smoke 19/19, image 261 MB, compose-smoke 28/28.
Linux rehearsal (third run today), EXIT=0: source PASS (54/54, 28/28, 19/19),
compose 28/28 with enforcement lag 36 ms, console 26/26. The freshness script
itself in `mcr.microsoft.com/dotnet/sdk:10.0`: current pin exits 0, a forced
`PIN=2026-05` exits 1 with the bump command. actionlint 1.7.12 over both
workflow files: 0 problems; shellcheck and `bash -n` clean.
Honest edges. The scheduled job has never run, because the repository has no
remote; and GitHub disables schedules on a repository with no push for 60 days,
so a cron is a nudge with a failure mode. A red freshness job can mean "no
network" rather than "stale" — the message names which, and exit 2 is not exit 0.
114 lines
4.6 KiB
Bash
Executable File
114 lines
4.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Is the geo database the image bakes still the one the vendor publishes?
|
|
#
|
|
# The Dockerfile pins ARG DBIP_VERSION=YYYY-MM. A pin is a decision with a shelf
|
|
# life: dbip ships a new lite database every month, and a country policy
|
|
# enforced on a stale one misclassifies reallocated ranges invisibly. /healthz
|
|
# already reports geoBuiltAt and geoStale at runtime; this is the build-time
|
|
# half, and it is the half that was left to memory ("the pin was moved by hand
|
|
# on 2026-10-01, and the month before that it was 2026-09").
|
|
#
|
|
# scripts/geo-db-freshness.sh # compare the pin with the vendor
|
|
# DBIP_EDITION=city scripts/geo-db-freshness.sh
|
|
# MACHINE=1 scripts/geo-db-freshness.sh # one line: <pin> <newest> <state>
|
|
#
|
|
# 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 <project>_openwaf-geoip' — the volume is seeded once." >&2
|
|
fi
|
|
exit 1
|