diff --git a/dotnet/OpenWaf.Tests.Integration/RetentionPruneTests.cs b/dotnet/OpenWaf.Tests.Integration/RetentionPruneTests.cs new file mode 100644 index 0000000..d370bcc --- /dev/null +++ b/dotnet/OpenWaf.Tests.Integration/RetentionPruneTests.cs @@ -0,0 +1,120 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using OpenWaf.Domain.Security; +using OpenWaf.Infrastructure.Persistence; + +namespace OpenWaf.Tests.Integration; + +/// +/// Retention is the privacy answer this project gives: it stores aggregates, +/// not a request log, and the aggregates age out. Until now nothing ran +/// RetentionWorker.PruneAsync — not a unit test, not the smoke script, not the +/// compose gate — so "how long does a source key sit in the database" was a +/// number in a record nobody had ever executed. These tests execute it. +/// +/// The fixture's policy keeps the defaults: BanRetention and +/// ObservationRetention are both 7 days, and neither is read from +/// configuration. Rows are backdated past that window rather than the window +/// being shrunk, because shrinking it would test a setting that does not exist. +/// +public sealed class RetentionPruneTests +{ + private static async Task ObservationCount(EdgeFixture f) + { + using var scope = f.Factory.Services.CreateScope(); + return await scope.ServiceProvider.GetRequiredService().Observations.CountAsync(); + } + + private static async Task BackdateObservations(EdgeFixture f, int howMany, int days) + { + using var scope = f.Factory.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + foreach (var o in await db.Observations.OrderBy(o => o.SourceKey).Take(howMany).ToListAsync()) + { + o.LastSeenAt = DateTimeOffset.UtcNow.AddDays(-days); + } + await db.SaveChangesAsync(); + } + + [Fact] + public async Task Retention_ages_out_idle_observations_and_keeps_the_recent_one() + { + await using var f = new EdgeFixture(); + var client = f.Client; + + foreach (var ip in new[] { "203.0.113.11", "203.0.113.12", "203.0.113.13" }) + { + await f.SendAsync(client, "/", ip); + } + await f.FlushObservationsAsync(); + Assert.Equal(3, await ObservationCount(f)); + + await BackdateObservations(f, 2, days: 8); + await f.Service().PruneAsync(CancellationToken.None); + + // The comparison is DateTimeOffset against a column SQLite stores as + // text; if the offsets sorted differently the prune would take all + // three or none. + Assert.Equal(1, await ObservationCount(f)); + } + + [Fact] + public async Task Retention_deletes_expired_engine_bans_but_never_an_operator_row() + { + await using var f = new EdgeFixture(); + var now = DateTimeOffset.UtcNow; + + using (var scope = f.Factory.Services.CreateScope()) + { + var db = scope.ServiceProvider.GetRequiredService(); + db.Blocks.AddRange( + new BlockEntry { SourceKey = "203.0.113.21", Source = BlockSource.Engine, ExpiresAt = now.AddDays(-8), Reason = "engine, long expired" }, + new BlockEntry { SourceKey = "203.0.113.22", Source = BlockSource.Engine, ExpiresAt = now.AddDays(-1), Reason = "engine, expired inside the window" }, + new BlockEntry { SourceKey = "203.0.113.23", Source = BlockSource.Operator, ExpiresAt = now.AddDays(-30), Reason = "operator row with a past expiry" }, + new BlockEntry { SourceKey = "203.0.113.24", Source = BlockSource.Operator, ExpiresAt = null, Reason = "operator row that never expires" }); + await db.SaveChangesAsync(); + } + + await f.Service().PruneAsync(CancellationToken.None); + + using (var scope = f.Factory.Services.CreateScope()) + { + var db = scope.ServiceProvider.GetRequiredService(); + var left = await db.Blocks.OrderBy(b => b.SourceKey).Select(b => new { b.SourceKey, b.Source }).ToListAsync(); + + // Only the engine ban older than the retention window goes. The + // one that expired yesterday stays: an automatic ban has to stay + // explainable after it stops being live. + Assert.Equal( + new[] { "203.0.113.22", "203.0.113.23", "203.0.113.24" }, + left.Select(b => b.SourceKey).ToArray()); + Assert.DoesNotContain(left, b => b.SourceKey == "203.0.113.21"); + } + } + + [Fact] + public async Task Retention_ages_out_alerts_older_than_the_ban_window() + { + await using var f = new EdgeFixture(); + var now = DateTimeOffset.UtcNow; + + using (var scope = f.Factory.Services.CreateScope()) + { + var db = scope.ServiceProvider.GetRequiredService(); + db.Alerts.AddRange( + new EdgeAlert { SourceKey = "203.0.113.31", Kind = "server_error_streak", Count = 12, CreatedAt = now.AddDays(-9), Detail = "old outage" }, + new EdgeAlert { SourceKey = "203.0.113.32", Kind = "server_error_streak", Count = 4, CreatedAt = now.AddDays(-6), Detail = "inside the window" }, + new EdgeAlert { SourceKey = "203.0.113.33", Kind = "candidate_ban", Count = 3, CreatedAt = now.AddHours(-2), Detail = "fresh" }); + await db.SaveChangesAsync(); + } + + await f.Service().PruneAsync(CancellationToken.None); + + using (var scope = f.Factory.Services.CreateScope()) + { + var db = scope.ServiceProvider.GetRequiredService(); + var keys = await db.Alerts.OrderBy(a => a.SourceKey).Select(a => a.SourceKey).ToListAsync(); + Assert.Equal(new[] { "203.0.113.32", "203.0.113.33" }, keys); + } + } +}