Files
computer-playbook/tests/unit/python/utils/roles/test_rbac_scoped.py
T
kevinveenbirkenbachandClaude Opus 5 7162b7bf4c Merge fork/feature/svc-net-tor into feature/ai-agent-employees
834 commits from the Tor onion-service branch against 299 of ours, from merge
base 7b646bdece: 127 conflicted files, 3506 files changed.

The two sides are largely orthogonal. Theirs adds svc-net-tor and a `tor` service
across the role tree, plus two sweeping refactors: shipped scripts sorted by
language (files/ruby/, files/python/, files/shell/, scripts/lint/*.sh) and the
unit suite moved under tests/unit/python/. Ours is requirement 036, the native AI
gateway, plus the MCP role work. Most conflicts were both sides appending to the
same service map, resolved as unions.

Resolutions that were not unions:

Credentials moved namespace. Theirs renamed meta/schema.yml to meta/secrets.yml
and re-addressed credentials as secrets.credentials.<name>. Git carried our
litellm_api_key blocks through the rename, but every consumer we added still read
'credentials.<name>', which no longer resolves. All 45 call sites are migrated.

plugins/filter/container_volumes.py is deleted by theirs and relocated to
plugins/lookup/container_volumes.py, where it carries the same tmpfs defect our
f85996b43 had just fixed: a hard-coded {"size": ...} that silently drops a per
mount `mode`. The fix is re-applied in the relocated file, as that commit's merge
note anticipated.

compose.yml keeps only INFINITO_PLAYWRIGHT_REPORTS_BASE_DIR. Theirs also forwards
INFINITO_PLAYWRIGHT_STAGE_BASE_DIR; that line is deliberately dropped, per the
analysis recorded in 45804e9ad -- STAGE has a drift-proof role-vars fallback,
while forwarding it promotes a setdefault-pinnable .env value above that SPOT.

tasks/stages/01_constructor.yml takes their include, ./tasks/utils/setup/group.yml,
because their refactor removed tasks/groups/*-roles.yml, leaving our include
target nonexistent. Our task name is kept, since svc-virt is in the loop.

Makefile swarm-zombie is a union: their validate plus teardown, then our
lab_subnet cleanup, which their side dropped while leaving lab_subnet.sh as the
only orphaned reference in the file.

01_bootstrap.sh keeps our deterministic --ip pinning, which distinguishes an
already-attached node from a real failure, over their `|| true`.

Their language-sorted layout orphaned references our side still held: zammad's
two scripts/apply/*.rb, jenkins' java-with-ca-trust.sh, and the notify topics
their handlers renamed to hyphenated form. All corrected.

roles/sys-svc-compose/files/shell/swarm/stack_ready.sh gains our latched-update
block: paused, rollback_paused, rollback_started and rollback_completed exit 2
rather than polling a state that cannot resolve without another deploy.

config_from_run is unioned to (title, logged, jobs); the production caller in
trigger/__main__.py now passes jobs, without which our suite suppression would
have been inert.

READMEs are regenerated rather than hand-merged: 15 LiteLLM rows and 91 Tor rows
now derive from the merged metadata, which is itself evidence the meta merge is
coherent.

Verified: 5161 unit tests pass. The single failure, test_query.py TestSortSpec,
also fails on fork/feature/svc-net-tor at b51d2f9919, measured in a baseline
worktree -- so this merge introduces no new unit-test failure. Two modules are
excluded from that run because `baudolo`, a dependency their branch introduces,
is not installed on this host.
Not verified: no deploy has run against the merged tree, and make quality has not
been run end to end here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 16:32:30 +02:00

142 lines
5.6 KiB
Python

"""The application-scoped role boundary.
``mcp`` grants reach whatever the deployment issued the provider's credentials
for, not the caller's own data, so holding it on one application must never
imply holding it on another. ``APPLICATION_SCOPED_ROLES`` is what keeps an
unscoped ``roles: [mcp]`` from becoming every deployed application at once, and
the Keycloak realm builder, the LDAP entry builder and the Open WebUI reconciler
all resolve membership through this one module.
"""
from __future__ import annotations
import unittest
from typing import ClassVar
from utils.roles.rbac.scoped import (
APPLICATION_SCOPED_ROLES,
granted_roles,
members_with_role,
)
class TestGrantedRoles(unittest.TestCase):
def test_a_scoped_grant_reaches_only_the_named_application(self) -> None:
user = {"application_roles": {"web-app-baserow": ["mcp"]}}
self.assertEqual({"mcp", "mcp-reader"}, granted_roles(user, "web-app-baserow"))
self.assertEqual(set(), granted_roles(user, "web-app-n8n"))
def test_an_unscoped_scoped_role_grants_nothing_anywhere(self) -> None:
user = {"roles": ["mcp"]}
for application_id in ("web-app-baserow", "web-app-n8n"):
with self.subTest(application_id=application_id):
self.assertEqual(set(), granted_roles(user, application_id))
def test_an_unscoped_ordinary_role_reaches_every_application(self) -> None:
user = {"roles": ["administrator"]}
for application_id in ("web-app-baserow", "web-app-n8n"):
with self.subTest(application_id=application_id):
self.assertEqual({"administrator"}, granted_roles(user, application_id))
def test_scoped_and_unscoped_grants_combine_on_the_named_application(self) -> None:
user = {
"roles": ["administrator"],
"application_roles": {"web-app-baserow": ["mcp"]},
}
self.assertEqual(
{"administrator", "mcp", "mcp-reader"},
granted_roles(user, "web-app-baserow"),
)
self.assertEqual({"administrator"}, granted_roles(user, "web-app-n8n"))
def test_two_applications_carry_independent_scoped_grants(self) -> None:
user = {
"application_roles": {
"web-app-baserow": ["mcp"],
"web-app-n8n": ["administrator"],
}
}
self.assertEqual({"mcp", "mcp-reader"}, granted_roles(user, "web-app-baserow"))
self.assertEqual({"administrator"}, granted_roles(user, "web-app-n8n"))
def test_a_user_without_any_declaration_holds_nothing(self) -> None:
self.assertEqual(set(), granted_roles({}, "web-app-baserow"))
def test_null_declarations_are_treated_as_empty(self) -> None:
user = {"roles": None, "application_roles": None}
self.assertEqual(set(), granted_roles(user, "web-app-baserow"))
def test_a_legacy_mcp_grant_confers_reading_not_writing(self) -> None:
user = {"application_roles": {"web-app-baserow": ["mcp"]}}
granted = granted_roles(user, "web-app-baserow")
self.assertIn("mcp-reader", granted)
self.assertNotIn("mcp-writer", granted)
def test_an_explicit_writer_grant_is_kept(self) -> None:
user = {"application_roles": {"web-app-baserow": ["mcp-writer"]}}
self.assertEqual({"mcp-writer"}, granted_roles(user, "web-app-baserow"))
def test_mcp_is_application_scoped(self) -> None:
self.assertIn("mcp", APPLICATION_SCOPED_ROLES)
class TestMembersWithRole(unittest.TestCase):
USERS: ClassVar[dict] = {
"biber": {"application_roles": {"web-app-baserow": ["mcp"]}},
"otter": {"roles": ["mcp"]},
"admin": {"roles": ["administrator"]},
}
def test_only_the_scoped_holder_is_a_member(self) -> None:
self.assertEqual(
["biber"], members_with_role(self.USERS, "web-app-baserow", "mcp-reader")
)
def test_the_unscoped_holder_is_a_member_of_no_application(self) -> None:
self.assertEqual([], members_with_role(self.USERS, "web-app-n8n", "mcp"))
def test_the_declared_username_wins_over_the_key(self) -> None:
users = {
"biber": {
"username": "biber@infinito.example",
"application_roles": {"web-app-baserow": ["mcp"]},
}
}
self.assertEqual(
["biber@infinito.example"],
members_with_role(users, "web-app-baserow", "mcp-reader"),
)
def test_members_are_sorted_so_the_grant_diff_is_stable(self) -> None:
users = {
name: {"application_roles": {"web-app-baserow": ["mcp"]}}
for name in ("otter", "biber", "adler")
}
self.assertEqual(
["adler", "biber", "otter"],
members_with_role(users, "web-app-baserow", "mcp-reader"),
)
def test_no_users_yields_no_members(self) -> None:
self.assertEqual([], members_with_role(None, "web-app-baserow", "mcp"))
def test_a_null_user_entry_does_not_abort_the_scan(self) -> None:
users = {"ghost": None, "biber": {"application_roles": {"app": ["mcp"]}}}
self.assertEqual(["biber"], members_with_role(users, "app", "mcp-reader"))
class TestMcpRoleVocabulary(unittest.TestCase):
def test_both_mcp_roles_are_application_scoped(self) -> None:
from utils.roles.rbac.scoped import APPLICATION_SCOPED_ROLES, MCP_ROLES
for role in MCP_ROLES:
self.assertIn(role, APPLICATION_SCOPED_ROLES)
def test_a_writer_grant_does_not_confer_the_reader_name(self) -> None:
user = {"application_roles": {"app": ["mcp-writer"]}}
self.assertEqual({"mcp-writer"}, granted_roles(user, "app"))
if __name__ == "__main__":
unittest.main()