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>
108 lines
3.3 KiB
Rust
108 lines
3.3 KiB
Rust
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
#![cfg(feature = "azure_policy")]
|
|
|
|
use crate::common::*;
|
|
use anyhow::Result;
|
|
use std::os::raw::c_char;
|
|
|
|
/// Register a target from JSON definition.
|
|
///
|
|
/// The target JSON should follow the target schema format.
|
|
/// Once registered, the target can be referenced in Rego policies using `__target__` rules.
|
|
///
|
|
/// * `target_json`: JSON encoded target definition
|
|
#[no_mangle]
|
|
#[cfg(feature = "azure_policy")]
|
|
pub extern "C" fn regorus_register_target_from_json(target_json: *const c_char) -> RegorusResult {
|
|
to_regorus_result(|| -> Result<()> {
|
|
let target_str = from_c_str(target_json)?;
|
|
let target = regorus::Target::from_json_str(&target_str)?;
|
|
regorus::registry::targets::register(regorus::Rc::new(target))?;
|
|
Ok(())
|
|
}())
|
|
}
|
|
|
|
/// Check if a target is registered.
|
|
///
|
|
/// # Parameters
|
|
/// * `name` - Name of the target to check
|
|
///
|
|
/// # Returns
|
|
/// Returns a RegorusResult with boolean value indicating if the target is registered.
|
|
///
|
|
/// # Safety
|
|
/// The name parameter must be a valid null-terminated UTF-8 string.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_target_registry_contains(name: *const c_char) -> RegorusResult {
|
|
let target_name = match from_c_str(name) {
|
|
Ok(s) => s,
|
|
Err(e) => {
|
|
return RegorusResult::err_with_message(
|
|
RegorusStatus::InvalidArgument,
|
|
format!("Invalid target name string: {e}"),
|
|
)
|
|
}
|
|
};
|
|
|
|
let contains = regorus::registry::targets::contains(&target_name);
|
|
RegorusResult::ok_bool(contains)
|
|
}
|
|
|
|
/// Get a list of all registered target names as JSON array.
|
|
#[no_mangle]
|
|
#[cfg(feature = "azure_policy")]
|
|
pub extern "C" fn regorus_target_registry_list_names() -> RegorusResult {
|
|
let names = regorus::registry::targets::list_names();
|
|
let output = serde_json::to_string_pretty(&names).map_err(anyhow::Error::msg);
|
|
|
|
match output {
|
|
Ok(out) => RegorusResult::ok_string(out),
|
|
Err(e) => to_regorus_result(Err(e)),
|
|
}
|
|
}
|
|
|
|
/// Remove a target from the registry by name.
|
|
///
|
|
/// * `name`: The target name to remove
|
|
#[no_mangle]
|
|
#[cfg(feature = "azure_policy")]
|
|
pub extern "C" fn regorus_target_registry_remove(name: *const c_char) -> RegorusResult {
|
|
to_regorus_result(|| -> Result<()> {
|
|
let name_str = from_c_str(name)?;
|
|
regorus::registry::targets::remove(&name_str);
|
|
Ok(())
|
|
}())
|
|
}
|
|
|
|
/// Clear all targets from the registry.
|
|
#[no_mangle]
|
|
#[cfg(feature = "azure_policy")]
|
|
pub extern "C" fn regorus_target_registry_clear() -> RegorusResult {
|
|
regorus::registry::targets::clear();
|
|
RegorusResult::ok_void()
|
|
}
|
|
|
|
/// Get the number of registered targets.
|
|
///
|
|
/// # Returns
|
|
/// Returns a RegorusResult with the count as an integer value.
|
|
#[no_mangle]
|
|
#[cfg(feature = "azure_policy")]
|
|
pub extern "C" fn regorus_target_registry_len() -> RegorusResult {
|
|
let count = regorus::registry::targets::len();
|
|
RegorusResult::ok_int(count as i64)
|
|
}
|
|
|
|
/// Check if the target registry is empty.
|
|
///
|
|
/// # Returns
|
|
/// Returns a RegorusResult with boolean value indicating if the registry is empty.
|
|
#[no_mangle]
|
|
#[cfg(feature = "azure_policy")]
|
|
pub extern "C" fn regorus_target_registry_is_empty() -> RegorusResult {
|
|
let is_empty = regorus::registry::targets::is_empty();
|
|
RegorusResult::ok_bool(is_empty)
|
|
}
|