Files
openwaf/dotnet/OpenWaf.Tests.Integration/EdgeFixture.cs
T
Pavlo Ratushnyi 39dece60e4 the retention window is a config key now, and both tables say what it is
Retention was a constant on the policy record: 7 days for observations, 7 for
bans and alerts, read from nothing and shown nowhere (OPEN-19). Two keys now,
OpenWaf:Policy:ObservationRetentionDays and OpenWaf:Policy:BanRetentionDays,
1..365 whole days, and an out-of-range value aborts startup naming the key
rather than falling back to 7 — the same shape OPEN-13 chose for the refresh
TTL, because a silently widened window keeps rows longer than the deployment
asked for and nothing on any surface would say so. Split into two keys: the
two windows answer different questions and their sharing 7 days was
coincidence.

The window is printed where the rows it governs are printed: /admin-api/state
and /admin-api/policy answer observationRetentionDays and banRetentionDays,
the observation table appends "rows are pruned after Nd" and the alert table
"alerts are pruned after Nd".

Measured on this tree: build 0 warnings / 0 errors, unit 132/132, integration
181/181 in 20 s (8 new: 5 startup-refusal and bound cases, a 1-day window
pruning a 3-day-old observation, a 30-day window keeping a ban ten days past
expiry, both surfaces reporting 30 and 2, and the two defaults), smoke 28/0,
console 76 assertions 0 failed. The console gate needed two of its own
assertions rewritten, not relaxed: they compared the note text for equality
and the sentence grew.

Run against the real binary: ObservationRetentionDays=30 BanRetentionDays=2
reports 30 and 2 on both surfaces; ObservationRetentionDays=0 exits with
"OpenWaf:Policy:ObservationRetentionDays must be a whole number of days
between 1 and 365; got 0".
2026-10-04 15:37:27 +02:00

84 lines
3.8 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,
int? observationRetentionDays = null,
int? banRetentionDays = null,
string? adminToken = AdminToken)
{
Factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.UseSetting("ConnectionStrings:OpenWaf", $"Data Source={_dbPath}");
builder.UseSetting("Database:Provider", "sqlite");
// An empty token is a real configuration, not a missing one: it is
// how the admin surface is switched off, and a test that asserts what
// the page sees has to be able to ask for it.
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());
// The retention window is configuration as of OPEN-19, so a test can
// ask for a short window instead of backdating rows past a constant.
if (observationRetentionDays is { } ord)
builder.UseSetting("OpenWaf:Policy:ObservationRetentionDays", ord.ToString());
if (banRetentionDays is { } brd)
builder.UseSetting("OpenWaf:Policy:BanRetentionDays", brd.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);
}
}