mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
* perf!: add LRU caches for compiled regex and glob patterns
Add bounded LRU caches for compiled regex and glob patterns used by
Rego builtins, avoiding repeated recompilation of the same patterns
during policy evaluation.
New `cache` feature (included in `full-opa` and `opa-no-std`) backed by
the `lru` crate (no_std compatible) with `spin::Mutex` for thread safety.
- `src/cache.rs`: generic `LruCache<V>` wrapper, global `REGEX_CACHE`
(default capacity 256) and `GLOB_CACHE` (default capacity 128)
- `src/builtins/regex.rs`: all regex builtins route through the cache
- `src/builtins/glob.rs`: glob.match routes through the cache
- Public API: `regorus::cache::{Config, configure, clear}`
Compilation costs avoided per cache hit:
regex 10-55 µs (simple to complex patterns)
glob 10-12 µs
LRU hit ~10 ns
BREAKING CHANGE: new `cache` Cargo feature added to `full-opa` and
`opa-no-std` feature sets; adds `lru` as a dependency.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* perf(vm): amortize per-instruction memory and time limit checks
Deduplicate per-instruction memory_check calls by hoisting them to the
main dispatch loop, and amortize monotonic_now() syscalls in the
execution timer by checking elapsed time every N instructions instead
of on every tick.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* fix(vm): correct object membership to check values only, not keys
The Contains instruction for objects was checking both keys and values:
object_fields.contains_key(v) || object_fields.values().any(|v| ...)
Per the Rego specification, `x in obj` tests whether x is a VALUE of
the object, not a key. The two-argument form `k, v in obj` is needed
to access keys. The interpreter already implemented this correctly
(values-only scan), but the RVM had the extra contains_key() check
which would incorrectly return true when the search value happened to
match a key name.
Remove the contains_key() branch so the behavior matches the interpreter
and the Rego spec. Add two regression tests:
- object_membership_checks_values_not_keys: "foo" in {"foo": "bar"}
must be false (key, not a value)
- object_membership_finds_value: "bar" in {"foo": "bar"} must be true
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* perf(compiler): hoist all-constant collection literals to the literal table
When an array, set, or object literal consists entirely of compile-time
constant expressions (numbers, strings, bools, null, and nested constant
collections), the compiler now evaluates them at compile time and emits a
single Load instruction from the literal table instead of generating
per-element instructions at runtime.
Previously, a Rego expression like `x in [1, 2, 3]` would emit
ArrayCreate + three Load + three ArrayAppend instructions, allocating a
new Vec and Rc on every evaluation. With this change, the entire array
is built once during compilation and loaded as a single constant.
This optimization applies to all three collection types:
- Array literals: avoids ArrayCreate + N x (Load + ArrayAppend)
- Set literals: avoids SetCreate + N x (Load + SetAdd)
- Object literals: avoids ObjectCreate + N x (Load + Load + ObjectInsert)
The implementation adds a try_eval_const() helper that recursively
evaluates an AST expression as a constant Value, returning None if any
sub-expression is non-constant. Each compile method for collection
literals attempts the all-constant fast path first and falls through to
the existing instruction-by-instruction codegen otherwise.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* perf(compiler): fuse Eq + AssertCondition into AssertEq instruction
Add a new `AssertEq { left, right }` instruction that combines equality
comparison and condition assertion into a single operation. This replaces
the previous two-instruction pattern of `Eq { dest, left, right }` followed
by `AssertCondition { condition: dest }`, saving one instruction and one
register per equality assertion.
The fused instruction checks two registers for equality and directly calls
handle_condition with the result, avoiding the intermediate boolean
register entirely. If either operand is undefined or the values differ,
the condition fails and the rule/loop backtracks.
The optimization applies to four destructuring sites:
- EqualityCheck (assignment re-binding with `x = expr; x = expr`)
- EqualityExpr (destructuring against an expression)
- EqualityValue (destructuring against a literal value)
- assert_array_length (array length validation in destructuring)
In soft_assert_mode the compiler still emits the original Eq instruction
since the boolean result register is needed by callers.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* perf(compiler): fuse Not + AssertCondition into AssertNot instruction
Add a new `AssertNot { operand }` instruction that combines logical
negation and condition assertion into a single operation. This replaces
the previous two-instruction pattern of `Not { dest, operand }` followed
by `AssertCondition { condition: dest }`, saving one instruction and one
register allocation.
The fused instruction checks the operand register and passes the
condition if the value is false or undefined (per Rego semantics where
`not expr` succeeds when the expression has no results or is false),
and fails the condition if the value is true or any non-boolean truthy
value.
This was the only emission site for the Not+AssertCondition pair,
occurring in the compilation of `Literal::NotExpr` statements.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* perf(vm): early exit for same-value multi-definition rules
When a rule has multiple definitions that all produce the same value
(e.g. implicit true, or identical literal), set early_exit_on_first_success
on RuleInfo so the VM can stop after the first successful definition.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
* feat!: expose cache configuration API to all language bindings
Add `set_cache_config` and `clear_cache` functions to every binding
so callers can tune or reset the global regex/glob pattern caches
introduced in the cache feature.
Bindings updated:
- FFI (C): `regorus_set_cache_config`, `regorus_clear_cache`
- C++ header: free functions `regorus::set_cache_config`, `regorus::clear_cache`
- Python: module-level `set_cache_config(*, regex, glob)`, `clear_cache()`
- Java: static methods on new `CacheConfig` class
- Go: package-level `SetCacheConfig`, `ClearCache`
- Ruby: module functions `Regorus.set_cache_config`, `Regorus.clear_cache`
- WASM: free functions `setCacheConfig`, `clearCache`
- C#: static methods `Engine.SetCacheConfig`, `Engine.ClearCache`
BREAKING CHANGE: Bump SERIALIZATION_VERSION from 4 to 5 due to new
AssertEq and AssertNot instruction variants added in the instruction
fusion commits. Programs serialized with version 5 cannot be loaded
by older versions of regorus.
* fix: address PR review feedback
Cache subsystem:
- Gate REGEX_CACHE and related imports behind #[cfg(feature = "regex")]
so that building with --features cache without regex compiles correctly.
- Gate LruCache struct behind #[cfg(any(feature = "regex", feature = "glob"))].
- Add Config::MAX_CAPACITY (2^16) hard upper bound; clamp values in
configure() to prevent unbounded cache growth.
- Use parking_lot::Mutex for std builds and spin::Mutex for no_std to
avoid CPU spinning under contention in tight regex/glob eval loops.
- Narrow lock scopes in regex/glob builtins: release the mutex before
compiling a pattern, then re-acquire to insert.
Java JNI binding:
- Fix cache config overflow: negative jlong values now saturate to 0
and positive overflow saturates to usize::MAX (then clamped by
MAX_CAPACITY) instead of silently disabling the cache.
- Gate JNI cache config/clear functions behind #[cfg(feature = "cache")].
Compiler:
- Refactor static_value_of_expr to delegate to try_eval_const,
gaining support for negated numbers and constant collections.
- Make try_eval_const pub(in crate::languages::rego::compiler) and
re-export through expressions.rs.
- Handle Expr::UnaryExpr with numeric literals in try_eval_const so
collections containing negated numbers (e.g. [-1, 2]) are hoisted.
VM correctness:
- Fix Not instruction to follow Rego semantics: not expr yields
true when expr is undefined or false, false for any other defined
value (including non-booleans) -- no longer errors on non-boolean
operands.
- Add enforce_memory_check() call at execute_suspendable_entry to
ensure memory limits are checked before the first instruction.
- Update AssertNot listing comment to "exit if any defined truthy
value" to match actual VM behaviour.
- Add doc comment on Not instruction clarifying Rego negation
semantics.
Bindings:
- Fix C++ header indentation for set_cache_config / clear_cache.
- Propagate Cargo.lock parking_lot addition across ffi, java, python,
and wasm binding lockfiles.
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
---------
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
610 lines
21 KiB
Rust
610 lines
21 KiB
Rust
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
// Unsafe code should not be used.
|
|
// Hard to reason about correctness, and maintainability.
|
|
#![forbid(unsafe_code)]
|
|
// Ensure that all lint names are valid.
|
|
#![deny(unknown_lints)]
|
|
// Fail-fast lints: correctness, safety, and API surface
|
|
#![deny(
|
|
// Panic sources - catch all ways code can panic
|
|
clippy::panic, // forbid explicit panic! macro
|
|
clippy::unreachable, // catches unreachable! macro usage
|
|
clippy::todo, // blocks remaining todo! placeholders
|
|
clippy::unimplemented, // blocks unimplemented! placeholders
|
|
clippy::unwrap_used, // reject Result/Option unwraps
|
|
clippy::expect_used, // reject expect with panic messages
|
|
clippy::manual_assert, // prefer assert! over manual if/panic
|
|
clippy::indexing_slicing, // reject unchecked [] indexing
|
|
clippy::arithmetic_side_effects, // reject overflowing/unchecked math
|
|
clippy::panic_in_result_fn, // disallow panic inside functions returning Result
|
|
|
|
// Rust warnings/upstream
|
|
dead_code, // ban unused items
|
|
deprecated, // prevent use of deprecated APIs
|
|
deprecated_in_future, // catch items scheduled for deprecation
|
|
exported_private_dependencies, // avoid leaking private deps in public API
|
|
future_incompatible, // catch patterns slated to break
|
|
invalid_doc_attributes, // ensure doc attributes are valid
|
|
keyword_idents, // disallow identifiers that are keywords
|
|
macro_use_extern_crate, // block legacy macro_use extern crate
|
|
missing_debug_implementations, // require Debug on public types
|
|
// TODO: Address in future pass
|
|
// missing_docs, // require docs on public items
|
|
non_ascii_idents, // disallow non-ASCII identifiers
|
|
nonstandard_style, // enforce idiomatic naming/style
|
|
noop_method_call, // catch no-op method calls
|
|
trivial_bounds, // forbid useless trait bounds
|
|
trivial_casts, // block needless casts
|
|
unreachable_code, // catch dead/unreachable code
|
|
unreachable_patterns, // catch unreachable match arms
|
|
// TODO: Address in future pass
|
|
// unreachable_pub,
|
|
unused_extern_crates, // remove unused extern crate declarations
|
|
unused_import_braces, // avoid unused braces in imports
|
|
absolute_paths_not_starting_with_crate, // enforce crate:: prefix for absolute paths
|
|
|
|
// Unsafe code / low-level hazards
|
|
clippy::unseparated_literal_suffix, // enforce underscore before literal suffixes
|
|
clippy::print_stderr, // discourage printing to stderr
|
|
clippy::use_debug, // discourage Debug formatting in display contexts
|
|
|
|
// Documentation & diagnostics
|
|
// TODO: Address in future pass
|
|
// clippy::doc_link_with_quotes, // avoid quoted intra-doc links
|
|
// clippy::doc_markdown, // flag bad Markdown in docs
|
|
// clippy::missing_docs_in_private_items, // require docs on private items
|
|
// clippy::missing_errors_doc, // require docs for error cases
|
|
|
|
// API correctness / style
|
|
clippy::missing_const_for_fn, // suggest const fn where possible
|
|
clippy::option_if_let_else, // prefer map_or/unwrap_or_else over if/let
|
|
clippy::if_then_some_else_none, // prefer Option combinators over if/else
|
|
clippy::semicolon_if_nothing_returned, // enforce trailing semicolon for unit
|
|
clippy::unused_self, // remove unused self parameters
|
|
clippy::used_underscore_binding, // avoid using bindings prefixed with _
|
|
clippy::useless_let_if_seq, // simplify let-if sequences
|
|
clippy::similar_names, // flag confusingly similar identifiers
|
|
clippy::shadow_unrelated, // discourage shadowing unrelated variables
|
|
clippy::redundant_pub_crate, // avoid pub(crate) on already pub items
|
|
clippy::wildcard_dependencies, // disallow wildcard Cargo dependency versions
|
|
// TODO: Address in future pass
|
|
// clippy::wildcard_imports, // discourage glob imports
|
|
|
|
// Numeric correctness
|
|
// TODO: Address in future pass
|
|
clippy::float_cmp, // avoid exact float equality checks
|
|
clippy::float_cmp_const, // avoid comparing floats to consts directly
|
|
clippy::float_equality_without_abs, // require tolerance in float equality
|
|
clippy::suspicious_operation_groupings, // catch ambiguous operator precedence
|
|
|
|
// no_std hygiene
|
|
clippy::std_instead_of_core, // prefer core/alloc over std in no_std
|
|
|
|
// Misc polish
|
|
clippy::dbg_macro, // forbid dbg! in production code
|
|
clippy::debug_assert_with_mut_call, // avoid mutating inside debug_assert
|
|
clippy::empty_line_after_outer_attr, // enforce spacing after outer attrs
|
|
clippy::empty_structs_with_brackets, // use unit structs without braces
|
|
)]
|
|
// Advisory lints: useful, but not fatal
|
|
#![warn(
|
|
clippy::assertions_on_result_states, // avoid asserts on Result state
|
|
clippy::match_like_matches_macro, // prefer matches! macro over verbose match
|
|
clippy::needless_continue, // remove redundant continue statements
|
|
clippy::unused_trait_names, // drop unused trait imports
|
|
clippy::verbose_file_reads, // prefer concise file read helpers
|
|
clippy::as_conversions, // discourage lossy as casts
|
|
clippy::pattern_type_mismatch, // catch mismatched types in patterns
|
|
)]
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
|
// Use README.md as crate documentation.
|
|
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
|
|
// We'll default to building for no_std - use core, alloc instead of std.
|
|
#![no_std]
|
|
|
|
extern crate alloc;
|
|
use serde::Serialize;
|
|
|
|
// Import std crate if building with std support.
|
|
// We don't import types or macros from std.
|
|
// As a result, types and macros from std must be qualified via `std::`
|
|
// making dependencies on std easier to spot.
|
|
#[cfg(any(feature = "std", test))]
|
|
extern crate std;
|
|
|
|
#[cfg(feature = "mimalloc")]
|
|
mimalloc::assign_global!();
|
|
|
|
mod ast;
|
|
mod builtins;
|
|
mod compile;
|
|
mod compiled_policy;
|
|
mod compiler;
|
|
mod engine;
|
|
mod indexchecker;
|
|
mod interpreter;
|
|
|
|
pub mod languages {
|
|
#[cfg(feature = "azure-rbac")]
|
|
pub mod azure_rbac;
|
|
|
|
#[cfg(feature = "rvm")]
|
|
pub mod rego;
|
|
}
|
|
|
|
mod lexer;
|
|
pub(crate) mod lookup;
|
|
mod number;
|
|
mod parser;
|
|
mod policy_info;
|
|
mod query;
|
|
#[cfg(feature = "azure_policy")]
|
|
pub mod registry;
|
|
#[cfg(feature = "rvm")]
|
|
pub mod rvm;
|
|
mod scheduler;
|
|
#[cfg(feature = "azure_policy")]
|
|
mod schema;
|
|
#[cfg(feature = "azure_policy")]
|
|
pub mod target;
|
|
#[cfg(any(test, all(feature = "yaml", feature = "std")))]
|
|
pub mod test_utils;
|
|
pub mod utils;
|
|
mod value;
|
|
|
|
#[cfg(feature = "azure_policy")]
|
|
pub use {
|
|
compile::compile_policy_for_target,
|
|
schema::{error::ValidationError, validate::SchemaValidator, Schema},
|
|
target::Target,
|
|
};
|
|
|
|
pub use compile::{compile_policy_with_entrypoint, PolicyModule};
|
|
pub use compiled_policy::CompiledPolicy;
|
|
pub use engine::Engine;
|
|
pub use lexer::Source;
|
|
pub use policy_info::PolicyInfo;
|
|
pub use utils::limits::LimitError;
|
|
pub use utils::limits::PolicyLengthConfig;
|
|
#[cfg(all(feature = "allocator-memory-limits", not(miri)))]
|
|
pub use utils::limits::{
|
|
check_global_memory_limit, enforce_memory_limit, flush_thread_memory_counters,
|
|
global_memory_limit, set_global_memory_limit, set_thread_flush_threshold_override,
|
|
thread_memory_flush_threshold,
|
|
};
|
|
pub use value::Value;
|
|
|
|
/// Compiled-pattern caches for the `regex.*` and `glob.*` Rego builtins.
|
|
///
|
|
/// When the `cache` feature is enabled, compiled patterns are held in
|
|
/// bounded LRU caches so that repeated evaluations avoid recompilation.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```ignore
|
|
/// use regorus::cache;
|
|
///
|
|
/// cache::configure(cache::Config {
|
|
/// regex: 256,
|
|
/// glob: 128,
|
|
/// });
|
|
/// cache::clear();
|
|
/// ```
|
|
#[cfg(feature = "cache")]
|
|
pub mod cache;
|
|
|
|
#[cfg(feature = "arc")]
|
|
pub use alloc::sync::Arc as Rc;
|
|
|
|
#[cfg(not(feature = "arc"))]
|
|
pub use alloc::rc::Rc;
|
|
|
|
#[cfg(feature = "std")]
|
|
use std::collections::{hash_map::Entry as MapEntry, HashMap as Map, HashSet as Set};
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
use alloc::collections::{btree_map::Entry as MapEntry, BTreeMap as Map, BTreeSet as Set};
|
|
|
|
use alloc::{
|
|
borrow::ToOwned as _,
|
|
boxed::Box,
|
|
format,
|
|
string::{String, ToString as _},
|
|
vec,
|
|
vec::Vec,
|
|
};
|
|
|
|
use core::fmt;
|
|
|
|
/// Location of an [`Expression`] in a Rego query.
|
|
///
|
|
/// ```
|
|
/// # use regorus::Engine;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate " \n 1 + 2".
|
|
/// let results = Engine::new().eval_query(" \n 1 + 2".to_string(), false)?;
|
|
///
|
|
/// // Fetch the location for the expression.
|
|
/// let loc = &results.result[0].expressions[0].location;
|
|
///
|
|
/// assert_eq!(loc.row, 2);
|
|
/// assert_eq!(loc.col, 3);
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ````
|
|
/// See also [`QueryResult`].
|
|
#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
|
|
pub struct Location {
|
|
/// Line number. Starts at 1.
|
|
pub row: u32,
|
|
/// Column number. Starts at 1.
|
|
pub col: u32,
|
|
}
|
|
|
|
/// An expression in a Rego query.
|
|
///
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "1 + 2".
|
|
/// let results = Engine::new().eval_query("1 + 2".to_string(), false)?;
|
|
///
|
|
/// // Fetch the expression from results.
|
|
/// let expr = &results.result[0].expressions[0];
|
|
///
|
|
/// assert_eq!(expr.value, Value::from(3u64));
|
|
/// assert_eq!(expr.text.as_ref(), "1 + 2");
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
/// See also [`QueryResult`].
|
|
#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
|
|
pub struct Expression {
|
|
/// Computed value of the expression.
|
|
pub value: Value,
|
|
|
|
/// The Rego expression.
|
|
pub text: Rc<str>,
|
|
|
|
/// Location of the expression in the query string.
|
|
pub location: Location,
|
|
}
|
|
|
|
/// Result of evaluating a Rego query.
|
|
///
|
|
/// A query containing single expression.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "1 + 2".
|
|
/// let results = Engine::new().eval_query("1 + 2".to_string(), false)?;
|
|
///
|
|
/// // Fetch the first (sole) result.
|
|
/// let result = &results.result[0];
|
|
///
|
|
/// assert_eq!(result.expressions[0].value, Value::from(3u64));
|
|
/// assert_eq!(result.expressions[0].text.as_ref(), "1 + 2");
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// A query containing multiple expressions.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "1 + 2; 3.5 * 4".
|
|
/// let results = Engine::new().eval_query("1 + 2; 3.55 * 4".to_string(), false)?;
|
|
///
|
|
/// // Fetch the first (sole) result.
|
|
/// let result = &results.result[0];
|
|
///
|
|
/// // First expression.
|
|
/// assert_eq!(result.expressions[0].value, Value::from(3u64));
|
|
/// assert_eq!(result.expressions[0].text.as_ref(), "1 + 2");
|
|
///
|
|
/// // Second expression.
|
|
/// assert_eq!(result.expressions[1].value, Value::from(14.2));
|
|
/// assert_eq!(result.expressions[1].text.as_ref(), "3.55 * 4");
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// Expressions that create bindings (i.e. associate names to values) evaluate to
|
|
/// either true or false. The value of bindings are available in the `bindings` field.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "x = 1; y = x > 0".
|
|
/// let results = Engine::new().eval_query("x = 1; y = x > 0".to_string(), false)?;
|
|
///
|
|
/// // Fetch the first (sole) result.
|
|
/// let result = &results.result[0];
|
|
///
|
|
/// // First expression is true.
|
|
/// assert_eq!(result.expressions[0].value, Value::from(true));
|
|
/// assert_eq!(result.expressions[0].text.as_ref(), "x = 1");
|
|
///
|
|
/// // Second expression is true.
|
|
/// assert_eq!(result.expressions[1].value, Value::from(true));
|
|
/// assert_eq!(result.expressions[1].text.as_ref(), "y = x > 0");
|
|
///
|
|
/// // bindings contains the value for each named expession.
|
|
/// assert_eq!(result.bindings[&Value::from("x")], Value::from(1u64));
|
|
/// assert_eq!(result.bindings[&Value::from("y")], Value::from(true));
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// If any expression evaluates to false, then no results are produced.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "true; true; false".
|
|
/// let results = Engine::new().eval_query("true; true; false".to_string(), false)?;
|
|
///
|
|
/// assert!(results.result.is_empty());
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
|
|
pub struct QueryResult {
|
|
/// Expressions in the query.
|
|
///
|
|
/// Each statement in the query is treated as a separte expression.
|
|
///
|
|
pub expressions: Vec<Expression>,
|
|
|
|
/// Bindings created in the query.
|
|
#[serde(skip_serializing_if = "Value::is_empty_object")]
|
|
pub bindings: Value,
|
|
}
|
|
|
|
impl Default for QueryResult {
|
|
fn default() -> Self {
|
|
Self {
|
|
bindings: Value::new_object(),
|
|
expressions: vec![],
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Results of evaluating a Rego query.
|
|
///
|
|
/// Generates the same `json` representation as `opa eval`.
|
|
///
|
|
/// Queries typically produce a single result.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "1 + 1".
|
|
/// let results = Engine::new().eval_query("1 + 1".to_string(), false)?;
|
|
///
|
|
/// assert_eq!(results.result.len(), 1);
|
|
/// assert_eq!(results.result[0].expressions[0].value, Value::from(2u64));
|
|
/// assert_eq!(results.result[0].expressions[0].text.as_ref(), "1 + 1");
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// If a query contains only one expression, and even if the expression evaluates
|
|
/// to false, the value will be returned.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "1 > 2" which is false.
|
|
/// let results = Engine::new().eval_query("1 > 2".to_string(), false)?;
|
|
///
|
|
/// assert_eq!(results.result.len(), 1);
|
|
/// assert_eq!(results.result[0].expressions[0].value, Value::from(false));
|
|
/// assert_eq!(results.result[0].expressions[0].text.as_ref(), "1 > 2");
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// In a query containing multiple expressions, if any expression evaluates to false,
|
|
/// then no results are produced.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "true; true; false".
|
|
/// let results = Engine::new().eval_query("true; true; false".to_string(), false)?;
|
|
///
|
|
/// assert!(results.result.is_empty());
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// Note that `=` is different from `==`. The former evaluates to undefined if the LHS and RHS
|
|
/// are not equal. The latter evaluates to either true or false.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// // Create engine and evaluate "1 = 2" which is undefined and produces no resutl.
|
|
/// let results = Engine::new().eval_query("1 = 2".to_string(), false)?;
|
|
///
|
|
/// assert_eq!(results.result.len(), 0);
|
|
///
|
|
/// // Create engine and evaluate "1 == 2" which evaluates to false.
|
|
/// let results = Engine::new().eval_query("1 == 2".to_string(), false)?;
|
|
///
|
|
/// assert_eq!(results.result.len(), 1);
|
|
/// assert_eq!(results.result[0].expressions[0].value, Value::from(false));
|
|
/// assert_eq!(results.result[0].expressions[0].text.as_ref(), "1 == 2");
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// Queries containing loops produce multiple results.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// let results = Engine::new().eval_query("x = [1, 2, 3][_]".to_string(), false)?;
|
|
///
|
|
/// // Three results are produced, one of each value of x.
|
|
/// assert_eq!(results.result.len(), 3);
|
|
///
|
|
/// // Assert expressions and bindings of results.
|
|
/// assert_eq!(results.result[0].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[0].expressions[0].text.as_ref(), "x = [1, 2, 3][_]");
|
|
/// assert_eq!(results.result[0].bindings[&Value::from("x")], Value::from(1u64));
|
|
///
|
|
/// assert_eq!(results.result[1].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[1].expressions[0].text.as_ref(), "x = [1, 2, 3][_]");
|
|
/// assert_eq!(results.result[1].bindings[&Value::from("x")], Value::from(2u64));
|
|
///
|
|
/// assert_eq!(results.result[2].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[2].expressions[0].text.as_ref(), "x = [1, 2, 3][_]");
|
|
/// assert_eq!(results.result[2].bindings[&Value::from("x")], Value::from(3u64));
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// Loop iterations that evaluate to false or undefined don't produce results.
|
|
/// ```
|
|
/// # use regorus::*;
|
|
/// # fn main() -> anyhow::Result<()> {
|
|
/// let results = Engine::new().eval_query("x = [1, 2, 3][_]; x >= 2".to_string(), false)?;
|
|
///
|
|
/// // Two results are produced, one for x = 2 and another for x = 3.
|
|
/// assert_eq!(results.result.len(), 2);
|
|
///
|
|
/// // Assert expressions and bindings of results.
|
|
/// assert_eq!(results.result[0].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[0].expressions[0].text.as_ref(), "x = [1, 2, 3][_]");
|
|
/// assert_eq!(results.result[0].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[0].expressions[1].text.as_ref(), "x >= 2");
|
|
/// assert_eq!(results.result[0].bindings[&Value::from("x")], Value::from(2u64));
|
|
///
|
|
/// assert_eq!(results.result[1].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[1].expressions[0].text.as_ref(), "x = [1, 2, 3][_]");
|
|
/// assert_eq!(results.result[1].expressions[0].value, Value::Bool(true));
|
|
/// assert_eq!(results.result[1].expressions[1].text.as_ref(), "x >= 2");
|
|
/// assert_eq!(results.result[1].bindings[&Value::from("x")], Value::from(3u64));
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
///
|
|
/// See [QueryResult] for examples of different kinds of results.
|
|
#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
|
|
pub struct QueryResults {
|
|
/// Collection of results of evaluting a query.
|
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
|
pub result: Vec<QueryResult>,
|
|
}
|
|
|
|
/// A user defined builtin function implementation.
|
|
///
|
|
/// It is not necessary to implement this trait directly.
|
|
pub trait Extension: FnMut(Vec<Value>) -> anyhow::Result<Value> + Send + Sync {
|
|
/// Fn, FnMut etc are not sized and cannot be cloned in their boxed form.
|
|
/// clone_box exists to overcome that.
|
|
fn clone_box<'a>(&self) -> Box<dyn 'a + Extension>
|
|
where
|
|
Self: 'a;
|
|
}
|
|
|
|
/// Automatically make matching closures a valid [`Extension`].
|
|
impl<F> Extension for F
|
|
where
|
|
F: FnMut(Vec<Value>) -> anyhow::Result<Value> + Clone + Send + Sync,
|
|
{
|
|
fn clone_box<'a>(&self) -> Box<dyn 'a + Extension>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::new(self.clone())
|
|
}
|
|
}
|
|
|
|
/// Implement clone for a boxed extension using [`Extension::clone_box`].
|
|
impl Clone for Box<dyn '_ + Extension> {
|
|
fn clone(&self) -> Self {
|
|
(**self).clone_box()
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for dyn Extension {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
|
|
f.write_fmt(format_args!("<extension>"))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "coverage")]
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "coverage")))]
|
|
pub mod coverage {
|
|
use crate::*;
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
|
/// Coverage information about a rego policy file.
|
|
pub struct File {
|
|
/// Path of the policy file.
|
|
pub path: String,
|
|
|
|
/// The rego policy.
|
|
pub code: String,
|
|
|
|
/// Lines that were evaluated.
|
|
pub covered: alloc::collections::BTreeSet<u32>,
|
|
|
|
/// Lines that were not evaluated.
|
|
pub not_covered: alloc::collections::BTreeSet<u32>,
|
|
}
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
|
/// Policy coverage report.
|
|
pub struct Report {
|
|
/// Coverage information for files.
|
|
pub files: Vec<File>,
|
|
}
|
|
|
|
impl Report {
|
|
/// Produce an ANSI color encoded version of the report.
|
|
///
|
|
/// Covered lines are green.
|
|
/// Lines that are not covered are red.
|
|
///
|
|
/// <img src="https://github.com/microsoft/regorus/blob/main/docs/coverage.png?raw=true">
|
|
#[allow(clippy::arithmetic_side_effects)]
|
|
pub fn to_string_pretty(&self) -> anyhow::Result<String> {
|
|
let mut s = String::default();
|
|
s.push_str("COVERAGE REPORT:\n");
|
|
for file in self.files.iter() {
|
|
if file.not_covered.is_empty() {
|
|
s.push_str(&format!("{} has full coverage\n", file.path));
|
|
continue;
|
|
}
|
|
|
|
s.push_str(&format!("{}:\n", file.path));
|
|
for (line_idx, code) in file.code.split('\n').enumerate() {
|
|
let line = u32::try_from(line_idx + 1).unwrap_or(u32::MAX);
|
|
if file.not_covered.contains(&line) {
|
|
s.push_str(&format!("\x1b[31m {line:4} {code}\x1b[0m\n"));
|
|
} else if file.covered.contains(&line) {
|
|
s.push_str(&format!("\x1b[32m {line:4} {code}\x1b[0m\n"));
|
|
} else {
|
|
s.push_str(&format!(" {line:4} {code}\n"));
|
|
}
|
|
}
|
|
}
|
|
|
|
s.push('\n');
|
|
Ok(s)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Items in `unstable` are likely to change.
|
|
#[doc(hidden)]
|
|
pub mod unstable {
|
|
pub use crate::ast::*;
|
|
pub use crate::lexer::*;
|
|
pub use crate::parser::*;
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|