mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
* 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>
217 lines
5.5 KiB
Go
217 lines
5.5 KiB
Go
package regorus
|
|
|
|
// #cgo LDFLAGS: -L ../../../ffi/target/release -lregorus_ffi
|
|
// #include "../../../ffi/regorus.h"
|
|
import "C"
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
)
|
|
|
|
type Engine struct {
|
|
e *C.RegorusEngine
|
|
}
|
|
|
|
func NewEngine() *Engine {
|
|
e := new(Engine)
|
|
e.e = C.regorus_engine_new()
|
|
return e
|
|
}
|
|
|
|
func (e *Engine) Close() {
|
|
C.regorus_engine_drop(e.e)
|
|
}
|
|
|
|
func (e *Engine) Clone() *Engine {
|
|
c := new(Engine)
|
|
c.e = C.regorus_engine_clone(e.e)
|
|
return c
|
|
}
|
|
|
|
func (e *Engine) SetRegoV0(enable bool) error {
|
|
result := C.regorus_engine_set_rego_v0(e.e, C.bool(enable))
|
|
defer C.regorus_result_drop(result)
|
|
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) AddPolicy(path string, rego string) (string, error) {
|
|
path_c := C.CString(path)
|
|
defer C.free(unsafe.Pointer(path_c))
|
|
|
|
rego_c := C.CString(rego)
|
|
defer C.free(unsafe.Pointer(rego_c))
|
|
|
|
result := C.regorus_engine_add_policy(e.e, path_c, rego_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) AddPolicyFromFile(path string) (string, error) {
|
|
path_c := C.CString(path)
|
|
defer C.free(unsafe.Pointer(path_c))
|
|
|
|
result := C.regorus_engine_add_policy_from_file(e.e, path_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) GetPackages() (string, error) {
|
|
result := C.regorus_engine_get_packages(e.e)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) GetPolicies() (string, error) {
|
|
result := C.regorus_engine_get_policies(e.e)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) AddDataJson(data string) error {
|
|
data_c := C.CString(data)
|
|
defer C.free(unsafe.Pointer(data_c))
|
|
|
|
result := C.regorus_engine_add_data_json(e.e, data_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) AddDataFromJsonFile(path string) error {
|
|
path_c := C.CString(path)
|
|
defer C.free(unsafe.Pointer(path_c))
|
|
|
|
result := C.regorus_engine_add_data_from_json_file(e.e, path_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) SetInputJson(input string) error {
|
|
input_c := C.CString(input)
|
|
defer C.free(unsafe.Pointer(input_c))
|
|
|
|
result := C.regorus_engine_set_input_json(e.e, input_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) SetInputFromJsonFile(path string) error {
|
|
path_c := C.CString(path)
|
|
defer C.free(unsafe.Pointer(path_c))
|
|
|
|
result := C.regorus_engine_set_input_from_json_file(e.e, path_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) EvalQuery(query string) (string, error) {
|
|
query_c := C.CString(query)
|
|
defer C.free(unsafe.Pointer(query_c))
|
|
|
|
result := C.regorus_engine_eval_query(e.e, query_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) EvalRule(rule string) (string, error) {
|
|
rule_c := C.CString(rule)
|
|
defer C.free(unsafe.Pointer(rule_c))
|
|
|
|
result := C.regorus_engine_eval_rule(e.e, rule_c)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) SetEnableCoverage(enable bool) error {
|
|
result := C.regorus_engine_set_enable_coverage(e.e, C.bool(enable))
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) ClearCoverageData() error {
|
|
result := C.regorus_engine_clear_coverage_data(e.e)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) GetCoverageReport() (string, error) {
|
|
result := C.regorus_engine_get_coverage_report(e.e)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) GetCoverageReportPretty() (string, error) {
|
|
result := C.regorus_engine_get_coverage_report_pretty(e.e)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
|
|
return C.GoString(result.output), nil
|
|
}
|
|
|
|
func (e *Engine) SetGatherPrints(b bool) error {
|
|
result := C.regorus_engine_set_gather_prints(e.e, C.bool(b))
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Engine) TakePrints() (string, error) {
|
|
result := C.regorus_engine_take_prints(e.e)
|
|
defer C.regorus_result_drop(result)
|
|
if result.status != C.Ok {
|
|
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
|
}
|
|
|
|
return C.GoString(result.output), nil
|
|
}
|