`GET /admin-api/decide` answers what would happen to a named request and which pillar said so, and it stayed silent about the one pillar whose number the operator is choosing. The count is kept in the guard rather than read off a snapshot, so the preview printed `rate: not_evaluated` and left "would this source be rate-limited" as the one question the surface exists to answer could not ask. The rulings this had to make, and the answer each one got: * Reading, never spending, is the invariant. The section is built from peek reads of both trackers, so N dry runs leave `count` exactly where it was and write no alert. A preview that answered by counting its own question would refuse the next real request because somebody pressed a button — the tool for finding an over-tight budget would itself be the cause of it. * Four silences stay apart: `not_counted` (the pillar is off, so there is no budget to cross), `no_source` (no address was asked, and a budget is a claim about a source), `refused_now`, `would_be_counted`. A fifth, `over_budget_not_enforced`, exists because "over budget" and "would be refused" are two answers separated by the mode, and collapsing them is how an observe budget becomes a surprise the day someone switches it on. * `reached: false` says an earlier pillar answers the request, so the guard would never consult the counter for it. The volume section still reports the counters — they are true — but the note stops talking about a budget this request would never touch. * A budget in force for the path with nothing counted for this source yet prints `counted: false`, not `count: 0`. "0 of 2" and "nothing yet" are answers an operator acts on differently. * The path budgets are matched by the same `PathPattern.Matches`, in the same order, as the guard: a preview that matched differently would be a preview of a different edge. * The counters are this process's (`scope: "this-process"`), said in the answer rather than left for the operator to remember. * The `rate` pillar row keeps `not_evaluated` — the engine really did not evaluate it — and its note now points at the section that did, instead of ending as a silence. What the runs caught, in order: * The first version answered `refused_now` for an Observe crossing. The counters were right and the sentence was wrong: observe enforces nothing. The mode is now part of the verdict. * An integration expectation asserted the note of the `rate` pillar row after a crossing, and read the engine ban the crossing itself had written instead — the request was no longer reaching the volume pillar. The assertion moved to the preview taken before the crossing, and the ban case now asserts the honest note. * Rewording that note broke an older preview test that keyed on the word "preview" in it; the wording keeps the word, and the older test is the one that caught the rewording. * The smoke scene compared a listing's over-budget count against a request count, which would have passed for the wrong reason; it now asserts the pair (tracked, over) before and after six previews. Gates at this commit, each stage on its own: build 0 warnings / 0 errors, unit 371/371, integration 345/345 in 43 s, console 122 assertions 0 failed, smoke passed 182 failed 0, compose 304 passed 0 failed against the built image at 262 MB, 0 containers left behind. Refs OPEN-69.
205 lines
9.9 KiB
C#
205 lines
9.9 KiB
C#
using OpenWaf.Domain.Policy;
|
|
|
|
namespace OpenWaf.Domain.Engine;
|
|
|
|
/// <summary>What one pillar of the ordered decision did, in the answer an
|
|
/// operator reads. The distinction between the last three is the whole point of
|
|
/// the type: "this pillar did not match" and "this pillar was never asked" are
|
|
/// different facts, and an operator deciding whether to add a rule needs to know
|
|
/// which one they are looking at.</summary>
|
|
public enum PillarOutcome
|
|
{
|
|
Answered,
|
|
DidNotMatch,
|
|
NotReached,
|
|
/// <summary>The pillar exists but this configuration does not run it —
|
|
/// an engine ban in Observe mode, a geo policy switched off.</summary>
|
|
NotEnforcedInThisMode,
|
|
/// <summary>The pillar could not be asked at all: no address was given, or
|
|
/// the answer depends on state this call must not touch.</summary>
|
|
NotEvaluated
|
|
}
|
|
|
|
/// <summary>One line of the trace.</summary>
|
|
public sealed record PillarTrace(string Pillar, PillarOutcome Outcome, string? Note = null);
|
|
|
|
/// <summary>The outcome in the spelling the surface uses: lower-case and
|
|
/// underscored, the way the verdicts are spelled in the log and in the metrics,
|
|
/// so one word means one thing wherever an operator meets it.</summary>
|
|
public static class PillarOutcomes
|
|
{
|
|
public static string Label(PillarOutcome outcome) => outcome switch
|
|
{
|
|
PillarOutcome.Answered => "answered",
|
|
PillarOutcome.DidNotMatch => "did_not_match",
|
|
PillarOutcome.NotReached => "not_reached",
|
|
PillarOutcome.NotEnforcedInThisMode => "not_enforced_in_this_mode",
|
|
PillarOutcome.NotEvaluated => "not_evaluated",
|
|
_ => outcome.ToString().ToLowerInvariant()
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// The ordered decision, narrated (OPEN-59).
|
|
///
|
|
/// <see cref="DecisionEngine.Decide"/> returns one value, which is what the
|
|
/// response path needs and nothing like what an operator needs. The order of the
|
|
/// pillars is a documented property of the design — an allowlist outranks a
|
|
/// block, a path rule outranks a source list, an always-pass prefix outranks
|
|
/// everything — and the only way to show it is to say, pillar by pillar, what
|
|
/// each one got asked and what it said.
|
|
///
|
|
/// This is a pure function over the same snapshot and the same decision, so it
|
|
/// cannot disagree with the wire: it does not re-run any matching. It reads the
|
|
/// verdict and the order, and it is allowed exactly one extra question — whether
|
|
/// a live engine ban exists for a source the mode refused to enforce — because
|
|
/// that is the difference between "nothing was known about this address" and
|
|
/// "something was known and this mode chose not to act on it".
|
|
/// </summary>
|
|
public static class DecisionTrace
|
|
{
|
|
/// <summary>The pillars in the order the decision runs them. The rate pillar
|
|
/// is last because the guard runs it last, after everything that may already
|
|
/// have answered.</summary>
|
|
public static readonly string[] Pillars =
|
|
["alwaysPass", "allowlist", "requestRule", "operatorBlock", "engineBan", "geo", "rate"];
|
|
|
|
public static IReadOnlyList<PillarTrace> For(
|
|
EdgeSnapshot snapshot, EdgeDecision decision, DateTimeOffset now,
|
|
bool hasAddress, bool hasCountry, string? sourceKey = null)
|
|
{
|
|
var answered = decision.Verdict switch
|
|
{
|
|
EdgeVerdict.PassBypassed => "alwaysPass",
|
|
EdgeVerdict.PassAllowlisted => "allowlist",
|
|
EdgeVerdict.BlockRule or EdgeVerdict.PassRuleObserved => "requestRule",
|
|
EdgeVerdict.BlockOperator => "operatorBlock",
|
|
EdgeVerdict.BlockEngine => "engineBan",
|
|
EdgeVerdict.GeoReject => "geo",
|
|
EdgeVerdict.BlockRate => "rate",
|
|
_ => null
|
|
};
|
|
|
|
var order = Pillars;
|
|
var answeredAt = answered is null ? order.Length : Array.IndexOf(order, answered);
|
|
var rows = new List<PillarTrace>(order.Length);
|
|
|
|
for (var i = 0; i < order.Length; i++)
|
|
{
|
|
var name = order[i];
|
|
if (i == answeredAt)
|
|
{
|
|
rows.Add(new PillarTrace(name, PillarOutcome.Answered, NoteFor(decision)));
|
|
continue;
|
|
}
|
|
|
|
// Before the pillar that answered: it was asked and it said no —
|
|
// unless this configuration or this question meant it was never
|
|
// asked at all, which is a different answer and the operator has to
|
|
// be able to tell them apart.
|
|
if (i < answeredAt && WasAsked(name, snapshot.Policy, hasAddress, hasCountry))
|
|
{
|
|
rows.Add(new PillarTrace(name, PillarOutcome.DidNotMatch));
|
|
continue;
|
|
}
|
|
|
|
var outcome = Unasked(name, snapshot.Policy, hasAddress, hasCountry);
|
|
// A silent line carries its reason. "engineBan: not_evaluated" without
|
|
// "because no address was given" is the same half-answer the bare
|
|
// verdict was.
|
|
rows.Add(new PillarTrace(name, outcome,
|
|
outcome == PillarOutcome.NotReached
|
|
? null
|
|
: NoteForUnasked(name, snapshot, sourceKey, hasAddress, hasCountry, now)));
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
/// <summary>Whether this pillar was given the question at all. Each false is
|
|
/// a property of the design, not of this request: the source pillars need an
|
|
/// address because they are claims about a source; the engine pillar is
|
|
/// skipped entirely outside Enforce mode; the geo pillar needs a country,
|
|
/// which it may not have even when the pillar is on.</summary>
|
|
private static bool WasAsked(string name, EdgePolicy policy, bool hasAddress, bool hasCountry) => name switch
|
|
{
|
|
"operatorBlock" => hasAddress,
|
|
"engineBan" => hasAddress && policy.Mode == EnforcementMode.Enforce,
|
|
"geo" => policy.Geo != GeoMode.Off && hasCountry,
|
|
"rate" => false,
|
|
_ => true
|
|
};
|
|
|
|
/// <summary>Why a pillar did not speak, when it did not answer.</summary>
|
|
private static PillarOutcome Unasked(string name, EdgePolicy policy, bool hasAddress, bool hasCountry)
|
|
=> name switch
|
|
{
|
|
"operatorBlock" when !hasAddress => PillarOutcome.NotEvaluated,
|
|
"engineBan" when !hasAddress => PillarOutcome.NotEvaluated,
|
|
"engineBan" when policy.Mode != EnforcementMode.Enforce => PillarOutcome.NotEnforcedInThisMode,
|
|
"geo" when policy.Geo == GeoMode.Off => PillarOutcome.NotEnforcedInThisMode,
|
|
"geo" when !hasCountry => PillarOutcome.NotEvaluated,
|
|
// A preview never spends the live counter, whatever the mode.
|
|
"rate" => PillarOutcome.NotEvaluated,
|
|
_ => PillarOutcome.NotReached
|
|
};
|
|
|
|
/// <summary>The note that goes on the answering line. Short on purpose — the
|
|
/// full evidence (rule pattern, block reason, expiry) is in the response
|
|
/// beside the trace, not repeated inside it.</summary>
|
|
private static string? NoteFor(EdgeDecision decision) => decision.Verdict switch
|
|
{
|
|
EdgeVerdict.PassAllowlisted => string.IsNullOrWhiteSpace(decision.Reason)
|
|
? "an allow entry matched this address"
|
|
: $"an allow entry matched this address: {decision.Reason}",
|
|
EdgeVerdict.BlockRule => $"a request rule blocked this path ({decision.Reason})",
|
|
EdgeVerdict.PassRuleObserved => $"a request rule matched and is set to observe ({decision.Reason})",
|
|
EdgeVerdict.BlockOperator => decision.Range is null
|
|
? "the operator's own list"
|
|
: $"the operator's own list, a range: {decision.Range}",
|
|
EdgeVerdict.BlockEngine => "a ban this edge wrote from the responses this source produced",
|
|
EdgeVerdict.GeoReject => decision.Reason,
|
|
EdgeVerdict.BlockRate => "over the request budget",
|
|
EdgeVerdict.PassBypassed => "the path is an always-pass prefix",
|
|
_ => null
|
|
};
|
|
|
|
/// <summary>The note a silent line carries. This is the one place the trace
|
|
/// asks a second question of the snapshot: in Observe mode an engine ban
|
|
/// exists and does not act, and an operator has to be able to tell that apart
|
|
/// from there being nothing to act on.</summary>
|
|
public static string? NoteForUnasked(
|
|
string name, EdgeSnapshot snapshot, string? sourceKey, bool hasAddress, bool hasCountry, DateTimeOffset now)
|
|
{
|
|
var policy = snapshot.Policy;
|
|
switch (name)
|
|
{
|
|
case "engineBan" when !hasAddress:
|
|
case "operatorBlock" when !hasAddress:
|
|
case "geo" when policy.Geo != GeoMode.Off && !hasCountry:
|
|
return name == "geo"
|
|
? "the geo pillar is on and no country is known for this address"
|
|
: "no address was given, and this pillar is a claim about a source";
|
|
case "engineBan" when policy.Mode != EnforcementMode.Enforce:
|
|
var live = sourceKey is not null && snapshot.EngineBans.TryGetValue(sourceKey, out var ban)
|
|
&& ban.IsLive(now);
|
|
return live
|
|
? "a live engine ban exists for this source and Observe mode does not enforce it"
|
|
: "Observe mode does not enforce engine bans";
|
|
case "geo" when policy.Geo == GeoMode.Off:
|
|
return "the geo pillar is off";
|
|
case "rate":
|
|
// The pillar is not in the engine — a count is kept while requests
|
|
// arrive — but since OPEN-69 it is no longer unasked: the `volume`
|
|
// section of the same answer reads the counters without spending
|
|
// them, and the note says where to look rather than leaving the
|
|
// row as a silence.
|
|
return policy.Rate == RateMode.Off
|
|
? "the volume pillar is off: nothing is counted, and this preview's `volume` section says so"
|
|
: "answered by the `volume` section of this preview, which reads the counters without spending them";
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|