feat: Updates for Policy Framework (#405)

- Documentation
  - Regorus Engine is intended to be used from a single thread
  - Clone the engine after adding policies and data to use from another thread

- Builtin errors strictness:
  - default to less strict for OPA compatibility
  - Provide API to change strictness

- Expose GetAstAsJson to C#,
  This can allow writing policy validations in C#.

- Use spectre mitigated msvc crt libs (binskim compliance)

- Update dependencies

fixes #404

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2025-04-30 15:33:40 -05:00
committed by GitHub
parent b11007a1be
commit 130f9685fd
12 changed files with 277 additions and 144 deletions
+15 -1
View File
@@ -9,6 +9,12 @@ using System.Text;
#nullable enable
namespace Regorus
{
/// <summary>
/// C# Wrapper for the Regorus engine.
/// This class is not thread-safe. For multithreaded use, prefer cloning after adding policies and data to an instance.
/// Cloning is cheap and involves only incrementing reference counts for shared immutable objects like parsed policies,
/// data etc. Mutable state is deep copied as needed.
/// </summary>
public unsafe sealed class Engine : System.IDisposable
{
private Regorus.Internal.RegorusEngine* E;
@@ -80,6 +86,10 @@ namespace Regorus
public Engine Clone() => new(Internal.API.regorus_engine_clone(E));
public void SetStrictBuiltinErrors(bool strict)
{
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_strict_builtin_errors(E, strict));
}
byte[] NullTerminatedUTF8Bytes(string s)
{
return Encoding.UTF8.GetBytes(s + char.MinValue);
@@ -202,7 +212,10 @@ namespace Regorus
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_take_prints(E));
}
public string? GetAstAsJson()
{
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_ast_as_json(E));
}
string? StringFromUTF8(IntPtr ptr)
{
@@ -236,5 +249,6 @@ namespace Regorus
Regorus.Internal.API.regorus_result_drop(result);
return resultString;
}
}
}
+9
View File
@@ -143,6 +143,15 @@ namespace Regorus.Internal
[DllImport(__DllName, EntryPoint = "regorus_engine_get_coverage_report", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_engine_get_coverage_report(RegorusEngine* engine);
/// <summary>
/// Enable/disable strict builtin errors.
///
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_strict_builtin_errors
/// * `strict`: Whether to raise errors or return undefined on certain scenarios.
/// </summary>
[DllImport(__DllName, EntryPoint = "regorus_engine_set_strict_builtin_errors", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_engine_set_strict_builtin_errors(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool strict);
/// <summary>
/// Get pretty printed coverage report.
///