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.
150 lines
7.8 KiB
C#
150 lines
7.8 KiB
C#
using OpenWaf.Domain.Policy;
|
|
|
|
namespace OpenWaf.Edge.Configuration;
|
|
|
|
/// <summary>
|
|
/// The configuration-shaped mirror of <see cref="EdgePolicy"/>. It exists
|
|
/// because <c>appsettings.json</c> is strings: <see cref="StatusRange"/> is a
|
|
/// record struct the configuration binder cannot build from "400-499", and a
|
|
/// TimeSpan written as "00:05:00" in a deployment file is friendlier than a
|
|
/// number of minutes. One explicit mapping, rather than a binder that quietly
|
|
/// leaves a default in place when the shape is wrong.
|
|
/// </summary>
|
|
internal sealed class EdgePolicyOptions
|
|
{
|
|
public string Mode { get; set; } = "Observe";
|
|
public int StreakThreshold { get; set; } = 10;
|
|
public string StreakWindow { get; set; } = "00:05:00";
|
|
public string BanDuration { get; set; } = "01:00:00";
|
|
public string RefusalRange { get; set; } = "400-499";
|
|
public string ServerErrorRange { get; set; } = "500-599";
|
|
public string Geo { get; set; } = "Off";
|
|
public bool TrustProxyHeaders { get; set; }
|
|
public int MaxTrackedSources { get; set; } = 10_000;
|
|
public int Ipv4KeyPrefixLength { get; set; } = 32;
|
|
public int Ipv6KeyPrefixLength { get; set; } = 64;
|
|
public int EngineBlockStatus { get; set; } = 403;
|
|
public int OperatorBlockStatus { get; set; } = 301;
|
|
|
|
// The volume pillar (OPEN-56). Off by default, and the limit is validated
|
|
// at startup rather than defaulted: a deployment that writes
|
|
// rateLimitPerWindow=0 meant something, and reading it as "100" or as
|
|
// "refuse everything" are both a lie about what was asked for.
|
|
public string Rate { get; set; } = "Off";
|
|
public int RateLimitPerWindow { get; set; } = 100;
|
|
public double RateWindowSeconds { get; set; } = 60;
|
|
|
|
// Where the streak counters live. Not a runtime knob: it selects which
|
|
// store the process registers at startup, so changing it is a redeploy.
|
|
// Off by default — the in-process map costs nothing on the request path,
|
|
// and a single-edge deployment loses nothing by it.
|
|
public bool SharedStreaks { get; set; }
|
|
// Empty by default: the configuration binder appends to an existing
|
|
// collection rather than replacing it, so a non-empty default here would
|
|
// leave a duplicate target in every deployment.
|
|
public string[] DefaultRedirectTargets { get; set; } = [];
|
|
|
|
// Retention is the privacy promise this project makes: aggregates about a
|
|
// source outlive the traffic that produced them by exactly this long
|
|
// (OPEN-19). It used to be a constant on the domain record, so the operator
|
|
// who wanted either end of that promise — a shorter window, or an incident
|
|
// from nine days ago that is still explainable — had no lever at all.
|
|
// Two keys, not one: an observation ages out a fact about a source, while a
|
|
// ban's evidence row stays readable after the ban itself stopped being
|
|
// live. They shared 7 days by coincidence, not by design.
|
|
// Out-of-range refuses startup rather than falling back to 7. A silently
|
|
// widened window keeps rows a month longer than the deployment asked for,
|
|
// and nothing on any surface would say so — the same class of lie this
|
|
// project keeps chasing elsewhere.
|
|
public int ObservationRetentionDays { get; set; } = 7;
|
|
public int BanRetentionDays { get; set; } = 7;
|
|
|
|
// The change log (OPEN-64) is bounded twice, and both bounds are the
|
|
// operator's: an age window, and a row cap that holds even inside the
|
|
// window. 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.
|
|
public int ChangeRetentionDays { get; set; } = 30;
|
|
public int ChangeLogMaxRows { get; set; } = 5000;
|
|
|
|
public const int RetentionDaysFloor = 1;
|
|
public const int RetentionDaysCeiling = 365;
|
|
public const int ChangeLogRowsFloor = 100;
|
|
public const int ChangeLogRowsCeiling = 100_000;
|
|
|
|
public EdgePolicy ToPolicy() => new()
|
|
{
|
|
Mode = Enum.TryParse<EnforcementMode>(Mode, true, out var m) ? m : EnforcementMode.Observe,
|
|
StreakThreshold = StreakThreshold > 0 ? StreakThreshold : 10,
|
|
StreakWindow = ParseWindow(StreakWindow, TimeSpan.FromMinutes(5)),
|
|
BanDuration = ParseWindow(BanDuration, TimeSpan.FromHours(1)),
|
|
RefusalRange = StatusRange.Parse(RefusalRange, new StatusRange(400, 499)),
|
|
ServerErrorRange = StatusRange.Parse(ServerErrorRange, new StatusRange(500, 599)),
|
|
Geo = Enum.TryParse<GeoMode>(Geo, true, out var g) ? g : GeoMode.Off,
|
|
TrustProxyHeaders = TrustProxyHeaders,
|
|
MaxTrackedSources = MaxTrackedSources > 0 ? MaxTrackedSources : 10_000,
|
|
Ipv4KeyPrefixLength = Ipv4KeyPrefixLength,
|
|
Ipv6KeyPrefixLength = Ipv6KeyPrefixLength,
|
|
EngineBlockStatus = EngineBlockStatus,
|
|
OperatorBlockStatus = OperatorBlockStatus,
|
|
SharedStreaks = SharedStreaks,
|
|
Rate = Enum.TryParse<RateMode>(Rate, true, out var rm) ? rm : RateMode.Off,
|
|
RateLimitPerWindow = RateWindowLimit(RateLimitPerWindow),
|
|
RateWindow = TimeSpan.FromSeconds(RateWindow(RateWindowSeconds)),
|
|
DefaultRedirectTargets = DefaultRedirectTargets.Length == 0
|
|
? new[] { "https://www.iana.org/help/example-domains" }
|
|
: DefaultRedirectTargets,
|
|
ObservationRetention = RetentionDays(ObservationRetentionDays, "OpenWaf:Policy:ObservationRetentionDays"),
|
|
BanRetention = RetentionDays(BanRetentionDays, "OpenWaf:Policy:BanRetentionDays"),
|
|
ChangeRetention = RetentionDays(ChangeRetentionDays, "OpenWaf:Policy:ChangeRetentionDays"),
|
|
ChangeLogMaxRows = ChangeLogRows(ChangeLogMaxRows, "OpenWaf:Policy:ChangeLogMaxRows")
|
|
};
|
|
|
|
private static int RateWindowLimit(int v)
|
|
{
|
|
if (v < PolicyBounds.RateLimitPerWindowMin || v > PolicyBounds.RateLimitPerWindowMax)
|
|
throw new InvalidOperationException(
|
|
$"OpenWaf:Policy:RateLimitPerWindow must be {PolicyBounds.RangeOf("rateLimitPerWindow")}; got {v}");
|
|
return v;
|
|
}
|
|
|
|
private static double RateWindow(double v)
|
|
{
|
|
if (v < PolicyBounds.RateWindowSecondsMin || v > PolicyBounds.RateWindowSecondsMax)
|
|
throw new InvalidOperationException(
|
|
$"OpenWaf:Policy:RateWindowSeconds must be {PolicyBounds.RangeOf("rateWindowSeconds")}; got {v}");
|
|
return v;
|
|
}
|
|
|
|
/// <summary>Whole days, 1..365. Anything else is a deployment mistake and
|
|
/// is named as one at startup.</summary>
|
|
private static TimeSpan RetentionDays(int days, string configKey)
|
|
{
|
|
if (days < RetentionDaysFloor || days > RetentionDaysCeiling)
|
|
throw new InvalidOperationException(
|
|
$"{configKey} must be a whole number of days between {RetentionDaysFloor} and "
|
|
+ $"{RetentionDaysCeiling}; got {days}");
|
|
return TimeSpan.FromDays(days);
|
|
}
|
|
|
|
/// <summary>Whole rows, 100..100 000. A cap below 100 would drop the record
|
|
/// of the very edits it exists to remember, and a cap that high is not a
|
|
/// bound, it is a table the operator cannot read.</summary>
|
|
private static int ChangeLogRows(int rows, string configKey)
|
|
{
|
|
if (rows < ChangeLogRowsFloor || rows > ChangeLogRowsCeiling)
|
|
throw new InvalidOperationException(
|
|
$"{configKey} must be a whole number of rows between {ChangeLogRowsFloor} and "
|
|
+ $"{ChangeLogRowsCeiling}; got {rows}");
|
|
return rows;
|
|
}
|
|
|
|
/// <summary>Accepts both <c>00:05:00</c> and a bare number of minutes.</summary>
|
|
private static TimeSpan ParseWindow(string value, TimeSpan fallback)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return fallback;
|
|
if (TimeSpan.TryParse(value, out var ts)) return ts;
|
|
return double.TryParse(value, out var minutes) && minutes > 0 ? TimeSpan.FromMinutes(minutes) : fallback;
|
|
}
|
|
}
|