IAM - Fix error messages for resource permissions endpoints (#85773)

* IAM: fix many error messages in access-related code to provide more information

* Remove debug statement

* Refactor resourcepermissions package to use errutil

* Replace a few more errors with errutil and wrap errors found in users and teams services

* Apply diff of openAPI spec
This commit is contained in:
Aaron Godin
2024-04-17 08:53:28 -05:00
committed by GitHub
parent 60abf01526
commit d409d8e860
9 changed files with 140 additions and 26 deletions
+14 -1
View File
@@ -14,6 +14,7 @@ import (
"text/template"
"time"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/middleware/cookies"
"github.com/grafana/grafana/pkg/models/usertoken"
"github.com/grafana/grafana/pkg/services/auth/identity"
@@ -180,6 +181,13 @@ func AuthorizeInOrgMiddleware(ac AccessControl, authnService authn.Service) func
return func(c *contextmodel.ReqContext) {
targetOrgID, err := getTargetOrg(c)
if err != nil {
if errors.Is(err, ErrInvalidRequestBody) {
c.JSON(http.StatusBadRequest, map[string]string{
"message": err.Error(),
"traceID": tracing.TraceIDFromContext(c.Req.Context(), false),
})
return
}
deny(c, nil, fmt.Errorf("failed to get target org: %w", err))
return
}
@@ -254,6 +262,9 @@ func UseGlobalOrgFromRequestData(cfg *setting.Cfg) OrgIDGetter {
return func(c *contextmodel.ReqContext) (int64, error) {
query, err := getOrgQueryFromRequest(c)
if err != nil {
if errors.Is(err, ErrInvalidRequestBody) {
return NoOrgID, err
}
// Special case of macaron handling invalid params
return NoOrgID, org.ErrOrgNotFound.Errorf("failed to get organization from context: %w", err)
}
@@ -290,7 +301,9 @@ func getOrgQueryFromRequest(c *contextmodel.ReqContext) (*QueryWithOrg, error) {
}
if err := web.Bind(req, query); err != nil {
// Special case of macaron handling invalid params
if err.Error() == "unexpected EOF" {
return nil, fmt.Errorf("%w: unexpected end of JSON input", ErrInvalidRequestBody)
}
return nil, err
}