Every operator write already changes a row; what was missing is the answer an operator actually asks at 03:00 - when did this rule get here, who cleared that ban, did a document bring this edge's config. Since the transfer landed, an edge can be different from yesterday with nothing on it saying how.
config_changes carries one row per deliberate change: the instant, the section (block, allow, rule, country, policy, config), the action (added, removed, changed, imported), the target the operator typed, their own sentence where one was given, and the surface the change came through. GET /admin-api/changes answers it newest first, capped and paged like every other listing, filterable by section and since, readable by the read-only token because it is a read. A policy record says what moved ("mode Enforce->Off, streakThreshold 3->4") rather than only that something did. /admin-api/state counts the rows and the write failures.
The rulings, because each is a decision and not a detail:
* Engine bans are not in the log. A ban the engine earned is not a decision somebody made, and the block row already carries its evidence - consecutive refusals, status code, path, user agent, country, city. Logging bans would answer a different question and grow the table at the rate of traffic.
* An import is one record per section, not one per row: "3 added, 1 already there, 0 past expiry, 0 rejected". A 2 000-row document would otherwise bury a table whose whole use is being readable.
* The record names the surface, never a person. 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.
* A record that cannot be written never undoes the change it describes. Enforcement is the product, so the change stands and the failure is counted - changeWriteFailures, changesNotWritten, changeLastError - because "the config changed and the record of it did not land" has to be a sentence an operator can read rather than one that silently never happened.
* Bounded twice, both keys the operator's: ChangeRetentionDays (30) and ChangeLogMaxRows (5000, oldest dropped first). The age window alone would let a scripted churn grow the table without limit; the cap alone would drop last month's incident record the week someone did a lot of editing.
What the run caught, in order: (1) Two gate assertions were wrong rather than the product - a policy record was asserted in a scene where no policy write had happened yet, and "one imported record per section" was asserted against a stack that had already imported twice, so the assertion now reads the newest imported record instead of counting them. (2) DELETE /admin-api/countries/{code} answers 204, and the new assertion expected 200. (3) A shell trap in the new scene: a $VAR inside a single-quoted python -c is not interpolated, so the first version would have handed python a literal "$CHANGES_BEFORE" instead of a number.
Gates on this tree: build 0 warnings / 0 errors, unit 313/313, integration 298/298 in 23s, smoke 127 passed / 0 failed (14 new), image 262 MB, compose-smoke 278 passed / 0 failed with 0 containers left behind (11 new, against the shipped image), console 90 assertions / 0 failed.
Refs OPEN-64.
110 lines
5.2 KiB
C#
110 lines
5.2 KiB
C#
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.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// <summary>One deliberate change to the operator's config.</summary>
|
|
public sealed class ConfigChange
|
|
{
|
|
public long Id { get; set; }
|
|
|
|
/// <summary>When the change was applied, not when it was requested.</summary>
|
|
public DateTimeOffset At { get; set; }
|
|
|
|
/// <summary>Which part of the config: one of <see cref="ConfigChangeLog.Sections"/>.</summary>
|
|
public string Section { get; set; } = "";
|
|
|
|
/// <summary>What was done: one of <see cref="ConfigChangeLog.Actions"/>.</summary>
|
|
public string Action { get; set; } = "";
|
|
|
|
/// <summary>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).</summary>
|
|
public string Target { get; set; } = "";
|
|
|
|
/// <summary>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.</summary>
|
|
public string? Detail { get; set; }
|
|
|
|
/// <summary>Which surface made the change: <see cref="ConfigChangeLog.ActorOperator"/>
|
|
/// or <see cref="ConfigChangeLog.ActorImport"/>.</summary>
|
|
public string Actor { get; set; } = ConfigChangeLog.ActorOperator;
|
|
}
|
|
|
|
/// <summary>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.</summary>
|
|
public static class ConfigChangeLog
|
|
{
|
|
public const string SectionBlocks = "block";
|
|
public const string SectionAllow = "allow";
|
|
public const string SectionRules = "rule";
|
|
public const string SectionCountries = "country";
|
|
public const string SectionPolicy = "policy";
|
|
public const string SectionConfig = "config";
|
|
|
|
public static readonly string[] Sections =
|
|
[SectionBlocks, SectionAllow, SectionRules, 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";
|
|
|
|
/// <summary>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.</summary>
|
|
public const int MaxTargetLength = 128;
|
|
|
|
/// <summary>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.</summary>
|
|
public const int MaxDetailLength = 512;
|
|
|
|
/// <summary>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".</summary>
|
|
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);
|
|
|
|
/// <summary>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.</summary>
|
|
public static string Clip(string? value, int max)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return "";
|
|
var trimmed = value.Trim();
|
|
return trimmed.Length <= max ? trimmed : trimmed[..max];
|
|
}
|
|
}
|