Documentation
¶
Overview ¶
Package authz is the permission catalog and the pattern rules that turn a grant pattern into the set of permissions the grant means — design §1「权限目录(40 项)」 and §2「授权模型与判定」of docs/design/2026-10-14-db-capabilities-and-session-grants-design.md.
The package is leaf logic: it depends on the standard library, on internal/util and on internal/config (whose server configuration the §3 derivation reads) — nothing else, and no entry layer, job layer or storage ever grows into it. That is what lets the httpapi entry layer, the job layer (which receives a decision instead of reading caller config) and the offline documentation generator all use it without an import cycle (G022).
A pattern grant is never decided with Match alone: Match is the raw glob. Expand is the entry point for grants, because it also applies the safety rules of design §2 — no pattern ever reaches auth.manage, a wildcard never reaches a dangerous permission (dangerous permissions must be spelled out literally), and a session grant only reaches permissions that may be granted to a session.
Derivation of the root administrator set and of the config-derived grants (design §2「根管理员」 and §3「配置推出的授权(真值表)」of docs/design/2026-10-14-db-capabilities-and-session-grants-design.md).
Before this design a caller's authority lived in four capability bits and four governance switches, read at ~30 places through the config helpers (ServerConfig.CallerCanAnswer and friends). Derive restates exactly those decisions in the new vocabulary — permissions — so that from T1.4 on there is ONE decision path: the derived grants are the read-only floor (source=config, design §3), database grants (T2.1) and session grants (T2.2) add to them, and the root administrator has everything.
The truth table of design §3 is implemented in Derive and pinned cell by cell against the old helper functions by derive_truth_test.go.
Only a user-class caller has permissions: worker and job credentials are confined by their own route tables (workercredential.go / jobcredential.go) and never consult a Derivation.
Index ¶
- Constants
- Variables
- func Match(pattern, name string) bool
- func SubjectOf(kind SubjectKind, id string) string
- type Deprecated
- type Derivation
- func (d *Derivation) Deprecations() []Deprecated
- func (d *Derivation) Effective(callerID string) []string
- func (d *Derivation) For(callerID string) []string
- func (d *Derivation) Has(callerID, perm string) bool
- func (d *Derivation) IsRoot(callerID string) bool
- func (d *Derivation) Roots() []string
- func (d *Derivation) Users() []string
- type Expansion
- type Permission
- type Risk
- type Scope
- type Source
- type SubjectKind
Constants ¶
const ( NotSeeded = 0 // SeedV1 is the first seed (design §3): the group that has no check at all today. SeedV1 = 1 )
Seed versions. A permission's SeedVersion is the migration that plants it for the `users` subject; it is NotSeeded when the permission is never seeded (it comes from the config derivation or belongs to the root administrator alone).
const ( // jobs, workflows, workbench threads. PermJobReadOthers = "job.read.others" PermJobSubmit = "job.submit" PermJobCancelOthers = "job.cancel.others" PermJobManageOthers = "job.manage.others" PermJobDeleteOthers = "job.delete.others" PermJobAttach = "job.attach" PermJobAttachOthers = "job.attach.others" PermJobReview = "job.review" PermJobWakeOthers = "job.wake.others" PermInteractionAnswer = "interaction.answer" // terminal sessions. PermSessionReadOthers = "session.read.others" PermSessionReplyOthers = "session.reply.others" PermSessionManageOthers = "session.manage.others" PermSessionPermissionAnswer = "session.permission.answer" PermSessionTransfer = "session.transfer" // plans and their todos, schedules. PermPlanManageOthers = "plan.manage.others" PermPlanSuperviseOthers = "plan.supervise.others" PermScheduleManageOthers = "schedule.manage.others" // steward, memories, tracker. PermStewardManage = "steward.manage" PermMemoryWrite = "memory.write" PermTrackerSync = "tracker.sync" PermTrackerDelete = "tracker.delete" // tunnels and file transfers. PermTunnelOpen = "tunnel.open" PermTunnelHosted = "tunnel.hosted" PermTunnelManageOthers = "tunnel.manage.others" PermXferWrite = "xfer.write" PermXferManageOthers = "xfer.manage.others" PermSecretScanAll = "secret.scan.all" // update, workers. PermUpdateCheck = "update.check" PermUpdateApply = "update.apply" PermWorkerUpgrade = "worker.upgrade" PermWorkerUpgradeUpload = "worker.upgrade.upload" PermWorkerManage = "worker.manage" // configuration and the assets it carries. PermConfigRead = "config.read" PermMcpTest = "mcp.test" PermConfigWrite = "config.write" PermProjectWrite = "project.write" PermRulesWrite = "rules.write" PermSkillsWrite = "skills.write" // grants themselves. PermAuthManage = "auth.manage" )
Permission names, grouped by resource. A published name never changes (design decision 1), so handlers and route declarations spell them through these constants rather than repeating string literals.
`pty.recordings` is not in the catalog: the design folds pty recordings into job.attach / job.attach.others and keeps that row as an explanatory one only.
const ( DeprecatedIn = "v0.161" DeprecatedRemoveIn = "v0.164" )
Release markers of the deprecated capability bits and governance switches (G032: a compatibility path carries `DEPRECATED(v<in>): remove in v<in+3>` and a startup hint). The keys themselves are marked in internal/config/model.go; Deprecations reports them while they are still in use.
const ( SubjectCallerPrefix = "caller:" SubjectSessionPrefix = "session:" )
How a subject is SPELLED in a stored grant (design §2): the `users` subject is the bare literal, the other two carry their kind as a prefix, so a row of `auth_grants` is readable on its own and a subject id can never be confused with a permission.
const DefaultCallerID = "default"
DefaultCallerID is the caller id of the legacy single server token (httpapi buildCallers): a deployment that declares no `server.callers` has exactly this one authenticating caller, and design §2 makes it the root administrator.
Variables ¶
var ( // ErrNoMatch: the pattern matches no permission after the filters of design §2, so // there is nothing to grant (「保存时模式必须至少匹配一项」). ErrNoMatch = errors.New("pattern matches no permission") // ErrUnknownSubject: the subject kind is not one of users / caller / session. ErrUnknownSubject = errors.New("unknown grant subject kind") )
Errors returned by Expand and NewSince. Callers (the grant API and the web form) match them with errors.Is to tell the user which rule refused the pattern.
Functions ¶
func Match ¶
Match reports whether pattern matches name. `*` matches any run of characters — including `.` and the empty run — and every other byte matches literally. Patterns are not regular expressions: `?`, `[`, `\` and the rest have no special meaning.
Match is the raw glob, and it is what a route declaration wants when it checks a single permission name. A pattern-based GRANT must not be decided with Match alone: go through Expand, which additionally refuses auth.manage and refuses to reach a dangerous permission through a wildcard.
func SubjectOf ¶ added in v0.162.0
func SubjectOf(kind SubjectKind, id string) string
SubjectOf renders the stored spelling of a grant subject from its kind and id; it is the inverse of ParseSubject for the id it keeps. An unknown kind yields "" — the caller is a store or a handler that validated its input.
Types ¶
type Deprecated ¶
Deprecated is one deprecated capability key the configuration still uses. Caller names the caller that sets it for the per-caller bits, and is empty for the governance switches.
type Derivation ¶
type Derivation struct {
// contains filtered or unexported fields
}
Derivation is the permission set one configuration derives, plus the root administrators of that configuration. It is immutable after Derive returns, and it is one half of the httpapi configuration snapshot (design §3「一致性」).
func Derive ¶
func Derive(cfg *config.ServerConfig, legacyToken bool) *Derivation
Derive computes the root administrators and the config-derived grants of cfg (design §2 / §3).
legacyToken reports whether the deployment's single server token is live — the httpapi entry layer passes the effective token it authenticates with, which may come from the config, the environment or a flag and is therefore invisible to cfg. It only matters for a deployment that declares no callers at all: that `default` caller is the root administrator (design §2). Which deployments have a root at all is config.ServerConfig.HasRootAdmin — an allow_empty_token deployment that declares no callers has none, so the id is not invented for it.
A nil cfg yields an empty derivation: no root, no grant.
func (*Derivation) Deprecations ¶
func (d *Derivation) Deprecations() []Deprecated
Deprecations returns the deprecated capability keys the configuration still uses (G032 hint list, in a stable order).
func (*Derivation) Effective ¶
func (d *Derivation) Effective(callerID string) []string
Effective returns the config-derived permissions of callerID: every permission for a root administrator, otherwise the union of the `users` grants and the caller's own, in design §1 order. Grant management (auth.manage) is never derived, so only a root administrator ever has it.
func (*Derivation) For ¶
func (d *Derivation) For(callerID string) []string
For returns the permissions the configuration derives for callerID itself, without the `users` subject and without the root administrator's everything, in design §1 order.
func (*Derivation) Has ¶
func (d *Derivation) Has(callerID, perm string) bool
Has reports whether the configuration derives permission perm for callerID (see Effective).
func (*Derivation) IsRoot ¶
func (d *Derivation) IsRoot(callerID string) bool
IsRoot reports whether callerID is a root administrator, which holds every permission and is the only subject that may manage grants.
func (*Derivation) Roots ¶
func (d *Derivation) Roots() []string
Roots returns the caller ids of the root administrators, sorted. Empty means the deployment has none (design: the permission page is read-only then).
func (*Derivation) Users ¶
func (d *Derivation) Users() []string
Users returns the permissions the configuration derives for the `users` subject — the grants that hold for every user-class caller — in design §1 order.
type Expansion ¶
type Expansion struct {
// Pattern is the pattern as written by the grant.
Pattern string
// Subject is the subject kind the pattern was expanded for.
Subject SubjectKind
// Permissions are the matched catalog entries in design (§1) order. Expand never
// returns an empty expansion.
Permissions []Permission
}
Expansion is what one grant pattern means: the permissions it matches right now, after the filters of design §2.
func Expand ¶
func Expand(pattern string, subject SubjectKind) (Expansion, error)
Expand expands a grant pattern for subject and applies the safety rules of design §2:
- auth.manage — the only root-risk permission — is never matched by any pattern;
- a wildcard never expands to a dangerous permission: a dangerous permission is handed out only when the pattern spells its name literally;
- a session subject only expands to permissions that may be granted to a session;
- a pattern that matches nothing after these filters is an error (ErrNoMatch), so a grant can never be stored with an empty meaning.
A permission name that spells a dangerous permission literally is refused for a session subject as well, because no dangerous permission is session-grantable.
type Permission ¶
type Permission struct {
// Name is the fixed permission name, `<resource>.<action>[.others]`. A published
// name never changes (design decision 1).
Name string
// Description is what the permission covers (the design table's「覆盖」column): the
// routes and entry points it stands for.
Description string
// Risk grades the damage the permission can do.
Risk Risk
// SessionGrantable reports whether the permission may be granted to a session
// subject. Dangerous permissions never may.
SessionGrantable bool
// Scope records whether the permission can be limited to a scope.
Scope Scope
// Source records where the permission comes from.
Source Source
// SeedVersion is the seed migration that plants the permission for `users`, or
// NotSeeded.
SeedVersion int
}
Permission is one entry of the catalog. The fields mirror the columns of the design table (§1) one for one.
func Lookup ¶
func Lookup(name string) (Permission, bool)
Lookup returns the catalog entry for name, and false if name is not a permission.
func NewSince ¶
func NewSince(pattern string, subject SubjectKind, snapshot []string) ([]Permission, error)
NewSince returns the permissions pattern matches now that the grant's stored snapshot (Expansion.Snapshot, taken when the grant was saved) did not contain — the「有新权限 待确认」list the web shows after the catalog grew (design §2).
Only permissions the expansion really means are reported: a permission that the filters of Expand keep out (auth.manage, a dangerous permission behind a wildcard, a permission that may not be granted to a session) is never "new" for that grant, because the grant never grants it. The result is in design order and is empty when nothing is new.
func Permissions ¶
func Permissions() []Permission
Permissions returns the catalog in the order of the design table (§1). The result is a copy: callers may sort, filter or otherwise keep it.
func Seeded ¶
func Seeded(v int) []Permission
Seeded returns the permissions seed version v plants for the `users` subject, in design order. SeedV1 is the 16 permissions of design §3; when a later catalog addition has to keep an upgrade behaviour-compatible, the version is bumped and only the new rows are seeded (design §3「种入只做一次」).
func (Permission) Validate ¶
func (p Permission) Validate() error
Validate reports whether the entry is internally consistent. It is the invariant the catalog test enforces on every row, including the seeding rule: only a seeded row carries a seed version.
type Risk ¶
type Risk string
Risk grades what a permission lets its holder do if it ends up in the wrong hands (design §1). 危险 (dangerous) means the holder can execute arbitrary code on the server or a worker, or read the root administrator's token; 根 (root) is reserved for auth.manage, which no grant can ever match.
type Scope ¶
type Scope string
Scope answers the design table's「范围」column: may this permission be limited to a scope (a project, an agent, …)? Phase 1 stores `*` on every grant (design decision 10); the column records what the model will allow in phase 2.
type Source ¶
type Source string
Source says where a permission comes from (design §1「来源」).
const ( // SourceSeed: seeded to `users` once, so that an upgrade keeps today's behaviour // (design §3「数据库种入」). SourceSeed Source = "seed" // SourceConfig: derived from the old capability bits / governance switches on every // config load, read-only in the UI (design §3「配置推出」). SourceConfig Source = "config" // SourceRoot: only the root administrator has it, independent of any switch. SourceRoot Source = "root" )
type SubjectKind ¶
type SubjectKind string
SubjectKind is who a grant is issued to (design §2). Only SubjectSession narrows what a pattern may mean; SubjectUsers and SubjectCaller expand a pattern the same way.
const ( SubjectUsers SubjectKind = "users" // 全体用户类调用方 SubjectCaller SubjectKind = "caller" // caller:<id> SubjectSession SubjectKind = "session" // session:<sid> )
func ParseSubject ¶ added in v0.162.0
func ParseSubject(subject string) (SubjectKind, string, error)
ParseSubject splits a stored grant subject into its kind and its id. The `users` subject has no id (the second result is empty); the other two carry the id after their prefix. Anything else — including a prefix with nothing after it or a manager the design does not have (`worker:`, `job:`) — is ErrUnknownSubject, so a row no entry face can interpret is refused rather than silently read as one of the three.
func (SubjectKind) Label ¶
func (k SubjectKind) Label() string
Label is the label the permission reference and the web UI use.
func (SubjectKind) Valid ¶
func (k SubjectKind) Valid() bool
Valid reports whether k is one of the three subject kinds of design §2.