authz

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 10 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 authz implements per-action authorization (ADR-0009).

Every API route declares the action it performs by wrapping its handler with Guard.Protect, or explicitly opts out with Public. Verify walks the router and rejects routes that did neither, so a forgotten check fails at startup instead of silently allowing access (default deny).

The package does not depend on the authentication implementation: the guard obtains the caller through a SubjectFunc supplied by the server.

Index

Constants

View Source
const RoleAdmin = "admin"

RoleAdmin is the only role in MVP.

Variables

View Source
var ErrForbidden = errors.New("forbidden")

ErrForbidden is returned when a subject may not perform an action.

Functions

func Known

func Known(a Action) bool

Known reports whether a is in the catalog.

Types

type Action

type Action string

Action is a permission name in the form "<area>:<verb>".

const (
	SessionSelf Action = "session:self" // own session: me, logout

	// JobsRead lists background jobs (#126); each job is shown only to callers allowed the
	// action that started it, which is also required to cancel it.
	JobsRead Action = "jobs:read"

	ContainersRead    Action = "containers:read"    // list, inspect, logs, stats
	ContainersOperate Action = "containers:operate" // start, stop, restart, kill, pause
	ContainersWrite   Action = "containers:write"   // create, recreate, remove
	ContainersExec    Action = "containers:exec"    // web terminal (kept separate for #102)

	ImagesRead  Action = "images:read"
	ImagesWrite Action = "images:write" // pull, tag, push, remove

	VolumesRead   Action = "volumes:read"
	VolumesWrite  Action = "volumes:write"
	NetworksRead  Action = "networks:read"
	NetworksWrite Action = "networks:write"

	StacksRead   Action = "stacks:read"
	StacksWrite  Action = "stacks:write"  // edit files, adopt, remove
	StacksDeploy Action = "stacks:deploy" // up, down, restart, pull

	NamespacesRead  Action = "namespaces:read"
	NamespacesWrite Action = "namespaces:write" // create, remove (#195)

	SystemRead  Action = "system:read" // info, events, df, runtime versions
	SystemPrune Action = "system:prune"

	RegistriesRead  Action = "registries:read"
	RegistriesWrite Action = "registries:write"
	UsersRead       Action = "users:read"
	UsersWrite      Action = "users:write"
	SettingsRead    Action = "settings:read"
	SettingsWrite   Action = "settings:write"
)

Action catalog (ADR-0009). Add new actions here; the catalog doubles as the permission list for the future roles UI (#78).

func All

func All() []Action

All returns every known action.

func (Action) Read

func (a Action) Read() bool

Read reports whether an action only reads: "<area>:read", or the caller's own session.

type AdminOnly

type AdminOnly struct{}

AdminOnly is the MVP authorizer: the admin role may do everything, nobody else anything. Unknown actions are always denied.

func (AdminOnly) Authorize

func (AdminOnly) Authorize(_ context.Context, s Subject, a Action, _ Resource) error

Authorize implements Authorizer.

type Authorizer

type Authorizer interface {
	Authorize(ctx context.Context, s Subject, a Action, r Resource) error
}

Authorizer decides whether a subject may perform an action on a resource.

type Guard

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

Guard wraps handlers with authorization checks.

func NewGuard

func NewGuard(authorizer Authorizer, subject SubjectFunc, logger *slog.Logger) *Guard

NewGuard returns a guard using authorizer and subject.

func (*Guard) Allowed

func (g *Guard) Allowed(r *http.Request, action Action) bool

Allowed reports whether the request's subject may perform action. Use it inside public handlers that reveal extra data to authorized callers.

func (*Guard) Protect

func (g *Guard) Protect(action Action, h http.HandlerFunc) *Protected

Protect returns a handler that requires an authenticated subject allowed to perform action. It panics for actions missing from the catalog (programming error).

type Protected

type Protected struct {
	Action Action
	// contains filtered or unexported fields
}

Protected is a handler with a declared action.

func (*Protected) ServeHTTP

func (p *Protected) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type PublicHandler

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

PublicHandler is a handler explicitly marked as not requiring authorization.

func Public

func Public(h http.HandlerFunc) *PublicHandler

Public marks h as reachable without authorization (setup, login, version, health).

func (*PublicHandler) ServeHTTP

func (p *PublicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type Resource

type Resource struct {
	Environment string
	Namespace   string
	ID          string
}

Resource identifies what an action applies to. Fields are empty when not applicable; they allow resource-level rules later (e.g. #97 is out of scope, but namespaces may be).

func ResourceFromRequest

func ResourceFromRequest(r *http.Request) Resource

ResourceFromRequest builds the resource from the conventional route parameters ({env}, {id}) and the namespace query parameter (ADR-0006).

func (Resource) String

func (r Resource) String() string

String formats the resource for logs.

type Route

type Route struct {
	Method string
	Path   string
	Action Action // empty for public routes
	Public bool
}

Route describes a registered route and its declared access.

func Inspect

func Inspect(routes chi.Routes, prefix string) ([]Route, error)

Inspect lists routes whose path starts with prefix together with their declared access. It returns an error listing every route that declares neither an action nor Public.

type Subject

type Subject struct {
	UserID   int64
	Username string
	Role     string
	// APIKey is set when the caller authenticated with an API key (#87); ReadOnly keys may
	// only perform read actions.
	APIKey   bool
	ReadOnly bool
}

Subject is the authenticated caller.

type SubjectFunc

type SubjectFunc func(*http.Request) (Subject, bool)

SubjectFunc returns the authenticated caller of a request.

Jump to

Keyboard shortcuts

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