Go
The engine compiled to WebAssembly and run on wazero, with no cgo.
github.com/The-Hades-Organization/Strig/bindings/go is the Go binding over the same engine and the same JSON EngineSpec as the other APIs. It runs the engine compiled to WebAssembly (crates/strig-wasi, a plain C ABI with JSON in and out) on wazero, pure Go with no cgo, and the module is embedded with go:embed. Go 1.22 or later.
go get github.com/The-Hades-Organization/Strig/bindings/go@v0.10.0
Check and decide#
import strig "github.com/The-Hades-Organization/Strig/bindings/go"
engine, err := strig.NewEngine(spec) // spec: a map, a struct or a json.RawMessage
defer engine.Close()
ctx, _ := engine.NewContext(map[string]any{"grants": grants, "env": map[string]any{"dept": "eng"}})
ctx.AddGrant(grant); ctx.RemoveGrant("g1")
request := map[string]any{"subject": subject, "permission": "project.edit", "resource": resource}
allowed, err := engine.Check(ctx, request) // (bool, error): an error is never an allow
decision, err := engine.Decide(ctx, request) // *Decision{Effect, Reason, Matched, Warnings}
explanation, err := engine.Explain(ctx, request, strig.WithNow(time.Now()))
many, err := engine.CheckMany(ctx, []any{request, other})
policy, err := strig.Compile(text) // the policy language, no engine needed
Rules to know#
- Same JSON, same codes. Requests and results are the JSON of the JavaScript API with the same camelCase fields. A failure is a
*strig.ErrorwhoseCodeis theerrorCodeof the JavaScript binding (INVALID_INPUT,UNKNOWN_SUBJECT,DSL_PARSE, ...), withDiagnosticsfor the policy language. Usestrig.CodeOf(err)orerrors.As. - Fails closed.
Checkreturnsfalsewith the error, nevertrue. If the module traps, the instance is discarded and every later call returnsstrig.ErrBroken. A closed engine or context, or a context of another engine, is an error. - Concurrency. An
Engineis safe for concurrent use, but one WebAssembly instance is single-threaded, so calls are serialized. For parallel throughput give each worker its ownengine.Fork()(its own instance, same spec, no shared state). AContextbelongs to the engine that made it. - Time is explicit.
WithNow(t)orctx.SetNow(t)set the instant; without one the engine fails closed on validity windows.Apply,Propose,ResolveandExpiretake the host clock when the input has nonow.
Not in this binding yet#
Callbacks (the call condition, overrides, invariants written in Go), the audit sink and exclusive resources. A spec that needs a callback fails closed.
Other hosts#
The .wasm is also a release asset (strig-<version>.wasm) for any other host that speaks WebAssembly. To rebuild the embedded module: sh bindings/scripts/build-wasi.sh, then cd bindings/go && go test ./....
Where to go next#
- Run decisions as a service instead: Decision server.
- The JVM sibling: Java and Kotlin.