Documentation
¶
Overview ¶
Package bans holds the dynamic IP bans the console distributes outside configuration revisions (GetBans).
The agent keeps the console's bans and the sequence it has applied in memory and in <state-dir>/bans.json (0600), applies GetBans pages to them (a reset page drops every ban first) and pushes the result to the data plane (lua/edgeweir/bans.lua) and, for platform bans, to nftables. Bans the node creates itself (automatic mitigation) live only in the data plane until they are reported with ReportBans.
Greylist entries (proto v1.6.0, Ban.action) travel the same way: the edge layer challenges or rate-limits their addresses instead of refusing them, and the kernel never drops them. They are stored apart from the bans in bans.json ("greylist"), so an older agent reading the file never mistakes one for a ban.
Index ¶
Constants ¶
const ( MinBitsIPv4 = 16 MinBitsIPv6 = 48 )
Minimum prefix lengths: shorter prefixes are refused (the console enforces the same bounds).
const ( MaxRateRequests = 100000 MaxRateWindow = 3600 )
Bounds of a greylist rate limit.
const File = "bans.json"
File is the name of the persisted state inside the state directory.
const MaxLifetime = 7 * 24 * time.Hour
MaxLifetime bounds how far in the future a ban may expire (the console allows 1 minute to 7 days).
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶
Canonical masks p, unmaps IPv4-mapped IPv6 prefixes and checks the minimum prefix length.
func ParsePrefix ¶
ParsePrefix parses a ban CIDR and returns it in canonical form. It refuses prefixes shorter than MinBitsIPv4 / MinBitsIPv6.
Types ¶
type Action ¶ added in v1.6.0
type Action string
Action is what an entry does: "" refuses (a ban), the others make it a greylist entry.
type Ban ¶
type Ban struct {
ID string `json:"id"`
// Prefix is canonical: host bits zero, IPv4-mapped addresses unmapped.
Prefix netip.Prefix `json:"cidr"`
Scope Scope `json:"scope"`
SiteID string `json:"site_id,omitempty"`
Source Source `json:"source"`
Reason string `json:"reason,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
// Greylist entries only.
Action Action `json:"action,omitempty"`
Challenge string `json:"challenge,omitempty"`
RateRequests uint32 `json:"rate_requests,omitempty"`
RateWindow uint32 `json:"rate_window,omitempty"`
}
Ban is one console ban.
type Slot ¶
type Slot struct {
SlotKey
// ID is the ban the entry is reported under: the manual ban if there is
// one, otherwise the automatic ban that expires last.
ID string
Manual bool
// CreatedAt is the creation time of the ban with ID.
CreatedAt time.Time
// ExpiresAt is the latest expiry of the bans in the slot.
ExpiresAt time.Time
// The greylist entry's action and parameters (of the entry with ID).
Action Action
Challenge string
RateRequests uint32
RateWindow uint32
}
Slot is the data plane entry of one or more bans with the same key (several nodes can report an automatic ban for the same address, next to a manual one).
func Diff ¶
Diff returns the slots to write and the slots to delete (with the id the data plane holds them under) to turn from into to.
func Ordered ¶
Ordered returns the slots in the order the data plane must hold them: manual bans first (oldest first), then automatic bans from the newest to the oldest. Automatic bans beyond capacity are dropped and counted; manual bans are never dropped (the data plane reports those it cannot hold).
type SlotKey ¶
SlotKey is where a ban lives in the data plane: bans of the same scope, site and prefix share one entry, greylist entries one of their own.
type State ¶
type State struct {
// ClusterID is the cluster the bans belong to (a node enrolled into
// another cluster starts over).
ClusterID string
// Sequence is the console sequence the bans are complete up to.
Sequence uint64
// Greylist: the bans were fetched with greylist entries
// (GetBansRequest.greylist). A state written without them (by an
// older agent) is fetched again from the start.
Greylist bool
// Bans holds the bans and the greylist entries by id.
Bans map[string]Ban
}
State is the console's ban set as far as the node has applied it.
func Load ¶
Load reads the persisted state. A missing file is an empty state. Entries that fail validation are dropped (and reported in the error joined to a usable state).
func (*State) Apply ¶
Apply applies one GetBans page: a reset page drops every ban first, then bans are upserted by id and removed ids deleted. Bans that fail validation are skipped (returned as errors); bans already expired are dropped. The state then continues from the page's sequence.
func (*State) Platform ¶
Platform returns the unexpired platform bans (greylist entries are not bans: the kernel never drops them).