2021-03-22 07:22:48 -05:00
|
|
|
package accesscontrol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-23 07:03:20 -05:00
|
|
|
"strings"
|
2021-03-22 07:22:48 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccessControl interface {
|
2021-04-06 08:49:09 -05:00
|
|
|
// Evaluate evaluates access to the given resource.
|
2021-03-22 07:22:48 -05:00
|
|
|
Evaluate(ctx context.Context, user *models.SignedInUser, permission string, scope ...string) (bool, error)
|
|
|
|
|
2021-04-06 08:49:09 -05:00
|
|
|
// GetUserPermissions returns user permissions.
|
2021-04-13 08:28:11 -05:00
|
|
|
GetUserPermissions(ctx context.Context, user *models.SignedInUser) ([]*Permission, error)
|
2021-03-22 07:22:48 -05:00
|
|
|
|
2021-04-06 08:49:09 -05:00
|
|
|
// Middleware checks if service disabled or not to switch to fallback authorization.
|
|
|
|
IsDisabled() bool
|
2021-08-04 07:44:37 -05:00
|
|
|
|
|
|
|
// DeclareFixedRoles allow the caller to declare, to the service, fixed roles and their
|
|
|
|
// assignments to organization roles ("Viewer", "Editor", "Admin") or "Grafana Admin"
|
|
|
|
DeclareFixedRoles(...RoleRegistration) error
|
2021-03-22 07:22:48 -05:00
|
|
|
}
|
2021-04-16 08:02:16 -05:00
|
|
|
|
2021-04-19 04:23:29 -05:00
|
|
|
func HasAccess(ac AccessControl, c *models.ReqContext) func(fallback func(*models.ReqContext) bool, permission string, scopes ...string) bool {
|
|
|
|
return func(fallback func(*models.ReqContext) bool, permission string, scopes ...string) bool {
|
|
|
|
if ac.IsDisabled() {
|
|
|
|
return fallback(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
hasAccess, err := ac.Evaluate(c.Req.Context(), c.SignedInUser, permission, scopes...)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Error("Error from access control system", "error", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasAccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ReqGrafanaAdmin = func(c *models.ReqContext) bool {
|
|
|
|
return c.IsGrafanaAdmin
|
|
|
|
}
|
|
|
|
|
2021-04-22 05:19:41 -05:00
|
|
|
var ReqOrgAdmin = func(c *models.ReqContext) bool {
|
|
|
|
return c.OrgRole == models.ROLE_ADMIN
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildPermissionsMap(permissions []*Permission) map[string]bool {
|
|
|
|
permissionsMap := make(map[string]bool)
|
2021-04-16 08:02:16 -05:00
|
|
|
for _, p := range permissions {
|
2021-04-22 05:19:41 -05:00
|
|
|
permissionsMap[p.Action] = true
|
2021-04-16 08:02:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return permissionsMap
|
|
|
|
}
|
2021-08-23 07:03:20 -05:00
|
|
|
|
|
|
|
func ValidateScope(scope string) bool {
|
|
|
|
prefix, last := scope[:len(scope)-1], scope[len(scope)-1]
|
|
|
|
// verify that last char is either ':' or '/' if last character of scope is '*'
|
|
|
|
if len(prefix) > 0 && last == '*' {
|
|
|
|
lastChar := prefix[len(prefix)-1]
|
|
|
|
if lastChar != ':' && lastChar != '/' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !strings.ContainsAny(prefix, "*?")
|
|
|
|
}
|