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#
cargo add strig --features dsl,dsl-fs
cargo add serde_json
npm install @the-hades-organization/strig
pip install strig-auth
go get github.com/The-Hades-Organization/Strig/bindings/go@v0.10.0
<dependency>
<groupId>io.github.the-hades-organization</groupId>
<artifactId>strig</artifactId>
<version>0.10.0</version>
</dependency>
| Language | Needs |
|---|---|
| Rust | 1.88 or later |
| Node, Bun, browser | Node 18.17, Bun 1.1 or a current browser; one package, native module or WebAssembly |
| Python | 3.9 or later; one wheel per platform |
| Go | 1.22 or later; no cgo |
| Java, Kotlin | 17 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.
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.
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(())
}
cargo run -- document.edit
{
"effect": "allow",
"reason": {
"kind": "grant",
"grant_id": "g-edit",
"permission": "document.edit",
"rank": 1,
"strategy": "most_specific"
},
"matched": [
"g-edit"
]
}
import { readFileSync } from 'node:fs';
import { load } from '@the-hades-organization/strig';
const { compile, Engine, Context } = await load();
const policy = compile(readFileSync('policy.strig', 'utf8'), { file: 'policy.strig' });
const { catalog, roles, strategy } = policy;
const engine = new Engine({ catalog, roles, strategy });
const context = new Context({ grants: policy.grants });
const subject = policy.subjects.find((s) => s.id === 'ana');
const resource = {
path: [
{ kind: 'document', id: 'spec' },
{ kind: 'project', id: 'apollo' },
{ kind: 'org', id: 'acme' },
],
};
const decision = engine.decide(context, { subject, permission: process.argv[2], resource });
console.log(JSON.stringify(decision, null, 2));
node ask.mjs document.edit
{
"effect": "allow",
"matched": [
"g-edit"
],
"reason": {
"grantId": "g-edit",
"kind": "grant",
"permission": "document.edit",
"rank": 1,
"strategy": "most_specific"
}
}
Allow
Granted by g-edit.
Read the answer#
| Field | What it says |
|---|---|
effect | The verdict: allow or deny. |
reason.kind | Why: grant here. Other values appear below. |
reason.grant_id | The grant that decided. Node spells it grantId. |
reason.strategy | The rule that picked it: most_specific, the default. |
matched | Every 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.
g-freeze: deny anyone to document.edit on project in org:acme
Run the same question again.
{
"effect": "deny",
"reason": {
"kind": "grant",
"grant_id": "g-freeze",
"permission": "document.edit",
"rank": 1,
"strategy": "most_specific"
},
"matched": [
"g-edit",
"g-freeze"
]
}
{
"effect": "deny",
"matched": [
"g-edit",
"g-freeze"
],
"reason": {
"grantId": "g-freeze",
"kind": "grant",
"permission": "document.edit",
"rank": 1,
"strategy": "most_specific"
}
}
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.
cargo run -- document.delete
{
"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.
g-edit: allow role editor to document.edit on project in org:acme when subject.mfa == true
{
"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#
ConceptsCatalog, grants, scope and the strategies that combine them.
Policy languageThe same policy in Blocks, Rules and Actors.
A multi-tenant SaaSA complete policy, from the catalog to the checks in a service.