2022-04-28 03:46:18 -05:00
|
|
|
package accesscontrol
|
2021-03-22 07:22:48 -05:00
|
|
|
|
|
|
|
import (
|
2022-04-28 03:46:18 -05:00
|
|
|
"context"
|
2021-05-12 16:00:27 -05:00
|
|
|
"fmt"
|
2021-03-22 07:22:48 -05:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2021-05-12 16:00:27 -05:00
|
|
|
"time"
|
|
|
|
|
2021-03-22 07:22:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-11-04 03:59:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-08-24 04:36:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2021-03-22 07:22:48 -05:00
|
|
|
)
|
|
|
|
|
2022-04-28 03:46:18 -05:00
|
|
|
func Middleware(ac AccessControl) func(web.Handler, Evaluator) web.Handler {
|
|
|
|
return func(fallback web.Handler, evaluator Evaluator) web.Handler {
|
|
|
|
if ac.IsDisabled() {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(c *models.ReqContext) {
|
|
|
|
authorize(c, ac, c.SignedInUser, evaluator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authorize(c *models.ReqContext, ac AccessControl, user *models.SignedInUser, evaluator Evaluator) {
|
|
|
|
injected, err := evaluator.MutateScopes(c.Req.Context(), ScopeInjector(ScopeParams{
|
|
|
|
OrgID: c.OrgId,
|
|
|
|
URLParams: web.Params(c.Req),
|
|
|
|
}))
|
2021-11-17 03:12:28 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusInternalServerError, "Internal server error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hasAccess, err := ac.Evaluate(c.Req.Context(), user, injected)
|
|
|
|
if !hasAccess || err != nil {
|
2022-04-28 03:46:18 -05:00
|
|
|
deny(c, injected, err)
|
2021-11-17 03:12:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:46:18 -05:00
|
|
|
func deny(c *models.ReqContext, evaluator Evaluator, err error) {
|
2021-05-12 16:00:27 -05:00
|
|
|
id := newID()
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Error("Error from access control system", "error", err, "accessErrorID", id)
|
|
|
|
} else {
|
2021-08-24 04:36:28 -05:00
|
|
|
c.Logger.Info(
|
|
|
|
"Access denied",
|
2021-05-12 16:00:27 -05:00
|
|
|
"userID", c.UserId,
|
2021-08-24 04:36:28 -05:00
|
|
|
"accessErrorID", id,
|
2022-01-28 05:17:24 -06:00
|
|
|
"permissions", evaluator.GoString(),
|
2021-08-24 04:36:28 -05:00
|
|
|
)
|
2021-05-12 16:00:27 -05:00
|
|
|
}
|
|
|
|
|
2021-11-04 03:59:52 -05:00
|
|
|
if !c.IsApiRequest() {
|
|
|
|
// TODO(emil): I'd like to show a message after this redirect, not sure how that can be done?
|
|
|
|
c.Redirect(setting.AppSubUrl + "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:46:18 -05:00
|
|
|
message := ""
|
|
|
|
if evaluator != nil {
|
|
|
|
message = evaluator.String()
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:00:27 -05:00
|
|
|
// If the user triggers an error in the access control system, we
|
|
|
|
// don't want the user to be aware of that, so the user gets the
|
|
|
|
// same information from the system regardless of if it's an
|
|
|
|
// internal server error or access denied.
|
|
|
|
c.JSON(http.StatusForbidden, map[string]string{
|
|
|
|
"title": "Access denied", // the component needs to pick this up
|
2022-04-28 03:46:18 -05:00
|
|
|
"message": fmt.Sprintf("You'll need additional permissions to perform this action. Permissions needed: %s", message),
|
2021-05-12 16:00:27 -05:00
|
|
|
"accessErrorId": id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newID() string {
|
|
|
|
// Less ambiguity than alphanumerical.
|
|
|
|
numerical := []byte("0123456789")
|
|
|
|
id, err := util.GetRandomString(10, numerical...)
|
|
|
|
if err != nil {
|
|
|
|
// this should not happen, but if it does, a timestamp is as
|
|
|
|
// useful as anything.
|
|
|
|
id = fmt.Sprintf("%d", time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
return "ACE" + id
|
|
|
|
}
|
2021-10-06 06:15:09 -05:00
|
|
|
|
2021-11-17 03:12:28 -06:00
|
|
|
type OrgIDGetter func(c *models.ReqContext) (int64, error)
|
2022-04-28 03:46:18 -05:00
|
|
|
type userCache interface {
|
|
|
|
GetSignedInUserWithCacheCtx(ctx context.Context, query *models.GetSignedInUserQuery) error
|
|
|
|
}
|
2021-11-17 03:12:28 -06:00
|
|
|
|
2022-04-28 03:46:18 -05:00
|
|
|
func AuthorizeInOrgMiddleware(ac AccessControl, cache userCache) func(web.Handler, OrgIDGetter, Evaluator) web.Handler {
|
|
|
|
return func(fallback web.Handler, getTargetOrg OrgIDGetter, evaluator Evaluator) web.Handler {
|
2021-11-17 03:12:28 -06:00
|
|
|
if ac.IsDisabled() {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(c *models.ReqContext) {
|
|
|
|
// using a copy of the user not to modify the signedInUser, yet perform the permission evaluation in another org
|
|
|
|
userCopy := *(c.SignedInUser)
|
|
|
|
orgID, err := getTargetOrg(c)
|
|
|
|
if err != nil {
|
2022-04-28 03:46:18 -05:00
|
|
|
deny(c, nil, fmt.Errorf("failed to get target org: %w", err))
|
2021-11-17 03:12:28 -06:00
|
|
|
return
|
|
|
|
}
|
2022-04-28 03:46:18 -05:00
|
|
|
if orgID == GlobalOrgID {
|
2021-11-17 03:12:28 -06:00
|
|
|
userCopy.OrgId = orgID
|
|
|
|
userCopy.OrgName = ""
|
|
|
|
userCopy.OrgRole = ""
|
|
|
|
} else {
|
|
|
|
query := models.GetSignedInUserQuery{UserId: c.UserId, OrgId: orgID}
|
2022-04-28 03:46:18 -05:00
|
|
|
if err := cache.GetSignedInUserWithCacheCtx(c.Req.Context(), &query); err != nil {
|
|
|
|
deny(c, nil, fmt.Errorf("failed to authenticate user in target org: %w", err))
|
2021-11-17 03:12:28 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
userCopy.OrgId = query.Result.OrgId
|
|
|
|
userCopy.OrgName = query.Result.OrgName
|
|
|
|
userCopy.OrgRole = query.Result.OrgRole
|
|
|
|
}
|
|
|
|
|
|
|
|
authorize(c, ac, &userCopy, evaluator)
|
2022-03-24 02:58:10 -05:00
|
|
|
|
2022-04-28 03:46:18 -05:00
|
|
|
// Set the sign-ed in user permissions in that org
|
2022-03-24 02:58:10 -05:00
|
|
|
c.SignedInUser.Permissions = userCopy.Permissions
|
2021-11-17 03:12:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseOrgFromContextParams(c *models.ReqContext) (int64, error) {
|
2022-01-14 10:55:57 -06:00
|
|
|
orgID, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
|
2021-11-17 03:12:28 -06:00
|
|
|
// Special case of macaron handling invalid params
|
2022-01-14 10:55:57 -06:00
|
|
|
if orgID == 0 || err != nil {
|
2021-11-17 03:12:28 -06:00
|
|
|
return 0, models.ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return orgID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseGlobalOrg(c *models.ReqContext) (int64, error) {
|
2022-04-28 03:46:18 -05:00
|
|
|
return GlobalOrgID, nil
|
2021-12-20 02:52:24 -06:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:46:18 -05:00
|
|
|
func LoadPermissionsMiddleware(ac AccessControl) web.Handler {
|
2021-12-14 09:05:59 -06:00
|
|
|
return func(c *models.ReqContext) {
|
|
|
|
if ac.IsDisabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:40:43 -06:00
|
|
|
permissions, err := ac.GetUserPermissions(c.Req.Context(), c.SignedInUser,
|
2022-04-28 03:46:18 -05:00
|
|
|
Options{ReloadCache: false})
|
2021-12-14 09:05:59 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusForbidden, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SignedInUser.Permissions == nil {
|
|
|
|
c.SignedInUser.Permissions = make(map[int64]map[string][]string)
|
|
|
|
}
|
2022-04-28 03:46:18 -05:00
|
|
|
c.SignedInUser.Permissions[c.OrgId] = GroupScopesByAction(permissions)
|
2021-12-14 09:05:59 -06:00
|
|
|
}
|
|
|
|
}
|