policy

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package policy implements runeward's authority engine and cost/loop guardrails. Engine evaluates an Action against an ordered rule list, strictly first-match-wins: there is deliberately no deny-precedence pass, so the operator controls precedence through rule ordering. Guard caps wall-clock time, exec count, and egress while detecting failure loops.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWallClock    = errors.New("policy: wall-clock deadline exceeded")
	ErrMaxExecs     = errors.New("policy: max execs exceeded")
	ErrEgressBudget = errors.New("policy: egress budget exceeded")
	ErrLoopDetected = errors.New("policy: runaway loop detected")
)

Sentinel errors returned by Guard.

Functions

This section is empty.

Types

type Action

type Action struct {
	// Tool is the action surface, e.g. "shell", "file.read", "net".
	Tool string
	// Arg is the command line, path, or hostname the tool acts on.
	Arg string
	// Args is the raw argument vector when the tool is invoked with one (e.g.
	// shell). It powers argv-aware rule matching; nil for tools without argv.
	Args []string
}

Action is a single tool invocation the engine is asked to authorize.

type CELEngine

type CELEngine struct {
	// contains filtered or unexported fields
}

CELEngine is an authority engine backed by CEL rules. Each rule is a boolean expression over the variables `tool` and `arg`; the first rule that evaluates true wins, otherwise the default verdict applies. Safe for concurrent use: compiled programs are immutable and Eval is goroutine-safe.

func NewCEL

func NewCEL(rules []profile.CELRule, def profile.Verdict) (*CELEngine, error)

NewCEL compiles rules into a CELEngine. Each Expr must yield a bool over `tool` and `arg`; compile errors fail fast here rather than silently mis-authorizing at run time. An empty def falls back to allow.

func (*CELEngine) Evaluate

func (e *CELEngine) Evaluate(a Action) Decision

Evaluate renders a Decision for a; the first rule whose expression is true wins. A rule evaluation error fails closed to deny. No match returns the default verdict with a nil Rule.

type Decision

type Decision struct {
	Verdict profile.Verdict
	Reason  string
	// Rule is the rule that produced this decision, nil when the engine fell
	// back to its default verdict.
	Rule *profile.PolicyRule
}

Decision is the outcome of evaluating an Action.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine evaluates actions against an ordered rule set with a default verdict.

func New

func New(rules []profile.PolicyRule, def profile.Verdict) *Engine

New builds an Engine from an ordered rule slice and a default verdict (allow when empty). The rules slice is retained by reference; don't mutate it afterwards.

func (*Engine) Evaluate

func (e *Engine) Evaluate(a Action) Decision

Evaluate renders a Decision for a; first matching rule wins. A rule matches when its Tool equals a.Tool (or is "*") and a.Arg matches its glob (empty Match matches anything). No match returns the default verdict with a nil Rule.

type Evaluator

type Evaluator interface {
	Evaluate(Action) Decision
}

Evaluator renders a Decision for an Action, so the control plane can select an engine per profile without caring which is in use.

type Guard

type Guard struct {
	// contains filtered or unexported fields
}

Guard enforces per-session cost and loop guardrails from profile.Limits. All limits are opt-in: a zero/empty value disables that guard. Safe for concurrent use.

func NewGuard

func NewGuard(limits profile.Limits) (*Guard, error)

NewGuard builds a Guard from limits. It errors if WallClock or LoopWindow is set but not a valid duration string.

func (*Guard) CheckEgress

func (g *Guard) CheckEgress() error

CheckEgress must be called before each outbound request. It returns ErrEgressBudget once the budget is exhausted.

func (*Guard) CheckExec

func (g *Guard) CheckExec() error

CheckExec must be called before each tool invocation. It checks the wall-clock deadline, a tripped loop, and the exec budget in that order, incrementing the exec counter only when the call is permitted.

func (*Guard) LoopTripped

func (g *Guard) LoopTripped() (bool, string)

LoopTripped reports whether a failure loop has been detected and, if so, the key responsible.

func (*Guard) RecordOutcome

func (g *Guard) RecordOutcome(key string, failed bool)

RecordOutcome records the result of an action identified by key. A success clears the key's failure window; failures accumulate in a sliding window, and once a key hits loopThresh failures within loopWindow the detector trips and stays tripped until Reset.

func (*Guard) Reset

func (g *Guard) Reset()

Reset clears counters, failure windows, and tripped state so the Guard can be reused for a fresh session. The parsed limits are retained.

func (*Guard) Start

func (g *Guard) Start()

Start records the session start time for wall-clock enforcement. Only the first call takes effect.

type RegoEngine

type RegoEngine struct {
	// contains filtered or unexported fields
}

RegoEngine is an authority engine backed by an OPA/Rego module, compiled and prepared once at construction so a malformed policy fails fast. The query result may be a bare verdict string or an object {"verdict": ..., "reason": ...}; anything else falls back to the default verdict. Safe for concurrent use.

func NewRego

func NewRego(module, query string, def profile.Verdict) (*RegoEngine, error)

NewRego compiles and prepares module as a RegoEngine. query defaults to "data.runeward.decision" and def to allow. Modules use Rego v1 syntax (`if`/`contains`, no future.keywords import needed).

func (*RegoEngine) Evaluate

func (e *RegoEngine) Evaluate(a Action) (dec Decision)

Evaluate runs the prepared query against {tool, arg}. Evaluation errors fail closed to deny; undefined results and unrecognized verdicts return the default verdict with a nil Rule.

Jump to

Keyboard shortcuts

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