mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
cc917ea75d
* feat: Add Schema Registry and Validation Framework This commit introduces a comprehensive schema registry and validation framework, providing schema-based validation of resources and policy effects. - Thread-safe, in-memory registry for schema storage and management - Global registry patterns for effects and resources - Concurrent access with proper error handling - Unicode schema names support - JSON Schema-compliant validation for all primitive types - Advanced constraint validation (patterns, ranges, length limits) - Discriminated union support with anyOf schemas - Detailed error reporting with nested validation paths - Discriminated subobject validation for polymorphic schemas - **Registry Tests**: All registry operations - **Effect Tests**: Policy effect validation - **Resource Tests**: Resource validation - **Validation Tests**: Core validation engine - Thread-safety, error handling, integration scenarios, edge cases - **Dependencies**: dashmap, once_cell, regex - **Thread Safety**: Minimal locking with Rc<Schema> sharing - **Error Types**: TypeMismatch, OutOfRange, PatternMismatch, etc. - Complete schema registry and validation subsystem - Comprehensive test coverage - Foundation for policy validation in Regorus Benchmarks: - Criterion benchmarks for basic types, effects and Azure resources - Performance range: 3.22ns (string) to 34.74µs (Azure VM resource schema validation) - String withs patterns validation: 30.2µs. Need to explore whether regex caching helps bring this down. - Azure policy effects: 188ns-1.4µs Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com> * feat: Complete target system with C# bindings and resource inference - Add comprehensive target system with TargetRegistry and target-aware compilation - Implement resource type inference from policy equality expressions - Create modular C# bindings with separate wrapper classes for each concept - Add thread-safe CompiledPolicy with reference counting for safe disposal - Enhance FFI with detailed error propagation and target functionality - Create TargetExampleApp demonstrating Azure Policy integration - Add CI/CD pipeline testing for all C# applications - Support target definitions with schema validation and resource selectors - Implement PolicyModule struct and target-aware compilation methods - Add comprehensive test coverage for target functionality Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com> --------- Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
70 lines
2.2 KiB
Rust
70 lines
2.2 KiB
Rust
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
use crate::common::*;
|
|
use anyhow::Result;
|
|
use std::os::raw::c_char;
|
|
|
|
/// Wrapper for `regorus::CompiledPolicy`.
|
|
#[derive(Clone)]
|
|
pub struct RegorusCompiledPolicy {
|
|
pub(crate) compiled_policy: regorus::CompiledPolicy,
|
|
}
|
|
|
|
/// Drop a `RegorusCompiledPolicy`.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_compiled_policy_drop(compiled_policy: *mut RegorusCompiledPolicy) {
|
|
if let Ok(cp) = to_ref(compiled_policy) {
|
|
unsafe {
|
|
let _ = Box::from_raw(std::ptr::from_mut(cp));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Evaluate the compiled policy with the given input.
|
|
///
|
|
/// For target policies, evaluates the target's effect rule.
|
|
/// For regular policies, evaluates the originally compiled rule.
|
|
///
|
|
/// * `input`: JSON encoded input data (resource) to validate against the policy.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_compiled_policy_eval_with_input(
|
|
compiled_policy: *mut RegorusCompiledPolicy,
|
|
input: *const c_char,
|
|
) -> RegorusResult {
|
|
let output = || -> Result<String> {
|
|
let input_value = regorus::Value::from_json_str(&from_c_str(input)?)?;
|
|
let result = to_ref(compiled_policy)?
|
|
.compiled_policy
|
|
.eval_with_input(input_value)?;
|
|
result.to_json_str()
|
|
}();
|
|
|
|
match output {
|
|
Ok(out) => RegorusResult::ok_string(out),
|
|
Err(e) => to_regorus_result(Err(e)),
|
|
}
|
|
}
|
|
|
|
/// Get information about the compiled policy including metadata about modules,
|
|
/// target configuration, and resource types.
|
|
///
|
|
/// Returns a JSON-encoded `PolicyInfo` struct containing comprehensive
|
|
/// information about the compiled policy such as module IDs, target name,
|
|
/// applicable resource types, entry point rule, and parameters.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_compiled_policy_get_policy_info(
|
|
compiled_policy: *mut RegorusCompiledPolicy,
|
|
) -> RegorusResult {
|
|
let output = || -> Result<String> {
|
|
let info = to_ref(compiled_policy)?.compiled_policy.get_policy_info()?;
|
|
serde_json::to_string(&info)
|
|
.map_err(|e| anyhow::anyhow!("Failed to serialize policy info: {}", e))
|
|
}();
|
|
|
|
match output {
|
|
Ok(out) => RegorusResult::ok_string(out),
|
|
Err(e) => to_regorus_result(Err(e)),
|
|
}
|
|
}
|