Files
Pavlo Ratushnyi 3959f1fe6b a change to the operator's config leaves a record of itself
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.
2026-10-05 05:58:42 +02:00

74 lines
3.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenWaf.Domain.Security;
namespace OpenWaf.Infrastructure.Persistence;
/// <summary>
/// Writes the record of a deliberate config change (OPEN-64).
///
/// The posture is the one thing to read here. The change has already been
/// applied by the time this runs — enforcement is the product, and a rule that
/// was refused because its audit row would not fit is a rule the operator
/// believes they have and does not. So a failed record never rolls the change
/// back and never fails the request. What it does instead is get counted:
/// <c>changeWriteFailures</c> and <c>changesNotWritten</c> on /admin-api/state,
/// the same shape the other durable pillars have, because "the config changed
/// and the record of it did not land" has to be a sentence the operator can
/// read rather than one that silently never happened.
/// </summary>
public sealed class ConfigChangeWriter
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly DurabilityHealth _health;
private readonly ILogger<ConfigChangeWriter> _log;
public ConfigChangeWriter(IServiceScopeFactory scopeFactory, DurabilityHealth health,
ILogger<ConfigChangeWriter> log)
{
_scopeFactory = scopeFactory;
_health = health;
_log = log;
}
/// <summary>Record one change. Returns whether the row landed — the caller
/// does not act on the answer, and that is deliberate: the only thing a
/// caller could do with a false is undo a change the operator made.</summary>
public async Task<bool> RecordAsync(
string section, string action, string target, string? detail = null,
string actor = ConfigChangeLog.ActorOperator, CancellationToken ct = default)
{
var row = new ConfigChange
{
At = DateTimeOffset.UtcNow,
Section = section,
Action = action,
// Clipped, never refused: the change happened, and a record that
// drops it because a sentence was long is the worse failure.
Target = ConfigChangeLog.Clip(target, ConfigChangeLog.MaxTargetLength),
Detail = string.IsNullOrWhiteSpace(detail)
? null
: ConfigChangeLog.Clip(detail, ConfigChangeLog.MaxDetailLength),
Actor = actor
};
try
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<OpenWafDbContext>();
db.ConfigChanges.Add(row);
await db.SaveChangesAsync(ct);
_health.RecordSuccess(DurabilityHealth.Changes, 1);
return true;
}
catch (Exception ex)
{
_health.RecordFailure(DurabilityHealth.Changes, ex, 1);
_log.LogWarning(ex, "ConfigChangeWriter: the record of a {Section} {Action} did not land.",
section, action);
return false;
}
}
}