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>
197 lines
7.9 KiB
C#
197 lines
7.9 KiB
C#
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
#nullable enable
|
|
namespace Regorus
|
|
{
|
|
/// <summary>
|
|
/// Represents a policy module with an ID and content.
|
|
/// </summary>
|
|
public struct PolicyModule
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the unique identifier for this policy module.
|
|
/// </summary>
|
|
public string Id { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the Rego policy content.
|
|
/// </summary>
|
|
public string Content { get; set; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the PolicyModule struct.
|
|
/// </summary>
|
|
/// <param name="id">The unique identifier for this policy module</param>
|
|
/// <param name="content">The Rego policy content</param>
|
|
public PolicyModule(string id, string content)
|
|
{
|
|
Id = id;
|
|
Content = content;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Provides static methods for compiling policies into efficient compiled representations.
|
|
/// These are convenience methods that create an engine internally and perform compilation.
|
|
/// </summary>
|
|
public static unsafe class Compiler
|
|
{
|
|
/// <summary>
|
|
/// Compiles a policy from data and modules with a specific entry point rule.
|
|
/// This is a convenience function that sets up an Engine internally and calls the appropriate compilation method.
|
|
/// </summary>
|
|
/// <param name="dataJson">JSON string containing static data for policy evaluation</param>
|
|
/// <param name="modules">List of policy modules to compile</param>
|
|
/// <param name="entryPointRule">The specific rule path to evaluate (e.g., "data.policy.allow")</param>
|
|
/// <returns>A compiled policy that can be evaluated efficiently</returns>
|
|
/// <exception cref="Exception">Thrown when compilation fails</exception>
|
|
public static CompiledPolicy CompilePolicyWithEntrypoint(string dataJson, IEnumerable<PolicyModule> modules, string entryPointRule)
|
|
{
|
|
var dataBytes = Encoding.UTF8.GetBytes(dataJson + char.MinValue);
|
|
var entryPointBytes = Encoding.UTF8.GetBytes(entryPointRule + char.MinValue);
|
|
var modulesArray = modules.ToArray();
|
|
|
|
// Convert C# modules to native structs
|
|
var nativeModules = new Internal.RegorusPolicyModule[modulesArray.Length];
|
|
var pinnedHandles = new List<GCHandle>();
|
|
|
|
try
|
|
{
|
|
for (int i = 0; i < modulesArray.Length; i++)
|
|
{
|
|
var idBytes = Encoding.UTF8.GetBytes(modulesArray[i].Id + char.MinValue);
|
|
var contentBytes = Encoding.UTF8.GetBytes(modulesArray[i].Content + char.MinValue);
|
|
|
|
var idHandle = GCHandle.Alloc(idBytes, GCHandleType.Pinned);
|
|
var contentHandle = GCHandle.Alloc(contentBytes, GCHandleType.Pinned);
|
|
pinnedHandles.Add(idHandle);
|
|
pinnedHandles.Add(contentHandle);
|
|
|
|
nativeModules[i] = new Internal.RegorusPolicyModule
|
|
{
|
|
id = (byte*)idHandle.AddrOfPinnedObject(),
|
|
content = (byte*)contentHandle.AddrOfPinnedObject()
|
|
};
|
|
}
|
|
|
|
fixed (byte* dataPtr = dataBytes)
|
|
fixed (byte* entryPointPtr = entryPointBytes)
|
|
fixed (Internal.RegorusPolicyModule* modulesPtr = nativeModules)
|
|
{
|
|
var result = Internal.API.regorus_compile_policy_with_entrypoint(
|
|
dataPtr, modulesPtr, (UIntPtr)modulesArray.Length, entryPointPtr);
|
|
|
|
var policy = GetCompiledPolicyResult(result);
|
|
return policy;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
foreach (var handle in pinnedHandles)
|
|
{
|
|
handle.Free();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compiles a target-aware policy from data and modules.
|
|
/// This is a convenience function that sets up an Engine internally and calls target-aware compilation.
|
|
/// At least one module must contain a `__target__` declaration.
|
|
/// </summary>
|
|
/// <param name="dataJson">JSON string containing static data for policy evaluation</param>
|
|
/// <param name="modules">List of policy modules to compile</param>
|
|
/// <returns>A compiled policy that can be evaluated efficiently</returns>
|
|
/// <exception cref="Exception">Thrown when compilation fails</exception>
|
|
public static CompiledPolicy CompilePolicyForTarget(string dataJson, IEnumerable<PolicyModule> modules)
|
|
{
|
|
var dataBytes = Encoding.UTF8.GetBytes(dataJson + char.MinValue);
|
|
var modulesArray = modules.ToArray();
|
|
|
|
// Convert C# modules to native structs
|
|
var nativeModules = new Internal.RegorusPolicyModule[modulesArray.Length];
|
|
var pinnedHandles = new List<GCHandle>();
|
|
|
|
try
|
|
{
|
|
for (int i = 0; i < modulesArray.Length; i++)
|
|
{
|
|
var idBytes = Encoding.UTF8.GetBytes(modulesArray[i].Id + char.MinValue);
|
|
var contentBytes = Encoding.UTF8.GetBytes(modulesArray[i].Content + char.MinValue);
|
|
|
|
var idHandle = GCHandle.Alloc(idBytes, GCHandleType.Pinned);
|
|
var contentHandle = GCHandle.Alloc(contentBytes, GCHandleType.Pinned);
|
|
pinnedHandles.Add(idHandle);
|
|
pinnedHandles.Add(contentHandle);
|
|
|
|
nativeModules[i] = new Internal.RegorusPolicyModule
|
|
{
|
|
id = (byte*)idHandle.AddrOfPinnedObject(),
|
|
content = (byte*)contentHandle.AddrOfPinnedObject()
|
|
};
|
|
}
|
|
|
|
fixed (byte* dataPtr = dataBytes)
|
|
fixed (Internal.RegorusPolicyModule* modulesPtr = nativeModules)
|
|
{
|
|
var result = Internal.API.regorus_compile_policy_for_target(
|
|
dataPtr, modulesPtr, (UIntPtr)modulesArray.Length);
|
|
|
|
var policy = GetCompiledPolicyResult(result);
|
|
return policy;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
foreach (var handle in pinnedHandles)
|
|
{
|
|
handle.Free();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string? StringFromUTF8(IntPtr ptr)
|
|
{
|
|
#if NETSTANDARD2_1
|
|
return System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ptr);
|
|
#else
|
|
int len = 0;
|
|
while (System.Runtime.InteropServices.Marshal.ReadByte(ptr, len) != 0) { ++len; }
|
|
byte[] buffer = new byte[len];
|
|
System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
|
|
return Encoding.UTF8.GetString(buffer);
|
|
#endif
|
|
}
|
|
|
|
private static CompiledPolicy GetCompiledPolicyResult(Internal.RegorusResult result)
|
|
{
|
|
try
|
|
{
|
|
if (result.status != Internal.RegorusStatus.Ok)
|
|
{
|
|
var message = StringFromUTF8((IntPtr)result.error_message);
|
|
throw new Exception(message ?? "Unknown compilation error occurred");
|
|
}
|
|
|
|
if (result.data_type != Internal.RegorusDataType.Pointer || result.pointer_value == null)
|
|
{
|
|
throw new Exception("Expected compiled policy pointer but got different data type");
|
|
}
|
|
|
|
return new CompiledPolicy((Internal.RegorusCompiledPolicy*)result.pointer_value);
|
|
}
|
|
finally
|
|
{
|
|
Internal.API.regorus_result_drop(result);
|
|
}
|
|
}
|
|
}
|
|
}
|