Getting started

Install Strig, write a policy, ask the first question and read the answer. Then deny something and watch the engine fail closed.

Install#

Terminal
cargo add strig --features dsl,dsl-fs
cargo add serde_json
LanguageNeeds
Rust1.88 or later
Node, Bun, browserNode 18.17, Bun 1.1 or a current browser; one package, native module or WebAssembly
Python3.9 or later; one wheel per platform
Go1.22 or later; no cgo
Java, Kotlin17 or later; no native code

All five run the same engine and read the same policy file. The examples on this page run against Strig 0.10 in Rust and Node; Python, Go and Java have their own pages.

Write the smallest useful policy#

Save this as policy.strig. One file holds the catalog, the roles, who has them and the grants.

policy.strig
language rules

kinds project, document
broad org

permission document.view on project, document
permission document.edit on project, document depends_on document.view

role viewer
role editor inherits viewer

user:ana has role editor in project:apollo

g-view: allow role viewer to document.view on project in org:acme
g-edit: allow role editor to document.edit on project in org:acme

Ana edits in Apollo. An editor is also a viewer, and document.edit needs document.view on the same resource. Both grants sit on project inside org:acme.

Ask the first question#

Can Ana edit the document spec, in Apollo, in Acme? The program compiles the file, builds the engine, finds Ana and asks. The Node tab does the same in JavaScript.

src/main.rs
use strig::dsl::fs::read_file;
use strig::{Context, Request, Resource};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let permission = std::env::args().nth(1).expect("permission");
    let policy = read_file("policy.strig")?.policy;
    let engine = policy.engine_spec().build()?;
    let context = Context::new(&policy.grants);

    let ana = policy.subjects.iter().find(|s| s.id.as_deref() == Some("ana")).expect("ana");
    let document = Resource::new("document", "spec").within("project", "apollo").within("org", "acme");

    let decision = engine.decide(&context, &Request::new(ana, &permission, &document));
    println!("{}", serde_json::to_string_pretty(&decision)?);
    Ok(())
}
Terminal
cargo run -- document.edit
Output
{
  "effect": "allow",
  "reason": {
    "kind": "grant",
    "grant_id": "g-edit",
    "permission": "document.edit",
    "rank": 1,
    "strategy": "most_specific"
  },
  "matched": [
    "g-edit"
  ]
}

Allow

Granted by g-edit.

Read the answer#

FieldWhat it says
effectThe verdict: allow or deny.
reason.kindWhy: grant here. Other values appear below.
reason.grant_idThe grant that decided. Node spells it grantId.
reason.strategyThe rule that picked it: most_specific, the default.
matchedEvery grant that applied to the question, in source order.

document.view was not asked, but document.edit depends on it, and Ana holds it through g-view: she is an editor, and an editor is a viewer.

Deny something#

Add one line to policy.strig. It freezes editing for everyone in Acme.

policy.strig
g-freeze: deny anyone to document.edit on project in org:acme

Run the same question again.

Output
{
  "effect": "deny",
  "reason": {
    "kind": "grant",
    "grant_id": "g-freeze",
    "permission": "document.edit",
    "rank": 1,
    "strategy": "most_specific"
  },
  "matched": [
    "g-edit",
    "g-freeze"
  ]
}

Deny

Denied by g-freeze.

Both grants applied, on the same step of the path. With most_specific, a deny wins there. Viewing is untouched: cargo run -- document.view still answers allow, by g-view.

See it fail closed#

Ask for a permission the catalog does not declare. Remove g-freeze first, so the question stands on its own.

Terminal
cargo run -- document.delete
Output
{
  "effect": "deny",
  "reason": {
    "kind": "unknown_permission"
  }
}

Now make g-edit depend on something the engine cannot see. Replace its line with this one, and ask for document.edit again. Ana has no mfa attribute.

policy.strig
g-edit: allow role editor to document.edit on project in org:acme when subject.mfa == true
Output
{
  "effect": "deny",
  "reason": {
    "kind": "no_applicable_grant"
  },
  "warnings": [
    "grant g-edit: atributo ausente: subject.mfa"
  ]
}

A condition the engine cannot evaluate never opens access. The allow is dropped, nothing else applies, and the answer is deny with the cause in warnings.

Rust and Node print the same for these two.

Next steps#

Concepts

Catalog, grants, scope and the strategies that combine them.

Policy language

The same policy in Blocks, Rules and Actors.

A multi-tenant SaaS

A complete policy, from the catalog to the checks in a service.