#wasm-module #policy #opa #policies #input #agent #evaluate

bin+lib opa-wasm

A crate to use OPA policies compiled to WASM

4 releases

new 0.1.3 Nov 21, 2024
0.1.2 Nov 12, 2024
0.1.1 Oct 7, 2024
0.1.0 Jul 1, 2024

#51 in WebAssembly

Download history 1075/week @ 2024-08-01 250/week @ 2024-08-08 166/week @ 2024-08-15 66/week @ 2024-08-22 550/week @ 2024-08-29 248/week @ 2024-09-05 360/week @ 2024-09-12 168/week @ 2024-09-19 595/week @ 2024-09-26 1058/week @ 2024-10-03 329/week @ 2024-10-10 834/week @ 2024-10-17 547/week @ 2024-10-24 282/week @ 2024-10-31 692/week @ 2024-11-07 773/week @ 2024-11-14

2,447 downloads per month

Apache-2.0

140KB
2.5K SLoC

Rust Open Policy Agent SDK

A crate to use OPA policies compiled to WASM.

Try it out

This includes a CLI tool to try out the SDK implementation.

cargo run --features=cli --      \
    --module ./policy.wasm       \
    --data-path ./data.json      \
    --input '{"hello": "world"}' \
    --entrypoint 'hello/world'

Set the RUST_LOG environment variable to info to show timings informations about the execution.

opa-wasm
Evaluates OPA policies compiled as WASM modules

USAGE:
    opa-eval [OPTIONS] --entrypoint <ENTRYPOINT> <--module <MODULE>|--bundle <BUNDLE>>

OPTIONS:
    -m, --module <MODULE>            Path to the WASM module
    -b, --bundle <BUNDLE>            Path to the OPA bundle
    -e, --entrypoint <ENTRYPOINT>    Entrypoint to use
    -d, --data <JSON>                JSON literal to use as data
    -D, --data-path <PATH>           Path to a JSON file to load as data
    -i, --input <JSON>               JSON literal to use as input
    -I, --input-path <PATH>          Path to a JSON file to load as data
    -h, --help                       Print help information

As a library

use std::collections::HashMap;

use anyhow::Result;

use opa_wasm::{wasmtime, Runtime};

#[tokio::main]
async fn main() -> Result<()> {
    // Configure the WASM runtime
    let mut config = wasmtime::Config::new();
    config.async_support(true);

    let engine = wasmtime::Engine::new(&config)?;

    // Load the policy WASM module
    let module = tokio::fs::read("./policy.wasm").await?;
    let module = wasmtime::Module::new(&engine, module)?;

    // Create a store which will hold the module instance
    let mut store = wasmtime::Store::new(&engine, ());

    let data = HashMap::from([("hello", "world")]);
    let input = HashMap::from([("message", "world")]);

    // Instantiate the module
    let runtime = Runtime::new(&mut store, &module).await?;

    let policy = runtime.with_data(&mut store, &data).await?;

    // Evaluate the policy
    let res: serde_json::Value = policy.evaluate(&mut store, "hello/world", &input).await?;

    println!("{}", res);

    Ok(())
}

Dependencies

~16–31MB
~495K SLoC