OPEN-59. Every other admin surface answers what the edge already did, so the only test for a path rule, an exemption or a country rule is a real visitor getting the real refusal. GET /admin-api/decide?ip=&path=&country= runs the same DecisionEngine over the same snapshot the wire reads — there is no second decision to drift — and narrates the order. The trace is the product: a verdict says what would happen, the pillar list says which pillar to change, and it keeps four silences apart (did_not_match, not_reached, not_enforced_in_this_mode, not_evaluated). In Observe mode the engine line says whether a live ban for that source exists, because 'nothing was known about this address' and 'something was known and the mode chose not to act' are the two answers operators read as one. What a preview cannot honestly answer — the live rate counter and a streak that depends on responses the upstream has not produced — is named in the body rather than left out. Three things the run caught. IPAddress.TryParse accepts abbreviated IPv4, so '203.0.113' parses as 203.0.113.0 and a missing octet was answered 'pass' for an address the operator did not mean; the guard now requires a full dotted quad. The preview read state.Current while the guard calls GetOrRefresh(), so it could answer from a snapshot the wire had already replaced — the whole claim of the feature is that the two agree. And a Pass carries no source key, so the Observe note claimed 'no live ban' for a source that had one; the key is now computed for the question rather than read off the answer. Gates on this tree: build 0 warnings / 0 errors, unit 256/256, integration 245/245, smoke 65/0, compose 229/0 against the built image (261 MB), console 90/0.
198 lines
9.4 KiB
C#
198 lines
9.4 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 => "the operator's own list",
|
|
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":
|
|
return policy.Rate == RateMode.Off
|
|
? "the volume pillar is off, and a preview never spends the budget anyway"
|
|
: "a preview does not consume the live counter";
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|