module

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package module is the contract for controls compiled into the gateway binary. A module registers itself from init with Register, takes its settings from one member of a policy operation, compiles them once per published policy and then checks requests at one fixed phase of the request pipeline.

The order of phases is fixed in the gateway, never configured: a module cannot run before the checks its phase follows, cannot skip a later stage and never forwards anything. A refusal or failure ends the request. The gateway's five built-in controls (jwt, rbac, rate, body, validation) are registered under their own IDs in every build, which reserves those IDs.

A Check sees only a copy of the request's method, path, client address, headers and verified identity: no body, no token and no connection. It must not perform network or file I/O and must not keep mutable package-level state; per-key state belongs in a Table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(c Control)

Register adds a module. Call it from init. It panics on an invalid or duplicate ID, an unknown phase, or an ID that names another operation member, so a build with a conflicting module never starts.

Types

type Check

type Check interface {
	Check(ctx context.Context, r *Request) Result
}

Check decides one request using only r and the data compiled into it.

type Control

type Control interface {
	Info() Info
	// Compile runs inside the gateway's policy compiler, once per published
	// policy and operation, never per request. raw is this module's member of
	// the operation, nil when absent. A nil Check means the module is not
	// configured for the operation. An error or a panic refuses the policy.
	Compile(op Operation, raw json.RawMessage) (Check, error)
}

Control is a module.

func Extensions

func Extensions() []Control

Extensions returns the registered modules that are not built in, sorted by phase and then ID: the ones whose checks the pipeline runs and whose policy members the gateway reads.

type Info

type Info struct {
	ID      string // 1-32 of a-z, 0-9, '_'; unique; also the policy member name
	Phase   Phase
	Version string // the module's own version
}

Info identifies a module.

func Registered

func Registered() []Info

Registered lists every registered module, built-ins included, sorted by phase and then ID.

type Operation

type Operation struct {
	ID, API, Method, Target string
}

Operation is the policy operation a module's settings belong to.

type Phase

type Phase int

Phase is the point of the request pipeline at which a module runs.

const (
	Ingress   Phase = iota + 1 // after the ingress checks, before a policy is captured; sees no policy settings
	Identity                   // after routing and access, following token verification
	Authorise                  // following roles and blocked accounts
	Traffic                    // following the rate limit
	Validate                   // following body and header validation
)

func (Phase) String

func (p Phase) String() string

String names the phase as status reports and records do.

type Request

type Request struct {
	Method, Path, ClientAddress string
	Header                      http.Header // a copy: changing it changes nothing
	Subject                     string      // empty before or without a verified token
	Roles                       []string
	Operation, API              string // empty in the Ingress phase
}

Request is the read-only view of a request a Check receives.

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result is a Check's decision. Only Pass, Refuse and Fail build one; the zero Result is treated as a failure.

func Fail

func Fail(reason string) Result

Fail ends the request with 503 gateway_unavailable; reason is recorded with the module's ID and must not contain request data.

func Pass

func Pass() Result

Pass lets the request continue.

func Refuse

func Refuse(status int, code string) Result

Refuse ends the request with status (400-499) and the error code code. A status outside that range or a malformed code becomes a failure.

func (Result) Code

func (r Result) Code() string

func (Result) Failed

func (r Result) Failed() bool

func (Result) Passed

func (r Result) Passed() bool

Passed, Refused and Failed report the decision; Status and Code are the refusal's answer, and Reason a failure's reason.

func (Result) Reason

func (r Result) Reason() string

Reason is a failure's reason; a zero Result reads "no result".

func (Result) Refused

func (r Result) Refused() bool

func (Result) Status

func (r Result) Status() int

type Table

type Table[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Table holds per-key state for a module, bounded by a fixed number of keys. When it is full, Update refuses a new key instead of evicting one, so the module can fail closed. It is safe for concurrent use.

func NewTable

func NewTable[K comparable, V any](max int) *Table[K, V]

NewTable returns an empty table holding at most max keys; max must be positive.

func (*Table[K, V]) Delete

func (t *Table[K, V]) Delete(key K)

Delete removes key.

func (*Table[K, V]) Len

func (t *Table[K, V]) Len() int

Len returns the number of keys held.

func (*Table[K, V]) Update

func (t *Table[K, V]) Update(key K, change func(current V, found bool) V) bool

Update replaces key's value with change(current, found) as one atomic step. It returns false, changing nothing, when key is new and the table is full.

Jump to

Keyboard shortcuts

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