This is based off opa-wasm, which uses wasmer, but this targets wasmtime. This allows you to use OPA WASM binaries in Python versions above 3.10, which is the cutoff for wasmer.
It also appears to run significantly faster due to some optimizations I made and some optimizations in python in general since 3.10.
pip install opa-wasmtime
There are only a couple of steps required to start evaluating the policy.
# Import the module
from opa_wasmtime import OPAPolicy
# Load a policy by specifying its file path
policy = OPAPolicy('./policy.wasm')
# Optional: Set policy data
policy.set_data({"company_name": "ACME"})
# Evaluate the policy
input = {"user": "alice"}
result = policy.evaluate(input)This module has been tested on python 3.10 and above and wasmtime 27.0.2 and above.
See https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/
Either use the Compile REST API or opa build CLI tool.
For example, with OPA v0.20.5+:
opa build -t wasm -e 'example/allow' example.regoWhich compiles the example.rego policy file with the result set to
data.example.allow. The result will be an OPA bundle with the policy.wasm
binary included.
See opa build --help for more details.