OPEN-12's option (b) — share the block list and shorten the TTL — was not a deployment decision, it was a rebuild: EdgeStateStore.RefreshTtl was a property initialised to 30 s with no configuration binding anywhere in the tree. The measurement in research/2026-10-01-multi-instance/ showed why that matters: the TTL is not a tuning knob, it is the propagation delay between edges (23.7 s under ~36 req/s, 1.7 s on an idle one), so an operator who runs more than one instance is choosing a staleness bound they cannot set and cannot see. The key is OpenWaf:State:RefreshTtlSeconds, default 30, and the default is unchanged so nothing moves for anyone who does not touch it. A value outside 1-300 aborts startup instead of falling back silently, because a typo in the staleness bound is a security setting failing quietly. /healthz now reports snapshotTtlSeconds, so an operator can read the TTL they actually got. Verified on this commit: 49 unit and 23 integration tests pass (13 new unit cases on the parser; 3 new integration tests that write a ban the way another instance would — straight into the database, no InvalidateAsync — and show a 1 s TTL closing the window inside 5 s while a 300 s TTL keeps serving a source the database has already banned); scripts/smoke.sh 19/19; scripts/compose-smoke.sh 27/27 against the rebuilt image; in a container RefreshTtlSeconds=5 reports snapshotTtlSeconds:5 on /healthz, and =0 aborts the process with the key named in the log. One thing the check taught and the README now says instead of hiding: when startup aborts inside the image, the container stays Up with nothing listening until the HEALTHCHECK goes unhealthy. That is how every startup failure in this image looks, not something this change introduces.
72 lines
3.0 KiB
C#
72 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using OpenWaf.Infrastructure.Persistence;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// One edge instance per test, with its own SQLite file and its own upstream.
|
|
/// Isolation is not tidiness here: the streak map and the block table are the
|
|
/// things under test, and a shared one would make the assertions about "this
|
|
/// source is banned, that one is not" meaningless.
|
|
/// </summary>
|
|
internal sealed class EdgeFixture : IAsyncDisposable
|
|
{
|
|
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"openwaf-test-{Guid.NewGuid():N}.db");
|
|
|
|
public FakeUpstream Upstream { get; } = FakeUpstream.Start();
|
|
|
|
public WebApplicationFactory<Program> Factory { get; }
|
|
|
|
public const string AdminToken = "test-admin-token";
|
|
|
|
public EdgeFixture(
|
|
string mode = "Enforce",
|
|
int threshold = 3,
|
|
string streakWindow = "00:05:00",
|
|
string banDuration = "01:00:00",
|
|
string geo = "Off",
|
|
int? refreshTtlSeconds = null)
|
|
{
|
|
Factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseSetting("ConnectionStrings:OpenWaf", $"Data Source={_dbPath}");
|
|
builder.UseSetting("Database:Provider", "sqlite");
|
|
builder.UseSetting("OpenWaf:Admin:Token", AdminToken);
|
|
builder.UseSetting("OpenWaf:Policy:Mode", mode);
|
|
builder.UseSetting("OpenWaf:Policy:StreakThreshold", threshold.ToString());
|
|
builder.UseSetting("OpenWaf:Policy:StreakWindow", streakWindow);
|
|
builder.UseSetting("OpenWaf:Policy:BanDuration", banDuration);
|
|
builder.UseSetting("OpenWaf:Policy:Geo", geo);
|
|
// The only way to give a TestServer request a source address is a
|
|
// header, so these tests exercise the trusted-proxy path too.
|
|
builder.UseSetting("OpenWaf:Policy:TrustProxyHeaders", "true");
|
|
if (refreshTtlSeconds is { } ttl)
|
|
builder.UseSetting("OpenWaf:State:RefreshTtlSeconds", ttl.ToString());
|
|
builder.UseSetting("ReverseProxy:Clusters:upstream:Destinations:upstream1:Address", Upstream.Address);
|
|
});
|
|
}
|
|
|
|
public HttpClient Client => Factory.CreateClient();
|
|
|
|
public T Service<T>() where T : notnull => Factory.Services.GetRequiredService<T>();
|
|
|
|
public async Task FlushObservationsAsync() => await Service<ObservationWriter>().FlushAsync(CancellationToken.None);
|
|
|
|
public async Task SendAsync(HttpClient client, string path, string sourceIp)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, path);
|
|
request.Headers.TryAddWithoutValidation("X-Forwarded-For", sourceIp);
|
|
using var response = await client.SendAsync(request);
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await Factory.DisposeAsync();
|
|
Upstream.RequestCount = 0;
|
|
await Upstream.DisposeAsync();
|
|
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
|
|
if (File.Exists(_dbPath)) File.Delete(_dbPath);
|
|
}
|
|
}
|