Files
ai-agent/backend/authz/scopes.py
gabrielvidalandClaude Opus 5.5 aee457cdbf Merge origin/main into hub-mcp-plugin
main moved cron jobs into the cron-jobs plugin (#37) and retired the core
Send later doors for the scheduled-conversations plugin's commands (#36),
both touching the core MCP files this branch moves into the hub-mcp plugin.

- The MCP stays out of core: backend/mcpserver/, cli/mcp_tools.py,
  cli/mcp_server.py, their tests and business/aiApps/ stay deleted (main's
  edits to them — no schedule group, no cron / schedule tools — are the
  plugin's to apply).
- Cron and Send later follow main: backend/cron/routes.py gone (its
  by-name worker pick is runs.start's at fire time now), no ai-agent
  later / scheduled / unschedule — so a worker's narrowed CLI drops them
  from its command lists (workers/cli.py) and schedules nothing; Full
  reaches the plugin's commands.
- authz: neither runs.schedule nor mcp.connect.
- Settings: neither Scheduled jobs nor AI apps in the registry; both old
  addresses redirect to their plugin's page.
- Regenerated openapi.json, src/generated, rolesCatalog.json,
  matrix.golden.txt and telemetry features.json.
- cli/test_library_surface.py: parse_when left with `ai-agent later`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:14:16 +01:00

67 lines
3.0 KiB
Python

"""Credential scopes: what a caller that *acts for* someone may do.
A scope only ever removes: the caller keeps the route permissions it holds
that the scope lists — all of them for ``full``, safe methods (GET/HEAD) only
for ``read`` — and every feature permission it has (those decide what a run
*gets*, and a run a CLI call starts must get what its person's runs get).
============= ================================================== =====================
scope who holds it narrows
============= ================================================== =====================
cli a guest run's ``ai-agent`` CLI (the guard's that guest
broker), a plugin acting for a guest (hub.api)
worker.read a worker's ``ai-agent`` CLI, switch on *Read* the owner, + one profile
worker.run the same, on *Read + run here* the owner, + one profile
bot the ``READONLY_USERS`` Authelia account (the the owner
screenshot skills' ``claude`` bot)
phone ``READONLY_API_KEY`` (the desk phone) the owner
metrics ``METRICS_TOKEN`` (Prometheus' scrape) the owner
============= ================================================== =====================
The ``bot`` reads what the Admin role reads, minus people, security and the
machine — no guest's records, no security log, no Claude logins.
"""
from __future__ import annotations
import dataclasses
from authz import catalog as cat
SAFE_METHODS = frozenset({"GET", "HEAD", "OPTIONS"})
@dataclasses.dataclass(frozen=True)
class Scope:
name: str
full: frozenset = frozenset()
read: frozenset = frozenset()
# Mutating methods refused outright, whatever the permission.
read_only: bool = False
SCOPES: dict[str, Scope] = {s.name: s for s in (
Scope("cli",
full=frozenset({"conversations.read", "conversations.edit", "runs.start",
"inbox.read", "cron.own", "plugins.use"}),
read=frozenset({"whoami", "app.read", "projects.read"})),
Scope("worker.read",
full=frozenset({"conversations.read", "conversations.past"}),
read=frozenset({"app.read", "workers.read", "usage.read"})),
Scope("worker.run",
full=frozenset({"conversations.read", "conversations.past", "conversations.edit",
"runs.start"}),
read=frozenset({"app.read", "workers.read", "usage.read"})),
Scope("bot", read=(cat.ADMIN_PERMS - {"users.manage", "security.manage"}),
read_only=True),
Scope("phone", read=frozenset({"notifications.ledger"}), read_only=True),
Scope("metrics", read=frozenset({"metrics.read"}), read_only=True),
)}
def narrow(perms: frozenset[str], scope: Scope) -> tuple[frozenset[str], frozenset[str]]:
"""``(full, read_only)`` permission sets of a caller holding ``perms``
through ``scope``."""
features = perms & cat.FEATURE_PERMS
return (features | (perms & scope.full)), (perms & scope.read)