ci / lint the workflow and the shell (push) Successful in 10s
geo-freshness / the pinned geo database vs the vendor (push) Successful in 5s
ci / the operator page in a real browser (push) Successful in 3m42s
ci / build, test, smoke (push) Failing after 2m6s
ci / build the image and smoke it (push) Successful in 5m7s
The Dockerfile said "why not NativeAOT" on the authority of another repo's
Dockerfile. That claim is now this repo's own, and it is a measurement.
What was built to find out:
* Persistence/Schema/schema.sqlite.sql — the model's DDL, generated by
`dotnet ef dbcontext script`, embedded in the binary and read by
Persistence/EmbeddedSchema.cs. A NativeAOT build cannot ask EF to build a
model at run time, and EnsureCreated and GenerateCreateScript both go through
the design-time model, so the schema step has nowhere to get its SQL from.
OPENWAF_SCHEMA=script is the fourth strategy: same provider-shaped
statements, produced at build time instead of at start-up. The schema-script
CI step regenerates the file and fails the run when the committed copy is not
what the current model produces (5652 bytes today).
* SchemaUpgrader.ModelTables asks the runtime model, per entity type. The
design-time one is refused outright under NativeAOT, and GetRelationalModel()
on the runtime model of a compiled model throws
"The annotation 'Relational:DefaultMappings' cannot be added because an
annotation with the same name already exists on the object EntityType:
CountryRule" — building a compiled model's relational model a second time
collides with itself.
* The persistence layer's queries were rewritten into the shape EF's query
precompiler accepts: captured parameters copied to locals, conditional
IQueryable composition written as literal chains, aggregates that take a
predicate given their own Where. 40 of them were refused before that.
* dotnet-ef and EF Core moved 10.0.0 -> 10.0.12, because 10.0.0's design package
ships no build host and `--precompile-queries` cannot load a project without it.
What the measurement says, from scripts/aot-probe.sh on osx-arm64: the AOT build
compiles (48,797,072-byte native binary, 10 IL warnings, 0 errors), starts, and
creates all 12 tables from the DDL it carries. Then it never serves. EF refuses
every query at run time ("Query wasn't precompiled and dynamic code isn't
supported with NativeAOT"), and the precompiler that would fix that is not
usable on this model: after all 40 refusals were cleared it emitted 14
declarations with dots in the identifier
(`var openWaf.Domain.Security.BlockEntryPrimaryKeyProperties = …`) — 28 CS1002
and CS1003 errors, one per entity materialisation — with or without --nativeaot.
The refusals themselves trace to dotnet/efcore#35887, #38030 and #35494; the fix
landed in the 12.0-preview1 milestone, not in any 10.0.x.
Two things were not kept, and why:
* The atomic alert-queue claim and the claim release were rewritten as raw SQL
to dodge the precompiler's ExecuteUpdate failures. Reverted: with the
statement written as SQL, DateTimeOffset reaches sqlite as a provider
parameter instead of through the property's own mapping, the reclaim
comparison stops matching, and
AlertPushQueueTests.A_delivery_owed_by_a_process_that_stopped_is_owed_by_the_next_one
fails with the row still owed. A correctness risk here buys nothing when the
AOT build is blocked further along anyway.
* --nativeaot on the compiled model: its unsafe accessors do not compile
(CS1503 in BlockEntryEntityType.cs). The plain compiled model is what is
committed.
The compose gate's two escalation-count scenes also stopped being a coin toss.
They slept a fixed 2 s and then counted; on a loaded host the same tree read 3
rows once and 1 the next, and the assertion is about a settled number either
way. They now wait for three equal reads, bounded. The scene count is unchanged
at 368.
Gates at this commit: build 0 warnings / 0 errors, unit 378/378, integration
405/405, schema-script ok, smoke 256/0, compose-smoke 368/0, README counts
match (256, 368), lint clean (actionlint 1.7.12, shellcheck 0.11.0).
Not verified: the AOT image on linux-x64 — the probe ran on osx-arm64, and the
ILC failure mode that ends the attempt is a codegen bug in EF, not a
platform-specific one, but that is an inference rather than a measurement.
144 lines
7.3 KiB
Bash
Executable File
144 lines
7.3 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"
|
|
|
|
printf '\n=== the curl assertions ===\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")"
|
|
check 'an operator block is accepted' 204 "$(code -X POST -H "Authorization: Bearer $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")"
|
|
check 'the blocked source is redirected' 301 "$(code -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
|
|
check 'four probes draw the streak ban' " 403 403 403 403" "$ban"
|
|
check 'the state surface answers' 200 "$(code -H "Authorization: Bearer $TOKEN" \
|
|
"http://127.0.0.1:$EDGE_PORT/admin-api/state")"
|
|
HEALTHCHECK_PORT="$EDGE_PORT" "$BIN" --healthcheck >/dev/null 2>&1
|
|
check 'the healthcheck mode answers 0' 0 "$?"
|
|
|
|
printf '\npassed %s, failed %s\n' "$pass" "$fail"
|
|
printf '\n=== what the edge said about its own queries ===\n'
|
|
grep -c "Query wasn't precompiled and dynamic code isn't supported with NativeAOT" "$LOG" \
|
|
| sed 's/^/ queries refused at run time: /'
|
|
grep -c "Application startup exception" "$LOG" | sed 's/^/ startup exceptions: /'
|
|
printf ' full log: %s\n' "$LOG"
|
|
[ "$fail" -eq 0 ] || exit 1
|