Files
regorus/src/target/deserialize.rs
Anand Krishnamoorthi cc917ea75d feat: Complete target system with C# bindings and resource inference (#458)
* 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>
2025-08-19 20:23:43 -05:00

77 lines
2.8 KiB
Rust

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use crate::registry::instances::{EFFECT_SCHEMA_REGISTRY, RESOURCE_SCHEMA_REGISTRY};
use crate::{format, Rc, Schema, Vec};
use alloc::collections::BTreeMap;
use serde::de::{Deserializer, Error};
use serde::Deserialize;
type String = Rc<str>;
/// Deserialize resource schemas from either an array of schemas or schema names.
/// If specified as schema names, look them up from RESOURCE_SCHEMA_REGISTRY.
pub fn deserialize_resource_schemas<'de, D>(deserializer: D) -> Result<Vec<Rc<Schema>>, D::Error>
where
D: Deserializer<'de>,
{
let array: Vec<serde_json::Value> = Vec::deserialize(deserializer)
.map_err(|e| D::Error::custom(format!("Failed to deserialize resource_schemas: {}", e)))?;
let mut schemas = Vec::new();
for item in array.into_iter() {
let schema =
if let Some(name) = item.as_str() {
// Look up schema by name in the registry
RESOURCE_SCHEMA_REGISTRY.get(name).ok_or_else(|| {
D::Error::custom(format!("Resource schema '{}' not found in registry", name))
})?
} else {
// Treat as a direct schema definition
Rc::new(Schema::deserialize(item.clone()).map_err(|e| {
D::Error::custom(format!("Failed to deserialize schema: {}", e))
})?)
};
// Assert that the schema represents an object type
if !matches!(schema.as_type(), crate::schema::Type::Object { .. }) {
return Err(D::Error::custom("Resource schema must be an object type"));
}
schemas.push(schema);
}
Ok(schemas)
}
/// Deserialize effects from either an object of schemas or schema names.
/// If specified as schema names, look them up from EFFECT_SCHEMA_REGISTRY.
pub fn deserialize_effects<'de, D>(
deserializer: D,
) -> Result<BTreeMap<String, Rc<Schema>>, D::Error>
where
D: Deserializer<'de>,
{
let object: BTreeMap<String, serde_json::Value> = BTreeMap::deserialize(deserializer)
.map_err(|e| D::Error::custom(format!("Failed to deserialize effects: {}", e)))?;
let mut effects = BTreeMap::new();
for (key, item) in object.into_iter() {
if let Some(name) = item.as_str() {
// Look up schema by name in the registry
let schema = EFFECT_SCHEMA_REGISTRY.get(name).ok_or_else(|| {
D::Error::custom(format!("Effect schema '{}' not found in registry", name))
})?;
effects.insert(key, schema);
} else {
// Treat as a direct schema definition
let schema = Schema::deserialize(item.clone())
.map_err(|e| D::Error::custom(format!("Failed to deserialize schema: {}", e)))?;
effects.insert(key, Rc::new(schema));
}
}
Ok(effects)
}