archenstack-rbac-go
archenstack-rbac-go is the Go port of Archenstack — a roles × features → level access matrix with a standard-library-only core and a small net/http adapter for when you want an HTTP surface. It's the Go sibling of the Python and Java cores and speaks the same wire_version 1 contract byte-for-byte.
No third-party dependencies anywhere — the core and the adapter both run on the standard library alone. You bring the storage and the cache; the adapter brings a ready-to-serve http.Handler.
Install & test
Consume it as a tagged Go module — see Installation:
go get github.com/archenstack/archenstack-rbac-go@v1.0.0
Working on the module itself? Run its tests from a checkout:
cd archenstack-rbac-go
go test ./... # core + adapter + the shared conformance corpus
go vet ./...
The core lives in the rbac package (module github.com/archenstack/archenstack-rbac-go); the HTTP adapter is the sibling adapter package.
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 returns an error; querying an unknown pair is permissive and returns the fallback.
import rbac "github.com/archenstack/archenstack-rbac-go"
m := rbac.NewMatrix(rbac.NONE)
m.AddRole("manager").AddRole("staff").
AddSuperuser("admin"). // always FULL, never a column
AddAlias("acting_manager", "manager") // mirrors manager
m.AddFeature("reports", "Reports")
m.SetDefault("reports", "manager", rbac.FULL)
m.SetDefault("reports", "staff", rbac.VIEW)
m.Level("reports", "manager") // FULL
m.CanWrite("reports", "staff") // false — VIEW isn't writable
m.Level("reports", "admin") // FULL — superuser short-circuits
m.Level("reports", "acting_manager") // FULL — mirrors manager
m.Override("reports", "staff", rbac.FULL) // runtime override
m.CanWrite("reports", "staff") // true
m.Override("reports", "staff", rbac.VIEW) // back to the default clears it
m.IsOverridden("reports", "staff") // false
ExportOverrides() yields the canonical, sorted override delta ready to persist. The pure Diff, OrphanedOverrides, and PruneOrphans helpers keep a stored policy tidy, and MustCanonicalJSON renders any value as the shared byte-exact canonical JSON.
The spec-conformant core
Under the facade is the normative core every implementation shares: MatrixSnapshot (FromSnapshot / ToSnapshot / Decide / ApplyOverride / IsOverridden / Reseed / CheckMutation), Resolve, EvalScope, Validate, and canonical JSON. A MatrixRegistry maps an org to its snapshot and fails closed on an unknown org. AccessLevel is an int-typed constant set (NONE/VIEW/PARTIAL/FULL = 0..3), so the ordering is a plain comparison.
The net/http adapter
The adapter package exposes the wire-protocol HTTP API over an in-memory store as a plain http.Handler — mount it wherever you like:
import "archenstack-rbac-go/adapter"
// A resolver is required for a non-fail-closed app: it maps your verified
// principal to a role slug. A nil RoleResolver is fail-closed (401 everywhere).
srv := adapter.NewServer(adapter.Config{
RoleResolver: adapter.NewClaimsRoleResolver("role", ""),
})
http.ListenAndServe(":8080", srv.Handler())
| 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/ (plus detail routes) | list, create, inspect |
The caller's role comes from a pluggable RoleResolver (see below); responses are the standard { wire_version, kind, … } envelopes with the shared error taxonomy.
The role resolver — fail-closed by default
RoleResolver is the host SPI mapping an inbound request to a role slug (the matrix column the authorizer keys on). It is the adapter's one authentication extension point — Archenstack never validates a token itself.
A nil Config.RoleResolver now FAILS CLOSED. NewServer installs DenyAllResolver — every caller resolves to no role, so authenticated routes return 401 — and logs a startup warning. There is no header-trusting default anymore: the adapter never treats a request header or bearer as the role on its own. To run a non-fail-closed app you MUST supply a resolver, as in the example above.
ClaimsRoleResolver—adapter.NewClaimsRoleResolver(roleClaim, roleValue)— is the production resolver. A host's auth middleware verifies the bearer against the issuer's JWKS and stashes the validated claims on the request context withadapter.WithClaims(ctx, claims); the resolver only READS them, keying onroleClaim(default"role"). Left as a plain string the claim IS the role slug; setroleValueand the claim is read as a roles/groups array or a space-delimited scope string, withroleValue's presence selecting the role. Missing claims or a blank role resolve to""— deny.HeaderRoleResolver{Header: "X-Role"}— the insecure, header-trusting resolver — has moved toadapter/resolver_dev.goand is dev-only: it trusts a plain header with no authentication behind it, so any client can name any role. Opt into it explicitly for local demos, examples, or tests; never in production.
Config from the environment
adapter.ConfigFromEnv() binds the deployment settings from ARCHENSTACK_RBAC_* as the single source — no inline fallbacks. ARCHENSTACK_RBAC_CHECK_BATCH_MAX is required and the call fails loud when it is unset or malformed; the claims resolver and the bootstrap check are opt-in.
| Variable | Default | Does |
|---|---|---|
ARCHENSTACK_RBAC_CHECK_BATCH_MAX | required | max items in a POST /check/ batch |
ARCHENSTACK_RBAC_CLAIMS_RESOLVER_ENABLED | false | wire ClaimsRoleResolver (else RoleResolver stays nil → fail-closed) |
ARCHENSTACK_RBAC_ROLE_CLAIM | role | the role claim name |
ARCHENSTACK_RBAC_ROLE_CLAIM_VALUE | (empty) | membership-match value (array / scope-string mode) |
ARCHENSTACK_RBAC_BOOTSTRAP_ADMIN_SUBJECT | (empty) | empty = no bootstrap |
ARCHENSTACK_RBAC_BOOTSTRAP_ADMIN_ROLE | administrator | role the bootstrap ensures exists |
When the claims resolver is not enabled Config.RoleResolver stays nil, so NewServer applies the fail-closed DenyAllResolver. Store.BootstrapAdmin seeds a first management-FULL admin role into an empty store so a deployment never starts locked out — a verify-the-role-exists step, not a subject binding (the in-memory adapter has no assignment store; wiring a specific subject to the role stays the host resolver's job).
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
Go has no registry upload — a module is "published" by pushing a semver git tag. Because the module lives in a monorepo subdirectory, the tag carries the archenstack-rbac-go/ prefix:
git tag archenstack-rbac-go/v1.0.0
git push origin archenstack-rbac-go/v1.0.0
Consumers then go get github.com/archenstack/archenstack-rbac-go@v1.0.0 with the module path marked private (see Installation).
The module path is the repository path, resolved against GitHub directly. In Go the module path is the import statement, so changing it makes a new module rather than a new version: every consumer rewrites its imports, and the old path keeps its own version history. That is why the path is worth settling before the first tag.
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.