authmode

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package authmode owns whether the gateway requires a gateway API key.

The mode is one decision with three ways to state it — a configuration value, a command-line flag, and a console switch — and one rule about when it is safe to disable. Every one of those needs the same vocabulary, so the vocabulary lives here rather than being spelled once per package: a mode a reader fails to recognize is merely wrong, but a mode it recognizes when it should not is an open gateway.

Index

Constants

View Source
const (
	// StorageSchemaVersion identifies the only authentication-mode schema.
	StorageSchemaVersion = 1
	// StoragePrefix is the authentication-mode v1 namespace.
	StoragePrefix = "authmode:v1:"
	// StorageKey holds the one stored setting. The mode is deployment-wide, so
	// there is one record and not a keyed collection.
	StorageKey = StoragePrefix + "current"
)

Variables

View Source
var (
	// ErrRepositoryRequired reports an absent authentication-mode storage adapter.
	ErrRepositoryRequired = errors.New("authentication mode storage is required")
	// ErrNotFound reports that no mode has been stored.
	ErrNotFound = errors.New("stored authentication mode not found")
	// ErrConflict reports an authentication-mode revision conflict.
	ErrConflict = errors.New("authentication mode revision conflict")
	// ErrCorruptRecord reports invalid durable authentication-mode data.
	ErrCorruptRecord = errors.New("authentication mode record is invalid")
	// ErrInvalidMode reports a mode the gateway cannot run in.
	ErrInvalidMode = errors.New("authentication mode is invalid")
)

Functions

func AllowsDisabled

func AllowsDisabled(bindHost string, acknowledged bool) bool

AllowsDisabled reports whether authentication may be off for a gateway bound to bindHost.

An unauthenticated gateway on a reachable address is an open inference endpoint, and the bind address is the only evidence anything has about who can reach it. Turning it off there takes two deliberate acts, so the acknowledgment is a separate argument rather than a wider reading of the address.

This is the one place the rule lives. Startup validation and the runtime switch both call it, because a rule enforced at startup and restated at runtime is a rule with two versions.

func LoopbackAddr

func LoopbackAddr(addr string) bool

LoopbackAddr reports whether an address in host:port form, such as an http.Request RemoteAddr, reaches only this machine. An address without a port is read as a bare host.

func LoopbackHost

func LoopbackHost(host string) bool

LoopbackHost reports whether a bind address reaches only this machine.

An empty host is not loopback: an empty address binds every interface, which is the exposure the caller is asking about. A name other than localhost is not loopback either, because deciding otherwise would need a DNS lookup whose answer can change after startup, and a resolver is the wrong thing to trust with this question.

func LoopbackOrigin

func LoopbackOrigin(origin string) bool

LoopbackOrigin reports whether a browser Origin header names this machine.

An absent origin is loopback: a request from curl or an SDK carries none, and refusing those would make the header a requirement rather than a check. What the check catches is the origin that is present and names somewhere else, which is a page on another site driving a browser that can reach the gateway.

Types

type Mode

type Mode string

Mode selects whether a request must carry a gateway API key.

const (
	// Required refuses every request that carries no valid gateway API key.
	Required Mode = "required"
	// Disabled serves every request without checking for a key. The gateway
	// still meters, governs, and attributes the request; it just has no
	// caller-supplied name for it.
	Disabled Mode = "disabled"
)

func (Mode) Effective

func (m Mode) Effective() Mode

Effective returns the mode the gateway runs under. An unset value is Required: the state an operator reaches by not deciding has to be the safe one.

func (Mode) Valid

func (m Mode) Valid() bool

Valid reports whether the mode names a state the gateway can run in. An unset value is valid and means Required.

type Policy

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

Policy is the mode the gateway is running under right now.

It exists because the console can change the mode without a restart, and every request has to see the change. Reading a value that was captured when the router was built would make "disabled" mean "disabled at boot", so the authentication middleware reads the policy per request instead.

The zero value and a nil pointer both report Required. A server that never bound a policy authenticates, which is the direction this has to fail in.

func NewPolicy

func NewPolicy(setting Setting) *Policy

NewPolicy returns a policy running under setting.

func (*Policy) Current

func (p *Policy) Current() Setting

Current returns the setting every request is judged against.

func (*Policy) Disabled

func (p *Policy) Disabled() bool

Disabled reports whether a request may proceed without a gateway API key.

func (*Policy) Set

func (p *Policy) Set(setting Setting)

Set replaces the running setting. It takes effect on the next request.

type Record

type Record struct {
	Revision uint64
	Setting  Setting
}

Record is the versioned stored setting.

type Repository

type Repository interface {
	Get(context.Context) (Record, error)
	Put(context.Context, Setting, uint64) (Record, error)
}

Repository is the durable authentication-mode contract. A stored mode is what makes a console change outlive the process that accepted it.

func Open

func Open(store storage.KVStore) (Repository, error)

Open returns a storage-backed authentication-mode repository.

type Setting

type Setting struct {
	Mode      Mode      `json:"mode"`
	Source    Source    `json:"source"`
	UpdatedAt time.Time `json:"updated_at,omitzero"`
}

Setting is one stated mode and the place it was stated.

func Resolve

func Resolve(stated Mode, source Source, persisted Setting) Setting

Resolve returns the setting the gateway starts under.

A configuration value or a flag is the operator speaking about this process, and it wins: a stored value that silently overrode an explicit STARPORT_SECURITY_AUTH_MODE=required would turn a deployment's own statement into a suggestion, and a stored value that overrode --no-auth would leave an operator with no way to open a gateway whose stored mode they cannot reach to change. The stored value applies exactly when nobody stated anything, which is the case a console change exists to serve.

func (Setting) Effective

func (s Setting) Effective() Setting

Effective resolves the unset cases so a reader never has to.

type Source

type Source string

Source names where the running mode came from. An operator who wants to change the mode has to change the thing that set it, and the four sources are changed in four different places.

const (
	// SourceUnset means nobody stated a mode. It is the input to Resolve and
	// never the answer it returns.
	SourceUnset Source = ""
	// SourceDefault means no configuration, flag, or stored value existed.
	SourceDefault Source = "default"
	// SourceConfig means a configuration value or environment variable stated it.
	SourceConfig Source = "config"
	// SourceFlag means a command-line flag stated it for this process only.
	SourceFlag Source = "flag"
	// SourceConsole means an operator changed it at runtime and it was stored.
	SourceConsole Source = "console"
)

Jump to

Keyboard shortcuts

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