woodglue.access¶
Grants, Roles, and the Caller of a request.
Authorization model: Grants, Roles, and the Caller of a request.
A Grant permits one action on the targets matching a glob pattern. Actions:
call: invoke methods of a namespace.read: see runs, run IO, and triggers of a namespace.trigger: fire, activate, and deactivate triggers of a namespace.view: open a UI; its targets areui:<name>.admin: manage Users, Role assignments, and tokens. Its pattern is ignored.
A namespace may also declare its own actions, such as write: a node that
declares requires="write" (see required_action) runs only for a Caller
with a Grant for write on its namespace. The other built-in actions are
reserved, and declared names are lowercase names.
No action implies another, so call * never reaches a write node. Grants are
written as "<action> <pattern>" strings in woodglue.yaml; a bare "admin"
needs no pattern:
auth:
roles:
operator: ["call reports", "read *", "trigger reports"]
viewer: ["read *", "view ui:*"]
writer: ["call cointoss", "write cointoss"]
The admin Role is built in and holds every Grant, including every declared
action. There are no wildcard actions in config.
A Caller is who a request runs as. It carries one or more Grant sets and is
allowed an action only if every set allows it, so a narrowed Personal Access
Token never exceeds its User's current Grants. current_caller holds the Caller
while a JSON-RPC call runs; it is unset for trusted in-process calls (tests,
wgl run), which check then allows.
NAMESPACE_ACTIONS = (Action.call, Action.read, Action.trigger)
module-attribute
¶
Built-in actions whose targets are namespaces; any of them makes a namespace visible.
RESERVED_ACTIONS = (Action.read, Action.trigger, Action.view, Action.admin)
module-attribute
¶
Built-in actions a node cannot declare as its Required Action.
ANY_ACTION = '*'
module-attribute
¶
Action of the one Grant in the built-in admin Role that reaches every action,
including namespace-declared ones. It cannot be written in config.
ANONYMOUS = Caller(user_id=None, grant_sets=(ALL_GRANTS,), name='anonymous')
module-attribute
¶
The Caller of every request when auth is disabled.
Grant
¶
Bases: BaseModel
Permission to perform action on targets matching the glob pattern.
>>> Grant.parse("call reports")
Grant(action=<Action.call: 'call'>, pattern='reports')
>>> str(Grant.parse("admin"))
'admin *'
An action that is not built in is a namespace-declared one:
>>> Grant.parse("write cointoss")
Grant(action='write', pattern='cointoss')
>>> Grant.parse("* cointoss")
Traceback (most recent call last):
...
ValueError: Invalid action '*' in grant '* cointoss'; use a built-in action or a lowercase name
Source code in src/woodglue/access.py
Caller
dataclass
¶
Who a request runs as. user_id is None only for the anonymous Caller
used when auth is disabled. credential_id and credential_kind name the
token or Session that authenticated the request. A pending Caller is a
User awaiting approval: it holds no Grants, whatever its roles.
A namespace-declared action is reached only by a Grant naming it (or by
the built-in admin Role), never by a call wildcard, and it makes the
namespace visible like any namespace action:
>>> reader = Caller("u1", (tuple(Grant.parse(g) for g in ["call *"]),))
>>> reader.can(Action.call, "cointoss"), reader.can("write", "cointoss")
(True, False)
>>> writer = Caller("u2", (tuple(Grant.parse(g) for g in ["write cointoss"]),))
>>> writer.can("write", "cointoss"), writer.can(Action.call, "cointoss")
(True, False)
>>> writer.can_see("cointoss"), writer.can_see("reports")
(True, False)
>>> admin = Caller("u3", (resolve_roles({})[ADMIN_ROLE],))
>>> admin.can("write", "cointoss")
True
Source code in src/woodglue/access.py
has_grants
property
¶
Whether the User holds any Grant at all, e.g. False for a User with no
Roles or a pending one. Only the first Grant set is the User's own;
later sets narrow it.
may_issue_credentials
property
¶
Whether this Caller may create tokens or Sessions. Only an unnarrowed Personal Access Token or a Session may, and not while its User is pending: a narrowed token could mint one broader than itself, and a Delegated Token one that outlives its Session.
can_see(namespace)
¶
Whether namespace appears in discovery for this Caller.
Source code in src/woodglue/access.py
AccessDenied
¶
Bases: Exception
Raised by check when the current Caller lacks a Grant.
Source code in src/woodglue/access.py
is_declared_action(action)
¶
Whether action is a namespace-declared one, not built in or ANY_ACTION.
action_name(name)
¶
A built-in Action, or a namespace-declared action name, which must be a
lowercase name so that "<action> <pattern>" stays unambiguous.
Source code in src/woodglue/access.py
allows(grants, action, target='')
¶
Whether any of grants permits action on target.
>>> gs = [Grant.parse("call rep*"), Grant.parse("view ui:*")]
>>> allows(gs, Action.call, "reports"), allows(gs, Action.call, "billing")
(True, False)
>>> allows(gs, Action.read, "reports")
False
>>> allows(gs, Action.view, "ui:billing")
True
Source code in src/woodglue/access.py
resolve_roles(roles)
¶
Parse Role definitions from config and add the built-in admin Role.
>>> r = resolve_roles({"viewer": ["read *"]})
>>> sorted(r), [str(g) for g in r["viewer"]]
(['admin', 'viewer'], ['read *'])
Source code in src/woodglue/access.py
required_action(requires)
¶
The Required Action of a node from its declared requires: call unless
it names a namespace-declared action. The other built-in actions are
reserved, since they mean the same on every server.
>>> required_action(None), required_action("write")
(<Action.call: 'call'>, 'write')
>>> required_action("read")
Traceback (most recent call last):
...
ValueError: Required action 'read' is built in and reserved; declare a namespace action such as 'write'
Source code in src/woodglue/access.py
check(action, target='')
¶
Raise AccessDenied unless the current Caller may perform action on target.
Source code in src/woodglue/access.py
visible(names)
¶
Filter namespace names to those the current Caller can see.