policy

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decision

type Decision struct {
	Effect       Effect `json:"effect"`
	Policy       string `json:"policy,omitempty"`
	Rule         string `json:"rule,omitempty"`
	Message      string `json:"message,omitempty"`
	Matched      bool   `json:"matched"`
	Evaluated    int    `json:"evaluated_rules"`
	Error        error  `json:"error"`
	ErrorMessage string `json:"error_message,omitempty"`
}

Decision captures the result of evaluating a policy set.

type Document

type Document struct {
	Version       string   `json:"version,omitempty" yaml:"version,omitempty"`
	DefaultEffect *Effect  `json:"default_effect,omitempty" yaml:"default_effect,omitempty"`
	Policies      []Policy `json:"policies" yaml:"policies"`
}

Document describes a collection of policies that can be serialized as JSON or YAML.

func LoadJSONDocument

func LoadJSONDocument(path string) (Document, error)

LoadJSONDocument reads a JSON document from disk.

func ParseJSONDocument

func ParseJSONDocument(r io.Reader) (Document, error)

ParseJSONDocument decodes a policy document from JSON.

type Effect

type Effect string

Effect represents the outcome of a rule evaluation.

const (
	// EffectAllow grants the action.
	EffectAllow Effect = "ALLOW"
	// EffectDeny blocks the action.
	EffectDeny Effect = "DENY"
)

func (Effect) IsDeny added in v0.0.2

func (e Effect) IsDeny() bool

IsDeny returns true for the built-in deny effect. Deny effects short-circuit evaluation (first deny wins).

type Engine

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

Engine evaluates compiled policies against an input context.

func CompileDocument

func CompileDocument(doc Document, opts ...EngineOption) (*Engine, error)

CompileDocument converts a policy document into an executable engine.

func CompilePolicies

func CompilePolicies(policies []Policy, opts ...EngineOption) (*Engine, error)

CompilePolicies is a convenience helper when you already materialised policies.

func (*Engine) Evaluate

func (e *Engine) Evaluate(_ context.Context, input any) Decision

Evaluate runs all compiled policies against the provided context value. The final decision honours DENY over ALLOW, with a configurable default fallback.

When WithEffectPrecedence is configured, all matching rules are evaluated and the decision with the highest-priority effect wins. Otherwise, DENY short-circuits and the first non-deny match wins.

type EngineOption

type EngineOption func(*engineConfig)

EngineOption configures compilation behaviour.

func WithDefaultEffect

func WithDefaultEffect(effect Effect) EngineOption

WithDefaultEffect defines the fallback effect used when no rule matches.

func WithEffectPrecedence added in v0.0.3

func WithEffectPrecedence(effects ...Effect) EngineOption

WithEffectPrecedence defines the priority order of effects for conflict resolution. Effects listed first have higher priority. When multiple rules match, the effect with the highest priority wins regardless of rule or policy ordering. Any matched effect not in the list is treated as lowest priority (first-match-wins among them).

Example for custody: WithEffectPrecedence(EffectDeny, EffectReview, EffectAllow) This means DENY beats REVIEW beats ALLOW, regardless of rule order.

func WithExprOptions

func WithExprOptions(opts ...expr.Option) EngineOption

WithExprOptions passes expr compilation options for every rule.

func WithSchemaDefinition

func WithSchemaDefinition(schema any) EngineOption

WithSchemaDefinition defines the expected data structure for type validation at compile time. Pass an empty struct to define which fields exist and their types. Unknown fields or type mismatches will be caught during policy compilation. Example: policy.WithSchemaDefinition(TransactionContext{})

type Policy

type Policy struct {
	Name          string   `json:"name" yaml:"name"`
	Description   string   `json:"description,omitempty" yaml:"description,omitempty"`
	DefaultEffect *Effect  `json:"default_effect,omitempty" yaml:"default_effect,omitempty"`
	Rules         []Rule   `json:"rules" yaml:"rules"`
	Tags          []string `json:"tags,omitempty" yaml:"tags,omitempty"`
}

Policy groups a list of rules under a logical name.

type Rule

type Rule struct {
	ID          string            `json:"id,omitempty" yaml:"id,omitempty"`
	Description string            `json:"description,omitempty" yaml:"description,omitempty"`
	Effect      Effect            `json:"effect" yaml:"effect"`
	Condition   string            `json:"condition" yaml:"condition"`
	Metadata    map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
}

Rule contains a single expression condition paired with an outcome.

Jump to

Keyboard shortcuts

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