Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RBAC] failed grouprole permission migrations fix #2274

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions galaxy_ng/app/migrations/_dab_rbac.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging

from django.apps import apps as global_apps
from django.contrib.contenttypes.models import ContentType
from rest_framework.exceptions import ValidationError

from ansible_base.rbac.management import create_dab_permissions
from ansible_base.rbac.migrations._utils import give_permissions
from ansible_base.rbac.validators import permissions_allowed_for_role, combine_values


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -34,6 +38,12 @@ def create_permissions_as_operation(apps, schema_editor):


def split_pulp_roles(apps, schema_editor):
'''
For every user&group role that is tied to a specific content object,
split the role out into a new single content type role with permissions
that are only relevant to that content object. Afterwards, swap the
[User|Group]Role's .role with the new role.
'''
Role = apps.get_model('core', 'Role')
UserRole = apps.get_model('core', 'UserRole')
GroupRole = apps.get_model('core', 'GroupRole')
Expand All @@ -43,12 +53,32 @@ def split_pulp_roles(apps, schema_editor):
for assignment_cls in (UserRole, GroupRole):
for pulp_assignment in assignment_cls.objects.filter(role=corerole, content_type__isnull=False):
if pulp_assignment.content_type_id not in split_roles:

# Get all permissions relevant to this content model.
# If any model (like synclist) hasn't been registered in the permission
# system, it should not be split/recreated ...
cls = apps.get_model(pulp_assignment.content_type.app_label, pulp_assignment.content_type.model)
try:
ct_codenames = combine_values(permissions_allowed_for_role(cls))
except ValidationError:
continue

# Make a new role for this special content model
new_data = {
'description': corerole.description,
'name': f'{corerole.name}_{pulp_assignment.content_type.model}'
}
new_role = Role(**new_data)
new_role.save()

# Add the necesarry permissions to the new role ...
for perm in pulp_assignment.role.permissions.all():
# The pulp role may have had permissions related to some other
# content model we're not interested in, so we will skip adding those.
if ct_codenames and perm.codename not in ct_codenames:
continue
new_role.permissions.add(perm)
jctanner marked this conversation as resolved.
Show resolved Hide resolved

split_roles[pulp_assignment.content_type_id] = new_role

pulp_assignment.role = split_roles[pulp_assignment.content_type_id]
Expand Down Expand Up @@ -112,11 +142,10 @@ def migrate_role_assignments(apps, schema_editor):
if not rd:
continue

# FIXME - why?
if not hasattr(group_role.group, 'team'):
actor = Team.objects.filter(group=group_role.group).first()
if actor is None:
continue

actor = group_role.group.team
if not group_role.object_id:
RoleTeamAssignment.objects.create(role_definition=rd, team=actor)
else:
Expand Down
Loading