mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
* feat: add Azure Policy alias normalization/denormalization Add normalizer and denormalizer for ARM JSON resources, enabling Azure Policy alias short names to become direct paths into a flat structure. - Normalizer: flattens properties wrappers, lowercases keys, resolves per-alias versioned ARM paths, handles sub-resource array flattening, element-level field remaps, and array base renames - Denormalizer: reverses all transformations with casing restoration - AliasRegistry: loads production alias catalogs and data policy manifests - Types: serde deserialization for ARM provider alias formats - YAML test suite: 13 test files covering normalize, denormalize, round-trip, data-plane, edge cases, malformed input, sub-resources, and registry API - Benchmark suite for normalization performance * feat: add FFI and C# bindings for alias normalization - FFI: alias_registry.rs with C-compatible API for loading catalogs, normalizing resources, and denormalizing back to ARM JSON - C#: AliasRegistry wrapper class with NativeMethods P/Invoke bindings and integration tests - Updated Cargo.lock files for new serde_json dependency
235 lines
6.5 KiB
C#
235 lines
6.5 KiB
C#
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
#nullable enable
|
|
namespace Regorus
|
|
{
|
|
internal sealed class RegorusEngineHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|
{
|
|
private RegorusEngineHandle() : base(ownsHandle: true)
|
|
{
|
|
}
|
|
|
|
internal static RegorusEngineHandle Create()
|
|
{
|
|
unsafe
|
|
{
|
|
var raw = Internal.API.regorus_engine_new();
|
|
if (raw is null)
|
|
{
|
|
throw new InvalidOperationException("Failed to create Regorus engine.");
|
|
}
|
|
|
|
var handle = new RegorusEngineHandle();
|
|
handle.SetHandle((IntPtr)raw);
|
|
return handle;
|
|
}
|
|
}
|
|
|
|
internal static RegorusEngineHandle FromPointer(IntPtr pointer)
|
|
{
|
|
if (pointer == IntPtr.Zero)
|
|
{
|
|
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
|
}
|
|
|
|
var handle = new RegorusEngineHandle();
|
|
handle.SetHandle(pointer);
|
|
return handle;
|
|
}
|
|
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
if (!IsInvalid)
|
|
{
|
|
unsafe
|
|
{
|
|
Internal.API.regorus_engine_drop((Internal.RegorusEngine*)handle);
|
|
}
|
|
SetHandle(IntPtr.Zero);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
internal sealed class RegorusCompiledPolicyHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|
{
|
|
private RegorusCompiledPolicyHandle() : base(ownsHandle: true)
|
|
{
|
|
}
|
|
|
|
internal static RegorusCompiledPolicyHandle FromPointer(IntPtr pointer)
|
|
{
|
|
if (pointer == IntPtr.Zero)
|
|
{
|
|
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
|
}
|
|
|
|
var handle = new RegorusCompiledPolicyHandle();
|
|
handle.SetHandle(pointer);
|
|
return handle;
|
|
}
|
|
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
if (!IsInvalid)
|
|
{
|
|
unsafe
|
|
{
|
|
Internal.API.regorus_compiled_policy_drop((Internal.RegorusCompiledPolicy*)handle);
|
|
}
|
|
SetHandle(IntPtr.Zero);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
internal sealed class RegorusProgramHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|
{
|
|
private RegorusProgramHandle() : base(ownsHandle: true)
|
|
{
|
|
}
|
|
|
|
internal static RegorusProgramHandle Create()
|
|
{
|
|
unsafe
|
|
{
|
|
var raw = Internal.API.regorus_program_new();
|
|
if (raw is null)
|
|
{
|
|
throw new InvalidOperationException("Failed to create Regorus program.");
|
|
}
|
|
|
|
var handle = new RegorusProgramHandle();
|
|
handle.SetHandle((IntPtr)raw);
|
|
return handle;
|
|
}
|
|
}
|
|
|
|
internal static RegorusProgramHandle FromPointer(IntPtr pointer)
|
|
{
|
|
if (pointer == IntPtr.Zero)
|
|
{
|
|
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
|
}
|
|
|
|
var handle = new RegorusProgramHandle();
|
|
handle.SetHandle(pointer);
|
|
return handle;
|
|
}
|
|
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
if (!IsInvalid)
|
|
{
|
|
unsafe
|
|
{
|
|
Internal.API.regorus_program_drop((Internal.RegorusProgram*)handle);
|
|
}
|
|
SetHandle(IntPtr.Zero);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
internal sealed class RegorusRvmHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|
{
|
|
private RegorusRvmHandle() : base(ownsHandle: true)
|
|
{
|
|
}
|
|
|
|
internal static RegorusRvmHandle Create()
|
|
{
|
|
unsafe
|
|
{
|
|
var raw = Internal.API.regorus_rvm_new();
|
|
if (raw is null)
|
|
{
|
|
throw new InvalidOperationException("Failed to create Regorus RVM.");
|
|
}
|
|
|
|
var handle = new RegorusRvmHandle();
|
|
handle.SetHandle((IntPtr)raw);
|
|
return handle;
|
|
}
|
|
}
|
|
|
|
internal static RegorusRvmHandle FromPointer(IntPtr pointer)
|
|
{
|
|
if (pointer == IntPtr.Zero)
|
|
{
|
|
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
|
}
|
|
|
|
var handle = new RegorusRvmHandle();
|
|
handle.SetHandle(pointer);
|
|
return handle;
|
|
}
|
|
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
if (!IsInvalid)
|
|
{
|
|
unsafe
|
|
{
|
|
Internal.API.regorus_rvm_drop((Internal.RegorusRvm*)handle);
|
|
}
|
|
SetHandle(IntPtr.Zero);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
internal sealed class RegorusAliasRegistryHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|
{
|
|
private RegorusAliasRegistryHandle() : base(ownsHandle: true)
|
|
{
|
|
}
|
|
|
|
internal static RegorusAliasRegistryHandle Create()
|
|
{
|
|
unsafe
|
|
{
|
|
var raw = Internal.API.regorus_alias_registry_new();
|
|
if (raw is null)
|
|
{
|
|
throw new InvalidOperationException("Failed to create Regorus alias registry.");
|
|
}
|
|
|
|
var handle = new RegorusAliasRegistryHandle();
|
|
handle.SetHandle((IntPtr)raw);
|
|
return handle;
|
|
}
|
|
}
|
|
|
|
internal static RegorusAliasRegistryHandle FromPointer(IntPtr pointer)
|
|
{
|
|
if (pointer == IntPtr.Zero)
|
|
{
|
|
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
|
}
|
|
|
|
var handle = new RegorusAliasRegistryHandle();
|
|
handle.SetHandle(pointer);
|
|
return handle;
|
|
}
|
|
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
if (!IsInvalid)
|
|
{
|
|
unsafe
|
|
{
|
|
Internal.API.regorus_alias_registry_drop((Internal.RegorusAliasRegistry*)handle);
|
|
}
|
|
SetHandle(IntPtr.Zero);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|