archenstack-rbac-rust
archenstack-rbac-rust is the Rust port of Archenstack — a roles × features → level access matrix as a zero-dependency core crate, with an optional axum adapter behind a cargo feature when you want an HTTP surface. It's the Rust sibling of the Python and Java cores and speaks the same wire_version 1 contract byte-for-byte.
The core (cargo build with no features) pulls in nothing but std: you bring the storage, the cache, and the HTTP. Turn on the web feature and you also get an axum router.
Install & test
Consume the crate as a git dependency pinned to a release tag (GitLab's Cargo registry isn't production-ready) — see Installation:
# Cargo.toml
archenstack_rbac = { git = "https://git.atolytix.com/packages/archenstack.git", tag = "archenstack-rbac-rust/v1.0.0" }
Working on the crate itself? Run its tests directly:
cd archenstack-rbac-rust
cargo test # core + the shared conformance corpus
cargo test --features web # also the axum adapter's HTTP tests
The crate is archenstack_rbac; the web feature adds axum, tokio, and serde_json — used by the adapter for request parsing only, since every response is serialized by the core's own canonical encoder.
Quickstart
Build a matrix, declare its defaults, and query it. level returns an ordered AccessLevel; the can_* helpers are comparisons on top of it. Declaring a cell on an unknown role or feature is an error; querying an unknown pair is permissive and returns the fallback.
use archenstack_rbac::{Matrix, AccessLevel};
let mut matrix = Matrix::new();
matrix.add_role("manager").add_role("staff");
matrix.add_feature("reports", "Reports");
matrix.add_superuser("admin"); // always FULL, never a column
matrix.add_alias("acting_manager", "manager"); // mirrors manager
matrix.set_default("reports", "manager", AccessLevel::Full)?;
matrix.set_default("reports", "staff", AccessLevel::View)?;
matrix.level("reports", "manager"); // AccessLevel::Full
matrix.can_write("reports", "staff"); // false — VIEW isn't writable
matrix.level("reports", "admin"); // Full — superuser short-circuits
matrix.level("reports", "acting_manager"); // Full — mirrors manager
matrix.override_cell("reports", "staff", AccessLevel::Full)?; // runtime override
matrix.can_write("reports", "staff"); // true
matrix.override_cell("reports", "staff", AccessLevel::View)?; // back to default clears it
matrix.is_overridden("reports", "staff"); // false
export_overrides() yields the canonical, sorted override delta ready to persist. The pure diff, orphaned_overrides, and prune_orphans helpers keep a stored policy tidy.
The spec-conformant core
Under the facade is the normative core every implementation shares: MatrixSnapshot (from_snapshot / to_snapshot / decide / apply_override / is_overridden / reseed / check_mutation), resolve, eval_scope, validate, and canonical_json. AccessLevel is a repr-int enum (None/View/Partial/Full = 0..3), so the ordering is a plain comparison.
The axum adapter
With --features web, the web module exposes the wire-protocol HTTP API over an in-memory store. You build the router with app_with_config, passing the store, a role resolver, and a typed WebConfig. Every argument is explicit — the shipped crate has no zero-argument app() convenience, and a resolver is required:
use archenstack_rbac::web::{self, Store, WebConfig, fail_closed_resolver};
let router = web::app_with_config(
Store::demo(), // the in-memory matrix
fail_closed_resolver(), // a resolver is REQUIRED — this shipped default denies everyone
WebConfig::new(200), // deployment knobs (the POST /check/ batch bound)
);
// axum::serve(listener, router).await?;
| Method + path | Does |
|---|---|
GET /me/ | the caller's resolved permissions envelope |
POST /check/ | decide a feature — single, or a batch (fail-closed per item) |
GET /matrix/ · PUT /matrix/ | read the grid · write a cell override |
POST /matrix/reset/ | drop overrides back to the code defaults |
GET+POST /roles/ · /features/ · /groups/ | list + create |
Responses are the standard { wire_version, kind, … } envelopes with the shared error taxonomy. The in-memory store carries no subject→role assignment layer, so there is no bootstrap-admin insert (as the Django/Java adapters have); its equivalent is the commit validation, which verifies that some active standard role still resolves FULL on the management feature (rbac.manage) before any write commits — a write that would strip the last admin-capable role is rejected with a lockout error.
The role resolver (fail-closed by default)
The caller's role is supplied by a pluggable resolver — the adapter's authentication extension point, of type Fn(&HeaderMap) -> Option<String>. As of 0.2.x the crate ships no header-trusting default:
fail_closed_resolver()— the shipped default. Every caller resolves to no role, so caller-facing routes fail closed and management routes return401. Wire in a real resolver to grant any access.claims_role_resolver(ClaimsResolverConfig)— a provided OIDC resolver (mirrors the SpringJwtClaimsRoleResolver). It reads the role slug off a claim on theAuthorization: Bearer <jwt>token. Archenstack never validates the token — it decodes the payload without checking the signature, issuer, audience, or expiry, so it is safe only behind a trusted upstream (an OIDC resource server, gateway, or auth middleware) that already validated the token. A missing/malformed token or an absent role claim resolves toNone(deny).
use archenstack_rbac::web::{claims_role_resolver, ClaimsResolverConfig};
// Reads the caller's role from the "role" claim (the default).
let resolver = claims_role_resolver(ClaimsResolverConfig::default());
// Point it at your token's own role/group claim instead:
let resolver = claims_role_resolver(ClaimsResolverConfig::new("groups"));
The dev-only X-Role header resolver — the one that trusts the client — no longer ships in the library. It now lives in the runnable demo at examples/serve.rs; run it with cargo run --example serve --features web and poke it by hand (curl -H 'X-Role: admin' …). It is clearly labeled not-for-production.
Configuration
Deployment knobs live in the typed WebConfig struct rather than as hardcoded constants (mirroring the Spring adapter's ArchenstackRbacProperties). The one v1 knob is the POST /check/ batch upper bound. Construct it explicitly with WebConfig::new(max), or read it from the environment — the single source of truth — with WebConfig::from_env():
// Reads ARCHENSTACK_RBAC_CHECK_BATCH_MAX; no inline default — an unset, non-numeric,
// or zero value fails loudly at boot rather than substituting a hidden bound.
let config = WebConfig::from_env()?;
The serve example additionally reads ARCHENSTACK_RBAC_BIND_ADDR for the host:port to bind; see the crate's .env.example for the copy-pasteable values.
The core passes the shared corpus byte-for-byte — every behavioral vector (resolution, aliases, lock-out, scope truth-tables, validation) and every canonical-JSON golden — plus its own facade and adapter tests. If this port and the specification ever disagree, the spec wins.
Releasing
The crate is consumed as a git dependency, so a release is a git tag: archenstack-rbac-rust/vX.Y.Z. Bump version in Cargo.toml, tag, and push — consumers pin that tag (see Installation). Publishing to crates.io later means setting repository / description / keywords and cargo publish; consumers then swap the git dep for archenstack_rbac = "1.0".
The axum adapter stays behind the optional web feature, so library consumers get a lean, dependency-free crate by default.
See also
- The model — roles, features, levels, defaults vs. overrides.
- Resolution, scopes & the authorizer — how a cell is computed and enforced.
- The wire protocol — envelopes, the batch
/check/, and the error taxonomy. - The specification — the exhaustive normative contract.