fix(web-app-zammad): mirror the RBAC group into Zammad roles on every OIDC login

Under SSO the platform administrator signed in and landed on #profile as a
Customer, so the design spec never reached #manage/users. Three defects
stacked.

The group-to-role map was written with Jinja in its mapping keys. Ansible
renders a mapping's values and leaves its keys alone, so the rendered Ruby
keys were literal {{ ... }} and could never match a group path. The map is now
one expression that computes its keys through rbac_group_path, the shape
meta/addons already uses elsewhere.

The initializer patched User.create_from_hash and User.update_from_hash behind
`next unless User.respond_to?`. Zammad 7.1.2 defines only create_from_hash!,
so both guards fell through in silence and nothing was installed: the
initializer's name does not appear once in the failing run's Rails log. Patch
Authorization.find_from_hash and Authorization.create_from_hash instead, which
sessions_controller calls before current_user_set, so first and subsequent
logins are both mirrored.

administrator mapped to Admin alone. Zammad's Admin carries no ticket
permission, and the Dashboard navbar entry is gated on ticket.agent, so an
Admin-only account has neither Dashboard nor Overviews. Grant Admin and Agent,
matching what the password path converges.

The write is defended rather than assumed. Zammad's has_roles concern runs
validate_roles, validate_agent_limit_by_role and last_admin_check_by_role on
collection assignment and they raise, which inside the omniauth callback would
answer the sign-in with 500 - a lockout, worse than the demotion being fixed.
The assignment is wrapped, resets the association and logs on refusal, and the
sign-in completes on the roles the account already has. Role lookup filters on
active and reports a missing role instead of silently granting the subset. A
map that rendered as a String is refused at boot, because map[path] would
otherwise be String#[] returning a substring and the feature would be a silent
no-op. The patch targets are checked by signature, not by respond_to?, since
respond_to? was exactly what hid defect two.

An absent groups claim leaves the roles untouched and logs, rather than
demoting every account to Customer on a misconfiguration. A present but empty
claim still means "in no group" and maps to Customer.

biber is unaffected: inventories/development/default.yml declares roles: [] for
it, so it is in no RBAC group, maps to Customer as before, and keeps
#customer_ticket_new.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-10-16 12:31:56 +02:00
1 parent 207cdc70ff
commit 1cea2f9ffe
6 files changed
+149 -52

No files matched your search

@@ -5,18 +5,17 @@ import unittest
from utils.cache.files import PROJECT_ROOT, read_text
from utils.cache.yaml import load_yaml_str
from utils.roles.mapping import ROLE_FILE_VARS_MAIN
from utils.roles.mapping import ROLE_FILE_META_RBAC, ROLE_FILE_VARS_MAIN
ROLE = PROJECT_ROOT / "roles/web-app-zammad"
VARS = ROLE / ROLE_FILE_VARS_MAIN
RBAC = ROLE / ROLE_FILE_META_RBAC
NATIVE = ROLE / "files/ruby/apply/administrator.rb"
CORE = ROLE / "tasks/00_core.yml"
TEMPLATE = ROLE / "templates/zz/oidc/group_role_mapping.rb.j2"
GRANTS_KEY = "ZAMMAD_OIDC_ROLE_GRANTS"
MAP_KEY = "ZAMMAD_OIDC_GROUP_ROLE_MAP"
ADMIN_ROLE = "administrator"
INITIALIZER = "zz/oidc/group_role_mapping.rb.j2"
RECREATE = "container-recreate"
MACHINE_ROLES = {"mcp"}
def native_administrator_roles() -> list[str]:
@@ -37,17 +36,8 @@ def role_vars() -> dict:
return load_yaml_str(read_text(str(VARS)))
def staged_initializer() -> dict:
"""Find the staging entry that renders the OIDC group-to-role initializer.
Returns:
The ``role_templates`` entry whose ``src`` is the initializer template.
"""
for task in load_yaml_str(read_text(str(CORE))):
for entry in (task.get("vars") or {}).get("role_templates") or []:
if entry.get("src") == INITIALIZER:
return entry
raise AssertionError(f"{CORE} no longer stages {INITIALIZER}")
def declared_rbac_roles() -> set[str]:
return set(load_yaml_str(read_text(str(RBAC)))["roles"])
class TestOidcGroupRoleMap(unittest.TestCase):
@@ -76,6 +66,15 @@ class TestOidcGroupRoleMap(unittest.TestCase):
"text, matches no group Keycloak sends, and every account that signs in "
"through the identity provider falls back to Customer",
)
self.assertRegex(
expression,
r"(?s)\A\{\{.*\}\}\Z",
f"{VARS}: {MAP_KEY} must be the bare expression, with no character outside "
"'{{' and '}}'. One stray leading or trailing space makes native templating "
"hand over the Python repr of the mapping as a string; the Ruby side then "
"indexes a String, String#[] answers with the matching substring instead of "
"nil, no role resolves and the whole mapping becomes a silent no-op",
)
for role in variables[GRANTS_KEY]:
self.assertIn(
f"role='{role}'",
@@ -84,15 +83,17 @@ class TestOidcGroupRoleMap(unittest.TestCase):
f"never resolves its path, while {GRANTS_KEY} declares roles for it",
)
def test_restaging_the_initializer_recreates_the_rails_tasks(self) -> None:
self.assertIn(
RECREATE,
staged_initializer().get("notify") or [],
f"{CORE}: staging {INITIALIZER} must notify '{RECREATE}'. A Rails "
"initializer binds its patches at boot, and the default 'compose-up' "
"redeploys a swarm service only when its spec changed, which a "
"bind-mounted file is not, so an existing stack would keep granting the "
"roles of the initializer it booted with",
def test_every_rbac_role_a_person_can_hold_has_a_grant(self) -> None:
self.assertEqual(
declared_rbac_roles() - MACHINE_ROLES,
set(role_vars()[GRANTS_KEY]),
f"{RBAC} declares the RBAC roles this application hands out, and every one of "
f"them that a person can hold needs a grant in {GRANTS_KEY}. An RBAC role "
"without a grant maps to nothing, falls through to the Customer default and "
f"demotes that account on every sign-in. {sorted(MACHINE_ROLES)} is excluded "
"because the MCP agent is a password account converged by "
"files/ruby/apply/mcp_bot.rb, never an account that arrives through the "
"identity provider",
)
def test_a_patch_target_that_disappeared_stops_the_boot(self) -> None:
@@ -109,6 +110,33 @@ class TestOidcGroupRoleMap(unittest.TestCase):
self.assertIn("raise ", source, reason)
self.assertNotIn("User.respond_to?", source, reason)
def test_an_unresolvable_grant_never_silently_downgrades(self) -> None:
source = read_text(str(TEMPLATE))
reason = (
"Role.where without an active filter, or without checking that every mapped name "
"resolved, hands back a subset: rename or deactivate Admin in the Zammad UI and "
"the administrator is set to Agent alone, after which the idempotence guard "
"reports the roles as already correct and never self-heals. A map that rendered "
"as a String rather than a Hash is the same class of silent no-op, because "
"String#[] answers with a substring instead of nil"
)
self.assertIn("Role.where(active: true,", source, reason)
self.assertIn("missing.any?", source, reason)
self.assertIn("is_a?(Hash)", source, reason)
def test_a_refused_role_write_does_not_break_the_sign_in(self) -> None:
source = read_text(str(TEMPLATE))
self.assertIn(
"rescue StandardError",
source,
"Zammad guards its roles association (app/models/concerns/has_roles.rb:10-13): "
"before_remove runs last_admin_check_by_role and before_add runs "
"validate_agent_limit_by_role and validate_roles, each of which raises. The "
"mapping runs inside SessionsController#create_omniauth, so an unrescued raise "
"answers the sign-in with 500 and locks the account out instead of leaving it "
"on the roles it already has",
)
if __name__ == "__main__":
unittest.main()