preview

package
v1.46.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicOpt

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

BasicOpt is a preview option that holds multiple boolean-type simple options together so each does not need a separate option struct.

func NewBasicOpt

func NewBasicOpt(strictCompile, metrics, instrumentation, strictBuiltinErrors bool) *BasicOpt

NewBasicOpt creates a new BasicOpt struct holding several simple boolean preview options.

func (*BasicOpt) EvaluationOptions

func (b *BasicOpt) EvaluationOptions() []rego.EvalOption

EvaluationOptions set up the instrumentation setting for the evaluation.

func (*BasicOpt) Init

func (b *BasicOpt) Init(preview *Preview) error

Init registers the BasicOpt struct with the preview struct as a compiler preparer, rego option provider, evaluation option provider, and a result provider.

func (*BasicOpt) PrepareCompiler

func (b *BasicOpt) PrepareCompiler(compiler *ast.Compiler) error

PrepareCompiler sets the compiler to run in strict mode if the strict option is set.

func (*BasicOpt) RegoOptions

func (b *BasicOpt) RegoOptions() []func(*rego.Rego)

RegoOptions configures the instrumentation setting and sets the strict builtin errors settings for the Rego evaluation.

func (*BasicOpt) Result

func (b *BasicOpt) Result(preview *Preview, _ rego.ResultSet, response types.PreviewResponseV1) (types.PreviewResponseV1, error)

Result checks the metrics and instrumentation setting and if true adds the metrics information to the preview response.

type CompilerPreparer

type CompilerPreparer interface {
	PrepareCompiler(*ast.Compiler) error
}

CompilerPreparer is an interface representing a mutation on the Compiler instance, allowing options to modify its setup.

type EnvironmentOpt

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

EnvironmentOpts is a preview option which sets up the overall environment in which the preview request is run. This includes ensuring any user provided modules and data as well as already existing modules and data are available when running preview.

func NewEnvironmentOpt

func NewEnvironmentOpt(sandbox bool, manager *plugins.Manager, extraPolicies map[string]string, extraData []byte) *EnvironmentOpt

NewEnvironmentOpt creates a new EnvironmentOpt struct to set up the preview environment

func (*EnvironmentOpt) Init

func (e *EnvironmentOpt) Init(preview *Preview) error

Init decodes the raw data if present for use in the PreviewStorage struct before registering the EnvironmentOpt as a StoragePreparer and CompilerPreparer.

func (*EnvironmentOpt) PrepareCompiler

func (e *EnvironmentOpt) PrepareCompiler(compiler *ast.Compiler) error

PrepareCompiler side loads existing policies into the preview compiler. If sandbox mode is specified, this is skipped.

func (*EnvironmentOpt) PrepareStorage

func (e *EnvironmentOpt) PrepareStorage(store *PreviewStorage) error

PrepareStorage adds any user provided policies and data to the PreviewStorage. If not in sandbox mode, it also adds the primary store so that existing data is available.

func (*EnvironmentOpt) RegoOptions

func (e *EnvironmentOpt) RegoOptions() []func(*rego.Rego)

RegoOptions adds any preview policies sent into the rego object, which will compile them when the query is prepared.

type EvaluationOptionProvider

type EvaluationOptionProvider interface {
	EvaluationOptions() []rego.EvalOption
}

EvaluationOptionProvider is an interface allowing preview options to define any evaluation options to send when calling `Eval()` on the Rego instance.

type NDBuiltinCacheOpt

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

NDBuiltinCacheOpt injects any values sent to the preview API from provided input, allowing various builtin to return predefined results.

func NewNDBuiltinCacheOpt

func NewNDBuiltinCacheOpt(data map[string]map[string]any) *NDBuiltinCacheOpt

NewNDBuiltinCacheOpt creates a new NDBuiltinCacheOpt holding the provided cache values for use in the preview evaluation.

func (*NDBuiltinCacheOpt) EvaluationOptions

func (n *NDBuiltinCacheOpt) EvaluationOptions() []rego.EvalOption

EvaluationOptions sets the Rego evaluator to use the supplied data as the NDBuiltinCache for the Rego evaluation.

func (*NDBuiltinCacheOpt) Init

func (n *NDBuiltinCacheOpt) Init(preview *Preview) error

Init parses and sets up the actual NDBCache struct if valid cache data was provided. It then registers the NDBuiltinCacheOpt as an evaluation option provider.

type PostEvaluationHook

type PostEvaluationHook func()

PostEvaluation is a function which will run as a callback immediately following the Rego evaluation, but before processing the final results.

type Preview

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

Preview is a struct which controls the lifecycle of a preview request. It is primarily responsible for managing the request lifecycle, relying on preview options to control the preview evaluation behavior.

func NewPreview

func NewPreview(ctx context.Context) *Preview

NewPreview creates a new Preview struct with the provided context and a new preview store and compiler.

func (*Preview) Context

func (p *Preview) Context() context.Context

Context returns the currently stored preview context.

func (*Preview) Eval

func (p *Preview) Eval() (types.PreviewResponseV1, error)

Eval runs the preview evaluation. It does this by running through the Preview lifecycle, initializing all registered options, then setting up the preview storage and compiler, generating a Rego struct, and then running an Eval on it. When complete, it compiles the final preview response using the registered results providers.

func (*Preview) Metrics

func (p *Preview) Metrics() metrics.Metrics

Metrics returns the stored metrics.Metrics.

func (*Preview) Store

func (p *Preview) Store() *PreviewStorage

Store returns the stored PreviewStorage struct.

func (*Preview) Transaction

func (p *Preview) Transaction() storage.Transaction

Transaction returns the stored storage.Transaction.

func (*Preview) WithCompilerPreparer

func (p *Preview) WithCompilerPreparer(preparer CompilerPreparer) *Preview

WithCompilerPreparer registers a struct matching the CompilerPreparer interface allowing it to modify the preview compiler prior to evaluating the preview request. The preview struct is returned for chaining.

func (*Preview) WithEvaluationOptionProvider

func (p *Preview) WithEvaluationOptionProvider(provider EvaluationOptionProvider) *Preview

WithEvaluationOptionProvider registers a struct matching the EvaluationOptionProvider interface allowing it to supply additional options passed when calling `Eval()` on the Rego instance. The preview struct is returned for chaining.

func (*Preview) WithInput

func (p *Preview) WithInput(input ast.Value) *Preview

WithInput overrides the stored input value, returning the preview struct for chaining.

func (*Preview) WithMetrics

func (p *Preview) WithMetrics(m metrics.Metrics) *Preview

WithMetrics overrides the stored metrics.Metrics, returning the preview struct for chaining.

func (*Preview) WithOption

func (p *Preview) WithOption(option PreviewOption) *Preview

WithOption registers a new PreviewOption so it can modify the behavior of the preview eval during the lifecycle. It returns the preview struct for chaining.

func (*Preview) WithOptions

func (p *Preview) WithOptions(options ...PreviewOption) *Preview

WithOptions takes a variable number of PreviewOptions registering each. The preview struct is returned for chaining.

func (*Preview) WithPostEvalHook

func (p *Preview) WithPostEvalHook(hook PostEvaluationHook) *Preview

WithPostEvalHook registers a function matching PostEvaluation function type allowing it to run code after the Rego evaluation is complete but before creating the final response.

func (*Preview) WithRegoOptionProvider

func (p *Preview) WithRegoOptionProvider(provider RegoOptionProvider) *Preview

WithRegoOptionProvider registers a struct matching the RegoOptionProvider interface allowing it to supply additional options passed when calling `rego.New()`. The preview struct is returned for chaining.

func (*Preview) WithResultProvider

func (p *Preview) WithResultProvider(provider ResultProvider) *Preview

WithResultProvider registers a struct matching the WithResultProvider interface allowing it to modify the final response based on the result of either internal state or the Rego eval result.

func (*Preview) WithStoragePreparer

func (p *Preview) WithStoragePreparer(preparer StoragePreparer) *Preview

WithStoragePreparer registers a struct matching the StoragePreparer interface allowing it to modify the PreviewStore prior to evaluating the preview request. The preview struct is returned for chaining.

type PreviewConfig

type PreviewConfig struct {
	Enabled bool `json:"enabled"`
}

PreviewConfig is the shape of the configuration object the plugin expects

type PreviewHook

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

PreviewHook holds the state of the preview endpoint

func NewHook

func NewHook() *PreviewHook

func (*PreviewHook) Init

func (p *PreviewHook) Init(m *plugins.Manager)

func (*PreviewHook) OnConfig

func (p *PreviewHook) OnConfig(ctx context.Context, conf *config.Config) (*config.Config, error)

func (*PreviewHook) OnConfigDiscovery

func (p *PreviewHook) OnConfigDiscovery(ctx context.Context, conf *config.Config) (*config.Config, error)

func (*PreviewHook) ServeHTTP

func (p *PreviewHook) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP exposes the ability to run preview requests. The API is based primarily off of OPAs v1DataPost method mixing in parts from public OPA control-plane APIs.

type PreviewOption

type PreviewOption interface {
	Init(*Preview) error
}

PreviewOption is the primary interface supported by the Preview struct, defining the main Init method which is passed the Preview struct so that the struct can register itself with other preview lifecycle hooks.

type PreviewStorage

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

PreviewStorage sits over top of two possible store.Store instances: a primary store and a preview store. Preview Storage is compatible with the store.Store interface. If data is available in the preview store, that data is preferred. If the data is not present, data from the primary store is returned instead. If either store is nil, they are skipped.

The PreviewStorage struct also supplies extra policies so they are compiled prior to the query and available for the preview request.

func NewPreviewStorage

func NewPreviewStorage() *PreviewStorage

NewPreviewStorage create an empty PreviewStorage struct.

func (*PreviewStorage) Abort

func (s *PreviewStorage) Abort(ctx context.Context, txn storage.Transaction)

Abort will abort the transactions on both the preview and primary stores.

func (*PreviewStorage) Commit

func (s *PreviewStorage) Commit(ctx context.Context, txn storage.Transaction) error

Commit will call Abort to close the storage transaction. This ensures the transactions are closed and no mutation is taking place from the preview store.

func (*PreviewStorage) DeletePolicy

DeletePolicy is unsupported in the preview store and always returns an error.

func (*PreviewStorage) GetPolicy

GetPolicy is unsupported in the preview store and always returns an error.

func (*PreviewStorage) ListPolicies

ListPolicies is unsupported in the preview store and returns nil (no policies available)

func (*PreviewStorage) NewTransaction

func (s *PreviewStorage) NewTransaction(ctx context.Context, params ...storage.TransactionParams) (storage.Transaction, error)

NewTransaction generates a new PreviewTransaction, which abstracts over transactions in both the primary and preview stores when defined.

func (*PreviewStorage) Read

func (s *PreviewStorage) Read(ctx context.Context, txn storage.Transaction, path storage.Path) (interface{}, error)

Read will first attempt to read data from the preview store, if defined, and if it is not defined or the value is not present, the primary store is used.

func (*PreviewStorage) Register

Register is unsupported in the preview store and always returns and error

func (*PreviewStorage) Truncate

Truncate will proxy a truncate call to the primary store if defined, otherwise it is a noop.

func (*PreviewStorage) UpsertPolicy

UpsertPolicy is unsupported in the preview store and always returns an error.

func (*PreviewStorage) WithPreviewData

func (s *PreviewStorage) WithPreviewData(previewData json.Json) *PreviewStorage

WithPreviewData takes arbitrary JSON data and creates a new store.Store from the JSON object. This is used as the primary data source when data is requested.

func (*PreviewStorage) WithPrimaryStorage

func (s *PreviewStorage) WithPrimaryStorage(primaryStorage storage.Store) *PreviewStorage

WithPrimaryStore set the store.Store instance to use as the fallback store when data is not present in the Preview store. Once added, this store is read only.

func (*PreviewStorage) Write

Write is not supported by the preview store and always returns an error.

type PreviewTransaction

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

PreviewTransaction abstracts over two potential transactions within a PreviewStore

func (*PreviewTransaction) Get

func (t *PreviewTransaction) Get(ctx context.Context, key interface{}) (interface{}, bool, error)

Get will find the value at the provided key, first iterating the preview store transaction, falling back to the primary store transaction in the event the key is not available from the preview transaction.

func (*PreviewTransaction) ID

func (t *PreviewTransaction) ID() uint64

ID returns the transaction ID assigned to the PreviewTransaction

func (*PreviewTransaction) Iter

func (t *PreviewTransaction) Iter(ctx context.Context, f func(key, value any) (bool, error)) error

Iter will iterate over all elements of both the preview and primary transactions when defined, calling `f` for each element. If true is returned from the callback iteration stops

This method does not deduplicate when a key is declared in both transactions.

Preview data is iterated before primary data

type PrintOpt

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

PrintOpt is a preview option which collects and adds any Rego print() statements from the preview query to the response.

func NewPrintOpt

func NewPrintOpt(doPrint bool) *PrintOpt

NewPrintOpt creates a new PrintOpt struct ready for use in a preview request

func (*PrintOpt) Init

func (p *PrintOpt) Init(preview *Preview) error

Init is a noop if doPrint if off, otherwise it sets up a string builder for collecting printed output and registers itself as a preview compiler preparer, rego options provider, and result provider.

func (*PrintOpt) PrepareCompiler

func (p *PrintOpt) PrepareCompiler(compiler *ast.Compiler) error

PrepareCompiler set the compiler to enable print statement when the Rego is executed.

func (PrintOpt) Print

func (p PrintOpt) Print(_ print.Context, msg string) error

Print matches the Rego PrintHook interface, and collects any printed strings into the strings builder.

func (*PrintOpt) RegoOptions

func (p *PrintOpt) RegoOptions() []func(*rego.Rego)

RegoOptions sets up the PrintOpt as a print hook in the Rego struct.

func (*PrintOpt) Result

Result adds any printed string to the preview response.

type ProvenanceOpt

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

ProvenanceOpt is a preview option which will add provenance data to the response

func NewProvenanceOpt

func NewProvenanceOpt(addProvenance bool) *ProvenanceOpt

NewProvenanceOpt creates a new ProvenanceOpt for adding provenance data to a preview query.

func (*ProvenanceOpt) Init

func (p *ProvenanceOpt) Init(preview *Preview) error

Init ensures addProvenance is true, and if so registers the ProvenanceOpt with the preview struct as a result provider.

func (*ProvenanceOpt) Result

Result adds provenance data to the preview response. This gathers revisions from the store. This is done at this stage to ensure the environment and transaction are fully prepared prior to attempting to gather available revisions.

type QueryOpt

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

QueryOpt is a preview option which injects the correct query into the preview query. This is either a specific string query, or if no specific query was sent, it runs the query based on the URL path.

func NewQueryOpt

func NewQueryOpt(path string, query string) *QueryOpt

NewQueryOpt creates a new QueryOpt struct to inject the correct preview query.

func (*QueryOpt) Init

func (q *QueryOpt) Init(preview *Preview) error

Init registers the QueryOpt with the preview struct as both a rego option provider and a result provider

func (*QueryOpt) RegoOptions

func (q *QueryOpt) RegoOptions() []func(*rego.Rego)

RegoOptions sets up the query. If a specific query is provided, it set this up to run in the context of the package represented by the path. If not, the path reference is queried directly.

func (*QueryOpt) Result

func (q *QueryOpt) Result(_ *Preview, results rego.ResultSet, response types.PreviewResponseV1) (types.PreviewResponseV1, error)

Results is responsible for mixing the query result into the response. If a specific query was sent, it provides the full result expression value. Otherwise it extracts the result, returning it without the extra context.

Switching the result format in this way makes the return more compatible with some control-plane APIs.

type RegoOptionProvider

type RegoOptionProvider interface {
	RegoOptions() []func(*rego.Rego)
}

RegoOptionProvider is an interface allowing options to define Rego object options passed when `rego.New()` is called.

type ResultProvider

type ResultProvider interface {
	Result(*Preview, rego.ResultSet, types.PreviewResponseV1) (types.PreviewResponseV1, error)
}

ResultProvider is an interface which allows preview options to add or modify the preview response based on the the Rego eval result.

type StoragePreparer

type StoragePreparer interface {
	PrepareStorage(*PreviewStorage) error
}

StoragePreparer is an interface representing a mutation on the PreviewStorage instance, allowing options to modify its setup.

type WASMResolversOpt

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

WASMResolverOpts is a preview option which adds any defined WASM resolvers to the Rego object

func NewWASMResolversOpt

func NewWASMResolversOpt(manager *plugins.Manager) *WASMResolversOpt

NewWASMResolversOpt creates a new WASMResolversOpt struct

func (*WASMResolversOpt) Init

func (w *WASMResolversOpt) Init(preview *Preview) error

Init registers the WASMResolversOpt with the preview struct as a RegoOptionProvider

func (*WASMResolversOpt) RegoOptions

func (w *WASMResolversOpt) RegoOptions() []func(*rego.Rego)

RegoOptions pulls any WASM resolvers from the plugin manager and adds them to the Rego options so they are taken into account when processing the preview request.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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