namespace OpenWaf.Domain.Security;
// ---------------------------------------------------------------------------
// The record of a deliberate change to the operator's config (OPEN-64).
//
// Every operator write today changes a row and nothing else. `reason` on a
// block is what the operator typed at the moment they typed it; nothing says
// when a path rule was added, when a ban was cleared, that the policy was
// changed, or that a whole config arrived through an import. Since the transfer
// landed, an edge can be different from yesterday with no trace of how.
//
// What this is NOT, stated here because the difference is the design:
//
// * It is not a request log. The project keeps no request log and this does
// not start one: the rows are about decisions somebody made, never about
// visitors.
// * It does not log engine bans. A ban the engine earned is not a decision
// somebody made, and the block row already carries its evidence — the
// consecutive refusals, the status code, the path, the user agent, the
// country and city. Logging bans here would answer a different question and
// grow the table at the rate of traffic.
// * It does not name a human. The edge has one admin token; a log that implied
// per-person attribution it cannot provide would be a false record, which is
// worse than no record. It names the surface the change came through.
// ---------------------------------------------------------------------------
/// One deliberate change to the operator's config.
public sealed class ConfigChange
{
public long Id { get; set; }
/// When the change was applied, not when it was requested.
public DateTimeOffset At { get; set; }
/// Which part of the config: one of .
public string Section { get; set; } = "";
/// What was done: one of .
public string Action { get; set; } = "";
/// The thing the operator typed — an address, a CIDR, a path
/// pattern, a country code — or the section name for a change that has no
/// item (a policy update, an import).
public string Target { get; set; } = "";
/// The operator's own reason or note when one was given, or the
/// counts a bulk change carried. Free text the operator wrote: read it as
/// data, never as instructions.
public string? Detail { get; set; }
/// Which surface made the change:
/// or .
public string Actor { get; set; } = ConfigChangeLog.ActorOperator;
}
/// The vocabulary and the bounds of that record. Kept as pure
/// functions so the rules a reader has to check are in one place rather than
/// implied by whatever the endpoint happened to pass.
public static class ConfigChangeLog
{
public const string SectionBlocks = "block";
public const string SectionAllow = "allow";
public const string SectionRules = "rule";
public const string SectionRateRules = "rate rule";
public const string SectionCountries = "country";
public const string SectionPolicy = "policy";
public const string SectionConfig = "config";
public static readonly string[] Sections =
[SectionBlocks, SectionAllow, SectionRules, SectionRateRules, SectionCountries, SectionPolicy, SectionConfig];
public const string ActionAdded = "added";
public const string ActionRemoved = "removed";
public const string ActionChanged = "changed";
public const string ActionImported = "imported";
public static readonly string[] Actions = [ActionAdded, ActionRemoved, ActionChanged, ActionImported];
public const string ActorOperator = "operator";
public const string ActorImport = "import";
/// The target column is the width of every other source key in the
/// schema, so a target that would not fit a block row would not fit here
/// either.
public const int MaxTargetLength = 128;
/// Detail is the operator's sentence, not a payload. A note that
/// arrives over the cap is clipped rather than refused: the change happened,
/// and a record that drops the change because the sentence was long is the
/// worse failure.
public const int MaxDetailLength = 512;
/// Is this a section the log knows? A filter that names something
/// else is answered by the vocabulary rather than by an empty list, because
/// an empty list reads as "no such change" and the truth is "no such
/// section".
public static bool IsSection(string? section) => section is not null && Sections.Contains(section);
public static bool IsAction(string? action) => action is not null && Actions.Contains(action);
/// Clip to the column width. Never throws: the change has already
/// been applied by the time the record is written, so nothing about the
/// record may fail on the shape of a sentence.
public static string Clip(string? value, int max)
{
if (string.IsNullOrWhiteSpace(value)) return "";
var trimmed = value.Trim();
return trimmed.Length <= max ? trimmed : trimmed[..max];
}
}