Files
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

111 lines
4.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using OpenWaf.Edge.Configuration;
using OpenWaf.Infrastructure.Persistence;
namespace OpenWaf.Tests.Integration;
/// <summary>
/// Retention used to be a constant on the policy record: the operator could
/// neither shorten the privacy window nor lengthen the period an incident stays
/// explainable, and no surface said what the window was (OPEN-19). These tests
/// are the two halves of having a lever — a bad value is refused by name at
/// startup, and a good value is what the prune actually honours.
/// </summary>
public sealed class RetentionConfigTests
{
[Theory]
[InlineData(0)]
[InlineData(-5)]
[InlineData(400)]
public void A_retention_window_outside_the_bound_is_refused_by_name(int days)
{
var ex = Assert.Throws<InvalidOperationException>(() => new EdgePolicyOptions
{ ObservationRetentionDays = days }.ToPolicy());
Assert.Contains("OpenWaf:Policy:ObservationRetentionDays", ex.Message);
Assert.Contains(days.ToString(), ex.Message);
}
[Theory]
[InlineData(1)]
[InlineData(365)]
public void The_bound_is_inclusive_on_both_ends(int days)
{
var policy = new EdgePolicyOptions
{
ObservationRetentionDays = days,
BanRetentionDays = days
}.ToPolicy();
Assert.Equal(TimeSpan.FromDays(days), policy.ObservationRetention);
Assert.Equal(TimeSpan.FromDays(days), policy.BanRetention);
}
[Fact]
public async Task The_two_windows_are_separate_knobs_and_the_default_reports_itself_on_both_surfaces()
{
await using var f = new EdgeFixture(observationRetentionDays: 30, banRetentionDays: 2);
var state = await (await f.Client.GetAsync($"/admin-api/state?token={EdgeFixture.AdminToken}")).Content.ReadAsStringAsync();
Assert.Contains("\"observationRetentionDays\":30", state);
Assert.Contains("\"banRetentionDays\":2", state);
var policy = await (await f.Client.GetAsync($"/admin-api/policy?token={EdgeFixture.AdminToken}")).Content.ReadAsStringAsync();
Assert.Contains("\"observationRetentionDays\":30", policy);
Assert.Contains("\"banRetentionDays\":2", policy);
}
[Fact]
public async Task A_shortened_window_prunes_what_the_default_window_would_have_kept()
{
// The tooth: with the default 7-day window these rows survive, so a
// prune that ignored the configured value would still pass a test that
// only backdated rows by eight days.
await using var f = new EdgeFixture(observationRetentionDays: 1);
foreach (var ip in new[] { "203.0.113.41", "203.0.113.42", "203.0.113.43" })
await f.SendAsync(f.Client, "/", ip);
await f.FlushObservationsAsync();
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<OpenWafDbContext>();
foreach (var o in await db.Observations.OrderBy(o => o.SourceKey).Take(2).ToListAsync())
o.LastSeenAt = DateTimeOffset.UtcNow.AddDays(-3);
await db.SaveChangesAsync();
}
await f.Service<RetentionWorker>().PruneAsync(CancellationToken.None);
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<OpenWafDbContext>();
Assert.Equal(1, await db.Observations.CountAsync());
}
}
[Fact]
public async Task A_lengthened_ban_window_keeps_the_evidence_row_the_default_would_have_pruned()
{
await using var f = new EdgeFixture(banRetentionDays: 30);
var now = DateTimeOffset.UtcNow;
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<OpenWafDbContext>();
db.Blocks.Add(new OpenWaf.Domain.Security.BlockEntry
{
SourceKey = "203.0.113.44",
Source = OpenWaf.Domain.Security.BlockSource.Engine,
ExpiresAt = now.AddDays(-10),
Reason = "engine ban, ten days past expiry"
});
await db.SaveChangesAsync();
}
await f.Service<RetentionWorker>().PruneAsync(CancellationToken.None);
using (var scope = f.Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<OpenWafDbContext>();
Assert.Equal(1, await db.Blocks.CountAsync(b => b.SourceKey == "203.0.113.44"));
}
}
}