using Microsoft.EntityFrameworkCore;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Infrastructure.Persistence;
///
/// Six tables, all small. The edge never queries them on the request path —
/// holds the snapshot it reads — so the schema is
/// sized for operator inspection and for explaining a ban after the fact, not
/// for throughput.
///
public sealed class OpenWafDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet Blocks => Set();
public DbSet AllowEntries => Set();
public DbSet Observations => Set();
public DbSet CountryRules => Set();
public DbSet Alerts => Set();
public DbSet PolicySettings => Set();
protected override void OnModelCreating(ModelBuilder mb)
{
StoreTimestampsAsIntegersOnSqlite(mb);
mb.Entity(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();
});
mb.Entity(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(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(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(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(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();
});
}
///
/// 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.
///
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);
}
}
}
}
}
///
/// 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.
///
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;
}