Documentation
¶
Overview ¶
Package moderation is the shared vocabulary of R39 enforcement (docs/42): the Ban CRD Go types, target normalization, deterministic CR naming, and the in-memory Set the relay evaluates on the publish hot path.
It is deliberately PUBLIC (not internal/), exactly like the wire package is (R14 Decision 1): gawk-admin — the fourth top-level module — imports it through a `replace` directive so the relay and the admin plane agree byte-for-byte on normalization, target matching and expiry semantics (docs/42 D13). Two implementations of "is this banned?" would eventually disagree, and the disagreement would be invisible until it mattered.
Keep the dependency surface here small for the same reason wire's is: it is a contract package, not an implementation home. It imports apimachinery (unavoidable — it carries CRD types) and internal/broadcastid (see Normalize), and nothing else beyond the standard library.
Index ¶
- Constants
- Variables
- func CRName(t Target) (string, error)
- func CanonicalAddr(a netip.Addr) netip.Addr
- func ParsePrefix(value string) (netip.Prefix, error)
- type Ban
- type BanList
- type BanSpec
- type Record
- type Set
- func (s *Set) ActiveCounts(now time.Time) map[string]int
- func (s *Set) BannedID(normID string, now time.Time) (Record, bool)
- func (s *Set) BannedIP(ip netip.Addr, now time.Time) (Record, bool)
- func (s *Set) Remove(t Target) error
- func (s *Set) Replace(all []Record)
- func (s *Set) Upsert(r Record) error
- type Target
- type TargetType
Constants ¶
const ( GroupName = "gawk.ioio.fi" Version = "v1alpha1" Kind = "Ban" ListKind = "BanList" // Resource is the plural lowercase resource name — what an informer // watches and what `kubectl get bans` resolves to. Resource = "bans" )
The Ban CRD's group/version/kind (docs/42 §4.2). Namespaced: bans live beside the relay pods that enforce them.
Variables ¶
var ( SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) AddToScheme = SchemeBuilder.AddToScheme )
SchemeBuilder / AddToScheme follow the client-go convention so both the relay's informer and gawk-admin's client can register the types.
var ErrInvalidTarget = errors.New("moderation: invalid ban target")
ErrInvalidTarget is returned for any target that cannot be normalized. Check with errors.Is.
var GroupVersionResource = SchemeGroupVersion.WithResource(Resource)
GroupVersionResource addresses Bans through a dynamic client.
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}
SchemeGroupVersion is the group/version this package registers.
Functions ¶
func CRName ¶
CRName returns the deterministic Ban CR name for a target, so a re-ban updates the existing object instead of creating a duplicate (docs/42 §4.2). The result is always a valid DNS-1123 subdomain:
- ban-id-<lowercased broadcast ID>. The ID alphabet is alphanumeric and DNS-safe by construction — the Lease naming scheme gawk-bc-<id> (internal/cluster/cluster.go) already relies on exactly this.
- ban-ip-<first 12 hex of SHA-256 of the canonical CIDR>. CIDRs contain ':', '/' and '.', so they are hashed rather than munged; 48 bits is ample against accidental collision in an operator-curated list, and a collision would only ever coalesce two bans, never un-ban anything.
The target is normalized first, so CRName("203.0.113.7") and CRName("203.0.113.7/32") name the same object.
func CanonicalAddr ¶
CanonicalAddr is how an observed peer address becomes a lookup key: zone stripped, v4-mapped-v6 collapsed to v4. Callers on the publish path use it so ::ffff:203.0.113.7 is matched by a 203.0.113.7/32 ban.
func ParsePrefix ¶
ParsePrefix canonicalizes an IP ban value into a netip.Prefix: a bare address widens to a host prefix (/32 or /128), a prefix is masked to its network, and a v4-mapped v6 prefix collapses to plain v4. Exported because gawk-admin resolves a publisher IP into a ban target with the same rules.
Types ¶
type Ban ¶
type Ban struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec BanSpec `json:"spec"`
}
Ban is one enforcement object: exactly one target, optionally time-boxed. v1alpha1 has no status subresource — relays only ever read (docs/42 §4.2), which keeps relay RBAC to get/list/watch.
func (*Ban) DeepCopyInto ¶
DeepCopyInto copies the receiver into out.
func (*Ban) DeepCopyObject ¶
DeepCopyObject satisfies runtime.Object.
type BanList ¶
type BanList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Ban `json:"items"`
}
BanList is the list type for the Ban kind.
func (*BanList) DeepCopyInto ¶
DeepCopyInto copies the receiver into out.
func (*BanList) DeepCopyObject ¶
DeepCopyObject satisfies runtime.Object.
type BanSpec ¶
type BanSpec struct {
// Target is the handle being banned.
Target Target `json:"target"`
// ExpiresAt is RFC 3339; ABSENT means permanent. Relays compare it
// against their own clock at check time, so enforcement ends on schedule
// whether or not the janitor has deleted the object.
ExpiresAt *metav1.Time `json:"expiresAt,omitempty"`
// Reason is informational operator text. It may carry operator-private
// context, which is why the relay logs it at Debug only (docs/42 §4.3).
Reason string `json:"reason,omitempty"`
// CreatedBy is informational: an OIDC email, "system", or "kubectl" for
// a break-glass ban the reconciler later adopts.
CreatedBy string `json:"createdBy,omitempty"`
}
BanSpec mirrors the YAML in docs/42 §4.2 exactly.
func (*BanSpec) DeepCopyInto ¶
DeepCopyInto copies the receiver into out.
type Record ¶
type Record struct {
Target Target `json:"target"`
ExpiresAt *time.Time `json:"expiresAt,omitempty"`
Reason string `json:"reason,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
}
Record is one evaluated ban. ExpiresAt nil means permanent.
The JSON tags are the on-disk format of the file ban source (-moderation-source=file:<path>, docs/42 §4.14): a plain JSON array of these. They mirror the CRD's spec field names so the two representations read the same.
func Normalize ¶
Normalize canonicalizes a record's target, returning a copy. It is the single definition of "the same target" for the relay and gawk-admin alike:
- broadcastId: uppercased and validated by internal/broadcastid.Normalize. That package is internal to gawk-server, but the internal rule is enforced at the import site, and this package lives inside gawk-server — so importing it here is legal and it stays legal for gawk-admin, which reaches it only transitively. Chosen over exporting a second copy of the 31-symbol alphabet: CODE-REVIEW.md's "shared constants have exactly one definition per language" is the whole point.
- ip: parsed with net/netip. A bare address becomes a /32 or /128; a non-canonical prefix (203.0.113.7/24) is masked to its network (203.0.113.0/24); a v4-mapped v6 form collapses to plain v4 so that ::ffff:203.0.113.7 and 203.0.113.7 are one target, not two.
Everything else — an unknown type, an empty value, a malformed CIDR — is ErrInvalidTarget. Rejecting is the safe direction: an unparseable ban that silently became a wildcard would take the fleet down.
func RecordFromBan ¶
RecordFromBan converts a CR into the evaluation record, normalizing the target. An unparseable target is an error, not a silently-ignored object: the caller logs and skips it rather than enforcing something it did not understand.
func (Record) Active ¶
Active reports whether the record is in force at now. Expiry is evaluated lazily, here, against the caller's clock — never by asking whether some janitor has deleted the CR yet (docs/42 §6: "relays evaluate expiresAt themselves at check time, so enforcement ends on schedule even if the janitor is down"). An expiry exactly at now has passed.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is the in-memory ban list a relay pod evaluates on the publish path (docs/42 §4.2). ID bans are an O(1) map; CIDR bans live in a path-compressed binary radix trie (a patricia trie) keyed on address bits, so a lookup costs O(prefix length) pointer hops rather than one comparison per ban.
A linear scan was rejected explicitly (docs/42 §7): this is a foundational check on the publish hot path, the list grows, and prefixes are genuinely mixed — v4 and v6, /8 through /32 and /128, overlapping allowed. Any match means banned; the MOST SPECIFIC match is the record reported, because that is the one whose reason and expiry the operator meant to apply.
The trie is hand-rolled rather than pulled from a library (github.com/gaissmai/bart was the alternative the design allowed): ~150 lines against an unchanged dependency set for the relay image is the better trade here.
The zero value is not usable; call NewSet. Every method is safe for concurrent use, and every method is nil-receiver-safe so a relay with -moderation-source=off can carry a nil Set and pay nothing.
func (*Set) ActiveCounts ¶
ActiveCounts returns the number of unexpired records per target type, for the gawk_moderation_bans_active gauge. Both keys are always present so the series exist at zero.
func (*Set) BannedID ¶
BannedID reports whether an already-normalized broadcast ID is banned at now. Expiry is evaluated here, lazily: an expired record never matches, regardless of whether any janitor has deleted its CR (docs/42 §6).
func (*Set) BannedIP ¶
BannedIP reports whether an address falls inside a banned CIDR at now, returning the most specific unexpired match. IPv4-mapped IPv6 addresses are collapsed to v4 first, so a 203.0.113.7/32 ban catches ::ffff:203.0.113.7.
func (*Set) Remove ¶
Remove drops one target (informer Delete). Removing an absent target is a no-op, which is what makes replaying a delete safe.
type Target ¶
type Target struct {
Type TargetType `json:"type"`
Value string `json:"value"`
}
Target is one ban handle. Exactly one per Ban CR (docs/42 §4.2): clean expiry means an expired ban is a deleted object, never a half-patched one.
func (*Target) DeepCopyInto ¶
DeepCopyInto copies the receiver into out.
type TargetType ¶
type TargetType string
TargetType enumerates the two ban handles R39 has (docs/42 D4). There is no third: with no accounts, a broadcast ID and a publisher IP are the only identities the platform can observe.
const ( // TargetBroadcastID bans one broadcast ID. Because the resume token is // HMAC(key, broadcastID) — stateless — banning the ID is banning the // token (internal/transport/resume.go). TargetBroadcastID TargetType = "broadcastId" // TargetIP bans a publisher source CIDR: the only handle that spans a // re-mint loop. TargetIP TargetType = "ip" )