mirror of
https://github.com/matrix-org/rust-opa-wasm.git
synced 2026-11-17 04:20:31 +00:00
Open Policy Agent WebAssembly Rust SDK
- Rust 99.9%
- Makefile 0.1%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
## 🤖 New release * `opa-wasm`: 0.3.3 -> 0.3.4 (✓ API compatible changes) <details><summary><i><b>Changelog</b></i></summary><p> <blockquote> ## [0.3.4](https://github.com/matrix-org/rust-opa-wasm/compare/v0.3.3...v0.3.4) - 2026-10-27 ### Other - upgrade Clippy to 1.99.1 - *(deps)* update wasmtime requirement from >=42, <50 to >=42, <51 </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/release-plz/release-plz/). |
||
| .github | ||
| src | ||
| tests | ||
| .gitignore | ||
| .rustfmt.toml | ||
| Cargo.toml | ||
| CHANGELOG.md | ||
| clippy.toml | ||
| CONTRIBUTING.md | ||
| features.txt | ||
| LICENSE | ||
| Makefile | ||
| README.md | ||
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(())
}