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/.
133 lines
5.4 KiB
C#
133 lines
5.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using OpenWaf.Domain.Policy;
|
|
using OpenWaf.Domain.Security;
|
|
|
|
namespace OpenWaf.Infrastructure.Persistence;
|
|
|
|
/// <summary>
|
|
/// Six tables, all small. The edge never queries them on the request path —
|
|
/// <see cref="EdgeStateStore"/> holds the snapshot it reads — so the schema is
|
|
/// sized for operator inspection and for explaining a ban after the fact, not
|
|
/// for throughput.
|
|
/// </summary>
|
|
public sealed class OpenWafDbContext(DbContextOptions<OpenWafDbContext> options) : DbContext(options)
|
|
{
|
|
public DbSet<BlockEntry> Blocks => Set<BlockEntry>();
|
|
public DbSet<AllowEntry> AllowEntries => Set<AllowEntry>();
|
|
public DbSet<SourceObservation> Observations => Set<SourceObservation>();
|
|
public DbSet<CountryRule> CountryRules => Set<CountryRule>();
|
|
public DbSet<EdgeAlert> Alerts => Set<EdgeAlert>();
|
|
public DbSet<PolicySetting> PolicySettings => Set<PolicySetting>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder mb)
|
|
{
|
|
StoreTimestampsAsIntegersOnSqlite(mb);
|
|
mb.Entity<BlockEntry>(e =>
|
|
{
|
|
e.ToTable("block_entries");
|
|
e.HasKey(b => b.Id);
|
|
// One row per (source key, provenance): an operator block and an
|
|
// engine ban on the same address coexist on purpose — the operator
|
|
// entry outlives the ban and must not be overwritten by it.
|
|
e.HasIndex(b => new { b.SourceKey, b.Source }).IsUnique();
|
|
e.HasIndex(b => b.ExpiresAt);
|
|
e.Property(b => b.SourceKey).HasMaxLength(128).IsRequired();
|
|
e.Property(b => b.Reason).HasMaxLength(1024);
|
|
e.Property(b => b.RedirectTo).HasMaxLength(512);
|
|
e.Property(b => b.LastPath).HasMaxLength(512);
|
|
e.Property(b => b.LastUserAgent).HasMaxLength(256);
|
|
e.Property(b => b.LastCountryCode).HasMaxLength(2);
|
|
e.Property(b => b.LastCity).HasMaxLength(128);
|
|
e.Property(b => b.LastRegion).HasMaxLength(128);
|
|
e.Property(b => b.Source).HasConversion<int>();
|
|
});
|
|
|
|
mb.Entity<AllowEntry>(e =>
|
|
{
|
|
e.ToTable("allow_entries");
|
|
e.HasKey(a => a.Id);
|
|
e.HasIndex(a => a.Match).IsUnique();
|
|
e.Property(a => a.Match).HasMaxLength(128).IsRequired();
|
|
e.Property(a => a.Note).HasMaxLength(512);
|
|
});
|
|
|
|
mb.Entity<SourceObservation>(e =>
|
|
{
|
|
e.ToTable("source_observations");
|
|
e.HasKey(o => o.SourceKey);
|
|
e.Property(o => o.SourceKey).HasMaxLength(128);
|
|
e.HasIndex(o => o.LastSeenAt);
|
|
e.Property(o => o.LastPath).HasMaxLength(512);
|
|
e.Property(o => o.LastUserAgent).HasMaxLength(256);
|
|
e.Property(o => o.LastCountryCode).HasMaxLength(2);
|
|
e.Property(o => o.LastCity).HasMaxLength(128);
|
|
e.Property(o => o.LastRegion).HasMaxLength(128);
|
|
});
|
|
|
|
mb.Entity<CountryRule>(e =>
|
|
{
|
|
e.ToTable("country_rules");
|
|
e.HasKey(c => c.Code);
|
|
e.Property(c => c.Code).HasMaxLength(2);
|
|
e.Property(c => c.Name).HasMaxLength(128);
|
|
});
|
|
|
|
mb.Entity<EdgeAlert>(e =>
|
|
{
|
|
e.ToTable("edge_alerts");
|
|
e.HasKey(a => a.Id);
|
|
e.HasIndex(a => a.CreatedAt);
|
|
e.Property(a => a.SourceKey).HasMaxLength(128).IsRequired();
|
|
e.Property(a => a.Kind).HasMaxLength(64).IsRequired();
|
|
e.Property(a => a.Detail).HasMaxLength(1024).IsRequired();
|
|
});
|
|
|
|
mb.Entity<PolicySetting>(e =>
|
|
{
|
|
e.ToTable("policy_settings");
|
|
e.HasKey(p => p.Key);
|
|
e.Property(p => p.Key).HasMaxLength(64);
|
|
e.Property(p => p.Value).HasMaxLength(256).IsRequired();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// SQLite has no timestamp type: the provider stores a DateTimeOffset as
|
|
/// text and then refuses to compare it, so every "is this ban still live"
|
|
/// and "has this observation aged out" query dies at translation time.
|
|
/// Storing the instant as an integer makes those comparisons ordinary
|
|
/// integer comparisons and keeps the retention rules working on the default
|
|
/// provider. PostgreSQL keeps its native timestamptz, where an operator can
|
|
/// read the column.
|
|
/// </summary>
|
|
private void StoreTimestampsAsIntegersOnSqlite(ModelBuilder mb)
|
|
{
|
|
if (Database.ProviderName?.Contains("Sqlite") != true) return;
|
|
|
|
var converter = new Microsoft.EntityFrameworkCore.Storage.ValueConversion.DateTimeOffsetToBinaryConverter();
|
|
foreach (var entityType in mb.Model.GetEntityTypes())
|
|
{
|
|
foreach (var property in entityType.GetProperties())
|
|
{
|
|
var clr = property.ClrType;
|
|
if (clr == typeof(DateTimeOffset) || clr == typeof(DateTimeOffset?))
|
|
{
|
|
property.SetValueConverter(converter);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Operator-set policy overrides, one row per key, layered over what comes
|
|
/// from configuration. Stored as text on purpose: the set of knobs is small and
|
|
/// a column per knob would need a migration every time one is added.
|
|
/// </summary>
|
|
public sealed class PolicySetting
|
|
{
|
|
public string Key { get; init; } = string.Empty;
|
|
public string Value { get; set; } = string.Empty;
|
|
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
|
|
}
|