authz

package
v0.273.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 5 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 IsPluginVia ¶ added in v0.263.0

func IsPluginVia(via Via) bool

IsPluginVia reports whether via is a PLUGIN token's door: a person's or a team's own plugin (ViaPlugin) or a server app acting for a person (ViaServerPlugin). THE ONE PREDICATE every "is this a plugin?" branch asks (internal/api refusePluginTokenElsewhere, internal/plugins ruleWriter), so a server app is never let past a rule a plugin is held to.

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.

A TEAM principal (p.Team) is accepted only as the team acting principal: through a door in teamActingVias, with role member and no Teams of its own (a team is nobody's member, and muster.teams must stay empty for it).

Types ¶

type Membership ¶ added in v0.224.0

type Membership struct {
	Team int64
	Role TeamRole
}

Membership is one person's role in one team.

type Memberships ¶ added in v0.224.0

type Memberships string

Memberships is a person's memberships in ONE canonical, comparable form: "team:role" pairs sorted by team id and joined by commas ("" = none), so a Principal stays comparable with == and the same set always reads the same. Build it with NewMemberships; the zero value is "no teams".

func NewMemberships ¶ added in v0.224.0

func NewMemberships(ms []Membership) (Memberships, error)

NewMemberships is ms in canonical form. A team listed twice, a non-positive team id or an unknown role is an error, never a guess.

func (Memberships) List ¶ added in v0.224.0

func (m Memberships) List() []Membership

List is the memberships, by team id.

func (Memberships) RoleIn ¶ added in v0.224.0

func (m Memberships) RoleIn(team int64) (TeamRole, bool)

RoleIn is the role in team, and whether there is one.

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
	// Team is true when UserID names a TEAM (users.kind = 'team',
	// docs/design/team-sharing.md). A team owns things; nobody signs in as
	// one. WithPrincipal accepts such a principal only as the TEAM ACTING
	// PRINCIPAL (role member, no Teams) through one of teamActingVias, and
	// internal/api's admit answers 403 on every door but an agent's own token.
	Team bool
	// Teams is the acting person's team memberships, read with the role (one
	// read per request, internal/users Account). Phase 1 of team sharing reads
	// it for nothing: no row a team owns is visible to a member yet.
	Teams Memberships
}

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.

func (Principal) TeamScope ¶ added in v0.224.0

func (p Principal) TeamScope() Memberships

TeamScope is the memberships this principal's database queries may use: Teams for a person's own request doors (teamVias), none otherwise.

type Role ¶

type Role string

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

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

type TeamRole ¶ added in v0.224.0

type TeamRole string

TeamRole is a person's role in a team (docs/design/team-sharing.md). The set is closed and ordered: viewer < operator < manager.

const (
	TeamViewer   TeamRole = "viewer"
	TeamOperator TeamRole = "operator"
	TeamManager  TeamRole = "manager"
)

func (TeamRole) AtLeast ¶ added in v0.224.0

func (r TeamRole) AtLeast(min TeamRole) bool

AtLeast reports whether r is min or above it. An unknown role is below every role, and no role is at least an unknown one.

func (TeamRole) Valid ¶ added in v0.224.0

func (r TeamRole) Valid() bool

Valid reports whether r is one of the three roles.

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 (which
	// requireArmedHookToken is), 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"
	// ViaServerPlugin is a SERVER-INSTALLED APP's token (`mpl_…`, a plugins
	// row with scope 'server' and no owner; docs/design/meetings.md, "Core
	// seam: server-installed app plugins") acting FOR ONE PERSON: the person
	// whose live grant the request's Muster-Act-For subject names, read from
	// the grant row by requirePluginToken's server arm (internal/api
	// admitServerPlugin), never from anything the request says. Served on the
	// act-for routes alone (notify, its withdrawal, tasks). It never acts as a
	// team, and the server token's other routes carry no principal at all.
	ViaServerPlugin Via = "server-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 — for a team's row the team acting
	// principal (team sharing 2b.4; teamActingVias). 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 — or, the open-question scan,
	// for a question a waiting window still shows open. One site mints it —
	// internal/api's askPrincipal — handed the owner of a registered question,
	// or the owner the mirror hub's nudge named.
	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.
	// That owner may be a TEAM (team sharing phase 3): the principal is then
	// the team acting principal (teamActingVias).
	ViaOfficer Via = "officer"
	// ViaKickoff is the KICKOFF DELIVERER (internal/agentkickoff) running a
	// gateway agent's FIRST TURN with its tools in muster's process: each tool
	// call that turn makes acts as the agent's OWNER only, as the same call in
	// one of its chat turns does (the owner's session). One site mints it —
	// internal/api's turnPrincipal, reached only through gatewayTurnTools
	// (KickoffTurnTools), handed the agent row the deliverer read under System.
	// 🔴 Never System itself: that would run an agent's tools across every
	// owner's rows.
	ViaKickoff Via = "kickoff"
	// ViaAgentMessage is the MACHINE MESSAGE door (POST
	// /api/agents/{name}/messages, `chief ask`) running a gateway agent's turn
	// with its tools in muster's process: each tool call that turn makes acts
	// as the agent's OWNER only — never as the request's own principal, which
	// for the shared hook token is the operator, whoever owns the agent. One
	// site mints it — internal/api's turnPrincipal, reached only through
	// gatewayTurnTools (handleAPIAgentSendMessage's gatewayTurn).
	ViaAgentMessage Via = "agent-message"
	// 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"
	// ViaHostDispatch is the HOST-DISPATCH RECONCILER (internal/api's
	// host_dispatch_reconcile.go) moving one owner's host_dispatches rows on a
	// spawn reply, a listing or its sweep: a background job acting as the row's
	// OWNER only. One site mints it — internal/api's hostDispatchPrincipal.
	ViaHostDispatch Via = "host-dispatch"
	// ViaJobQueue is the MACHINE JOB QUEUE's runner (internal/api's job drain)
	// running a job that no request of this process is holding — one a crashed
	// or restarted server left queued — as the job's OWNER only, never as
	// everybody. One site mints it — internal/api's jobPrincipal — handed the
	// owner of a head jobs.PGStore.Heads read under System.
	ViaJobQueue Via = "job-queue"
	// ViaHostJob is the SCHEDULED HOST JOB runner (internal/api's
	// hostjobs.go): a host's jobs message, a due cron fire, a host's
	// job-result or the lost-run sweep acting on one owner's host_jobs and
	// host_job_runs rows, its job's page and its Attention item — as that
	// OWNER only, never as everybody. One site mints it — internal/api's
	// hostJobPrincipal. NOT a team acting door: a team host's jobs are
	// refused before it is reached (a job's page is personal), so a team principal
	// here is a bug and panics.
	ViaHostJob Via = "host-job"
	// ViaPageLink is a SHARE LINK of a page (internal/pages, migration 0116):
	// a viewer with no muster account, holding /s/<token>, acting as the
	// page's OWNER for that one page only — the link's capability, never a
	// session. internal/api's requirePageLink, on the /s/{token}/* routes.
	ViaPageLink Via = "page-link"
	// ViaTeamAdmin is a TEAM MANAGER's lifecycle door (team sharing phase 4,
	// slice 4.5): a person who manages a team, signed in, stopping, starting,
	// destroying or rotating the credentials of an agent the TEAM owns, or
	// provisioning and switching the team's chief (/teams/{id}/chief). The
	// operation runs AS THE TEAM — its credentials, its budget, its rows — never
	// on the manager's own. One site mints it — internal/api's teamAdminContext —
	// after a FRESH read of the person's role says manager of that team.
	ViaTeamAdmin Via = "team-admin"
	// 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