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

Alerting: Handle custom dashboard permissions in migration service #74504

Merged
merged 6 commits into from Oct 12, 2023

Conversation

JacobsonMT
Copy link
Member

@JacobsonMT JacobsonMT commented Sep 7, 2023

Followup to #72702 and #74503.

Summary

This PR replaces the obsolete and broken code that checks for custom dashboard ACLs for creating new folders during migration. The new code now uses the new RBAC permissions models / logic.

The main focus of this PR is the new permissions logic, however there are also other changes that were necessary or prudent to carry-out at the same time. I'll list them first before detailing the main logic.

Fixes: #71529, #67448

Other Changes

Permissions Migration

Background

In legacy alerting, dashboard alert permissions were determined by both their dashboard and folder scoped permissions. This was done by taking the greater permission defined between the two for each mapped OrgRole, User, or Team.

Now, alert rules only have folder scoped permissions. This can lead to a situation where simply migrating a legacy alert from panelA in dashboardA in folderA to a UA alert rule in folderA will cause the newly created alert rule to be viewable/editable/adminable by different users than in legacy.

To prevent this, we must make a determination of when this situation will occur, and instead migrate the legacy alert into a newly created folder that will ensure the same mapped permissions (or as close to as possible).

Logic

To determine when a migrated alert rule will have different mapped permissions, we calculate two sets of permissions:

  • The set of mapped permissions of the dashboard (including those inherited from the parent folder).
  • The set of mapped permissions of the parent folder.

These permissions include mappings of:

  • OrgRoles -> Viewer/Editor/Admin
  • Teams -> Viewer/Editor/Admin
  • Users -> Viewer/Editor/Admin

When the dashboard permissions differ from the parent folder permissions we need to create a new folder to represent the equivalent access-level of the legacy dashboard. To reduce folder clutter, we create one such new folder per set of unique permissions in a parent folder, instead of one per dashboard with differing permissions.

Permission Calculation

Permissions for a dashboard or folder are calculated using the accesscontrol.ResourcePermissions assigned to the resource. These ResourcePermissions are determined by roles with actions that are scoped to the dashboard or folder (or wildcard). For example, this can be managed:builtins:editor:permissions -> [folders:read, dashboards:write, ...] or managed:users:1:permissions -> [...] or fixed:dashboards:writer -> [...].

We iterate over each of these ResourcePermission and map the set of actions (dashboardPermissions.MapActions) to its equivalent dashboards.PermissionType of either PERMISSION_VIEW, PERMISSION_EDIT, or PERMISSION_ADMIN. When we have multiple sources of permission level for an OrgRole, User, or Team, we take the highest one.

Included in the calculation are:

  • managed roles (ex. managed:users:1:permissions, managed:builtins:editor:permissions, managed:teams:1:permissions)
  • basic roles (ex. basic:admin, basic:editor)
  • and for dashboards, inherited roles from the parent folder.

Currently, we do not attempt to include fixed or custom roles in the calculation but do attempt to warn the user during migration if one of these roles had the potential to override the folder permissions. Note that these overrides would always be to increase permissions not decrease them, so the risk of giving users access to alerts they didn't have access to before is mitigated.

Also, we don't attempt to reconcile any missing datasource permissions. Users without access to the necessary datasources to read/edit an alert rule will need to obtain said access separate from the migration.

@JacobsonMT JacobsonMT requested a review from a team as a code owner September 7, 2023 05:19
@JacobsonMT JacobsonMT requested review from rwwiv and grobinson-grafana and removed request for a team September 7, 2023 05:19
@grafana-delivery-bot grafana-delivery-bot bot added this to the 10.2.x milestone Sep 7, 2023
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_fix_permissions branch from 3f90c83 to 72d25b2 Compare September 8, 2023 14:04
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_remove_vendored branch from 84509f2 to ba3d80f Compare September 11, 2023 17:14
@JacobsonMT JacobsonMT requested review from a team as code owners September 11, 2023 17:14
@JacobsonMT JacobsonMT requested review from mildwonkey, suntala and yangkb09 and removed request for a team September 11, 2023 17:14
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_fix_permissions branch from 72d25b2 to 8a91edd Compare September 11, 2023 17:15
@JacobsonMT
Copy link
Member Author

Updated main and rebased

@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_fix_permissions branch from 8a91edd to 4a4eb61 Compare September 11, 2023 22:55
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_remove_vendored branch from 93473b8 to 3b23991 Compare October 6, 2023 13:45
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_fix_permissions branch 2 times, most recently from 057bf9e to dbc6381 Compare October 8, 2023 19:17
Copy link
Contributor

@yuri-tceretian yuri-tceretian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

PanelID: &da.PanelID,
RuleGroup: name,
RuleGroup: fmt.Sprintf("%s - %d", info.DashboardName, da.PanelID), // Unique to this dash alert but still contains useful info.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not related to this PR scope. Probably makes sense to address it in a follow-up PR because RuleGroup length is limited and we need to check that the concatenated name does not exceed it, and if it exceeds - the trimmed version is still unique.

if err != nil {
return nil, err
}
l := log.New("dashboardTitle", dash.Title, "dashboardUID", dash.UID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure the context labels follow the guidelines. In this case it should be dashboardUid. It can be addressed it later.

// Any dashboard that has greater read/write permissions for an orgRole/team/user compared to its folder will necessitate creating a new folder with the same permissions as the dashboard.
func (om *OrgMigration) getOrCreateMigratedFolder(ctx context.Context, l log.Logger, dash *dashboards.Dashboard, parentFolder *folder.Folder) (*folder.Folder, error) {
// If parentFolder does not exist then the dashboard is an orphan. We migrate the alert to the general alerting folder.
if parentFolder == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's mention here in the comment that the "General Alerting" folder is available to only admins.


// Check if the dashboard has custom permissions. If it does, we need to create a new folder for it.
// This folder will be cached for re-use for each dashboard in the folder with the same permissions.
customFolders, ok := om.permissionsMap[parentFolder.ID]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think name "customFolders" is misleading here. It reflects the permissions of the parent folder and dashboards in this folder.

Copy link
Contributor

@rwwiv rwwiv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Base automatically changed from jacobsonmt/migration_remove_vendored to main October 11, 2023 16:22
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_fix_permissions branch 2 times, most recently from 3994d4e to e56e6e7 Compare October 11, 2023 16:29
Dashboard alert permissions were determined by both its dashboard and
folder scoped permissions, while UA alert rules only have folder
scoped permissions.

This means, when migrating an alert, we'll need to decide if the parent folder
is a correct location for the newly created alert rule so that users, teams,
and org roles have the same access to it as they did in legacy.

To do this, we translate both the folder and dashboard resource
permissions to two sets of SetResourcePermissionCommands. Each of these
encapsulates a mapping of all:

OrgRoles -> Viewer/Editor/Admin
Teams -> Viewer/Editor/Admin
Users -> Viewer/Editor/Admin

When the dashboard permissions (including those inherited from the parent
folder) differ from the parent folder permissions alone, we need to create a
new folder to represent the access-level of the legacy dashboard.

Compromises:

When determining the SetResourcePermissionCommands we only take into account
managed and basic roles. Fixed and custom roles introduce significant complexity
and synchronicity hurdles. Instead, we log a warning they had the potential to
override the newly created folder permissions.

Also, we don't attempt to reconcile datasource permissions that were
not necessary in legacy alerting. Users without access to the necessary
datasources to edit an alert rule will need to obtain said access separate from
the migration.
@JacobsonMT JacobsonMT force-pushed the jacobsonmt/migration_fix_permissions branch from e56e6e7 to dc6b396 Compare October 12, 2023 13:05
@JacobsonMT
Copy link
Member Author

/deploy-to-hg

@ephemeral-instances-bot
Copy link

  • Preparing your instance. A comment containing your instance's url will be added to this PR when the instance is ready.
  • Your instance will be ready in ~10 minutes.
  • Check the GitHub actions tab to follow the workflow progress
  • Slack channel: #proj-ephemeral-hg-instances
  • Building instance with jacobsonmt/migration_fix_permissions oss branch and main enterprise branch. How to choose a branch

@ephemeral-instances-bot
Copy link

@JacobsonMT JacobsonMT merged commit 5f48619 into main Oct 12, 2023
19 checks passed
@JacobsonMT JacobsonMT deleted the jacobsonmt/migration_fix_permissions branch October 12, 2023 22:12
@zerok zerok modified the milestones: 10.2.x, 10.2.0 Oct 23, 2023
darrenjaneczek pushed a commit that referenced this pull request Oct 23, 2023
…74504)

* Fix migration of custom dashboard permissions

Dashboard alert permissions were determined by both its dashboard and
folder scoped permissions, while UA alert rules only have folder
scoped permissions.

This means, when migrating an alert, we'll need to decide if the parent folder
is a correct location for the newly created alert rule so that users, teams,
and org roles have the same access to it as they did in legacy.

To do this, we translate both the folder and dashboard resource
permissions to two sets of SetResourcePermissionCommands. Each of these
encapsulates a mapping of all:

OrgRoles -> Viewer/Editor/Admin
Teams -> Viewer/Editor/Admin
Users -> Viewer/Editor/Admin

When the dashboard permissions (including those inherited from the parent
folder) differ from the parent folder permissions alone, we need to create a
new folder to represent the access-level of the legacy dashboard.

Compromises:

When determining the SetResourcePermissionCommands we only take into account
managed and basic roles. Fixed and custom roles introduce significant complexity
and synchronicity hurdles. Instead, we log a warning they had the potential to
override the newly created folder permissions.

Also, we don't attempt to reconcile datasource permissions that were
not necessary in legacy alerting. Users without access to the necessary
datasources to edit an alert rule will need to obtain said access separate from
the migration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Alerting: Alerts invisible after legacy migration after previous opt-out
4 participants