rbac

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 7 Imported by: ? 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 rbac is a default-deny role-based authorization engine. A caller is allowed an action on a resource when the admin flag is set, a product rule allows it, or a binding naming one of the caller's subjects grants a role whose rules cover the action at a scope in the resource's scope chain. A credential scope, when the principal carries one, then narrows every allow.

Kinds, verbs, subjects and scopes are opaque strings to the engine; only the "get" and "list" verbs and the "system:authenticated" subject carry a meaning, documented where they are used. Storage, the vocabulary a product allows in a role, and its ownership shortcuts are the product's: it hands the engine a Source, a ScopeResolver and ordered ProductRules.

Index

Constants

View Source
const (
	// VerbGet is the verb Visible checks: seeing a resource is reading it.
	VerbGet = "get"
	// VerbList on a resource with no owner is admitted by a binding at any scope; the caller filters the rows it returns through Visible or ScopeOf. Callers that cannot filter rows (e.g. a Kubernetes-style authorizer) must set Owner, or one scoped binding admits the whole collection.
	VerbList = "list"
)
View Source
const SubjectAuthenticated = "system:authenticated"

SubjectAuthenticated is the reserved subject every authenticated principal matches, whether or not its Subjects list it.

View Source
const Wildcard = principal.Wildcard

Wildcard in a rule's kinds or verbs matches any kind or verb.

Variables

View Source
var ErrUnfilterable = errors.New("rbac: rule cannot be expressed as a filter")

ErrUnfilterable is returned by ScopeOf when a product rule cannot describe what it allows as a Filter; the caller filters row by row with Visible.

Functions

func EachPermission

func EachPermission(rules []Rule, kinds, verbs []string, fn func(kind, verb string) error) error

EachPermission calls fn for every (kind, verb) the rules cover, in rule order, expanding a wildcard over kinds or verbs as CheckGrant does, and stops at the first error.

func MatchesSubject

func MatchesSubject(p *principal.Principal, subject string) bool

MatchesSubject reports whether p acts under subject.

func SplitAction

func SplitAction(action string) (kind, verb string)

SplitAction cuts "<kind>[.<sub>].<verb>" into kind (everything before the first dot) and verb (everything after the last). An action without a dot is both. The middle segment shares its parent kind's permission; a sub-resource that needs a permission of its own makes it part of the kind ("servers/status.update" → kind "servers/status").

Types

type Binding

type Binding struct {
	RoleID string
	Scope  Scope
	// Origin is an opaque marker of where the binding came from (operator configuration, an applied manifest); the product reads it, the engine never does.
	Origin string
}

Binding grants RoleID at Scope to the subjects it names. Which subjects those are is the Source's index, not a field: the engine only ever asks for the bindings of one subject.

type Decision

type Decision int

Decision is a product rule's answer.

const (
	// Continue leaves the request to the next rule and then the bindings.
	Continue Decision = iota
	// Allow admits the request, subject to the credential scope.
	Allow
	// Deny refuses the request; no later rule or binding is consulted.
	Deny
)

type Engine

type Engine struct {
	Source Source
	Scopes ScopeResolver
	// Rules run in order before the bindings; the first Allow or Deny wins.
	Rules []ProductRule
	// Global is the scope every chain ends in. ScopeOf reads a binding there as reaching every resource.
	Global Scope
}

Engine evaluates decisions. It holds no state of its own, so a product may build one per call or share one.

func (*Engine) Allowed

func (e *Engine) Allowed(ctx context.Context, p *principal.Principal, action string, res authz.Resource) (bool, error)

Allowed is Authorize as a boolean; the error is only a failed lookup.

func (*Engine) Authorize

func (e *Engine) Authorize(ctx context.Context, p *principal.Principal, action string, res authz.Resource) error

Authorize returns nil when p may perform action on res, authz.ErrUnauthenticated or authz.ErrForbidden when not, and any other error when a lookup failed.

func (*Engine) CheckGrant

func (e *Engine) CheckGrant(ctx context.Context, p *principal.Principal, role Role, scope Scope, kinds, verbs []string) error

CheckGrant reports whether p may bind role at scope: binding hands out every permission in the role, so p must already hold each one there. A wildcard in a rule expands over kinds or verbs, the product's full vocabulary; with a nil vocabulary it is checked as itself, so binding a wildcard takes holding one.

func (*Engine) Explain added in v0.3.0

func (e *Engine) Explain(ctx context.Context, p *principal.Principal, action string, res authz.Resource) (Explanation, error)

Explain answers what Authorize would for the same arguments, and why. A denial is an Explanation with Allowed unset, not an error; the error is authz.ErrUnauthenticated or a failed lookup, exactly as Authorize returns them.

func (*Engine) ScopeOf

func (e *Engine) ScopeOf(ctx context.Context, p *principal.Principal, verb, kind string) (Filter, error)

ScopeOf returns the owned resources of kind that p may perform verb on, as a Filter for a store-side query; Visible stays the per-row check, and the two agree on every owned resource as long as each rule's Filter describes its Decide. A resource with no owner (a list call) is outside what ScopeOf describes. Every product rule must be a FilterRule, or ScopeOf returns ErrUnfilterable.

func (*Engine) Visible

func (e *Engine) Visible(ctx context.Context, p *principal.Principal, kind string, res authz.Resource) bool

Visible reports whether p may get res, an action of kind. A failed lookup hides the resource.

type Explanation added in v0.3.0

type Explanation struct {
	Allowed bool
	Reason  Reason
	// Rule is the Name of the product rule that decided, when it has one.
	Rule string
	// Subject, RoleID and Scope are the binding that matched: the first binding the Source returned for Subject with that role and scope.
	Subject string
	RoleID  string
	Scope   Scope
	// Chain is the resource's scope chain, when the decision resolved one: an admin, a product rule and an unowned list decide without it.
	Chain []Scope
	// CredentialDenied is set with ReasonCredential; Rule or the binding fields still name what granted the permission the credential then refused.
	CredentialDenied bool
}

Explanation is Explain's answer: the decision Authorize would make and what made it. Only the first match is reported; allows are additive, so one reason is enough.

type Filter

type Filter struct {
	All        bool
	Scopes     []Scope
	Owners     []authz.Owner
	OwnerKinds []string
	Within     []Scope
}

Filter is ScopeOf's answer: which owned resources of a kind the caller may act on, in a form a store can turn into a query. A resource passes when All is set, its scope chain holds one of Scopes, its owner is one of Owners, or its owner's kind is one of OwnerKinds; and, when Within is not empty, its scope chain also holds one of Within. A scope in Scopes or Within whose kind is the queried kind means "this id": the resource is its own innermost scope.

func (Filter) None

func (f Filter) None() bool

None reports whether no resource passes.

type FilterRule

type FilterRule interface {
	ProductRule
	Filter(ctx context.Context, p *principal.Principal, kind, verb string) (Filter, error)
}

FilterRule is a ProductRule that can also describe, for ScopeOf, every resource of a kind it allows verb on. A rule that may Deny such a resource returns ErrUnfilterable: a Filter only adds.

type NamedRule added in v0.3.0

type NamedRule interface {
	ProductRule
	Name() string
}

NamedRule is a ProductRule that names itself in explanations.

type ProductRule

type ProductRule interface {
	Decide(ctx context.Context, p *principal.Principal, kind, verb string, res authz.Resource) (Decision, error)
}

ProductRule is an ownership-style shortcut a product consults ahead of the bindings. Grants that a role could express belong in a Source instead, so they go through Visible, ScopeOf and the grant check like any binding.

type Reason added in v0.3.0

type Reason string

Reason is what decided an Explanation.

const (
	// ReasonAdmin: the principal's Admin flag passed every rule and binding.
	ReasonAdmin Reason = "admin"
	// ReasonRule: a product rule allowed or denied the request.
	ReasonRule Reason = "rule"
	// ReasonBinding: a binding of one of the principal's subjects granted it.
	ReasonBinding Reason = "binding"
	// ReasonCredential: the principal held the permission, but its credential scope refused it.
	ReasonCredential Reason = "credential"
	// ReasonNone: nothing granted it.
	ReasonNone Reason = "none"
)

type Role

type Role struct {
	ID    string
	Rules []Rule
}

Role is a named rule set. It carries no scope; the binding supplies one.

func (Role) Allows

func (r Role) Allows(kind, verb string) bool

Allows reports whether any rule of the role covers (kind, verb).

type Rule

type Rule = principal.Rule

Rule grants every verb in Verbs on every kind in Kinds.

type RuleFunc

type RuleFunc func(ctx context.Context, p *principal.Principal, kind, verb string, res authz.Resource) (Decision, error)

RuleFunc adapts a function to ProductRule. It has no Filter, so an engine holding one cannot answer ScopeOf.

func (RuleFunc) Decide

func (f RuleFunc) Decide(ctx context.Context, p *principal.Principal, kind, verb string, res authz.Resource) (Decision, error)

Decide calls f.

type Scope

type Scope = principal.Scope

Scope is a place a binding attaches: the global scope, any resource that groups others, or a single resource. It and Rule are declared with the principal because a credential scope is made of both.

type ScopeResolver

type ScopeResolver interface {
	// ChainFor returns the scopes res lives in, most specific first, ending in the engine's global scope. A resource may be its own innermost scope, so a binding can attach to one resource. CheckGrant asks with Resource{Owner: &scope} and no ID, so a resolver must read an owner equal to a scope as living in that scope.
	ChainFor(ctx context.Context, res authz.Resource) ([]Scope, error)
}

ScopeResolver places a resource in its scopes.

type Source

type Source interface {
	// BindingsForSubject returns the bindings naming subject.
	BindingsForSubject(ctx context.Context, subject string) ([]Binding, error)
	// Role returns the role with this id, or false when there is none.
	Role(ctx context.Context, id string) (Role, bool, error)
}

Source is where the engine reads bindings and roles. A lookup error fails the decision with that error; it is never read as a denial. The engine is done with what a method returns before calling that method again, so a Source may reuse its buffers.

Jump to

Keyboard shortcuts

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