authz

package
v0.121.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 2 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 carries WHO a request (or a background loop) is acting as.

A Principal is attached to a context by exactly three kinds of code:

  • the require* authentication wrappers in internal/api, through WithPrincipal, once the credential has been checked;
  • the background loops, through System, which acts for every user; and
  • the operator's own out-of-request writes (the boot-time GitHub connect, the e2e seed), through Operator, which acts as user 1 and nobody else.

🔴 THE CONTEXT KEY IS UNEXPORTED, AND THAT IS THE COMPILE-LEVEL HALF OF THE GUARD. No package can plant a value that From will return by calling context.WithValue itself: the key type cannot be named outside this file, so the ONLY ways to put a Principal on a context are WithPrincipal, System and Operator. Which code may call those two is the test-level half, and it is a relationship over the source rather than a convention: see internal/api/principal_test.go (every authenticated route observes a Principal with the right Via) and ledger_test.go in this package (the type-checked set of code that reaches System, WithPrincipal, Operator and api's admit, and this package's exported names, are each a ledger).

🔴 THE DATABASE ENFORCES ON IT (accounts PR 3a). internal/db reads the principal on EVERY pool acquire and sets Postgres's `muster.uid` / `muster.scope` from it; row-level security (migration 0007) then shows a connection only its user's rows. A context with NO principal sees no rows and cannot insert one. Today every request principal names user 1 (the operator, admin) — see OperatorUserID — so single-user behaviour is unchanged.

Index

Constants

View Source
const OperatorUserID int64 = 1

OperatorUserID is the user every principal names today: the operator, the one user muster has. It is the seed admin row the accounts schema creates (users.id = 1), and the owner every existing row is backfilled to.

Variables

This section is empty.

Functions

func IsAdmin

func IsAdmin(ctx context.Context) bool

IsAdmin reports whether ctx carries an admin-role USER principal. A scope-all (System) principal names no user and answers false, as does a context with no principal: least privilege.

func Operator

func Operator(ctx context.Context) context.Context

Operator returns ctx carrying the OPERATOR (user 1, admin) for work the process does on the operator's behalf with no request to take a principal from — today the boot-time GitHub connect and the e2e seed. It is NOT a scope-all principal: it sees user 1's rows only, exactly like the operator's browser session. Its call sites are a ledger (ledger_test.go).

func System

func System(ctx context.Context) context.Context

System returns ctx carrying the system principal: a background loop acting across every user's rows. Only the background loops listed in this package's allowlist test may call it.

func WithPrincipal

func WithPrincipal(ctx context.Context, p Principal) context.Context

WithPrincipal returns ctx carrying p. It is for the require* wrappers in internal/api ONLY; the call sites are a ledger in that package's tests.

🔴 IT PANICS ON A MALFORMED PRINCIPAL rather than attaching it. A request principal with no user, an unknown role or a door outside the closed set is a programming error in a wrapper, and attaching it would hand PR 3's row-level security a value that silently selects nobody (or, for a forged ViaSystem, everybody). internal/api's recover middleware turns the panic into a 500 for that one request.

Types

type Principal

type Principal struct {
	// UserID is the acting user. Zero only for the System principal.
	UserID int64
	Role   Role
	Via    Via
	// RoleUncertain is true when Role is a FALLBACK: the user's role could not
	// be read and the principal acts as member (least privilege). Narrowing what
	// the request may do is safe on a fallback; CHANGING stored state because of
	// it is not — connectors.ReconcileGrant refuses to, and the connector door
	// answers a retryable 503 instead of serving it.
	RoleUncertain bool
}

Principal is who a context acts as.

func From

func From(ctx context.Context) (Principal, bool)

From returns the principal ctx carries, and whether it carries one.

func MustFrom

func MustFrom(ctx context.Context) Principal

MustFrom returns the principal ctx carries, panicking if there is none. Use it where reaching the code without one is a wiring bug.

func (Principal) All

func (p Principal) All() bool

All reports whether this principal acts across every user's rows — true only for System. internal/db maps it to the `muster.scope = 'all'` setting.

func (Principal) String

func (p Principal) String() string

String renders the principal for a log line.

type Role

type Role string

Role is a user's role. The set is closed.

const (
	RoleAdmin  Role = "admin"
	RoleMember Role = "member"
)

type Via

type Via string

Via names the door a principal came through: which credential admitted the request, or that no request is involved at all.

It is an audit fact, not an authorization input. Two requests from the same user through different doors carry the same UserID and different Vias.

const (
	// ViaSession is the human browser tier: requireSession (and
	// requireOperatorWrite, which wraps it).
	ViaSession Via = "session"
	// ViaHook is the shared machine hook token: requireHookToken,
	// requireArmedHookToken, and the hook branch of requireHookOrServiceToken.
	ViaHook Via = "hook"
	// ViaService is MUSTER_SERVICE_TOKEN plus an asserted X-Muster-Actor:
	// requireServiceToken and the service branch of requireHookOrServiceToken.
	ViaService Via = "service"
	// ViaAgent is an agent's own per-agent token on the agent self-service API
	// (requireAgentToken: /agent/task, /agent/muster).
	ViaAgent Via = "agent"
	// ViaConnect is an agent's own per-agent token on the connector surface
	// (requireConnectorAgent: /connect/, /agent/connectors, /mcp) — the routes
	// the dedicated connect listener serves.
	ViaConnect Via = "connect"
	// ViaRelay is an agent's own per-agent token on the claude-code approval
	// relay (requireRelayAgent: /agent/approvals, /agent/blocked).
	ViaRelay Via = "relay"
	// ViaToken is a user's own PERSONAL API token (`mst_…`, accounts PR 6),
	// presented on the machine tier (requireHookToken and its siblings, and
	// requireAPIToken). It acts as the token's OWNER, never as the machine
	// user. The spelling is the audit_log `via` value migration 0006 names.
	ViaToken Via = "api-token"
	// ViaRunner is a host's BROWSER-FLOW RUNNER token (`mfr_…`, internal/flows):
	// requireRunnerToken, on the /runner/flows/* routes only. It acts as the
	// token's OWNER, so row-level security confines the runner to that owner's
	// jobs; the token's host confines it further, to jobs addressed to it.
	ViaRunner Via = "flow-runner"
	// ViaPlugin is a PLUGIN token (`mpl_…`, internal/plugins): requirePluginToken,
	// on the /plugin/v1/* routes only. It acts as the token's OWNER, so row-level
	// security confines the plugin to that owner's plugin rows.
	ViaPlugin Via = "plugin"
	// ViaQueue is the SERVER-SIDE SEND QUEUE's dispatcher probing the window
	// and delivering a message its owner queued from a signed-in session
	// earlier (internal/sendqueue): the deferred end of that owner's own
	// request, acting as the OWNER only. One site mints it — internal/api's
	// queuedPrincipal, called only by the dispatcher — and it is a request door
	// in the sense that matters here: one user, never everybody.
	ViaQueue Via = "send-queue"
	// ViaFocus is the SESSION FOCUS refresher (internal/focus) reading one
	// owner's sessions and writing their focus: a background job acting as the
	// OWNER only, never as everybody. One site mints it — internal/api's
	// focusPrincipal, called only by the refresher and the retention sweep
	// (their AsOwner), handed each owner after focus.PGStore.Owners read the
	// owner ids under System.
	ViaFocus Via = "focus"
	// ViaRoute is SESSION ROUTING's timeout sweep (internal/plugins/routes.go)
	// timing a route out and handing its message back to the chief: a
	// background job acting as the route's OWNER only. One site mints it —
	// internal/api's routePrincipal, called only by the sweep, handed each owner
	// after plugins.PGStore.ExpiredRoutes (or RoutedPastes) read the routes
	// under System.
	ViaRoute Via = "session-route"
	// ViaAttention is the ATTENTION QUEUE's background work acting as one
	// owner: reading that owner's mirrored windows to see which became (or
	// stopped) waiting on the person, and telling that owner's devices about
	// an ask the expiry sweep closed, and delivering an answer into that
	// owner's agent's session (migration 0031). One site mints it —
	// internal/api's attentionPrincipal — handed the owner by the mirror hub's
	// change nudge, by attention.PGStore.ExpireDue's row, or by an answer's own
	// row (the answer request's, or the delivery sweep's DueDeliveries).
	ViaAttention Via = "attention"
	// ViaAsk is the AskUserQuestion watcher (internal/api's question watch)
	// acting as one owner: reading that owner's mirrored window's transcript
	// for how a question it shows was answered. One site mints it —
	// internal/api's askPrincipal — handed the owner of the question its host's
	// hook registered.
	ViaAsk Via = "ask"
	// ViaReap is the STOPPED-AGENT REAPER (internal/api's reapStoppedAgents)
	// destroying one owner's agent that has been stopped for
	// MUSTER_AGENT_REAP_STOPPED_AFTER: a background job acting as the agent's
	// OWNER only, through the same teardown the owner's own Delete takes. One
	// site mints it — internal/api's reapPrincipal — handed each owner after
	// agents.PGStore.ListReapableStopped read the due rows under System.
	ViaReap Via = "agent-reap"
	// ViaOfficer is the OFFICER RUNTIME's wake loop (internal/officers, bound
	// in internal/api's officer fleet) reading, creating, starting and
	// stopping one declared officer's instance agents: a background job acting
	// as the officer's configured OWNER only. One site mints it —
	// internal/api's officerPrincipal — handed the owner from MUSTER_OFFICERS.
	ViaOfficer Via = "officer"
	// ViaMirrorHost is a mirror host's token (`mmt_…`) on the mirrored
	// sessions' MCP door (POST /mirror/mcp): a Claude Code session on that host,
	// through the host's muster-mirror, acting as the token's owner — and only
	// for a session that host lists now. internal/api's requireMirrorHostToken.
	ViaMirrorHost Via = "mirror-host"
	// ViaDevice is a paired phone's DEVICE credential (`mdv_…`,
	// internal/devices): requireDeviceToken, on the /api/device/* routes only.
	// It acts as the device's OWNER, so row-level security confines the device
	// to that owner's rows.
	ViaDevice Via = "device"
	// ViaSystem is a background loop acting for every user. Only System sets
	// it.
	ViaSystem Via = "system"
	// ViaOperator is the operator acting OUTSIDE any request: a one-shot write
	// the process makes on the operator's behalf (cmd/muster-server's boot-time
	// GitHub connect) or a fixture (e2e/seed). Only Operator sets it.
	ViaOperator Via = "operator"
)

Jump to

Keyboard shortcuts

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