Skip to content

Commit 4738c83

Browse files
authored
Fix object-level permission bugs with DAB RBAC system (#15284)
* Fix object-level permission bugs with DAB RBAC system * Fix NT organization change regression * Mark tests to AAP number
1 parent 13dcea0 commit 4738c83

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

awx/main/access.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ class InstanceGroupAccess(BaseAccess):
598598
- a superuser
599599
- admin role on the Instance group
600600
I can add/delete Instance Groups:
601-
- a superuser(system administrator)
601+
- a superuser(system administrator), because these are not org-scoped
602602
I can use Instance Groups when I have:
603603
- use_role on the instance group
604604
"""
@@ -627,7 +627,7 @@ def can_admin(self, obj):
627627
def can_delete(self, obj):
628628
if obj.name in [settings.DEFAULT_EXECUTION_QUEUE_NAME, settings.DEFAULT_CONTROL_PLANE_QUEUE_NAME]:
629629
return False
630-
return self.user.is_superuser
630+
return self.user.has_obj_perm(obj, 'delete')
631631

632632

633633
class UserAccess(BaseAccess):
@@ -2628,7 +2628,7 @@ def can_delete(self, obj):
26282628

26292629
class NotificationTemplateAccess(BaseAccess):
26302630
"""
2631-
I can see/use a notification_template if I have permission to
2631+
Run standard logic from DAB RBAC
26322632
"""
26332633

26342634
model = NotificationTemplate
@@ -2649,10 +2649,7 @@ def can_add(self, data):
26492649

26502650
@check_superuser
26512651
def can_change(self, obj, data):
2652-
if obj.organization is None:
2653-
# only superusers are allowed to edit orphan notification templates
2654-
return False
2655-
return self.check_related('organization', Organization, data, obj=obj, role_field='notification_admin_role', mandatory=True)
2652+
return self.user.has_obj_perm(obj, 'change') and self.check_related('organization', Organization, data, obj=obj, role_field='notification_admin_role')
26562653

26572654
def can_admin(self, obj, data):
26582655
return self.can_change(obj, data)
@@ -2662,9 +2659,7 @@ def can_delete(self, obj):
26622659

26632660
@check_superuser
26642661
def can_start(self, obj, validate_license=True):
2665-
if obj.organization is None:
2666-
return False
2667-
return self.user in obj.organization.notification_admin_role
2662+
return self.can_change(obj, None)
26682663

26692664

26702665
class NotificationAccess(BaseAccess):

awx/main/tests/functional/api/test_instance_group.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ def fn(hostname, node_type):
3232
return fn
3333

3434

35-
@pytest.fixture
36-
def instance_group(job_factory):
37-
ig = InstanceGroup(name="east")
38-
ig.save()
39-
return ig
40-
41-
4235
@pytest.fixture
4336
def containerized_instance_group(instance_group, kube_credential):
4437
ig = InstanceGroup(name="container")

awx/main/tests/functional/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# AWX
2222
from awx.main.models.projects import Project
23-
from awx.main.models.ha import Instance
23+
from awx.main.models.ha import Instance, InstanceGroup
2424

2525
from rest_framework.test import (
2626
APIRequestFactory,
@@ -730,6 +730,11 @@ def jt_linked(organization, project, inventory, machine_credential, credential,
730730
return jt
731731

732732

733+
@pytest.fixture
734+
def instance_group():
735+
return InstanceGroup.objects.create(name="east")
736+
737+
733738
@pytest.fixture
734739
def workflow_job_template(organization):
735740
wjt = WorkflowJobTemplate.objects.create(name='test-workflow_job_template', organization=organization)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
from awx.main.access import InstanceGroupAccess, NotificationTemplateAccess
4+
5+
from ansible_base.rbac.models import RoleDefinition
6+
7+
8+
@pytest.mark.django_db
9+
def test_instance_group_object_role_delete(rando, instance_group, setup_managed_roles):
10+
"""Basic functionality of IG object-level admin role function AAP-25506"""
11+
rd = RoleDefinition.objects.get(name='InstanceGroup Admin')
12+
rd.give_permission(rando, instance_group)
13+
access = InstanceGroupAccess(rando)
14+
assert access.can_delete(instance_group)
15+
16+
17+
@pytest.mark.django_db
18+
def test_notification_template_object_role_change(rando, notification_template, setup_managed_roles):
19+
"""Basic functionality of NT object-level admin role function AAP-25493"""
20+
rd = RoleDefinition.objects.get(name='NotificationTemplate Admin')
21+
rd.give_permission(rando, notification_template)
22+
access = NotificationTemplateAccess(rando)
23+
assert access.can_change(notification_template, {'name': 'new name'})

awx/main/tests/functional/test_rbac_notifications.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ def test_notification_template_access_org_user(notification_template, user):
9999
@pytest.mark.django_db
100100
def test_notificaiton_template_orphan_access_org_admin(notification_template, organization, org_admin):
101101
notification_template.organization = None
102+
notification_template.save(update_fields=['organization'])
102103
access = NotificationTemplateAccess(org_admin)
104+
assert not org_admin.has_obj_perm(notification_template, 'change')
103105
assert not access.can_change(notification_template, {'organization': organization.id})
104106

105107

0 commit comments

Comments
 (0)