Call Open Policy Agent (OPA) policies in WASM (Web Assembly) from .NET
$ dotnet add package Opa.WasmBuilt and tested against Open Policy Agent v1.9.0
The simplest use case - load a WASM module into a policy object, pass data and evaluate based on input:
using var module = OpaPolicyModule.Load("example.wasm");
using var opaPolicy = module.CreatePolicyInstance();
// Use the typed methods to interact with the policy instance
opaPolicy.SetData(new { world = "world" });
var output = opaPolicy.Evaluate<bool>(new { message = "world" });
// Alternatively, send raw Json for the utmost control (advanced scenario)
opaPolicy.SetDataJson(@"{""world"": ""world""}");
string output = opaPolicy.EvaluateJson(@"{""message"": ""world""}");
For higher-performance scenarios, you can keep the engine as well as the loaded WASM module around. Note that one engine can handle multiple modules, and the OpaPolicyModule keeps the correct reference to the engine to guarantee thread safety:
using var engine = OpaPolicyModule.CreateEngine();
using var opaPolicyModule = OpaPolicyModule.Load("example.wasm", engine);
// Now instantiate as many policy objects you want on top of the engine & module
using var opaPolicy = opaPolicyModule.CreatePolicyInstance();