2022-03-03 08:05:47 -06:00
|
|
|
package guardian
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-03-14 11:12:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-03-03 08:05:47 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2022-03-09 10:57:50 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-03-03 08:05:47 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-03-03 08:05:47 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
var permissionMap = map[string]models.PermissionType{
|
|
|
|
"View": models.PERMISSION_VIEW,
|
|
|
|
"Edit": models.PERMISSION_EDIT,
|
|
|
|
"Admin": models.PERMISSION_ADMIN,
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ DashboardGuardian = new(AccessControlDashboardGuardian)
|
|
|
|
|
|
|
|
func NewAccessControlDashboardGuardian(
|
2022-08-10 04:56:48 -05:00
|
|
|
ctx context.Context, dashboardId int64, user *user.SignedInUser,
|
2022-05-10 08:48:47 -05:00
|
|
|
store sqlstore.Store, ac accesscontrol.AccessControl,
|
|
|
|
folderPermissionsService accesscontrol.FolderPermissionsService,
|
|
|
|
dashboardPermissionsService accesscontrol.DashboardPermissionsService,
|
2022-05-17 13:52:22 -05:00
|
|
|
dashboardService dashboards.DashboardService,
|
2022-03-03 08:05:47 -06:00
|
|
|
) *AccessControlDashboardGuardian {
|
|
|
|
return &AccessControlDashboardGuardian{
|
2022-05-10 08:48:47 -05:00
|
|
|
ctx: ctx,
|
|
|
|
log: log.New("dashboard.permissions"),
|
|
|
|
dashboardID: dashboardId,
|
|
|
|
user: user,
|
|
|
|
store: store,
|
|
|
|
ac: ac,
|
|
|
|
folderPermissionsService: folderPermissionsService,
|
|
|
|
dashboardPermissionsService: dashboardPermissionsService,
|
2022-05-17 13:52:22 -05:00
|
|
|
dashboardService: dashboardService,
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccessControlDashboardGuardian struct {
|
2022-05-10 08:48:47 -05:00
|
|
|
ctx context.Context
|
|
|
|
log log.Logger
|
|
|
|
dashboardID int64
|
|
|
|
dashboard *models.Dashboard
|
2022-08-10 04:56:48 -05:00
|
|
|
user *user.SignedInUser
|
2022-05-10 08:48:47 -05:00
|
|
|
store sqlstore.Store
|
|
|
|
ac accesscontrol.AccessControl
|
|
|
|
folderPermissionsService accesscontrol.FolderPermissionsService
|
|
|
|
dashboardPermissionsService accesscontrol.DashboardPermissionsService
|
2022-05-17 13:52:22 -05:00
|
|
|
dashboardService dashboards.DashboardService
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) CanSave() (bool, error) {
|
|
|
|
if err := a.loadDashboard(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.dashboard.IsFolder {
|
2022-05-04 09:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalPermission(dashboards.ActionFoldersWrite, dashboards.ScopeFoldersProvider.GetResourceScopeUID(a.dashboard.Uid)))
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-06-09 04:53:16 -05:00
|
|
|
return a.evaluate(
|
2022-05-04 09:12:09 -05:00
|
|
|
accesscontrol.EvalPermission(dashboards.ActionDashboardsWrite, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
2022-06-09 04:53:16 -05:00
|
|
|
)
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) CanEdit() (bool, error) {
|
|
|
|
if err := a.loadDashboard(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if setting.ViewersCanEdit {
|
|
|
|
return a.CanView()
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.dashboard.IsFolder {
|
2022-05-04 09:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalPermission(dashboards.ActionFoldersWrite, dashboards.ScopeFoldersProvider.GetResourceScopeUID(a.dashboard.Uid)))
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-06-09 04:53:16 -05:00
|
|
|
return a.evaluate(
|
2022-05-04 09:12:09 -05:00
|
|
|
accesscontrol.EvalPermission(dashboards.ActionDashboardsWrite, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
2022-06-09 04:53:16 -05:00
|
|
|
)
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) CanView() (bool, error) {
|
|
|
|
if err := a.loadDashboard(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.dashboard.IsFolder {
|
2022-05-04 09:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalPermission(dashboards.ActionFoldersRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(a.dashboard.Uid)))
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-06-09 04:53:16 -05:00
|
|
|
return a.evaluate(
|
2022-05-04 09:12:09 -05:00
|
|
|
accesscontrol.EvalPermission(dashboards.ActionDashboardsRead, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
2022-06-09 04:53:16 -05:00
|
|
|
)
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) CanAdmin() (bool, error) {
|
|
|
|
if err := a.loadDashboard(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.dashboard.IsFolder {
|
2022-03-14 11:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalAll(
|
2022-05-04 09:12:09 -05:00
|
|
|
accesscontrol.EvalPermission(dashboards.ActionFoldersPermissionsRead, dashboards.ScopeFoldersProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
|
|
|
accesscontrol.EvalPermission(dashboards.ActionFoldersPermissionsWrite, dashboards.ScopeFoldersProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
2022-03-03 08:05:47 -06:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:53:16 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalAll(
|
|
|
|
accesscontrol.EvalPermission(dashboards.ActionDashboardsPermissionsRead, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
|
|
|
accesscontrol.EvalPermission(dashboards.ActionDashboardsPermissionsWrite, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
2022-03-03 08:05:47 -06:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) CanDelete() (bool, error) {
|
|
|
|
if err := a.loadDashboard(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.dashboard.IsFolder {
|
2022-05-04 09:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalPermission(dashboards.ActionFoldersDelete, dashboards.ScopeFoldersProvider.GetResourceScopeUID(a.dashboard.Uid)))
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-06-09 04:53:16 -05:00
|
|
|
return a.evaluate(
|
2022-05-04 09:12:09 -05:00
|
|
|
accesscontrol.EvalPermission(dashboards.ActionDashboardsDelete, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(a.dashboard.Uid)),
|
2022-06-09 04:53:16 -05:00
|
|
|
)
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) CanCreate(folderID int64, isFolder bool) (bool, error) {
|
|
|
|
if isFolder {
|
2022-03-14 11:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalPermission(dashboards.ActionFoldersCreate))
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
2022-03-30 08:14:26 -05:00
|
|
|
folder, err := a.loadParentFolder(folderID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-05-04 09:12:09 -05:00
|
|
|
return a.evaluate(accesscontrol.EvalPermission(dashboards.ActionDashboardsCreate, dashboards.ScopeFoldersProvider.GetResourceScopeUID(folder.Uid)))
|
2022-03-14 11:12:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) evaluate(evaluator accesscontrol.Evaluator) (bool, error) {
|
|
|
|
ok, err := a.ac.Evaluate(a.ctx, a.user, evaluator)
|
|
|
|
if err != nil {
|
2022-08-11 06:28:55 -05:00
|
|
|
a.log.Debug("Failed to evaluate access control to folder or dashboard", "error", err, "userId", a.user.UserID, "id", a.dashboardID)
|
2022-03-14 11:12:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok && err == nil {
|
2022-08-11 06:28:55 -05:00
|
|
|
a.log.Debug("Access denied to folder or dashboard", "userId", a.user.UserID, "id", a.dashboardID, "permissions", evaluator.GoString())
|
2022-03-14 11:12:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok, err
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
func (a *AccessControlDashboardGuardian) CheckPermissionBeforeUpdate(permission models.PermissionType, updatePermissions []*models.DashboardACL) (bool, error) {
|
2022-03-03 08:05:47 -06:00
|
|
|
// always true for access control
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
// GetACL translate access control permissions to dashboard acl info
|
|
|
|
func (a *AccessControlDashboardGuardian) GetACL() ([]*models.DashboardACLInfoDTO, error) {
|
2022-03-03 08:05:47 -06:00
|
|
|
if err := a.loadDashboard(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-10 08:48:47 -05:00
|
|
|
var svc accesscontrol.PermissionsService
|
2022-03-03 08:05:47 -06:00
|
|
|
if a.dashboard.IsFolder {
|
2022-05-10 08:48:47 -05:00
|
|
|
svc = a.folderPermissionsService
|
|
|
|
} else {
|
|
|
|
svc = a.dashboardPermissionsService
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-05-18 02:13:41 -05:00
|
|
|
permissions, err := svc.GetPermissions(a.ctx, a.user, a.dashboard.Uid)
|
2022-03-03 08:05:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
acl := make([]*models.DashboardACLInfoDTO, 0, len(permissions))
|
2022-03-03 08:05:47 -06:00
|
|
|
for _, p := range permissions {
|
2022-03-17 11:08:51 -05:00
|
|
|
if !p.IsManaged {
|
2022-03-03 08:05:47 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
var role *org.RoleType
|
2022-03-03 08:05:47 -06:00
|
|
|
if p.BuiltInRole != "" {
|
2022-08-10 04:56:48 -05:00
|
|
|
tmp := org.RoleType(p.BuiltInRole)
|
2022-03-03 08:05:47 -06:00
|
|
|
role = &tmp
|
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
acl = append(acl, &models.DashboardACLInfoDTO{
|
2022-03-03 08:05:47 -06:00
|
|
|
OrgId: a.dashboard.OrgId,
|
|
|
|
DashboardId: a.dashboard.Id,
|
|
|
|
FolderId: a.dashboard.FolderId,
|
|
|
|
Created: p.Created,
|
|
|
|
Updated: p.Updated,
|
|
|
|
UserId: p.UserId,
|
|
|
|
UserLogin: p.UserLogin,
|
|
|
|
UserEmail: p.UserEmail,
|
|
|
|
TeamId: p.TeamId,
|
|
|
|
TeamEmail: p.TeamEmail,
|
|
|
|
Team: p.Team,
|
|
|
|
Role: role,
|
|
|
|
Permission: permissionMap[svc.MapActions(p)],
|
|
|
|
PermissionName: permissionMap[svc.MapActions(p)].String(),
|
|
|
|
Uid: a.dashboard.Uid,
|
|
|
|
Title: a.dashboard.Title,
|
|
|
|
Slug: a.dashboard.Slug,
|
|
|
|
IsFolder: a.dashboard.IsFolder,
|
|
|
|
Url: a.dashboard.GetUrl(),
|
|
|
|
Inherited: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return acl, nil
|
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
func (a *AccessControlDashboardGuardian) GetACLWithoutDuplicates() ([]*models.DashboardACLInfoDTO, error) {
|
|
|
|
return a.GetACL()
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
func (a *AccessControlDashboardGuardian) GetHiddenACL(cfg *setting.Cfg) ([]*models.DashboardACL, error) {
|
|
|
|
var hiddenACL []*models.DashboardACL
|
2022-03-03 11:29:39 -06:00
|
|
|
if a.user.IsGrafanaAdmin {
|
|
|
|
return hiddenACL, nil
|
|
|
|
}
|
|
|
|
|
2022-07-18 08:14:58 -05:00
|
|
|
existingPermissions, err := a.GetACL()
|
2022-03-03 11:29:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return hiddenACL, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range existingPermissions {
|
|
|
|
if item.Inherited || item.UserLogin == a.user.Login {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, hidden := cfg.HiddenUsers[item.UserLogin]; hidden {
|
2022-07-18 08:14:58 -05:00
|
|
|
hiddenACL = append(hiddenACL, &models.DashboardACL{
|
2022-03-03 11:29:39 -06:00
|
|
|
OrgID: item.OrgId,
|
|
|
|
DashboardID: item.DashboardId,
|
|
|
|
UserID: item.UserId,
|
|
|
|
TeamID: item.TeamId,
|
|
|
|
Role: item.Role,
|
|
|
|
Permission: item.Permission,
|
|
|
|
Created: item.Created,
|
|
|
|
Updated: item.Updated,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hiddenACL, nil
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccessControlDashboardGuardian) loadDashboard() error {
|
|
|
|
if a.dashboard == nil {
|
2022-08-11 06:28:55 -05:00
|
|
|
query := &models.GetDashboardQuery{Id: a.dashboardID, OrgId: a.user.OrgID}
|
2022-05-17 13:52:22 -05:00
|
|
|
if err := a.dashboardService.GetDashboard(a.ctx, query); err != nil {
|
2022-03-03 08:05:47 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.dashboard = query.Result
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:14:26 -05:00
|
|
|
func (a *AccessControlDashboardGuardian) loadParentFolder(folderID int64) (*models.Dashboard, error) {
|
|
|
|
if folderID == 0 {
|
|
|
|
return &models.Dashboard{Uid: accesscontrol.GeneralFolderUID}, nil
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
folderQuery := &models.GetDashboardQuery{Id: folderID, OrgId: a.user.OrgID}
|
2022-05-17 13:52:22 -05:00
|
|
|
if err := a.dashboardService.GetDashboard(a.ctx, folderQuery); err != nil {
|
2022-03-30 08:14:26 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return folderQuery.Result, nil
|
|
|
|
}
|