enforce

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 11 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 enforce is the whole of borg's decision procedure (docs/spec.md section 13) behind one call, for a service that decides on a requester's behalf: the organization's latest token and the subject's grants, groups and policy records, each verified against the service's own headers, folded into a verdict, signed into a receipt and handed back. A service adds authorization in one file and keeps its own interface in front of this one, so the dependency on borg lives here and nowhere else in it.

It fails closed. Any read it cannot complete is a deny with a reason, and an allow is reused only within the freshness the service states, which the receipt makes visible by naming the seq it rested on.

Index

Constants

View Source
const DefaultDenyFor = 10 * time.Second

DefaultDenyFor is how long a Decider reuses a deny when it is told no bound of its own.

View Source
const DefaultFreshness = 60 * time.Second

DefaultFreshness is how long an allow is reused for one subject and one permission at one seq when the service states no bound of its own.

View Source
const MaxCached = 10000

MaxCached bounds the allows an Enforcer keeps; past it the cache is cleared, which costs a read and never an answer.

Variables

View Source
var ErrNotReady = errors.New("enforce: a reader, a spend source and a wallet are required")

ErrNotReady is a nil reader, spend source or wallet: the Enforcer cannot decide, so it denies.

View Source
var ErrStale = errors.New("enforce: the organization could not be read; its last allow stands")

ErrStale marks an allow kept through a failed read: the organization could not be read, and the subject was allowed when it last was, within StaleFor.

Functions

This section is empty.

Types

type Decider added in v0.9.0

type Decider struct {
	// Reader asks the hosts and verifies what they answer against the
	// service's own headers. Give it a Floor (reader.MemFloor at least), so
	// a host set that answers an older organization token than one already
	// verified is refused; without one a Decider still never goes back to
	// an earlier seq itself, but a restart, or a second Decider, can.
	Reader *reader.Client
	// Spends says whether an outpoint is spent. Without one nothing is
	// current and every call denies.
	Spends reader.Spends
	// AllowFor is how long an allow is reused, and how often the
	// organization's latest token is read again while every subject asked
	// about is allowed; zero is DefaultFreshness. A revoked grant is
	// answered at most this long past the hosts holding the token that
	// revoked it.
	AllowFor time.Duration
	// DenyFor is how long a deny, or a failed read of an organization, is
	// reused, and how often the latest token is read again while a subject
	// is denied: so a subject just granted waits this long once the hosts
	// hold the token. Zero is DefaultDenyFor; it is held to AllowFor.
	DenyFor time.Duration
	// StaleFor is how long an allow stands through failed reads, from the
	// read that allowed it, and only while no later token is verified.
	// Zero keeps none: a failed read denies.
	StaleFor time.Duration
	// Now is the clock; nil is time.Now.
	Now func() time.Time
	// contains filtered or unexported fields
}

Decider is the decision procedure of spec section 13 for a service that decides for itself, keeps no receipt and answers for any organization a caller names: a renderer that admits writers by a permission, say. It is Enforcer without the wallet and the receipt, and with what a service that asks often and from many places needs: denies reused too, a failed read remembered, and an allow kept through an outage for a bound the service states. Make one and keep it; it is safe for concurrent use and holds no lock over a read.

It fails closed. A read it cannot complete is an error, and the answer is a deny, except an allow read before the failure and within StaleFor, which is answered true with an error that wraps ErrStale.

func (*Decider) Allows added in v0.9.0

func (d *Decider) Allows(ctx context.Context, hall string, org, subject []byte, permission string) (bool, error)

Allows decides whether subject holds permission in the organization org of hall now. The permission is asked in full, with no wildcard.

func (*Decider) Forget added in v0.9.0

func (d *Decider) Forget()

Forget drops every reused answer, view and failure.

type Enforcer

type Enforcer struct {
	// Reader asks the hosts and verifies what they answer. Its Headers are
	// the service's own, which is what makes a decision the service's.
	Reader *reader.Client
	// Spends says whether an outpoint is spent: a node or an index. Without
	// one nothing is current, and every decision denies (spec section 13).
	Spends reader.Spends
	// Wallet signs receipts; its identity key is the service's, and is the
	// key a checker verifies the receipt under. Originator is the wallet's
	// originator for the service.
	Wallet     wallet.Interface
	Originator string
	// Hall and Org name the organization.
	Hall string
	Org  []byte
	// Freshness bounds how long an allow is reused; zero is
	// DefaultFreshness. A deny is never reused: a member just granted is
	// read afresh.
	Freshness time.Duration
	// Now is the clock; nil is time.Now.
	Now func() time.Time
	// contains filtered or unexported fields
}

Enforcer decides for one organization. Make one per organization a service answers for and keep it: it holds the organization's latest view and the allows it may reuse.

func (*Enforcer) Allow

func (e *Enforcer) Allow(ctx context.Context, subject []byte, permission string) (Verdict, error)

Allow decides whether subject holds permission in the organization now. The permission is what a service asks, in full and with no wildcard. The verdict is a deny with a reason whenever the read could not be completed, and the error says why; a verdict with a receipt is one a checker can verify, whichever way it went.

func (*Enforcer) Forget

func (e *Enforcer) Forget()

Forget drops every reused allow, for a service that learns of a change before the freshness would.

type Verdict

type Verdict struct {
	Allow    bool
	Reason   string
	Decision reader.Decision
	Receipt  *authz.Signed
	Cached   bool
}

Verdict is one decision: the reader's, the signed receipt a checker can verify alone, and whether it was reused from the cache.

Jump to

Keyboard shortcuts

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