permission

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package permission manages permission documents (decision 0006): the single internal shape behind the v1 and v2 permission-target APIs and the Access v2 permissions API. Each API converts its wire form to the model form and calls the service, so validation and storage happen once whichever form a client writes.

Role

access module, core layer: the permission service. The permission target and Access permission handlers in access/adapters/http build a Service over the configured store and the artifacts module's repositories, and call it; the evaluator in core/authz reads the stored documents through the permission store, not through this package. The package imports the standard library, the kernel, access/api (resource types, targets and actions), core/model and core/port, and artifacts/api (Repositories, and repo with its ResolveKey).

Inputs

  • Service: a Store (the access ports with their unit of work), which the metadata store satisfies; the artifacts module's Repositories over the same unit of work; a clock.
  • Permission documents in model form, one section of one (PutResource, MergeResource, DeleteResource), and the principals a filtered listing asks about.
  • For a holder of MANAGE who is not an administrator, its Manager, which CheckManagerEdit judges an edit against.

Outputs

  • Permissions in the permission store, ordered by name when listed, and those granting to given users or groups (Granting). Page reads either list a page at a time from the store, for the principals PrincipalsOf gathers as Granting does (#1408).
  • ErrInvalid with the message a handler returns after the sentinel, ErrNotFound, ErrConflict and ErrForbidden; store errors come back wrapped with "permission: ".

Invariants

  • Each write stores in one transaction of the Store. The repository reads take the context they are given, so a read made inside that transaction joins it.
  • A target names a special class of repositories, an existing repository that is not virtual, or a remote repository's "-cache" alias (decision 0006).
  • A holder of MANAGE grants or revokes only what it holds, judged against the stored grants, never the ones it sends.

References

Status

Implemented P4: the permission target and Access permission APIs (pull request 104), a MANAGE holder's limits (pull requests 435 and 459) and the Manage Resources role and listing filters (pull request 470). Moved here in P9d, where Service takes Store and reads repositories through the artifacts module's API.

Testing

  • permission_test.go: TestUnitCreateGetDelete, TestUnitValidate, TestUnitPutAndPutResource, TestUnitWrite, TestUnitCheckManagerEdit, TestUnitMergeAndDeleteResource, TestUnitGrantingAndList, TestUnitValidResourceType, TestUnitDefaultClock, TestUnitNarrow and TestUnitFoldUsers.
  • username_test.go: TestUnitUserGrantsFold and TestUnitManagerOwnEntryFolds (user grants in any case, #1432).
  • page_test.go: TestUnitPrincipalsOfAndPage (a walk of Page at every page size visits what Granting collects).
  • fault_test.go: TestUnitStoreFailures.

The tests run on the metadata store's in-memory store, with a stand-in for the artifacts module's repositories (repositories_test.go); export_test.go lets a test cross a listing page cheaply.

Index

Constants

View Source
const (
	// MaxNameLength is the longest permission name; the v2 and Access
	// documentation both cap names at 64 characters.
	MaxNameLength = 64
	// MaxPatternLength is the longest include or exclude pattern.
	MaxPatternLength = 1024
)

Variables

View Source
var (
	// ErrInvalid reports a document the caller must change; handlers answer
	// it 400 with the message after the sentinel.
	ErrInvalid = errors.New("permission: invalid permission")
	// ErrNotFound reports a missing permission, or a missing section of one.
	ErrNotFound = errors.New("permission: permission not found")
	// ErrConflict reports a create of a name already in use.
	ErrConflict = errors.New("permission: permission already exists")
	// ErrForbidden reports a change the caller may not make to a permission
	// it manages; handlers answer it 403 with the message after the sentinel.
	ErrForbidden = errors.New("permission: permission change not allowed")
)

Handlers remove the sentinel prefix and return the remaining message to clients; preserve the casing of those permission API response templates.

Functions

func CheckManagerEdit

func CheckManagerEdit(
	old, p model.Permission,
	sections []accessapi.ResourceType,
	m Manager,
) error

CheckManagerEdit checks a replace of the sections of old by those of p made by m. Such a holder may change only actions (createorreplacepermissiontargetv2.json): every section and target stays, with the same patterns. It may not change its own entry, or the entry of a group it belongs to: the permissions page lets a manager change the grants of "other users and groups", and without the rule a holder could strip or rewrite the grants an administrator gave it. In each section it may grant or revoke only what m.Grantable allows from the stored grants; judged on p's grants instead, a holder could first give itself an action and then pass it on.

func ValidName

func ValidName(name string) error

ValidName refuses names that cannot be a path segment or exceed the documented length.

func ValidResourceType

func ValidResourceType(rt accessapi.ResourceType) bool

ValidResourceType reports whether rt names a section the API accepts.

Types

type Manager

type Manager interface {
	// Grantable returns the actions it may grant or revoke in a section
	// whose stored grants are g.
	Grantable(g model.Grants) accessapi.Actions
	// Self returns its user name and the groups whose entries are its own.
	Self() (user string, groups []string)
}

Manager is a holder of MANAGE who is not an administrator, as CheckManagerEdit asks about it; *authz.Decider is one.

type Principals

type Principals struct {
	Users  []string
	Groups []string
}

Principals are the users and groups a filtered listing asks about: those named, and the named users' groups when inherited grants count.

func (Principals) Narrow

Narrow returns p with only the grants it makes to pr. An expanded listing shows what the requested principals hold, not what the permission grants to others (getpermissions.json).

type Service

type Service struct {
	// Store holds the permissions and the users and groups they name. Every
	// write runs in one Tx of Store, and the calls it makes join it through
	// the context.
	Store Store
	// Repositories reads the repositories a permission's artifact section
	// names. Its errors come back unwrapped, so kernel.ErrNotFound still
	// means a missing repository, and its calls take the context of Store's
	// transaction, so it must be the artifacts module's binding over the same
	// unit of work.
	Repositories artifactsapi.Repositories
	// Now supplies the time; nil means time.Now.
	Now func() time.Time
}

Service validates permission documents and reads and writes them in Store, checking the repositories they name through Repositories.

func (Service) Create

func (s Service) Create(ctx context.Context, p model.Permission) error

Create stores a new permission; ErrConflict when the name is taken. Its user grants are stored under folded names (model.Permission.FoldGrants).

func (Service) Delete

func (s Service) Delete(ctx context.Context, name string) error

Delete removes the permission.

func (Service) DeleteResource

func (s Service) DeleteResource(ctx context.Context, name string, rt accessapi.ResourceType) error

DeleteResource removes one section; the permission stays, even empty.

func (Service) FoldUsers

func (s Service) FoldUsers(ctx context.Context, names []string) ([]string, error)

FoldUsers returns, sorted and without repeats, the stored names of the users that names name in any case, as the Access listing's user filter matches them (getpermissions). The stores hold only folded names (model.FoldUsername), so each name's folded spelling is looked up exactly. ErrNotFound names the first that matches no user, as the request spelled it.

func (Service) Get

func (s Service) Get(ctx context.Context, name string) (model.Permission, error)

Get returns one permission.

func (Service) Granting

func (s Service) Granting(
	ctx context.Context,
	users, groups []string,
	inherited bool,
) ([]model.Permission, Principals, error)

Granting returns the permissions granting any action to one of users or one of groups, ordered by name, and the principals that matched. With inherited, a user's groups count as well. A user name matches in any case, and the principals carry its stored name; a group name matches exactly. ErrNotFound names the first user or group that does not exist.

func (Service) List

func (s Service) List(ctx context.Context) ([]model.Permission, error)

List returns every permission, ordered by name.

func (Service) MergeResource

func (s Service) MergeResource(
	ctx context.Context,
	name string,
	rt accessapi.ResourceType,
	patch model.Resource,
) (model.Resource, error)

MergeResource merges patch into an existing section: principals and targets in patch replace their stored counterparts, others stay.

func (Service) Page

func (s Service) Page(
	ctx context.Context,
	after string,
	limit int,
	pr *Principals,
) ([]model.Permission, bool, error)

Page returns up to limit permissions named after after, ordered by name, and whether more follow, read from the store a page at a time rather than collected: every permission, or with pr only those granting some action to one of its users or groups, which is the union Granting collects.

func (Service) Pages

func (s Service) Pages() *kernel.Pages[model.Permission]

Pages walks every permission a store page at a time, in List's order, for a list written as it is read. A failed read carries List's prefix.

func (Service) PrincipalsOf

func (s Service) PrincipalsOf(
	ctx context.Context,
	users, groups []string,
	inherited bool,
) (Principals, error)

PrincipalsOf returns the principals a filtered listing asks about, as Granting gathers them: the named users' stored names and, with inherited, their groups, and the named groups. ErrNotFound names the first user, then the first group, that does not exist, as Granting's does.

func (Service) Put

func (s Service) Put(ctx context.Context, p model.Permission) (bool, error)

Put creates p or replaces the stored permission of that name whole, and reports whether it created it.

func (Service) PutResource

func (s Service) PutResource(
	ctx context.Context,
	name string,
	rt accessapi.ResourceType,
	r model.Resource,
	create bool,
) (bool, error)

PutResource replaces one section of the named permission, creating the permission when create is set and it does not exist, and keeping its other sections. The v1 API writes its repository section this way, so build and release bundle sections set through v2 survive a v1 PUT.

func (Service) Validate

func (s Service) Validate(ctx context.Context, p model.Permission) error

Validate checks a permission against the rules every API shares: a valid name, known resource types, targets that exist and are not virtual, patterns within length, and principals that exist. A user grant's name matches its user in any case; an unknown one is named as the document spells it. The writes fold the user grants after Validate, for that message.

func (Service) Write

func (s Service) Write(
	ctx context.Context,
	name string,
	change func(old model.Permission, exists bool) (model.Permission, error),
) (bool, error)

Write applies change to the named permission in one transaction and reports whether it created it. change gets the stored permission, with exists false when there is none, and returns the permission to store or an error that stores nothing. A caller's checks against the stored permission belong in change: run before the transaction, they could pass on a permission a concurrent write has since replaced.

type Store

type Store interface {
	port.Store
}

Store is what the permission service reads and writes: the access module's stores, of which it uses the permission, user and group stores, and their unit of work (port.Store's Tx). The application passes its composite store, which satisfies it. Store holds no repository store: the repositories a permission names belong to the artifacts module, which the service reaches only through Service.Repositories.

Jump to

Keyboard shortcuts

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