Files
openwaf/scripts/aot-probe.sh
T
Pavlo Ratushnyi 0a3d28184f
ci / lint the workflow and the shell (push) Failing after 54s
ci / build, test, smoke (push) Successful in 6m4s
ci / the operator page in a real browser (push) Failing after 4m53s
ci / build the image and smoke it (push) Failing after 10m29s
ci / the same smoke on an image with no base (push) Failing after 9m2s
the aot edge serves traffic now, and the shape still missing a contract was its own healthcheck
Measured on this tree with scripts/aot-probe.sh (osx-arm64, a 49,609,680-byte
native binary, 10 IL warnings, 0 errors): the AOT edge reaches "Now listening
on:", creates its 12 tables from the DDL it carries, proxies a request to the
upstream for 200, and its own --healthcheck mode exits 0. The API wall that
stood between the first cut and a serving edge came down in two edits, and the
json-refusal count went 55 -> 1 -> 0.

The 55 were one shape. The anonymous 8-property /healthz literal had no
compiled contract, so the edge was serving traffic while the endpoint a scratch
container's HEALTHCHECK asks answered 500 - an edge that serves and is restarted
anyway. Health/HealthDtos.cs names it (HealthDto, ReadinessDto). The 1 after
that was Microsoft.AspNetCore.Mvc.ProblemDetails, the body Results.Problem
writes and nobody's DTO in this project: its visible cost was a /readyz 500
naming nothing where a 503 naming the failure is the truth. It now says
"Query wasn't precompiled and dynamic code isn't supported with NativeAOT."

The probe's scoreboard is split the way the finding is. Three assertions that
need no query are gated (healthz 200, proxy 200, --healthcheck exit 0), and the
host must be listening with no response refused for want of a contract. Five
that do need a query are recorded with the answer a working edge would give -
operator block 503 where 204 is right, the streak ban never forms, /readyz 503
where 200 is right - and any of them that ever matches prints MOVED, which is
the signal that the query wall moved. queries refused at run time: 32 in that
window, 30 in the run before it; it counts a window, not a total, because the
background refresh loop retries on its own clock.

Two probe bugs surfaced doing this. It sent "Authorization: Bearer" to the admin
API, which AdminAuth does not read, so three of its assertions were measuring a
401 rather than the query wall; and it asserted nothing about whether the host
had listened at all.

Gates on this tree: build 0 warnings, unit 378/378, integration 408/408 in 51 s
(the new one demands a contract for ProblemDetails and ValidationProblemDetails),
smoke 256/256.

Two stale figures corrected rather than left standing. The registered-type count
was 24 in README and ARCHITECTURE while the file at that commit holds 25 -
CountrySummaryDto arrived without the count being re-derived - and it is 29 now.
The anonymous-literal count is 29 in 23 shapes: 22 response shapes, and 7 inside
an EF query's Select, which belong to the query wall rather than this one. The
"48.8 MB" native binary in ARCHITECTURE matches no measured binary of this tree.
2026-10-07 19:28:22 +02:00

188 lines
10 KiB
Bash
Executable File

#!/usr/bin/env bash
# What a NativeAOT edge actually does on this tree, measured rather than argued.
#
# This is not a gate. It is the reproduction of a verdict: the AOT build compiles,
# starts, and creates its own schema, and then every EF query it makes fails,
# because EF cannot compile a LINQ query at run time in a NativeAOT binary and the
# only alternative — precompiling them at build time — does not produce compilable
# code for this model. Run it when the EF version changes, before deciding again
# whether AOT is worth the price.
#
# scripts/aot-probe.sh # publish + start + curl
# scripts/aot-probe.sh --regen # regenerate the EF compiled model first
#
# Needs the NativeAOT toolchain (clang/lld) and takes a few minutes: the ILC step
# alone is over a minute. It is deliberately not in `ci-gates.sh all` — a machine
# without clang would then report a build failure as if the product were broken.
#
# The compiled model is generated for sqlite, the provider the shipped image uses,
# and is compiled into the binary only under -p:OpenWafCompiledModel=true; the
# framework-dependent build still builds its model at start-up and serves both
# providers. See dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj.
set -uo pipefail
cd "$(dirname "$0")/.." || exit 2
RUNTIME=$(dotnet --info 2>/dev/null | sed -n 's/^ *RID: *//p' | head -1)
[ -n "$RUNTIME" ] || { printf 'cannot work out this RID from the dotnet --info output\n' >&2; exit 2; }
PUBLISH=dotnet/OpenWaf.Edge/bin/Release/net10.0/$RUNTIME/publish
BIN=$PUBLISH/OpenWaf.Edge
if [ "${1:-}" = "--regen" ]; then
printf '\n=== regenerate the EF compiled model (sqlite) ===\n'
rm -rf dotnet/OpenWaf.Infrastructure/Persistence/CompiledModel
# OPENWAF_MIGRATE_PROVIDER=sqlite: the design-time factory defaults to postgres,
# and a postgres compiled model in a sqlite image is a different kind of broken.
(cd dotnet && OPENWAF_MIGRATE_PROVIDER=sqlite dotnet ef dbcontext optimize \
--project OpenWaf.Infrastructure --startup-project OpenWaf.Infrastructure \
--context OpenWafDbContext --namespace OpenWaf.Infrastructure.Persistence.CompiledModel \
--output-dir Persistence/CompiledModel) || { printf 'compiled model generation failed\n' >&2; exit 1; }
fi
printf '\n=== publish NativeAOT (%s) ===\n' "$RUNTIME"
# IlcTreatWarningsAsErrors=false and the two NoWarn codes are what let the build
# finish at all: EF's assemblies produce IL2104/IL3053 trim warnings and the
# schema step still calls three design-time APIs. Those are recorded, not hidden —
# the counts are printed below from the same log.
AOT_LOG=/tmp/openwaf-aot-publish.log
(cd dotnet && dotnet publish OpenWaf.Edge/OpenWaf.Edge.csproj -c Release -r "$RUNTIME" \
-p:PublishAot=true -p:InvariantGlobalization=true -p:DebugType=none \
-p:OpenWafCompiledModel=true -p:NoWarn=IL2026%3BIL3050 \
-p:IlcTreatWarningsAsErrors=false) >"$AOT_LOG" 2>&1
publish_status=$?
# The warning count only means something if the ILC actually ran. An up-to-date
# publish skips "Generating native code" and would otherwise report zero warnings
# for a build that produced no output at all.
generated=no
grep -q 'Generating native code' "$AOT_LOG" && generated=yes
printf 'publish exit %s; native code generated: %s; IL warnings: %s; errors: %s\n' \
"$publish_status" "$generated" "$(grep -c ' warning IL[0-9]' "$AOT_LOG")" \
"$(grep -c ' error ' "$AOT_LOG")"
[ "$publish_status" -eq 0 ] || { tail -20 "$AOT_LOG"; exit 1; }
[ -x "$BIN" ] || { printf 'no native binary at %s\n' "$BIN" >&2; exit 1; }
# Absolute, because --contentRoot is resolved against the binary's own directory
# rather than the caller's: measured, a relative one came back doubled
# (…/publish/dotnet/OpenWaf.Edge/…/publish/) and the host died in
# DirectoryNotFoundException before it could say anything useful.
PUBLISH="$(cd "$PUBLISH" && pwd)"
printf 'native binary: %s bytes\n' "$(wc -c < "$BIN" | tr -d ' ')"
EDGE_PORT=${EDGE_PORT:-18077}
UP_PORT=${UP_PORT:-18078}
DB=/tmp/openwaf-aot-probe-$$.db
LOG=/tmp/openwaf-aot-probe-edge.log
TOKEN=aot-probe-token
rm -f "$DB" "$DB-shm" "$DB-wal"
cleanup() {
[ -n "${EDGE_PID:-}" ] && kill "$EDGE_PID" 2>/dev/null
[ -n "${UP_PID:-}" ] && kill "$UP_PID" 2>/dev/null
rm -f "$DB" "$DB-shm" "$DB-wal"
}
trap cleanup EXIT
mkdir -p /tmp/openwaf-upstream && printf 'aot probe upstream\n' > /tmp/openwaf-upstream/body.txt
python3 -m http.server "$UP_PORT" --bind 127.0.0.1 -d /tmp/openwaf-upstream >/dev/null 2>&1 &
UP_PID=$!
# OPENWAF_SCHEMA=script is the point of the exercise: EnsureCreated and
# GenerateCreateScript both go through EF's design-time model, which a NativeAOT
# build refuses outright, so the DDL is generated at build time and read out of
# the binary. The schema-script gate keeps that file equal to what the model
# generates.
ASPNETCORE_URLS="http://127.0.0.1:$EDGE_PORT" \
OPENWAF_ADMIN_TOKEN="$TOKEN" \
OPENWAF_SCHEMA=script \
ConnectionStrings__OpenWaf="Data Source=$DB" \
Database__Provider=sqlite \
OpenWaf__Policy__Mode=Enforce \
OpenWaf__Policy__TrustProxyHeaders=true \
OpenWaf__Policy__StreakThreshold=3 \
ReverseProxy__Clusters__upstream__Destinations__upstream1__Address="http://127.0.0.1:$UP_PORT/" \
"$BIN" --contentRoot "$PUBLISH" >"$LOG" 2>&1 &
EDGE_PID=$!
for _ in $(seq 1 60); do
curl -fsS "http://127.0.0.1:$EDGE_PORT/healthz" >/dev/null 2>&1 && break
sleep 0.25
done
pass=0; fail=0
check() {
if [ "$2" = "$3" ]; then pass=$((pass+1)); printf ' ok %-52s %s\n' "$1" "$3"
else fail=$((fail+1)); printf ' FAIL %-52s expected %s, got %s\n' "$1" "$2" "$3"; fi
}
code() { curl -s -o /dev/null -m 5 -w '%{http_code}' "$@"; }
printf '\n=== the schema step ===\n'
tables=$(python3 -c "import sqlite3;print(len([r for r in sqlite3.connect('$DB').execute(\"select name from sqlite_master where type='table' and name != 'sqlite_sequence'\")]))" 2>/dev/null || echo 0)
check 'tables the shipped DDL created' 12 "$tables"
# Two halves, and the split is the finding, not a convenience. Measured 2026-10-07
# on this tree (osx-arm64, the same binary): the AOT edge reaches "Now listening
# on:", creates its 12 tables from the DDL it carries and proxies a request to the
# upstream for 200 — everything below in the first half passes. Every assertion in
# the second half fails, and fails with the SAME sentence 130 times in the log:
# "Query wasn't precompiled and dynamic code isn't supported with NativeAOT". They
# are recorded rather than checked because a check that fails for a reason this
# project already knows, and has a ticket for, stops being a signal; and they are
# recorded rather than deleted because the day EF compiles a query again, one of
# these answers changes and says so.
printf '\n=== the half that needs no query (gated) ===\n'
check 'healthz answers' 200 "$(code "http://127.0.0.1:$EDGE_PORT/healthz")"
check 'a request reaches the upstream' 200 "$(code "http://127.0.0.1:$EDGE_PORT/body.txt")"
HEALTHCHECK_PORT="$EDGE_PORT" "$BIN" --healthcheck >/dev/null 2>&1
check 'the healthcheck mode answers 0' 0 "$?"
# The header the edge actually reads. The probe used to send "Authorization:
# Bearer", which the admin surface does not look at, so three of these assertions
# were measuring a 401 rather than the query wall. Measured 2026-10-07: AdminAuth
# reads X-Admin-Token (or ?token=, which it names out loud).
printf '\n=== the half behind the query wall (recorded, not gated) ===\n'
recorded=0; drifted=0
record() {
if [ "$2" = "$3" ]; then
drifted=$((drifted+1)); printf ' MOVED %-50s %s — the query wall moved here, promote it back to a check\n' "$1" "$3"
else
recorded=$((recorded+1)); printf ' %-56s got %s, a working edge answers %s\n' "$1" "$3" "$2"
fi
}
record 'an operator block is accepted' 204 "$(code -X POST -H "X-Admin-Token: $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"ip":"198.51.100.7","reason":"aot probe","redirectTo":"https://honeypot.example.org/"}' \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
record 'the blocked source is redirected' 301 "$(code -H 'X-Admin-Token: $TOKEN' -H 'X-Forwarded-For: 198.51.100.7' \
"http://127.0.0.1:$EDGE_PORT/body.txt")"
ban=""
for _ in 1 2 3 4; do ban="$ban $(code -H 'X-Forwarded-For: 203.0.113.66' "http://127.0.0.1:$EDGE_PORT/probe")"; done
record 'four probes draw the streak ban' " 403 403 403 403" "$ban"
record 'the state surface answers' 200 "$(code -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/state")"
record 'readiness answers' 200 "$(code "http://127.0.0.1:$EDGE_PORT/readyz")"
# The body, not just the code. Before ProblemDetails had a contract this endpoint
# answered 500 and said nothing; the point of registering it is that the 503
# names the failure an operator would otherwise have to find in the log.
printf ' what /readyz says about itself: %s\n' \
"$(curl -s -m 5 "http://127.0.0.1:$EDGE_PORT/readyz" | tr -d '\n\r' | head -c 220)"
printf '\npassed %s, failed %s; recorded behind the wall %s, moved %s\n' \
"$pass" "$fail" "$recorded" "$drifted"
printf '\n=== the two walls, counted from the edge log ===\n'
listening=$(grep -c 'Now listening on:' "$LOG")
json_refusals=$(grep -c 'JsonTypeInfo metadata for type' "$LOG")
queries=$(grep -c "Query wasn't precompiled and dynamic code isn't supported with NativeAOT" "$LOG")
printf ' lines where the edge started listening: %s\n' "$listening"
printf ' json metadata refusals: %s\n' "$json_refusals"
{ [ "$json_refusals" -gt 0 ] && grep -m1 -o "JsonTypeInfo metadata for type '[^']*'" "$LOG" | sed 's/^/ first type without a contract: /'; }
printf ' queries refused at run time: %s\n' "$queries"
grep -c "Application startup exception" "$LOG" | sed 's/^/ startup exceptions: /'
printf ' full log: %s\n' "$LOG"
# The API half is a claim now, so it is gated: the host must be listening, and no
# response may be refused for want of a compiled contract. Before 2026-10-06 the
# first number was 0 (the host threw before Kestrel opened a port); on 2026-10-07
# the second was 55, all of them one anonymous /healthz literal.
[ "$listening" -ge 1 ] || { printf 'the edge never listened: the API half regressed\n' >&2; exit 1; }
[ "$json_refusals" -eq 0 ] || { printf 'a response shape still has no compiled JSON contract\n' >&2; exit 1; }
[ "$fail" -eq 0 ] || exit 1