Files
openwaf/Dockerfile
T
Pavlo Ratushnyi 7afa7541a2 The geo pin has a shelf life now, and a gate that checks it
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.
2026-10-01 16:09:11 +02:00

121 lines
5.8 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# syntax=docker/dockerfile:1.7
#
# openwaf edge — framework-dependent publish on a chiseled distroless runtime.
#
# Why not NativeAOT: the reference deployment (~/work/neemle/landing) tried it
# and recorded the reason in its Dockerfile — EF Core under AOT needs every LINQ
# query precompiled and every request/response shape annotated, and the size win
# washes out against the chiseled runtime base. Not relitigated here.
#
# Stages:
# 1. restore – NuGet layer, cached independently of source changes
# 2. build – framework-dependent publish, debug artefacts stripped
# 3. runtime – mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled, no shell
#
# HEALTHCHECK re-executes the binary in --healthcheck mode: a chiseled base has
# no shell, no wget and no curl, so the binary is the only probe available.
ARG DOTNET_VERSION=10.0
# Global scope on purpose: an ARG declared inside a stage is not visible to a
# later stage, and the geoip stage re-declares these without defaults to pick
# these values up. Declaring them after the build stage is what made the URL
# expand to dbip-city-lite-.mmdb.gz and 404 in the geoip stage.
ARG DBIP_VERSION=2026-10
ARG DBIP_EDITION=country
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS restore
WORKDIR /src
COPY dotnet/global.json dotnet/Directory.Build.props dotnet/NuGet.Config dotnet/OpenWaf.slnx ./
COPY dotnet/OpenWaf.Domain/OpenWaf.Domain.csproj OpenWaf.Domain/
COPY dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj OpenWaf.Infrastructure/
COPY dotnet/OpenWaf.Edge/OpenWaf.Edge.csproj OpenWaf.Edge/
COPY dotnet/OpenWaf.Tests.Unit/OpenWaf.Tests.Unit.csproj OpenWaf.Tests.Unit/
COPY dotnet/OpenWaf.Tests.Integration/OpenWaf.Tests.Integration.csproj OpenWaf.Tests.Integration/
RUN dotnet restore OpenWaf.Edge/OpenWaf.Edge.csproj
FROM restore AS build
COPY dotnet/ ./
RUN dotnet publish OpenWaf.Edge/OpenWaf.Edge.csproj \
-c Release \
-p:DebugType=none \
-p:DebugSymbols=false \
-o /publish/api \
&& cd /publish/api \
&& rm -f *.pdb \
&& rm -rf BuildHost-net472 BuildHost-netcore
# Stage the volume mount points with app:app ownership — a chiseled image has
# no shell, so there is no mkdir at runtime.
RUN mkdir -p /publish/data /publish/geoip
# GeoIP: dbip lite editions, free and redistributable, same MMDB binary format as
# MaxMind. Kept under the legacy GeoLite2-Country.mmdb name so one env var
# covers both, and the reader sniffs Metadata.DatabaseType rather than trusting
# the filename — a database stored under a country name is exactly the
# trap the reference repo documented. MaxMindGeoIpLookup reads DatabaseType,
# 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), 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
# time it is created. An existing deployment keeps the OLD database after a
# rebuild; `docker volume rm <project>_openwaf-geoip` is what makes the new one
# land. /healthz's geoBuiltAt is how you check which one you are actually
# running.
#
# DBIP_EDITION=country is the default: the geo pillar decides on the ISO country
# code, and country-lite is 4 MB compressed / 6 MB on disk. city-lite is 60 MB
# compressed / 127 MB on disk and buys only the city+region columns on
# observations (the map UI). Build --build-arg DBIP_EDITION=city for that.
#
# Downloaded in its own stage with a static curl image: doing it in the SDK
# stage means apt-get update on every cache miss, and that is the slowest and
# least reproducible part of the build (observed: it stalls, the download
# itself takes two seconds). gunzip is part of curlimages/curl's busybox.
# The image runs as uid 100 (curl_user), so the download lands in /tmp, the
# only world-writable directory in it; COPY re-owns the file as app:app.
FROM curlimages/curl:8.10.1 AS geoip
ARG DBIP_VERSION
ARG DBIP_EDITION
RUN mkdir -p /tmp/geoip && \
curl --fail --silent --show-error --location --retry 3 --retry-delay 2 \
"https://download.db-ip.com/free/dbip-${DBIP_EDITION}-lite-${DBIP_VERSION}.mmdb.gz" \
| gunzip > /tmp/geoip/GeoLite2-Country.mmdb && \
ls -la /tmp/geoip/GeoLite2-Country.mmdb
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}-noble-chiseled AS runtime
# Chiseled ships a non-root `app` user (UID 1654) and no shell.
WORKDIR /app/api
COPY --from=build --chown=app:app /publish/api /app/api
COPY --from=build --chown=app:app /publish/data /app/data
COPY --from=geoip --chown=app:app /tmp/geoip /app/geoip
USER app:app
ENV ASPNETCORE_URLS=http://+:8080 \
ASPNETCORE_FORWARDEDHEADERS_ENABLED=false \
ConnectionStrings__OpenWaf="Data Source=/app/data/openwaf.db" \
Database__Provider=sqlite \
GEOIP_MAXMIND_DB_PATH=/app/geoip/GeoLite2-Country.mmdb \
HEALTHCHECK_PORT=8080
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=10s \
CMD ["dotnet", "/app/api/OpenWaf.Edge.dll", "--healthcheck"]
ENTRYPOINT ["dotnet", "/app/api/OpenWaf.Edge.dll"]