Initial implementation of the OPEN-2 epic: a .NET 10 YARP edge that decides from what the upstream answered. Design (docs/ARCHITECTURE.md, tracker authority 4c9de3f6): - one short-circuit point, ordered: always-pass prefixes -> allowlist -> operator block (301) -> engine ban (403 + Retry-After) -> geo (451) - a post-response observer feeds a consecutive-refusal streak; N refusals in M minutes ban for BanDuration; 5xx alerts and never bans - provenance on the block row, so clearing engine bans cannot touch an operator's entries; the toggle is Off/Observe/Enforce and changes no rows - inherited protections from the reference deployment: one durable write per source per 30s, LRU-bounded streak map, no DB on the request path Verification: 36 unit + 14 integration tests, plus scripts/smoke.sh (19 assertions against a running edge and a real upstream, including the distroless --healthcheck mode). Findings worth keeping (documented in docs/ARCHITECTURE.md): - ctx.RequestAborted silently cancelled the ban write, because the observer runs after the response is written - SQLite cannot translate DateTimeOffset comparisons; timestamps are stored as integers there and stay timestamptz on PostgreSQL - CIDR masking order silently widened prefixes (198.51.100.24/19 -> /16) - an empty string in appsettings.json defeats the env fallback (?? on "") - concurrent ban writes raced into the unique index and lost evidence Also moves the unrelated 2026-09-30 inference benchmarks out of the project root into research/.
69 lines
2.9 KiB
C#
69 lines
2.9 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")
|
|
{
|
|
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");
|
|
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);
|
|
}
|
|
}
|