Skip to content

Commit 3e01bb7

Browse files
committed
Handle team update missing in some cases
1 parent d5e0875 commit 3e01bb7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

ansible_base/rbac/triggers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ def bulk_rbac_caching(memory_safe=False):
110110
logger.info(f'Performing bulk RBAC cache update: teams={needs_team_update}, object_roles={len(object_roles_to_update)}')
111111
if needs_team_update:
112112
compute_team_member_roles()
113-
if object_roles_to_update:
113+
# When team memberships change, always recompute object role permissions
114+
# to ensure the permission cache reflects new team relationships
115+
compute_object_role_permissions()
116+
elif object_roles_to_update:
117+
# Only object roles changed, no team updates needed
114118
compute_object_role_permissions(object_roles=object_roles_to_update)
115119

116120

test_app/tests/rbac/test_triggers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,41 @@ def test_bulk_caching_memory_safe_mixed_operations(self, rando, team, inv_rd, or
474474

475475
# Should call full recomputation since we had net updates
476476
mock_obj_update.assert_called_once_with()
477+
478+
def test_bulk_caching_with_removal(self, rando, inv_rd, inventory):
479+
"""Test bulk caching when object role gets deleted during removal"""
480+
# First give permission normally
481+
inv_rd.give_permission(rando, inventory)
482+
483+
with patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update:
484+
485+
with bulk_rbac_caching():
486+
# Remove permission in bulk mode - this will delete the object role
487+
inv_rd.remove_permission(rando, inventory)
488+
mock_obj_update.assert_not_called()
489+
490+
# Should not be called since object role was deleted (nothing to update)
491+
mock_obj_update.assert_not_called()
492+
493+
def test_bulk_caching_team_only_updates_fix(self, rando, team, member_rd):
494+
"""Test that team-only updates properly call compute_object_role_permissions"""
495+
with (
496+
patch('ansible_base.rbac.triggers.compute_team_member_roles') as mock_team_update,
497+
patch('ansible_base.rbac.triggers.compute_object_role_permissions') as mock_obj_update,
498+
):
499+
500+
with bulk_rbac_caching():
501+
# This assignment affects team membership but doesn't create a new object role
502+
# that would be added to object_roles_to_update
503+
member_rd.give_permission(rando, team)
504+
505+
# Should not be called during bulk mode
506+
mock_team_update.assert_not_called()
507+
mock_obj_update.assert_not_called()
508+
509+
# CRITICAL: Both should be called when team updates happen
510+
# This ensures the permission cache reflects new team relationships
511+
mock_team_update.assert_called_once()
512+
# This is the bug fix - compute_object_role_permissions should be called
513+
# even when only team updates occurred (no object_roles_to_update)
514+
mock_obj_update.assert_called_once_with()

0 commit comments

Comments
 (0)