2021-08-04 14:44:37 +02:00
|
|
|
package accesscontrol
|
|
|
|
|
|
2022-11-07 11:30:45 +01:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2024-04-17 08:53:28 -05:00
|
|
|
|
2024-06-13 07:11:35 +03:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/errutil"
|
2024-04-17 08:53:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
invalidBuiltInRoleMessage = `built-in role [{{ .Public.builtInRole }}] is not valid`
|
|
|
|
|
assignmentEntityNotFoundMessage = `{{ .Public.assignment }} not found`
|
2022-11-07 11:30:45 +01:00
|
|
|
)
|
2021-08-04 14:44:37 +02:00
|
|
|
|
|
|
|
|
var (
|
2024-04-17 08:53:28 -05:00
|
|
|
ErrInvalidBuiltinRole = errutil.BadRequest("accesscontrol.invalidBuiltInRole").
|
|
|
|
|
MustTemplate(invalidBuiltInRoleMessage, errutil.WithPublic(invalidBuiltInRoleMessage))
|
|
|
|
|
ErrNoneRoleAssignment = errutil.BadRequest("accesscontrol.noneRoleAssignment", errutil.WithPublicMessage("none role cannot receive permissions"))
|
|
|
|
|
ErrAssignmentEntityNotFound = errutil.BadRequest("accesscontrol.assignmentEntityNotFound").
|
|
|
|
|
MustTemplate(assignmentEntityNotFoundMessage, errutil.WithPublic(assignmentEntityNotFoundMessage))
|
|
|
|
|
|
|
|
|
|
// Note: these are intended to be replaced by equivalent errutil implementations.
|
|
|
|
|
// Avoid creating new errors with errors.New and prefer errutil
|
|
|
|
|
ErrInvalidRequestBody = errors.New("invalid request body")
|
2021-08-04 14:44:37 +02:00
|
|
|
ErrFixedRolePrefixMissing = errors.New("fixed role should be prefixed with '" + FixedRolePrefix + "'")
|
2022-01-18 17:34:35 +01:00
|
|
|
ErrInvalidScope = errors.New("invalid scope")
|
2022-08-25 12:50:27 +02:00
|
|
|
ErrResolverNotFound = errors.New("no resolver found")
|
2022-11-07 11:30:45 +01:00
|
|
|
ErrPluginIDRequired = errors.New("plugin ID is required")
|
2023-05-09 13:19:38 +02:00
|
|
|
ErrRoleNotFound = errors.New("role not found")
|
2021-08-04 14:44:37 +02:00
|
|
|
)
|
2022-11-07 11:30:45 +01:00
|
|
|
|
2024-04-17 08:53:28 -05:00
|
|
|
func ErrInvalidBuiltinRoleData(builtInRole string) errutil.TemplateData {
|
|
|
|
|
return errutil.TemplateData{
|
|
|
|
|
Public: map[string]any{
|
|
|
|
|
"builtInRole": builtInRole,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ErrAssignmentEntityNotFoundData(assignment string) errutil.TemplateData {
|
|
|
|
|
return errutil.TemplateData{
|
|
|
|
|
Public: map[string]any{
|
|
|
|
|
"assignment": assignment,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 11:30:45 +01:00
|
|
|
type ErrorInvalidRole struct{}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorInvalidRole) Error() string {
|
|
|
|
|
return "role is invalid"
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 11:12:00 +01:00
|
|
|
type ErrorRoleNameMissing struct{}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorRoleNameMissing) Error() string {
|
|
|
|
|
return "role has been defined without a name"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorRoleNameMissing) Unwrap() error {
|
|
|
|
|
return &ErrorInvalidRole{}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 11:30:45 +01:00
|
|
|
type ErrorRolePrefixMissing struct {
|
|
|
|
|
Role string
|
|
|
|
|
Prefixes []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorRolePrefixMissing) Error() string {
|
|
|
|
|
return fmt.Sprintf("expected role '%s' to be prefixed with any of '%v'", e.Role, e.Prefixes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorRolePrefixMissing) Unwrap() error {
|
|
|
|
|
return &ErrorInvalidRole{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ErrorActionPrefixMissing struct {
|
|
|
|
|
Action string
|
|
|
|
|
Prefixes []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorActionPrefixMissing) Error() string {
|
|
|
|
|
return fmt.Sprintf("expected action '%s' to be prefixed with any of '%v'", e.Action, e.Prefixes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorActionPrefixMissing) Unwrap() error {
|
|
|
|
|
return &ErrorInvalidRole{}
|
|
|
|
|
}
|
2022-11-30 13:55:07 +01:00
|
|
|
|
|
|
|
|
type ErrorScopeTarget struct {
|
|
|
|
|
Action string
|
|
|
|
|
Scope string
|
|
|
|
|
ExpectedScope string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorScopeTarget) Error() string {
|
|
|
|
|
return fmt.Sprintf("expected action '%s' to be scoped with '%v', found '%v'", e.Action, e.ExpectedScope, e.Scope)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorScopeTarget) Unwrap() error {
|
|
|
|
|
return &ErrorInvalidRole{}
|
|
|
|
|
}
|