// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Runtime.InteropServices; #pragma warning disable CS8500 #pragma warning disable CS8981 namespace Regorus.Internal { /// /// Native FFI method declarations for Regorus. /// This file contains all P/Invoke declarations for the Regorus native library. /// internal static unsafe partial class API { private const string LibraryName = "regorus_ffi"; #region Common Methods /// /// Drop a RegorusResult. /// output and error_message strings are not valid after drop. /// [DllImport(LibraryName, EntryPoint = "regorus_result_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_result_drop(RegorusResult result); #endregion #region Engine Methods /// /// Construct a new Engine. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html /// [DllImport(LibraryName, EntryPoint = "regorus_engine_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusEngine* regorus_engine_new(); /// /// Clone a RegorusEngine. /// To avoid having to parse same policy again, the engine can be cloned /// after policies and data have been added. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusEngine* regorus_engine_clone(RegorusEngine* engine); /// /// Drop a RegorusEngine. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_engine_drop(RegorusEngine* engine); /// /// Add a policy. /// The policy is parsed into AST. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_policy /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_policy", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_policy(RegorusEngine* engine, byte* path, byte* rego); /// /// Add a policy from file. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_policy_from_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_policy_from_file(RegorusEngine* engine, byte* path); /// /// Add policy data. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_data /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_data_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_data_json(RegorusEngine* engine, byte* data); /// /// Get list of loaded Rego packages as JSON. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_packages /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_packages", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_packages(RegorusEngine* engine); /// /// Get list of policies as JSON. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_policies /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policies", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_policies(RegorusEngine* engine); /// /// Add data from JSON file. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_data_from_json_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_data_from_json_file(RegorusEngine* engine, byte* path); /// /// Clear policy data. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_data /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_clear_data(RegorusEngine* engine); /// /// Set input. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_input /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_input_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_input_json(RegorusEngine* engine, byte* input); /// /// Set input from JSON file. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_input_from_json_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_input_from_json_file(RegorusEngine* engine, byte* path); /// /// Evaluate query. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.eval_query /// [DllImport(LibraryName, EntryPoint = "regorus_engine_eval_query", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_eval_query(RegorusEngine* engine, byte* query); /// /// Evaluate specified rule. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.eval_rule /// [DllImport(LibraryName, EntryPoint = "regorus_engine_eval_rule", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_eval_rule(RegorusEngine* engine, byte* rule); /// /// Enable/disable coverage. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_enable_coverage /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_enable_coverage", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_enable_coverage(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable); /// /// Get coverage report. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_coverage_report /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_coverage_report", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_coverage_report(RegorusEngine* engine); /// /// Enable/disable strict builtin errors. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_strict_builtin_errors /// [DllImport(LibraryName, 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); /// /// Get pretty printed coverage report. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Report.html#method.to_string_pretty /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_coverage_report_pretty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_coverage_report_pretty(RegorusEngine* engine); /// /// Clear coverage data. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_coverage_data /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_coverage_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_clear_coverage_data(RegorusEngine* engine); /// /// Whether to gather output of print statements. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_gather_prints /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_gather_prints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_gather_prints(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable); /// /// Take all the gathered print statements. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.take_prints /// [DllImport(LibraryName, EntryPoint = "regorus_engine_take_prints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_take_prints(RegorusEngine* engine); /// /// Get AST of policies. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_ast_as_json /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_ast_as_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_ast_as_json(RegorusEngine* engine); /// /// Gets the package names defined in each policy added to the engine. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_policy_package_names /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policy_package_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_policy_package_names(RegorusEngine* engine); /// /// Gets the parameters defined in each policy added to the engine. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_policy_parameters /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policy_parameters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_policy_parameters(RegorusEngine* engine); /// /// Enable/disable rego v1. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_rego_v0 /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_rego_v0", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_rego_v0(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable); /// /// Compile a target-aware policy from the current engine state. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.compile_for_target /// [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_for_target", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_compile_for_target(RegorusEngine* engine); /// /// Compile a policy with a specific entry point rule. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.compile_with_entrypoint /// [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_compile_with_entrypoint(RegorusEngine* engine, byte* rule); #endregion #region Compilation Methods /// /// Compiles a policy from data and modules with a specific entry point rule. /// This is a convenience function that wraps regorus::compile_policy_with_entrypoint. /// [DllImport(LibraryName, EntryPoint = "regorus_compile_policy_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compile_policy_with_entrypoint(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len, byte* entry_point_rule); /// /// Compiles a target-aware policy from data and modules. /// This is a convenience function that wraps regorus::compile_policy_for_target. /// [DllImport(LibraryName, EntryPoint = "regorus_compile_policy_for_target", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compile_policy_for_target(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len); #endregion #region Compiled Policy Methods /// /// Drop a RegorusCompiledPolicy. /// [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_compiled_policy_drop(RegorusCompiledPolicy* compiled_policy); /// /// Evaluate the compiled policy with the given input. /// For target policies, evaluates the target's effect rule. /// For regular policies, evaluates the originally compiled rule. /// [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_eval_with_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compiled_policy_eval_with_input(RegorusCompiledPolicy* compiled_policy, byte* input); /// /// Get information about the compiled policy including metadata about modules, /// target configuration, and resource types. /// Returns a JSON-encoded PolicyInfo struct containing comprehensive /// information about the compiled policy such as module IDs, target name, /// applicable resource types, entry point rule, and parameters. /// [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_get_policy_info", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compiled_policy_get_policy_info(RegorusCompiledPolicy* compiled_policy); #endregion #region Target Registry Methods /// /// Register a target from JSON definition. /// The target JSON should follow the target schema format. /// Once registered, the target can be referenced in Rego policies using __target__ rules. /// [DllImport(LibraryName, EntryPoint = "regorus_register_target_from_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_register_target_from_json(byte* target_json); /// /// Check if a target is registered. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_contains(byte* name); /// /// Get a list of all registered target names as JSON array. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_list_names(); /// /// Remove a target from the registry by name. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_remove(byte* name); /// /// Clear all targets from the registry. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_clear(); /// /// Get the number of registered targets. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_len(); /// /// Check if the target registry is empty. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_is_empty(); #endregion #region Resource Schema Registry Methods /// /// Register a resource schema from JSON with a given name. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_register", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_register(byte* name, byte* schema_json); /// /// Check if a resource schema with the given name exists. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_contains(byte* name); /// /// Get the number of registered resource schemas. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_len(); /// /// Check if the resource schema registry is empty. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_is_empty(); /// /// List all registered resource schema names as a JSON array. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_list_names(); /// /// Remove a resource schema by name. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_remove(byte* name); /// /// Clear all resource schemas from the registry. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_clear(); #endregion #region Effect Schema Registry Methods /// /// Register an effect schema from JSON with a given name. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_register", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_register(byte* name, byte* schema_json); /// /// Check if an effect schema with the given name exists. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_contains(byte* name); /// /// Get the number of registered effect schemas. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_len(); /// /// Check if the effect schema registry is empty. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_is_empty(); /// /// List all registered effect schema names as a JSON array. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_list_names(); /// /// Remove an effect schema by name. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_remove(byte* name); /// /// Clear all effect schemas from the registry. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_clear(); #endregion } #region Native Structures /// /// Type of data contained in RegorusResult. /// internal enum RegorusDataType : uint { /// /// No data / void. /// None, /// /// String data (output field is valid). /// String, /// /// Boolean data (bool_value field is valid). /// Boolean, /// /// Integer data (int_value field is valid). /// Integer, /// /// Pointer data (pointer_value field is valid). /// Pointer, } /// /// Status of a call on RegorusEngine. /// internal enum RegorusStatus : uint { /// /// The operation was successful. /// Ok, /// /// The operation was unsuccessful. /// Error, /// /// Invalid data format provided. /// InvalidDataFormat, /// /// Invalid entrypoint rule specified. /// InvalidEntrypoint, /// /// Compilation failed. /// CompilationFailed, /// /// Invalid argument provided. /// InvalidArgument, /// /// Invalid module ID. /// InvalidModuleId, /// /// Invalid policy content. /// InvalidPolicy, } /// /// Result of a call on RegorusEngine. /// Must be freed using regorus_result_drop. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusResult { /// /// Status. /// public RegorusStatus status; /// /// Type of data contained in this result. /// public RegorusDataType data_type; /// /// String output produced by the call. /// Valid when data_type is String. Owned by Rust. /// public byte* output; /// /// Boolean value. /// Valid when data_type is Boolean. /// public bool bool_value; /// /// Integer value. /// Valid when data_type is Integer. /// public long int_value; /// /// Pointer value. /// Valid when data_type is Pointer. /// public void* pointer_value; /// /// Errors produced by the call. /// Owned by Rust. /// public byte* error_message; } /// /// Wrapper for regorus::Engine. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusEngine { } /// /// Wrapper for regorus::CompiledPolicy. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusCompiledPolicy { } /// /// FFI wrapper for PolicyModule struct. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusPolicyModule { public byte* id; public byte* content; } #endregion }