bans

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	MinBitsIPv4 = 16
	MinBitsIPv6 = 48
)

Minimum prefix lengths: shorter prefixes are refused (the console enforces the same bounds).

View Source
const (
	MaxRateRequests = 100000
	MaxRateWindow   = 3600
)

Bounds of a greylist rate limit.

View Source
const File = "bans.json"

File is the name of the persisted state inside the state directory.

View Source
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

func Canonical(p netip.Prefix) (netip.Prefix, error)

Canonical masks p, unmaps IPv4-mapped IPv6 prefixes and checks the minimum prefix length.

func ParsePrefix

func ParsePrefix(s string) (netip.Prefix, error)

ParsePrefix parses a ban CIDR and returns it in canonical form. It refuses prefixes shorter than MinBitsIPv4 / MinBitsIPv6.

func ValidID

func ValidID(id string) bool

ValidID reports whether id can be used as a ban or site id.

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.

const (
	ActionBlock     Action = ""
	ActionChallenge Action = "challenge"
	ActionRateLimit Action = "rate_limit"
)

Actions.

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.

func FromProto

func FromProto(pb *nodev1.Ban) (Ban, error)

FromProto validates a console ban.

func (Ban) Grey added in v1.6.0

func (b Ban) Grey() bool

Grey reports whether b is a greylist entry.

func (Ban) Key

func (b Ban) Key() SlotKey

Key returns the slot key of a ban.

func (Ban) Manual

func (b Ban) Manual() bool

Manual reports whether the ban was created by a person (manual bans are never dropped for capacity).

type Scope

type Scope string

Scope is where a ban applies.

const (
	ScopePlatform Scope = "platform" // every site; also enforced by nftables
	ScopeSite     Scope = "site"     // one site, at the edge layer only
)

Scopes.

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

func Diff(from, to map[SlotKey]Slot) (upsert, remove []Slot)

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

func Ordered(slots map[SlotKey]Slot, capacity int) (ordered, dropped []Slot)

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

type SlotKey struct {
	Scope  Scope
	SiteID string
	Prefix netip.Prefix
	Grey   bool
}

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 Source

type Source string

Source tells who created a ban.

const (
	SourceManual Source = "manual" // an operator or a tenant
	SourceAuto   Source = "auto"   // a node's automatic mitigation
)

Sources.

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

func Load(path string) (*State, error)

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 New

func New() *State

New returns an empty state.

func (*State) Apply

func (s *State) Apply(page *nodev1.GetBansResponse, now time.Time) []error

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) Clone

func (s *State) Clone() *State

Clone returns a copy of s.

func (*State) Platform

func (s *State) Platform(now time.Time) []Ban

Platform returns the unexpired platform bans (greylist entries are not bans: the kernel never drops them).

func (*State) Prune

func (s *State) Prune(now time.Time) int

Prune drops expired bans and returns how many were dropped.

func (*State) Save

func (s *State) Save(path string) error

Save writes the state atomically (0600).

func (*State) Slots

func (s *State) Slots(now time.Time) map[SlotKey]Slot

Slots groups the unexpired bans by data plane key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL