2021-12-20 02:52:24 -06:00
|
|
|
package resourcepermissions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2021-12-20 02:52:24 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2021-12-20 02:52:24 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
type api struct {
|
|
|
|
ac accesscontrol.AccessControl
|
|
|
|
router routing.RouteRegister
|
|
|
|
service *Service
|
|
|
|
permissions []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newApi(ac accesscontrol.AccessControl, router routing.RouteRegister, manager *Service) *api {
|
|
|
|
permissions := make([]string, 0, len(manager.permissions))
|
|
|
|
// reverse the permissions order for display
|
|
|
|
for i := len(manager.permissions) - 1; i >= 0; i-- {
|
|
|
|
permissions = append(permissions, manager.permissions[i])
|
|
|
|
}
|
|
|
|
return &api{ac, router, manager, permissions}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) registerEndpoints() {
|
2022-04-28 03:46:18 -05:00
|
|
|
auth := accesscontrol.Middleware(a.ac)
|
2022-12-02 06:19:14 -06:00
|
|
|
licenseMW := a.service.options.LicenseMW
|
|
|
|
if licenseMW == nil {
|
|
|
|
licenseMW = nopMiddleware
|
|
|
|
}
|
2022-04-29 04:05:51 -05:00
|
|
|
|
2021-12-20 02:52:24 -06:00
|
|
|
a.router.Group(fmt.Sprintf("/api/access-control/%s", a.service.options.Resource), func(r routing.RouteRegister) {
|
2022-04-05 07:28:23 -05:00
|
|
|
actionRead := fmt.Sprintf("%s.permissions:read", a.service.options.Resource)
|
|
|
|
actionWrite := fmt.Sprintf("%s.permissions:write", a.service.options.Resource)
|
2022-03-21 11:58:18 -05:00
|
|
|
scope := accesscontrol.Scope(a.service.options.Resource, a.service.options.ResourceAttribute, accesscontrol.Parameter(":resourceID"))
|
2023-05-24 03:49:42 -05:00
|
|
|
r.Get("/description", auth(accesscontrol.EvalPermission(actionRead)), routing.Wrap(a.getDescription))
|
|
|
|
r.Get("/:resourceID", auth(accesscontrol.EvalPermission(actionRead, scope)), routing.Wrap(a.getPermissions))
|
|
|
|
r.Post("/:resourceID", licenseMW, auth(accesscontrol.EvalPermission(actionWrite, scope)), routing.Wrap(a.setPermissions))
|
2022-07-26 03:43:29 -05:00
|
|
|
if a.service.options.Assignments.Users {
|
2023-05-24 03:49:42 -05:00
|
|
|
r.Post("/:resourceID/users/:userID", licenseMW, auth(accesscontrol.EvalPermission(actionWrite, scope)), routing.Wrap(a.setUserPermission))
|
2022-05-10 08:48:47 -05:00
|
|
|
}
|
|
|
|
if a.service.options.Assignments.Teams {
|
2023-05-24 03:49:42 -05:00
|
|
|
r.Post("/:resourceID/teams/:teamID", licenseMW, auth(accesscontrol.EvalPermission(actionWrite, scope)), routing.Wrap(a.setTeamPermission))
|
2022-05-10 08:48:47 -05:00
|
|
|
}
|
|
|
|
if a.service.options.Assignments.BuiltInRoles {
|
2023-05-24 03:49:42 -05:00
|
|
|
r.Post("/:resourceID/builtInRoles/:builtInRole", licenseMW, auth(accesscontrol.EvalPermission(actionWrite, scope)), routing.Wrap(a.setBuiltinRolePermission))
|
2022-05-10 08:48:47 -05:00
|
|
|
}
|
2021-12-20 02:52:24 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Assignments struct {
|
2023-10-06 10:48:13 -05:00
|
|
|
Users bool `json:"users"`
|
|
|
|
ServiceAccounts bool `json:"serviceAccounts"`
|
|
|
|
Teams bool `json:"teams"`
|
|
|
|
BuiltInRoles bool `json:"builtInRoles"`
|
2021-12-20 02:52:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Description struct {
|
|
|
|
Assignments Assignments `json:"assignments"`
|
|
|
|
Permissions []string `json:"permissions"`
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (a *api) getDescription(c *contextmodel.ReqContext) response.Response {
|
2021-12-20 02:52:24 -06:00
|
|
|
return response.JSON(http.StatusOK, &Description{
|
|
|
|
Permissions: a.permissions,
|
|
|
|
Assignments: a.service.options.Assignments,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type resourcePermissionDTO struct {
|
2023-10-06 10:48:13 -05:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
RoleName string `json:"roleName"`
|
|
|
|
IsManaged bool `json:"isManaged"`
|
|
|
|
IsInherited bool `json:"isInherited"`
|
|
|
|
IsServiceAccount bool `json:"isServiceAccount"`
|
|
|
|
UserID int64 `json:"userId,omitempty"`
|
|
|
|
UserLogin string `json:"userLogin,omitempty"`
|
|
|
|
UserAvatarUrl string `json:"userAvatarUrl,omitempty"`
|
|
|
|
Team string `json:"team,omitempty"`
|
|
|
|
TeamID int64 `json:"teamId,omitempty"`
|
|
|
|
TeamAvatarUrl string `json:"teamAvatarUrl,omitempty"`
|
|
|
|
BuiltInRole string `json:"builtInRole,omitempty"`
|
|
|
|
Actions []string `json:"actions"`
|
|
|
|
Permission string `json:"permission"`
|
2021-12-20 02:52:24 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (a *api) getPermissions(c *contextmodel.ReqContext) response.Response {
|
2021-12-20 02:52:24 -06:00
|
|
|
resourceID := web.Params(c.Req)[":resourceID"]
|
|
|
|
|
2022-03-01 03:58:41 -06:00
|
|
|
permissions, err := a.service.GetPermissions(c.Req.Context(), c.SignedInUser, resourceID)
|
2021-12-20 02:52:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusInternalServerError, "failed to get permissions", err)
|
|
|
|
}
|
2022-03-07 06:28:39 -06:00
|
|
|
|
2022-05-25 13:40:41 -05:00
|
|
|
if a.service.options.Assignments.BuiltInRoles && !a.service.license.FeatureEnabled("accesscontrol.enforcement") {
|
2022-03-07 06:28:39 -06:00
|
|
|
permissions = append(permissions, accesscontrol.ResourcePermission{
|
|
|
|
Actions: a.service.actions,
|
|
|
|
Scope: "*",
|
2022-08-10 04:56:48 -05:00
|
|
|
BuiltInRole: string(org.RoleAdmin),
|
2022-03-07 06:28:39 -06:00
|
|
|
})
|
|
|
|
}
|
2021-12-20 02:52:24 -06:00
|
|
|
|
|
|
|
dto := make([]resourcePermissionDTO, 0, len(permissions))
|
|
|
|
for _, p := range permissions {
|
2022-03-03 08:05:47 -06:00
|
|
|
if permission := a.service.MapActions(p); permission != "" {
|
2021-12-20 02:52:24 -06:00
|
|
|
teamAvatarUrl := ""
|
|
|
|
if p.TeamId != 0 {
|
|
|
|
teamAvatarUrl = dtos.GetGravatarUrlWithDefault(p.TeamEmail, p.Team)
|
|
|
|
}
|
|
|
|
|
|
|
|
dto = append(dto, resourcePermissionDTO{
|
2023-10-06 10:48:13 -05:00
|
|
|
ID: p.ID,
|
|
|
|
RoleName: p.RoleName,
|
|
|
|
UserID: p.UserId,
|
|
|
|
UserLogin: p.UserLogin,
|
|
|
|
UserAvatarUrl: dtos.GetGravatarUrl(p.UserEmail),
|
|
|
|
Team: p.Team,
|
|
|
|
TeamID: p.TeamId,
|
|
|
|
TeamAvatarUrl: teamAvatarUrl,
|
|
|
|
BuiltInRole: p.BuiltInRole,
|
|
|
|
Actions: p.Actions,
|
|
|
|
Permission: permission,
|
|
|
|
IsManaged: p.IsManaged,
|
|
|
|
IsInherited: p.IsInherited,
|
|
|
|
IsServiceAccount: p.IsServiceAccount,
|
2021-12-20 02:52:24 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(http.StatusOK, dto)
|
|
|
|
}
|
|
|
|
|
|
|
|
type setPermissionCommand struct {
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
}
|
|
|
|
|
2022-10-31 06:46:58 -05:00
|
|
|
type setPermissionsCommand struct {
|
|
|
|
Permissions []accesscontrol.SetResourcePermissionCommand `json:"permissions"`
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (a *api) setUserPermission(c *contextmodel.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":userID"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "userID is invalid", err)
|
|
|
|
}
|
2021-12-20 02:52:24 -06:00
|
|
|
resourceID := web.Params(c.Req)[":resourceID"]
|
|
|
|
|
|
|
|
var cmd setPermissionCommand
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:42:18 -05:00
|
|
|
_, err = a.service.SetUserPermission(c.Req.Context(), c.SignedInUser.GetOrgID(), accesscontrol.User{ID: userID}, resourceID, cmd.Permission)
|
2021-12-20 02:52:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "failed to set user permission", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return permissionSetResponse(cmd)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (a *api) setTeamPermission(c *contextmodel.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
teamID, err := strconv.ParseInt(web.Params(c.Req)[":teamID"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "teamID is invalid", err)
|
|
|
|
}
|
2021-12-20 02:52:24 -06:00
|
|
|
resourceID := web.Params(c.Req)[":resourceID"]
|
|
|
|
|
|
|
|
var cmd setPermissionCommand
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:42:18 -05:00
|
|
|
_, err = a.service.SetTeamPermission(c.Req.Context(), c.SignedInUser.GetOrgID(), teamID, resourceID, cmd.Permission)
|
2021-12-20 02:52:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "failed to set team permission", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return permissionSetResponse(cmd)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (a *api) setBuiltinRolePermission(c *contextmodel.ReqContext) response.Response {
|
2021-12-20 02:52:24 -06:00
|
|
|
builtInRole := web.Params(c.Req)[":builtInRole"]
|
|
|
|
resourceID := web.Params(c.Req)[":resourceID"]
|
|
|
|
|
|
|
|
cmd := setPermissionCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:42:18 -05:00
|
|
|
_, err := a.service.SetBuiltInRolePermission(c.Req.Context(), c.SignedInUser.GetOrgID(), builtInRole, resourceID, cmd.Permission)
|
2021-12-20 02:52:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "failed to set role permission", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return permissionSetResponse(cmd)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (a *api) setPermissions(c *contextmodel.ReqContext) response.Response {
|
2022-10-31 06:46:58 -05:00
|
|
|
resourceID := web.Params(c.Req)[":resourceID"]
|
|
|
|
|
|
|
|
cmd := setPermissionsCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:42:18 -05:00
|
|
|
_, err := a.service.SetPermissions(c.Req.Context(), c.SignedInUser.GetOrgID(), resourceID, cmd.Permissions...)
|
2022-10-31 06:46:58 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "failed to set permissions", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Success("Permissions updated")
|
|
|
|
}
|
|
|
|
|
2021-12-20 02:52:24 -06:00
|
|
|
func permissionSetResponse(cmd setPermissionCommand) response.Response {
|
|
|
|
message := "Permission updated"
|
|
|
|
if cmd.Permission == "" {
|
|
|
|
message = "Permission removed"
|
|
|
|
}
|
|
|
|
return response.Success(message)
|
|
|
|
}
|