Legion::Rbac
Role-based access control for LegionIO, following Vault-style flat policy patterns.
Version: 0.3.2
Features
- Flat policy model: deny-always-wins, no role inheritance
- Glob pattern matching for resources (
*any segments,+single segment) - Four built-in roles: worker, supervisor, admin, governance-observer
- Team scoping with cross-team as explicit privilege
- Two-layer enforcement: Rack middleware for API routes +
authorize_execution!for all execution sources - Dual-mode store: DB-backed via Sequel or static fallback
- Entra ID claims mapping (roles and groups to Legion roles)
- Kerberos claims mapping (AD group DNs to Legion roles, with Entra fallback)
- Rich identity profile from AD/LDAP (name, email, title, department, company, location)
- Fully optional: guarded by
if defined?(Legion::Rbac)in LegionIO
Installation
Add to your Gemfile:
gem 'legion-rbac'Usage
Setup
require 'legion/rbac'
Legion::Rbac.setupAuthorization Check
principal = Legion::Rbac::Principal.new(id: 'user-1', roles: [:worker], team: 'team-a')
Legion::Rbac.authorize!(principal: principal, action: :execute, resource: 'runners/lex-http/request/get')
# Kerberos principals carry full AD profile
principal.first_name # => "Jane"
principal.title # => "Senior Engineer"
principal.department # => "Platform Engineering"
principal.profile # => { first_name: "Jane", last_name: "Doe", ... }Execution Authorization
Legion::Rbac.authorize_execution!(principal: principal, runner_class: 'Legion::Extensions::LexHttp::Runners::Request', function: :get)Capability Audit (Extension Security)
Audit an extension's source code for dangerous patterns and enforce capability declarations:
result = Legion::Rbac.audit_extension(
extension_name: 'lex-codegen',
source_path: '/path/to/lex-codegen/lib',
declared_capabilities: [:shell_execute, :filesystem_write]
)
result.blocked? # => false (all capabilities declared)
result.detected_capabilities # => [:shell_execute, :filesystem_write]
# Query the capability registry
Legion::Rbac::CapabilityRegistry.for_extension('lex-codegen')
# => [:filesystem_write, :shell_execute]
Legion::Rbac::CapabilityRegistry.extensions_with(:shell_execute)
# => ["lex-codegen", "lex-exec"]Capability Authorization
Check if a principal's role allows a specific capability:
Legion::Rbac.authorize_capability!(principal: principal, capability: :shell_execute, extension_name: 'lex-codegen')
# Raises AccessDenied if the principal's role denies shell_executeDry-Run Check
result = Legion::Rbac::PolicyEngine.evaluate(principal: principal, action: :read, resource: 'runners/*', enforce: false)
# => { allowed: true, reason: "...", would_deny: false }Audit Events
When the optional Core Legion::Events bus is loaded and rbac.emit_events is enabled, subscribers receive rbac.granted and rbac.denied decisions with principal, resource/capability, enforcement and request-correlation context. Dry-run denials still emit rbac.denied with would_deny: true; authorization remains nonblocking.
Successful policy mutations emit rbac.policy_changed with the actor, target record and request correlation. RBAC 0.3.5 uses Core's keyword payload contract, so these notifications reach ordinary Legion::Events.on subscribers rather than being lost to an argument error. Event-delivery failures are logged without changing the authorization decision.
CLI
legion rbac roles # list role definitions
legion rbac show admin # show role permissions
legion rbac assignments # list role assignments from DB
legion rbac assign user-1 worker --team team-a
legion rbac check user-1 runners/lex-http/* --action execute --roles workerConfiguration
Loading legion/rbac registers security defaults in Legion::Settings[:rbac]. Custom roles can be added under rbac.roles via settings merge or config files. Runtime enablement, enforcement, capability-audit policy, and group-role mappings read the live tree: enabled: false disables RBAC, while enforce: false retains evaluation and reports would_deny without blocking. After a full Legion::Settings.reset!, explicitly register RBAC defaults again before setup.
Requirements
- Ruby >= 3.4
- legion-json >= 1.2
- legion-settings >= 1.3
License
Apache-2.0