Documentation
¶
Overview ¶
Package admin provisions mash endpoints: the permission template a NATS user needs, and the streams, consumer and directory record behind it. mash never mints users or holds account seeds; the operator's own tooling issues the user with these permissions.
Index ¶
- Constants
- Variables
- func NSCFlags(owner, name string) []string
- func Permissions(owner, name string) jwt.Permissions
- func PermissionsTo(owner, name string, recipients []string) jwt.Permissions
- func ZonePermissions(owner, zone string, wildcardZones []string) jwt.Permissions
- func ZonePermissionsWithDeny(owner, zone string, wildcardZones []string) jwt.Permissions
- type Admin
- func (a *Admin) ApplyLimits(ctx context.Context, names ...string) ([]string, error)
- func (a *Admin) Deprovision(ctx context.Context, name string) error
- func (a *Admin) DeprovisionIfUser(ctx context.Context, name, user string) error
- func (a *Admin) EnsureInbox(ctx context.Context, name string) ([]string, error)
- func (a *Admin) Init(ctx context.Context) error
- func (a *Admin) List(ctx context.Context) ([]store.Record, error)
- func (a *Admin) Provision(ctx context.Context, name, user, account, note string, meta Meta) (store.Record, error)
- func (a *Admin) SetLimits(l store.Limits) error
- func (a *Admin) SetMeta(ctx context.Context, name string, u MetaUpdate) (store.Record, error)
- type Meta
- type MetaUpdate
Constants ¶
const ( AllowTag = "allow" DenyTag = "deny" // NoDenyTag is the harmless deny tag a user of a zone whose key has deny templates carries when it has no other. // nats-server refuses to authenticate a user whose scoped key has a {{tag(deny)}} template in a deny list and // that has no deny tag ("generated invalid subject"; allow lists just drop the line). No zone or endpoint name // starts with a digit, so agents.<verb>.*.<owner>.0 addresses nothing. NoDenyTag = DenyTag + ":0" )
Tag names a user's JWT carries to widen or narrow what its zone's signing key grants, and the templates that expand them. A tag is "<name>:<zone>.<endpoint>" (or "<name>:<zone>.*"), lower case, and its value must be a valid subject tail: it is placed in a permission subject as it stands.
Variables ¶
var ( // ErrRecordOtherUser: the directory record's user is not the key given. ErrRecordOtherUser = errors.New("its record belongs to another user key (left in place)") // ErrRecordChanged: the record was replaced or changed between the read and the delete. ErrRecordChanged = errors.New("the record changed; left in place") // ErrNoRecord: there is no directory record, so nothing proves who owns the name (or its inbox stream). ErrNoRecord = errors.New("it has no directory record, so nothing proves the key owns it (left in place)") )
The refusals of DeprovisionIfUser: each leaves the record, the lease and the inbox as they were.
Functions ¶
func Permissions ¶
func Permissions(owner, name string) jwt.Permissions
Permissions is the complete permission set of endpoint name under owner (spec section 8). Anything not listed is denied.
func PermissionsTo ¶ added in v0.3.2
func PermissionsTo(owner, name string, recipients []string) jwt.Permissions
PermissionsTo is Permissions with the three peer-addressing publish lines (mail, prompt and status to any endpoint of the owner) narrowed to recipients: one line per recipient and verb, agents.<verb>.*.<owner>.<recipient>. A nil recipients keeps the wildcard lines, so PermissionsTo(owner, name, nil) is Permissions. An empty non-nil slice grants no mail, prompt or status line at all; the fixed lines (own heartbeat, inbox, directory read, lease) remain, so the block is never empty. Recipients must be valid endpoint names: they are placed in subjects as they are.
func ZonePermissions ¶ added in v0.3.2
func ZonePermissions(owner, zone string, wildcardZones []string) jwt.Permissions
ZonePermissions is the permission template of the scoped signing key of zone under owner, in the subject layout agents.<verb>.<kind>.<owner>.<zone>.<name> (heartbeats keep SAP's five tokens). Every endpoint of the zone is a user signed by this key and gets, by expansion, its own copy of the fixed per-endpoint lines ({{name()}} is the user's name) and these peer-addressing lines:
- one publish line per verb (mail, prompt, status) and wildcard zone: agents.<verb>.*.<owner>.<zone>.*, so every endpoint of the zone may address every endpoint of each wildcardZones entry;
- the exception line agents.<verb>.*.<owner>.{{tag(allow)}}, one subject per "allow:" tag of the user; a user with no allow tag gets none.
Nothing else addresses a peer. The block is never empty. See ZonePermissionsWithDeny for deny exceptions. The templates need the scoped-key user to carry empty permissions of its own.
func ZonePermissionsWithDeny ¶ added in v0.3.2
func ZonePermissionsWithDeny(owner, zone string, wildcardZones []string) jwt.Permissions
ZonePermissionsWithDeny is ZonePermissions plus the publish deny lines agents.<verb>.*.<owner>.{{tag(deny)}}: a user's "deny:" tags remove peers from what the zone's wildcards grant (a deny beats any allow). Every user of the zone must then carry at least one deny tag, NoDenyTag if it has no real one.
Types ¶
type Admin ¶
type Admin struct {
// contains filtered or unexported fields
}
Admin provisions endpoints with the account's admin credentials.
func (*Admin) ApplyLimits ¶ added in v0.3.1
ApplyLimits raises the configured limits on existing inboxes in place with a stream update: max_bytes and max_age where they are below the configured value, and max_consumers where it differs. With no names it covers every directory endpoint. Lowering max_bytes or max_age is refused with an error naming the endpoint and field (nothing changes for that endpoint), because it could discard stored mail; the other endpoints are still processed and the errors are joined. It returns one line per endpoint saying what changed.
func (*Admin) Deprovision ¶
Deprovision fences name's lease with a tombstone, then removes its directory record and inbox stream. The fence comes first: a session that is live or starting cannot take the lease back once its endpoint is gone (#10).
func (*Admin) DeprovisionIfUser ¶ added in v0.3.2
DeprovisionIfUser is Deprovision for a record that must still belong to user (a public user key): it reads the record's value and revision, refuses unless its user equals user, and deletes the record with a revision guard (Purge with LastRevision), so a record replaced between the read and the delete is never removed. A revision conflict re-reads the record (up to setMetaTries times): a metadata-only change by another admin leaves the user unchanged, so the delete is retried on the new revision; a changed user, a vanished record or exhausted retries refuse.
Order: guard and guarded record delete first, then the lease fence and the inbox delete. A refused call therefore touches nothing: no tombstone, no stream delete. The whole call runs under the name's lock (ADR-0048), so another admin's Provision of the name waits until the fence and the inbox delete are done and is never fenced and never has its inbox deleted. For sessions the gap between the record delete and the fence is safe because a session cannot hold a record-less name: Open re-reads the directory record after taking the lease and gives the lease back when it is gone (see Open), and a session that already held the lease is stopped by the tombstone at its next refresh, exactly as in Deprovision. The cost of this order against Deprovision's is that a failure between the two steps leaves a record-less name with an inbox and no fence; re-running `deprovision` (without --user) finishes it.
With no record nothing proves ownership, so it refuses even when the inbox stream exists (a provision that failed before writing its record): remove that stream with a plain deprovision.
func (*Admin) EnsureInbox ¶
EnsureInbox makes sure name's inbox stream and its durable consumer exist, creating whichever is missing with the same configuration Provision uses. It is for an endpoint whose directory record already exists (Provision refuses those) and reports what it had to create: "stream", "consumer", both, or neither. An existing stream or consumer is only checked, never updated: one that differs from that configuration in any field mail depends on is an error naming the field, because fixing it in place could discard stored mail (a lower MaxBytes or MaxAge would). The user decides to deprovision it, which removes whatever the inbox holds, and provisions again.
func (*Admin) Init ¶
Init creates the directory and lease buckets. It is idempotent. A bucket from before buckets had a size cap (no MaxBytes) is bounded in place with a stream update, keeping its keys; a bucket whose MaxBytes is smaller than the configured limit is raised to it; a larger one is kept (a limit is never lowered), and nothing is purged.
func (*Admin) Provision ¶
func (a *Admin) Provision(ctx context.Context, name, user, account, note string, meta Meta) (store.Record, error)
Provision creates name's inbox stream, its consumer and, last, its directory record, so a half-done provision can simply be re-run.
func (*Admin) SetLimits ¶ added in v0.3.1
SetLimits sets the caps Init, Provision and EnsureInbox use (default: store.DefaultLimits). It validates them.
func (*Admin) SetMeta ¶
SetMeta changes name's role, host and labels, only those given. It merges into the stored JSON object, so every other field, including ones a newer mash wrote, survives, and writes with a compare-and-set against the revision it read, re-reading and retrying if another write got in between.
type MetaUpdate ¶
type MetaUpdate struct {
Role, Host *string
Labels map[string]string
Description *string
AddCaps []string
DropCaps []string
ClearCaps bool
}
MetaUpdate names the metadata fields SetMeta changes; a nil field is left alone and a pointer to "" clears it. Labels is applied per key: a value sets that label, an empty value clears it, other labels are left alone. Description is set like Role. Caps are applied as a merge: AddCaps are added, DropCaps removed, and ClearCaps removes every capability first (then AddCaps still apply).