Documentation
¶
Overview ¶
Package pipeline turns a request into a verdict: it runs the rule evaluator under a per-request deadline and applies the enforcement mode (detect or block) and the failure mode (fail open or closed).
Index ¶
Constants ¶
const DefaultTimeout = 250 * time.Millisecond
DefaultTimeout is the per-request inspection deadline used when Config does not set one. It is a safety cap, not the expected latency: it is set so that requests within the default argument and body limits finish in time, because in block mode a timeout rejects the request.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Admission ¶
type Admission struct {
// contains filtered or unexported fields
}
Admission bounds how many evaluations run at once.
func NewAdmission ¶
NewAdmission returns an Admission for n concurrent evaluations; n <= 0 means 2 * GOMAXPROCS.
type Config ¶
type Config struct {
Mode Mode
FailureMode FailureMode
// Timeout bounds inspection time per request. Zero means DefaultTimeout.
Timeout time.Duration
// MaxConcurrent bounds how many evaluations run at once, including ones
// that already timed out and are still finishing. When all slots are
// busy, new requests get the failure mode immediately (ReasonOverload)
// instead of adding work. Zero means 2 * GOMAXPROCS. Ignored when
// Admission is set.
MaxConcurrent int
// Admission, if set, is shared with other pipelines (one per policy),
// so the bound holds for the whole engine.
Admission *Admission
// Oversize decides what happens to bodies longer than the inspection
// limit (request.Request.BodyTruncated).
Oversize OversizeAction
// Observer, if set, is told about every verdict (metrics).
Observer Observer
// Routes decides which paths skip inspection entirely or skip only body
// inspection (tier T0). Nil inspects everything.
Routes *router.Table
}
Config configures a Pipeline.
type FailureMode ¶
type FailureMode uint8
FailureMode decides what happens when a request cannot be inspected: inspection timed out, failed, or was shed under overload.
const ( // FailAuto is the default: fail closed in block mode and open in detect // mode. A fail-open block mode is bypassable: padding a request until // inspection times out gets it forwarded uninspected. FailAuto FailureMode = iota // FailOpen allows the request, choosing availability over enforcement. FailOpen // FailClosed rejects the request with 503 Service Unavailable. FailClosed )
func (FailureMode) Resolve ¶
func (f FailureMode) Resolve(m Mode) FailureMode
Resolve returns the effective failure mode for an enforcement mode.
type Inspector ¶
type Inspector interface {
// Check inspects r and returns the verdict.
Check(ctx context.Context, r *request.Request) Verdict
// BodyLimit returns how many body bytes to read for r, which carries
// no body yet: 0 means the body isn't inspected, so it isn't read. max
// is the adapter's own limit, used when nothing more specific applies.
BodyLimit(r *request.Request, max int64) int64
}
Inspector is what adapters inspect requests with: one Pipeline, or a set of policies that picks a pipeline per route.
type Observer ¶
type Observer interface {
ObserveVerdict(v Verdict)
}
Observer receives every verdict. Implementations must be fast and safe for concurrent use: they run on the request path.
type OversizeAction ¶
type OversizeAction uint8
OversizeAction decides what happens to a request whose body is longer than the inspection limit.
const ( // OversizeAuto is the default: deny in block mode, allow in detect mode. OversizeAuto OversizeAction = iota // OversizeAllow inspects headers and URI only and forwards the body // uninspected; the verdict carries ReasonOversize so it is logged and // counted. A truncated body is never inspected: a prefix of JSON, XML // or multipart cannot be parsed, and inspecting a prefix of any body // leaves the rest as a hiding place. OversizeAllow // OversizeDeny rejects oversized bodies with 413 Content Too Large in // block mode (detect mode logs them), so no body escapes inspection. OversizeDeny )
func (OversizeAction) Resolve ¶
func (o OversizeAction) Resolve(m Mode) OversizeAction
Resolve returns the effective oversize action for an enforcement mode.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline evaluates requests. It is safe for concurrent use.
type Reason ¶
type Reason uint8
Reason explains a verdict.
const ( // ReasonNone means no rule interrupted the request. ReasonNone Reason = iota // ReasonRule means a rule interrupted the request. ReasonRule // ReasonTimeout means inspection exceeded the deadline. ReasonTimeout // ReasonError means the evaluator failed. ReasonError // ReasonCanceled means the caller canceled the request. ReasonCanceled // ReasonOverload means all evaluation slots were busy, so the request // was not inspected. ReasonOverload // ReasonOversize means the body was longer than the inspection limit // and the policy rejects oversized bodies. ReasonOversize // ReasonSkipped means the route is configured to skip inspection. ReasonSkipped // ReasonNoPolicy means no policy covers the request's route, so it was // not inspected. ReasonNoPolicy // ReasonSuspended means inspection is suspended (the emergency switch). ReasonSuspended )
type Verdict ¶
type Verdict struct {
Action Action
// Status is the HTTP status to return when Action is ActionDeny.
Status int
Reason Reason
// InterruptingRuleID is the rule that interrupted the request, if any.
InterruptingRuleID int
// RuleIDs lists every rule that matched, including score-only matches.
RuleIDs []int
// Matches says where they matched (unredacted), and AnomalyScore is the
// CRS inbound anomaly score.
Matches []rules.Match
AnomalyScore int
// Duration is the time spent inspecting.
Duration time.Duration
// Policy is the policy that decided, in bundle mode ("namespace/name").
Policy string
}
Verdict is the pipeline's decision for one request.