|
1 | 1 | import logging |
2 | 2 |
|
3 | | -from django.contrib.contenttypes.models import ContentType |
4 | | -from django.db.models.query import IntegrityError |
5 | | - |
6 | 3 | from ansible_base.jwt_consumer.common.auth import JWTAuthentication |
7 | | -from ansible_base.jwt_consumer.common.exceptions import InvalidService |
8 | | -from ansible_base.rbac.models import RoleDefinition, RoleUserAssignment |
9 | | -from ansible_base.resource_registry.models import Resource |
10 | 4 |
|
11 | 5 | logger = logging.getLogger('ansible_base.jwt_consumer.hub.auth') |
12 | 6 |
|
13 | 7 |
|
14 | 8 | class HubJWTAuth(JWTAuthentication): |
15 | | - |
16 | | - def get_galaxy_models(self): |
17 | | - '''This is separate from process_permissions purely for testability.''' |
18 | | - try: |
19 | | - from galaxy_ng.app.models import Organization, Team |
20 | | - except ImportError: |
21 | | - raise InvalidService("automation-hub") |
22 | | - |
23 | | - return Organization, Team |
24 | | - |
25 | | - def _apply_rbac_permissions(self, objects, object_roles, global_roles): |
26 | | - # Map teams in the JWT to Automation Hub groups. |
27 | | - Organization, Team = self.get_galaxy_models() |
28 | | - self.team_content_type = ContentType.objects.get_for_model(Team) |
29 | | - self.org_content_type = ContentType.objects.get_for_model(Organization) |
30 | | - |
31 | | - # TODO - galaxy does not have an org admin roledef yet |
32 | | - # admin_orgs = [] |
33 | | - |
34 | | - # TODO - galaxy does not have an org member roledef yet |
35 | | - # member_orgs = [] |
36 | | - |
37 | | - # The "shared" [!local] teams this user admins |
38 | | - admin_teams = [] |
39 | | - |
40 | | - # the teams this user should have a "shared" [!local] assignment to |
41 | | - member_teams = [] |
42 | | - |
43 | | - for role_name in object_roles.keys(): |
44 | | - if role_name.startswith('Team'): |
45 | | - for object_index in object_roles[role_name]['objects']: |
46 | | - team_data = objects['team'][object_index] |
47 | | - ansible_id = team_data['ansible_id'] |
48 | | - try: |
49 | | - team = Resource.objects.get(ansible_id=ansible_id).content_object |
50 | | - except Resource.DoesNotExist: |
51 | | - try: |
52 | | - team = self.common_auth.get_or_create_resource('team', team_data)[1] |
53 | | - except IntegrityError as e: |
54 | | - logger.warning( |
55 | | - f"Got integrity error ({e}) on {team_data}. Skipping team assignment. " |
56 | | - "Please make sure the sync task is running to prevent this warning in the future." |
57 | | - ) |
58 | | - continue |
59 | | - |
60 | | - if role_name == 'Team Admin': |
61 | | - admin_teams.append(team) |
62 | | - elif role_name == 'Team Member': |
63 | | - member_teams.append(team) |
64 | | - |
65 | | - for roledef_name, teams in [('Team Admin', admin_teams), ('Team Member', member_teams)]: |
66 | | - |
67 | | - # the "shared" "non-local" definition ... |
68 | | - try: |
69 | | - roledef = RoleDefinition.objects.get(name=roledef_name) |
70 | | - except RoleDefinition.DoesNotExist: |
71 | | - raise RoleDefinition.DoesNotExist(f'Expected JWT role {roledef_name} does not exist locally') |
72 | | - |
73 | | - # pks for filtering ... |
74 | | - team_pks = [team.pk for team in teams] |
75 | | - |
76 | | - # delete all assignments not defined by this jwt ... |
77 | | - for assignment in RoleUserAssignment.objects.filter(user=self.common_auth.user, role_definition=roledef).exclude(object_id__in=team_pks): |
78 | | - team = Team.objects.get(pk=assignment.object_id) |
79 | | - roledef.remove_permission(self.common_auth.user, team) |
80 | | - |
81 | | - # assign "non-local" for each team ... |
82 | | - for team in teams: |
83 | | - roledef.give_permission(self.common_auth.user, team) |
84 | | - |
85 | | - auditor_roledef = RoleDefinition.objects.get(name='Platform Auditor') |
86 | | - if "Platform Auditor" in global_roles: |
87 | | - auditor_roledef.give_global_permission(self.common_auth.user) |
88 | | - else: |
89 | | - auditor_roledef.remove_global_permission(self.common_auth.user) |
| 9 | + use_rbac_permissions = True |
0 commit comments