mcl_core_guard (mcl_core v1.0.0)
View SourceThe guard''s counters, audit ring, and alert facts (mcl-om#13).
Owns the named ETS table the rate stage hits on EVERY inbound call: a fixed-window counter per (procedure, caller), plus cumulative denial counters per (procedure, kind). allow/3 is atomic ETS work in the caller''s process -- no gen_server hop on the hot path. The rare paths do go through this process: record_change/2 (audit), the audit reads inside stats/1, and the periodic alert report.
ALERT FACTS: sensing for the guardian is push, not poll. On a timer this process walks every declared procedure and publishes one denials_observed fact per procedure per window -- and only when that window saw a denial or an over-limit caller (should_report/3): a quiet window publishes nothing, so an attacker cannot use a quiet service as a fact amplifier, and a denial flood collapses into one fact per window. mcl-sec-guard subscribes to the topic and acts or not. The payload is binaries and numbers only; the procedure is in the payload, never in the topic.
The window is keyed by its START time ((Now div WindowMs) * WindowMs), so one sweep can be conservative across procedures with different window lengths: a window older than three times the LARGEST window length in use is definitely stale for everyone.
The audit ring is in-memory, bounded (32 entries per procedure), and additionally logger-logged per change -- a readable trail, NOT the tamper-evident event stream the register''s Security audit log row still owes. The guardian''s own store is where that stream will live.
Summary
Functions
Pure: the denials_observed fact payload -- binaries and numbers only, the procedure in the payload, never in the topic. top_callers rides along (the stats already hold it in a wire-safe shape: a list of maps with hex-encoded caller ids) so the guardian's rule can name the actual offenders instead of proposing a blind floor; distinct_callers tells it how wide the window's activity was.
One fixed-window count for (Procedure, Caller); the denial counter is bumped inside here so a denied call is observable immediately in stats/1.
One fixed-window denial counter, rate or 'size', keyed by the window start exactly like the caller buckets: a quiet window reads zero, so should_report/3 stops once its denials age out, and the alert fact's denied counts describe the window they rode in — a cumulative counter made one denial mark every later window denied, turning the service into the fact amplifier should_report exists to prevent.
Record one applied limits change on the audit ring (and in the log). Called by the control capabilities after a successful apply.
Pure: does a procedure''s current window deserve an alert fact? Yes when it is a NEW window (Start differs from the last reported one) AND it saw a denial or an over-limit caller.
A guardian-facing view of one procedure''s CURRENT window: limits and envelope in effect, global fill, callers over their budget, the heaviest callers, both denial counters, and the recent audit entries.
Functions
Pure: the denials_observed fact payload -- binaries and numbers only, the procedure in the payload, never in the topic. top_callers rides along (the stats already hold it in a wire-safe shape: a list of maps with hex-encoded caller ids) so the guardian's rule can name the actual offenders instead of proposing a blind floor; distinct_callers tells it how wide the window's activity was.
One fixed-window count for (Procedure, Caller); the denial counter is bumped inside here so a denied call is observable immediately in stats/1.
A caller is a distinct node id, and the bucket is allocated BEFORE the rate check — a Sybil flood (fresh identities, a few calls each) would grow the table regardless of global_max: memory would scale with attacker-chosen distinct callers within the retention window. Once the window has seen max_distinct_callers, a NEW caller is denied before its bucket exists; callers the window already knows keep their normal per-caller budget. The `$distinct' counter rides the same windowed sweep as the buckets.
-spec count_denial(binary(), rate | size) -> ok.
One fixed-window denial counter, rate or 'size', keyed by the window start exactly like the caller buckets: a quiet window reads zero, so should_report/3 stops once its denials age out, and the alert fact's denied counts describe the window they rode in — a cumulative counter made one denial mark every later window denied, turning the service into the fact amplifier should_report exists to prevent.
Record one applied limits change on the audit ring (and in the log). Called by the control capabilities after a successful apply.
Pure: does a procedure''s current window deserve an alert fact? Yes when it is a NEW window (Start differs from the last reported one) AND it saw a denial or an over-limit caller.
A guardian-facing view of one procedure''s CURRENT window: limits and envelope in effect, global fill, callers over their budget, the heaviest callers, both denial counters, and the recent audit entries.