feat: update RBAC permissioning to support grafana-irm-app#5149
Conversation
| if organization.is_rbac_permissions_enabled: | ||
| # it is more efficient to check permissions on the subset of users filtered above | ||
| # than performing a regex query for the required permission | ||
| users_found_in_ical = [u for u in users_found_in_ical if {"action": required_permission.value} in u.permissions] | ||
| else: | ||
| users_found_in_ical = users_found_in_ical.filter(role__lte=required_permission.fallback_role.value) | ||
|
|
||
| return list(users_found_in_ical) |
There was a problem hiding this comment.
this makes things much cleaner. users_in_ical shouldn't need to know anything about how to conditionally check permissions (ie. is_rbac_permissions_enabled) or the structure of RBAC permissions ({"action": required_permission.value}).
See the new LegacyAccessControlCompatiblePermission.user_has_permission method
| self.value = f"{prefix}.{resource.value}:{action.value}" | ||
| self.fallback_role = fallback_role | ||
|
|
||
| def user_has_permission(self, user: "User") -> bool: |
|
|
||
|
|
||
| def get_permission_from_permission_string(perm: str) -> typing.Optional[LegacyAccessControlCompatiblePermission]: | ||
| def get_permission_from_permission_string( |
There was a problem hiding this comment.
(see here for more context on how this is used; tldr; query filtering for the user's endpoint)
… github.com:grafana/oncall into jorlando/update-rbac-permissions-to-support-irm-app
matiasb
left a comment
There was a problem hiding this comment.
LGTM, minor comments added.
| permission_class_value = permission_class.value | ||
| irm_permission_value = convert_oncall_permission_to_irm(permission_class) | ||
|
|
||
| if permission_class_value == perm or organization.is_grafana_irm_enabled and irm_permission_value == perm: |
There was a problem hiding this comment.
Precedence works ok here, but maybe add parenthesis grouping the and conditions to make it easier to read?
There was a problem hiding this comment.
decided to get rid of this method in favour of ALL_PERMISSION_NAME_TO_CLASS_MAP (dict lookup should be more performant than the for loop)
| @@ -90,14 +93,9 @@ def users_in_ical( | |||
| (Q(username__in=usernames_from_ical) | Q(email__lower__in=emails_from_ical)) | |||
| ).distinct() | |||
There was a problem hiding this comment.
Wondering if we should do a select_related for organization here, since we will be accessing u.organization for each user when checking user_has_permission below.
What this PR does
Closes https://github.com/grafana/irm/issues/31 (and supersedes #4784)
Main changes:
apps.api.permissions.user_is_authorizedto check the value oforganization.is_grafana_irm_enabled. If it is, we check for the presence ofgrafana-irm-appprefixed RBAC permissions rather thangrafana-oncall-appengine/apps/api/tests/test_permissions.py(bulk of the changes in the PR)apps.user_management.models.User.build_permissions_queryto aUserQuerySetmethod insteadChecklist
pr:no public docsPR label added if not required)release:). These labels dictate how your PR willshow up in the autogenerated release notes.