OpaDotNet.Wasm 1.0.0-preview0012

This is a prerelease version of OpaDotNet.Wasm.
There is a newer version of this package available.
See the version list below for details.
dotnet add package OpaDotNet.Wasm --version 1.0.0-preview0012                
NuGet\Install-Package OpaDotNet.Wasm -Version 1.0.0-preview0012                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpaDotNet.Wasm" Version="1.0.0-preview0012" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpaDotNet.Wasm --version 1.0.0-preview0012                
#r "nuget: OpaDotNet.Wasm, 1.0.0-preview0012"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpaDotNet.Wasm as a Cake Addin
#addin nuget:?package=OpaDotNet.Wasm&version=1.0.0-preview0012&prerelease

// Install OpaDotNet.Wasm as a Cake Tool
#tool nuget:?package=OpaDotNet.Wasm&version=1.0.0-preview0012&prerelease                

Open Policy Agent (OPA) WebAssembly dotnet core SDK

This is SDK for using WebAssembly (wasm) compiled Open Policy Agent Rego policies with dotnet core.

Initial implementation was based on Open Policy Agent WebAssemby NPM Module

Supported ABI

Version Status
1.0 ✔️
1.2 ✔️
1.3 ✔️

Getting Started

Install nuget package

dotnet add package OpaDotNet.Wasm

Usage

To evaluate OPA policy you need to:

Load compiled policy

using using OpaDotNet.Wasm;

var factory = new OpaEvaluatorFactory();

const string data = "{ \"world\": \"world\" }";

using var engine = factory.CreateWithJsonData(
    File.OpenRead("policy.wasm"),
    data
    );

Evaluate policy

IOpaEvaluator has several APIs for policy evaluation:

  • EvaluatePredicate - Evaluates named policy with specified input. Response interpreted as simple true/false result.
  • Evaluate - Evaluates named policy with specified input.
  • EvaluateRaw - Evaluates named policy with specified raw JSON input.
var policyResult = engine.EvaluatePredicate(inp);

Check result

if (policyResult)
{
    // We've been authorized.
}
else
{
    // Can't do that.
}

Writing policy

See writing policy

Compiling policy

You have several options to compile rego policy into wasm module:

package example

default hello = false

hello {
    x := input.message
    x == data.world
}

Manually

Either use the Compile REST API or opa build CLI tool.

For example, with OPA v0.20.5+:

opa build -t wasm -e example/hello example.rego

Which is compiling the example.rego policy file. The result will be an OPA bundle with the policy.wasm binary included. See (./samples) for a more comprehensive example.

See opa build --help for more details.

With OpaDotNet.Wasm.Compilation

You can use SDK to do compilation for you.

Important. You will need opa cli tool to be in your PATH or provide full path in RegoCliCompilerOptions.

using OpaDotNet.Wasm;
using OpaDotNet.Wasm.Compilation;

var options = new OptionsWrapper<RegoCliCompilerOptions>(new RegoCliCompilerOptions());
var compiler = new RegoCliCompiler(options);
var policyStream = await compiler.CompileFile("example.rego", new[] { "example/hello" });

// Use compiled policy.
var factory = new OpaEvaluatorFactory();

using var engine = factory.CreateWithJsonData(policyStream);

3rd Party Libraries and Contributions

  • OPA - An open source, general-purpose policy engine that unifies policy enforcement across the stack.
  • Moq - The most popular and friendly mocking library for .NET.
  • xUnit.net - Free, open source, community-focused unit testing tool for the .NET Framework.
  • wasmtime-dotnet - .NET embedding of Wasmtime.
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OpaDotNet.Wasm:

Package Downloads
OpaDotNet.Extensions.AspNetCore

OpaDotNet.Wasm for AspNetCore applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.0 420 2/12/2024
2.4.1 142 1/26/2024
2.4.1-pre0004 95 1/23/2024
2.4.0 173 1/10/2024
2.3.0 230 11/21/2023
2.2.0 192 10/11/2023
2.2.0-preview0010 144 10/9/2023
2.2.0-preview0008 143 10/3/2023
2.1.1 154 9/29/2023
2.1.0 165 9/28/2023
2.0.0 284 8/18/2023
2.0.0-preview0012 115 8/18/2023
2.0.0-preview 121 8/17/2023
1.4.0 214 8/15/2023
1.3.0 156 8/9/2023
1.3.0-preview0010 136 8/7/2023
1.2.1 190 7/27/2023
1.2.0 193 7/26/2023
1.2.0-preview0033 139 7/25/2023
1.2.0-preview0028 129 7/24/2023
1.2.0-preview0012 150 7/20/2023
1.2.0-preview0009 128 7/19/2023
1.2.0-preview0007 133 7/19/2023
1.1.0 174 7/13/2023
1.1.0-preview0078 142 7/13/2023
1.1.0-preview0076 110 7/13/2023
1.1.0-preview0073 119 7/13/2023
1.1.0-preview0068 139 7/12/2023
1.1.0-preview0067 159 7/12/2023
1.1.0-preview0065 152 7/12/2023
1.1.0-preview0063 139 7/12/2023
1.1.0-preview0061 159 7/11/2023
1.1.0-preview0057 126 7/11/2023
1.1.0-preview0053 117 7/11/2023
1.1.0-preview0049 105 7/10/2023
1.1.0-preview0047 117 7/10/2023
1.1.0-preview0018 136 7/6/2023
1.1.0-preview0015 126 7/6/2023
1.1.0-preview0012 130 7/6/2023
1.1.0-preview0011 134 7/5/2023
1.1.0-preview0010 138 7/5/2023
1.1.0-preview0009 121 7/5/2023
1.1.0-preview0007 122 7/3/2023
1.0.0 159 6/27/2023
1.0.0-preview0059 139 6/27/2023
1.0.0-preview0058 127 6/27/2023
1.0.0-preview0056 124 6/27/2023
1.0.0-preview0049 119 6/23/2023
1.0.0-preview0048 122 6/23/2023
1.0.0-preview0047 103 6/23/2023
1.0.0-preview0045 134 6/23/2023
1.0.0-preview0043 118 6/23/2023
1.0.0-preview0041 86 6/22/2023
1.0.0-preview0040 132 6/22/2023
1.0.0-preview0037 105 6/22/2023
1.0.0-preview0036 118 6/22/2023
1.0.0-preview0032 130 6/22/2023
1.0.0-preview0031 129 6/22/2023
1.0.0-preview0029 114 6/21/2023
1.0.0-preview0027 129 6/21/2023
1.0.0-preview0025 122 6/21/2023
1.0.0-preview0023 137 6/21/2023
1.0.0-preview0021 101 6/20/2023
1.0.0-preview0012 109 6/20/2023
1.0.0-preview0011 135 6/20/2023