rego

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: Apache-2.0 Imports: 14 Imported by: 636

Documentation

Overview

Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the github.com/open-policy-agent/opa/v1 package instead. See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information.

Package rego exposes high level APIs for evaluating Rego policies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuiltinErrorList added in v0.48.0

func BuiltinErrorList(list *[]topdown.Error) func(r *Rego)

BuiltinErrorList supplies an error slice to store built-in function errors.

func Capabilities added in v0.32.0

func Capabilities(c *ast.Capabilities) func(r *Rego)

Capabilities configures the underlying compiler's capabilities. This option is ignored for module compilation if the caller supplies the compiler.

func Compiler

func Compiler(c *ast.Compiler) func(r *Rego)

Compiler returns an argument that sets the Rego compiler.

func DisableInlining added in v0.13.0

func DisableInlining(paths []string) func(r *Rego)

DisableInlining adds a set of paths to exclude from partial evaluation inlining.

func DistributedTracingOpts added in v0.36.0

func DistributedTracingOpts(tr tracing.Options) func(r *Rego)

DistributedTracingOpts sets the options to be used by distributed tracing.

func Dump added in v0.10.0

func Dump(w io.Writer) func(r *Rego)

Dump returns an argument that sets the writer to dump debugging information to.

func EnablePrintStatements added in v0.34.0

func EnablePrintStatements(yes bool) func(r *Rego)

EnablePrintStatements enables print() calls. If this option is not provided, print() calls will be erased from the policy. This option only applies to queries and policies that passed as raw strings, i.e., this function will not have any affect if the caller supplies the ast.Compiler instance.

func Function1 added in v0.14.0

func Function1(decl *Function, f Builtin1) func(*Rego)

Function1 returns an option that adds a built-in function to the Rego object.

func Function2 added in v0.14.0

func Function2(decl *Function, f Builtin2) func(*Rego)

Function2 returns an option that adds a built-in function to the Rego object.

func Function3 added in v0.14.0

func Function3(decl *Function, f Builtin3) func(*Rego)

Function3 returns an option that adds a built-in function to the Rego object.

func Function4 added in v0.14.0

func Function4(decl *Function, f Builtin4) func(*Rego)

Function4 returns an option that adds a built-in function to the Rego object.

func FunctionDecl added in v0.15.1

func FunctionDecl(decl *Function) func(*Rego)

FunctionDecl returns an option that adds a custom-built-in function __declaration__. NO implementation is provided. This is used for non-interpreter execution envs (e.g., Wasm).

func FunctionDyn added in v0.14.0

func FunctionDyn(decl *Function, f BuiltinDyn) func(*Rego)

FunctionDyn returns an option that adds a built-in function to the Rego object.

func GenerateJSON added in v0.32.0

func GenerateJSON(f func(*ast.Term, *EvalContext) (interface{}, error)) func(r *Rego)

GenerateJSON sets the AST to JSON converter for the results.

func Imports

func Imports(p []string) func(r *Rego)

Imports returns an argument that adds a Rego import to the query's context.

func Input

func Input(x interface{}) func(r *Rego)

Input returns an argument that sets the Rego input document. Input should be a native Go value representing the input document.

func Instrument added in v0.7.0

func Instrument(yes bool) func(r *Rego)

Instrument returns an argument that enables instrumentation for diagnosing performance issues.

func InterQueryBuiltinCache added in v0.23.0

func InterQueryBuiltinCache(c cache.InterQueryCache) func(r *Rego)

InterQueryBuiltinCache sets the inter-query cache that built-in functions can utilize during evaluation.

func InterQueryBuiltinValueCache added in v0.69.0

func InterQueryBuiltinValueCache(c cache.InterQueryValueCache) func(r *Rego)

InterQueryBuiltinValueCache sets the inter-query value cache that built-in functions can utilize during evaluation.

func IsPartialEvaluationNotEffectiveErr added in v0.21.0

func IsPartialEvaluationNotEffectiveErr(err error) bool

IsPartialEvaluationNotEffectiveErr returns true if err is an error returned by this package to indicate that partial evaluation was ineffective.

func Load added in v0.14.0

func Load(paths []string, filter loader.Filter) func(r *Rego)

Load returns an argument that adds a filesystem path to load data and Rego modules from. Any file with a *.rego, *.yaml, or *.json extension will be loaded. The path can be either a directory or file, directories are loaded recursively. The optional ignore string patterns can be used to filter which files are used. The Load option can only be used once. Note: Loading files will require a write transaction on the store.

func LoadBundle added in v0.14.0

func LoadBundle(path string) func(r *Rego)

LoadBundle returns an argument that adds a filesystem path to load a bundle from. The path can be a compressed bundle file or a directory to be loaded as a bundle. Note: Loading bundles will require a write transaction on the store.

func Metrics added in v0.4.9

func Metrics(m metrics.Metrics) func(r *Rego)

Metrics returns an argument that sets the metrics collection.

func Module

func Module(filename, input string) func(r *Rego)

Module returns an argument that adds a Rego module.

func NDBuiltinCache added in v0.44.0

func NDBuiltinCache(c builtins.NDBCache) func(r *Rego)

NDBuiltinCache sets the non-deterministic builtins cache.

func NewHaltError added in v0.31.0

func NewHaltError(err error) error

NewHaltError wraps an error such that the evaluation process will stop when it occurs.

func Package

func Package(p string) func(r *Rego)

Package returns an argument that sets the Rego package on the query's context.

func ParsedBundle added in v0.14.0

func ParsedBundle(name string, b *bundle.Bundle) func(r *Rego)

ParsedBundle returns an argument that adds a bundle to be loaded.

func ParsedImports added in v0.7.0

func ParsedImports(imp []*ast.Import) func(r *Rego)

ParsedImports returns an argument that adds Rego imports to the query's context.

func ParsedInput added in v0.7.0

func ParsedInput(x ast.Value) func(r *Rego)

ParsedInput returns an argument that sets the Rego input document.

func ParsedModule added in v0.13.0

func ParsedModule(module *ast.Module) func(*Rego)

ParsedModule returns an argument that adds a parsed Rego module. If a string module with the same filename name is added, it will override the parsed module.

func ParsedPackage added in v0.7.0

func ParsedPackage(pkg *ast.Package) func(r *Rego)

ParsedPackage returns an argument that sets the Rego package on the query's context.

func ParsedQuery added in v0.7.0

func ParsedQuery(q ast.Body) func(r *Rego)

ParsedQuery returns an argument that sets the Rego query.

func ParsedUnknowns added in v0.9.0

func ParsedUnknowns(unknowns []*ast.Term) func(r *Rego)

ParsedUnknowns returns an argument that sets the values to treat as unknown during partial evaluation.

func PartialNamespace added in v0.7.0

func PartialNamespace(ns string) func(r *Rego)

PartialNamespace returns an argument that sets the namespace to use for partial evaluation results. The namespace must be a valid package path component.

func PrintHook added in v0.34.0

func PrintHook(h print.Hook) func(r *Rego)

PrintHook sets the object to use for handling print statement outputs.

func PrintTrace added in v0.10.2

func PrintTrace(w io.Writer, r *Rego)

PrintTrace is a helper function to write a human-readable version of the trace to the writer w.

func PrintTraceWithLocation added in v0.20.0

func PrintTraceWithLocation(w io.Writer, r *Rego)

PrintTraceWithLocation is a helper function to write a human-readable version of the trace to the writer w.

func Query

func Query(q string) func(r *Rego)

Query returns an argument that sets the Rego query.

func QueryTracer added in v0.22.0

func QueryTracer(t topdown.QueryTracer) func(r *Rego)

QueryTracer returns an argument that adds a query tracer to r.

func RegisterBuiltin1 added in v0.19.0

func RegisterBuiltin1(decl *Function, impl Builtin1)

RegisterBuiltin1 adds a built-in function globally inside the OPA runtime.

func RegisterBuiltin2 added in v0.19.0

func RegisterBuiltin2(decl *Function, impl Builtin2)

RegisterBuiltin2 adds a built-in function globally inside the OPA runtime.

func RegisterBuiltin3 added in v0.19.0

func RegisterBuiltin3(decl *Function, impl Builtin3)

RegisterBuiltin3 adds a built-in function globally inside the OPA runtime.

func RegisterBuiltin4 added in v0.19.0

func RegisterBuiltin4(decl *Function, impl Builtin4)

RegisterBuiltin4 adds a built-in function globally inside the OPA runtime.

func RegisterBuiltinDyn added in v0.19.0

func RegisterBuiltinDyn(decl *Function, impl BuiltinDyn)

RegisterBuiltinDyn adds a built-in function globally inside the OPA runtime.

func RegisterPlugin added in v0.53.0

func RegisterPlugin(name string, p TargetPlugin)

func Resolver added in v0.25.0

func Resolver(ref ast.Ref, r resolver.Resolver) func(r *Rego)

Resolver sets a Resolver for a specified ref path.

func Runtime added in v0.10.0

func Runtime(term *ast.Term) func(r *Rego)

Runtime returns an argument that sets the runtime data to provide to the evaluation engine.

func Schemas added in v0.27.0

func Schemas(x *ast.SchemaSet) func(r *Rego)

Schemas sets the schemaSet

func Seed added in v0.30.0

func Seed(r io.Reader) func(*Rego)

Seed sets a reader that will seed randomization required by built-in functions. If a seed is not provided crypto/rand.Reader is used.

func SetRegoVersion added in v0.60.0

func SetRegoVersion(version ast.RegoVersion) func(r *Rego)

func ShallowInlining added in v0.22.0

func ShallowInlining(yes bool) func(r *Rego)

ShallowInlining prevents rules that depend on unknown values from being inlined. Rules that only depend on known values are inlined.

func SkipBundleVerification added in v0.22.0

func SkipBundleVerification(yes bool) func(r *Rego)

SkipBundleVerification skips verification of a signed bundle.

func SkipPartialNamespace added in v0.20.0

func SkipPartialNamespace(yes bool) func(r *Rego)

SkipPartialNamespace disables namespacing of partial evalution results for support rules generated from policy. Synthetic support rules are still namespaced.

func Store added in v0.5.0

func Store(s storage.Store) func(r *Rego)

Store returns an argument that sets the policy engine's data storage layer.

If using the Load, LoadBundle, or ParsedBundle options then a transaction must also be provided via the Transaction() option. After loading files or bundles the transaction should be aborted or committed.

func StoreReadAST added in v0.70.0

func StoreReadAST(enabled bool) func(r *Rego)

StoreReadAST returns an argument that sets whether the store should eagerly convert data to AST values.

Only applicable when no store has been set on the Rego object through the Store option.

func Strict added in v0.47.0

func Strict(yes bool) func(r *Rego)

Strict enables or disables strict-mode in the compiler

func StrictBuiltinErrors added in v0.25.0

func StrictBuiltinErrors(yes bool) func(r *Rego)

StrictBuiltinErrors tells the evaluator to treat all built-in function errors as fatal errors.

func Target added in v0.27.0

func Target(t string) func(r *Rego)

Target sets the runtime to exercise.

func Time added in v0.23.0

func Time(x time.Time) func(r *Rego)

Time sets the wall clock time to use during policy evaluation. Prepared queries do not inherit this parameter. Use EvalTime to set the wall clock time when executing a prepared query.

func Trace added in v0.10.2

func Trace(yes bool) func(r *Rego)

Trace returns an argument that enables tracing on r.

func Tracer added in v0.5.3

func Tracer(t topdown.Tracer) func(r *Rego)

Tracer returns an argument that adds a query tracer to r. Deprecated: Use QueryTracer instead.

func Transaction added in v0.5.2

func Transaction(txn storage.Transaction) func(r *Rego)

Transaction returns an argument that sets the transaction to use for storage layer operations.

Requires the store associated with the transaction to be provided via the Store() option. If using Load(), LoadBundle(), or ParsedBundle() options the transaction will likely require write params.

func Unknowns added in v0.7.0

func Unknowns(unknowns []string) func(r *Rego)

Unknowns returns an argument that sets the values to treat as unknown during partial evaluation.

func UnsafeBuiltins added in v0.13.0

func UnsafeBuiltins(unsafeBuiltins map[string]struct{}) func(r *Rego)

UnsafeBuiltins sets the built-in functions to treat as unsafe and not allow. This option is ignored for module compilation if the caller supplies the compiler. This option is always honored for query compilation. Provide an empty (non-nil) map to disable checks on queries.

Types

type Builtin1 added in v0.14.0

type Builtin1 = v1.Builtin1

Builtin1 defines a built-in function that accepts 1 argument.

type Builtin2 added in v0.14.0

type Builtin2 = v1.Builtin2

Builtin2 defines a built-in function that accepts 2 arguments.

type Builtin3 added in v0.14.0

type Builtin3 = v1.Builtin3

Builtin3 defines a built-in function that accepts 3 argument.

type Builtin4 added in v0.14.0

type Builtin4 = v1.Builtin4

Builtin4 defines a built-in function that accepts 4 argument.

type BuiltinContext added in v0.14.0

type BuiltinContext = v1.BuiltinContext

BuiltinContext contains additional attributes from the evaluator that built-in functions can use, e.g., the request context.Context, caches, etc.

type BuiltinDyn added in v0.14.0

type BuiltinDyn = v1.BuiltinDyn

BuiltinDyn defines a built-in function that accepts a list of arguments.

type CompileContext added in v0.11.0

type CompileContext = v1.CompileContext

CompileContext contains options for Compile calls.

type CompileOption added in v0.11.0

type CompileOption = v1.CompileOption

CompileOption defines a function to set options on Compile calls.

func CompilePartial added in v0.11.0

func CompilePartial(yes bool) CompileOption

CompilePartial defines an option to control whether partial evaluation is run before the query is planned and compiled.

type CompileResult added in v0.10.0

type CompileResult = v1.CompileResult

CompileResult represents the result of compiling a Rego query, zero or more Rego modules, and arbitrary contextual data into an executable.

type ErrorDetails added in v0.32.0

type ErrorDetails = v1.ErrorDetails

ErrorDetails interface is satisfied by an error that provides further details.

type Errors

type Errors = v1.Errors

Errors represents a collection of errors returned when evaluating Rego.

type EvalContext added in v0.11.0

type EvalContext = v1.EvalContext

EvalContext defines the set of options allowed to be set at evaluation time. Any other options will need to be set on a new Rego object.

type EvalOption added in v0.11.0

type EvalOption = v1.EvalOption

EvalOption defines a function to set an option on an EvalConfig

func EvalCopyMaps added in v0.47.0

func EvalCopyMaps(yes bool) EvalOption

EvalCopyMaps causes the evaluator to copy `map[string]interface{}`s before returning them.

func EvalDisableInlining added in v0.13.0

func EvalDisableInlining(paths []ast.Ref) EvalOption

EvalDisableInlining returns an argument that adds a set of paths to exclude from partial evaluation inlining.

func EvalEarlyExit added in v0.35.0

func EvalEarlyExit(enabled bool) EvalOption

EvalEarlyExit will disable 'early exit' optimizations for the evaluation. This should only be used when tracing in debug mode.

func EvalInput added in v0.11.0

func EvalInput(input interface{}) EvalOption

EvalInput configures the input for a Prepared Query's evaluation

func EvalInstrument added in v0.11.0

func EvalInstrument(instrument bool) EvalOption

EvalInstrument enables or disables instrumenting for a Prepared Query's evaluation

func EvalInterQueryBuiltinCache added in v0.23.0

func EvalInterQueryBuiltinCache(c cache.InterQueryCache) EvalOption

EvalInterQueryBuiltinCache sets the inter-query cache that built-in functions can utilize during evaluation.

func EvalInterQueryBuiltinValueCache added in v0.69.0

func EvalInterQueryBuiltinValueCache(c cache.InterQueryValueCache) EvalOption

EvalInterQueryBuiltinValueCache sets the inter-query value cache that built-in functions can utilize during evaluation.

func EvalMetrics added in v0.11.0

func EvalMetrics(metric metrics.Metrics) EvalOption

EvalMetrics configures the metrics for a Prepared Query's evaluation

func EvalNDBuiltinCache added in v0.44.0

func EvalNDBuiltinCache(c builtins.NDBCache) EvalOption

EvalNDBuiltinCache sets the non-deterministic builtin cache that built-in functions can use during evaluation.

func EvalParsedInput added in v0.11.0

func EvalParsedInput(input ast.Value) EvalOption

EvalParsedInput configures the input for a Prepared Query's evaluation

func EvalParsedUnknowns added in v0.11.0

func EvalParsedUnknowns(unknowns []*ast.Term) EvalOption

EvalParsedUnknowns returns an argument that sets the values to treat as unknown during partial evaluation.

func EvalPartialNamespace added in v0.11.0

func EvalPartialNamespace(ns string) EvalOption

EvalPartialNamespace returns an argument that sets the namespace to use for partial evaluation results. The namespace must be a valid package path component.

func EvalPrintHook added in v0.35.0

func EvalPrintHook(ph print.Hook) EvalOption

EvalPrintHook sets the object to use for handling print statement outputs.

func EvalQueryTracer added in v0.22.0

func EvalQueryTracer(tracer topdown.QueryTracer) EvalOption

EvalQueryTracer configures a tracer for a Prepared Query's evaluation

func EvalResolver added in v0.25.0

func EvalResolver(ref ast.Ref, r resolver.Resolver) EvalOption

EvalResolver sets a Resolver for a specified ref path for this evaluation.

func EvalRuleIndexing added in v0.15.0

func EvalRuleIndexing(enabled bool) EvalOption

EvalRuleIndexing will disable indexing optimizations for the evaluation. This should only be used when tracing in debug mode.

func EvalSeed added in v0.30.0

func EvalSeed(r io.Reader) EvalOption

EvalSeed sets a reader that will seed randomization required by built-in functions. If a seed is not provided crypto/rand.Reader is used.

func EvalSortSets added in v0.27.1

func EvalSortSets(yes bool) EvalOption

EvalSortSets causes the evaluator to sort sets before returning them as JSON arrays.

func EvalTime added in v0.23.0

func EvalTime(x time.Time) EvalOption

EvalTime sets the wall clock time to use during policy evaluation. time.now_ns() calls will return this value.

func EvalTracer added in v0.11.0

func EvalTracer(tracer topdown.Tracer) EvalOption

EvalTracer configures a tracer for a Prepared Query's evaluation Deprecated: Use EvalQueryTracer instead.

func EvalTransaction added in v0.11.0

func EvalTransaction(txn storage.Transaction) EvalOption

EvalTransaction configures the Transaction for a Prepared Query's evaluation

func EvalUnknowns added in v0.11.0

func EvalUnknowns(unknowns []string) EvalOption

EvalUnknowns returns an argument that sets the values to treat as unknown during partial evaluation.

func EvalVirtualCache added in v0.68.0

func EvalVirtualCache(vc topdown.VirtualCache) EvalOption

EvalVirtualCache sets the topdown.VirtualCache to use for evaluation. This is optional, and if not set, the default cache is used.

type ExpressionValue

type ExpressionValue = v1.ExpressionValue

ExpressionValue defines the value of an expression in a Rego query.

type Function added in v0.14.0

type Function = v1.Function

Function represents a built-in function that is callable in Rego.

type HaltError added in v0.31.0

type HaltError = v1.HaltError

HaltError is an error type to return from a custom function implementation that will abort the evaluation process (analogous to topdown.Halt).

type Location

type Location = v1.Location

Location defines a position in a Rego query or module.

type PartialQueries added in v0.9.0

type PartialQueries = v1.PartialQueries

PartialQueries contains the queries and support modules produced by partial evaluation.

type PartialResult added in v0.7.0

type PartialResult = v1.PartialResult

PartialResult represents the result of partial evaluation. The result can be used to generate a new query that can be run when inputs are known.

type PrepareConfig added in v0.11.0

type PrepareConfig = v1.PrepareConfig

PrepareConfig holds settings to control the behavior of the Prepare call.

type PrepareOption added in v0.11.0

type PrepareOption = v1.PrepareOption

PrepareOption defines a function to set an option to control the behavior of the Prepare call.

func WithBuiltinFuncs added in v0.56.0

func WithBuiltinFuncs(bis map[string]*topdown.Builtin) PrepareOption

WithBuiltinFuncs carries the rego.Function{1,2,3} per-query function definitions to the target plugins.

func WithNoInline added in v0.13.0

func WithNoInline(paths []string) PrepareOption

WithNoInline adds a set of paths to exclude from partial evaluation inlining.

func WithPartialEval added in v0.11.0

func WithPartialEval() PrepareOption

WithPartialEval configures an option for PrepareForEval which will have it perform partial evaluation while preparing the query (similar to rego.Rego#PartialResult)

type PreparedEvalQuery added in v0.11.0

type PreparedEvalQuery = v1.PreparedEvalQuery

PreparedEvalQuery holds the prepared Rego state that has been pre-processed for subsequent evaluations.

type PreparedPartialQuery added in v0.11.0

type PreparedPartialQuery = v1.PreparedPartialQuery

PreparedPartialQuery holds the prepared Rego state that has been pre-processed for partial evaluations.

type Rego

type Rego = v1.Rego

Rego constructs a query and can be evaluated to obtain results.

func New

func New(options ...func(r *Rego)) *Rego

New returns a new Rego object.

type Result

type Result = v1.Result

Result defines the output of Rego evaluation.

type ResultSet

type ResultSet = v1.ResultSet

ResultSet represents a collection of output from Rego evaluation. An empty result set represents an undefined query.

type TargetPlugin added in v0.53.0

type TargetPlugin = v1.TargetPlugin

type TargetPluginEval added in v0.53.0

type TargetPluginEval = v1.TargetPluginEval

type Vars

type Vars = v1.Vars

Vars represents a collection of variable bindings. The keys are the variable names and the values are the binding values.

Jump to

Keyboard shortcuts

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