pkg/web: restrict handler types (#48495)

Makes `pkg/web` only accept handles from the following set:

```go
	handlerStd       = func(http.ResponseWriter, *http.Request)
	handlerStdCtx    = func(http.ResponseWriter, *http.Request, *web.Context)
	handlerStdReqCtx = func(http.ResponseWriter, *http.Request, *models.ReqContext)
	handlerReqCtx    = func(*models.ReqContext)
	handlerReqCtxRes = func(*models.ReqContext) Response
	handlerCtx       = func(*web.Context)
```

This is a first step to reducing above set to only `http.Handler`.

---

Due to a cyclic import situation between `pkg/models` and `pkg/web`, parts of this PR were put into `pkg/api/response`, even though they definitely do not belong there. This however is _temporary_ until we untangle `models.ReqContext`.
This commit is contained in:
sh0rez
2022-05-20 12:45:18 -04:00
committed by GitHub
parent c980655f08
commit 3d5d8c785b
13 changed files with 115 additions and 23 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
"github.com/grafana/grafana/pkg/web"
)
@@ -90,5 +91,8 @@ func contextProvider() web.Handler {
SkipCache: true,
}
c.Map(reqCtx)
c.Req = c.Req.WithContext(ctxkey.Set(c.Req.Context(), reqCtx))
c.Map(c.Req)
}
}

View File

@@ -18,6 +18,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
@@ -500,6 +501,9 @@ func contextProvider(tc *testContext) web.Handler {
Logger: log.New("test"),
}
c.Map(reqCtx)
c.Req = c.Req.WithContext(ctxkey.Set(c.Req.Context(), reqCtx))
c.Map(c.Req)
}
}

View File

@@ -19,6 +19,7 @@ import (
"github.com/grafana/grafana/pkg/middleware/cookies"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/contexthandler/authproxy"
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/services/sqlstore"
@@ -69,7 +70,7 @@ type ContextHandler struct {
GetTime func() time.Time
}
type reqContextKey struct{}
type reqContextKey = ctxkey.Key
// FromContext returns the ReqContext value stored in a context.Context, if any.
func FromContext(c context.Context) *models.ReqContext {
@@ -94,7 +95,7 @@ func (h *ContextHandler) Middleware(mContext *web.Context) {
}
// Inject ReqContext into a request context and replace the request instance in the macaron context
mContext.Req = mContext.Req.WithContext(context.WithValue(mContext.Req.Context(), reqContextKey{}, reqContext))
mContext.Req = mContext.Req.WithContext(ctxkey.Set(mContext.Req.Context(), reqContext))
mContext.Map(mContext.Req)
traceID := tracing.TraceIDFromContext(mContext.Req.Context(), false)

View File

@@ -0,0 +1,13 @@
package ctxkey
import "context"
type Key struct{}
func Set(ctx context.Context, data interface{}) context.Context {
return context.WithValue(ctx, Key{}, data)
}
func Get(ctx context.Context) interface{} {
return ctx.Value(Key{})
}

View File

@@ -4,6 +4,9 @@ import (
"crypto/tls"
"gopkg.in/ldap.v3"
//TODO(sh0rez): remove once import cycle resolved
_ "github.com/grafana/grafana/pkg/api/response"
)
type searchFunc = func(request *ldap.SearchRequest) (*ldap.SearchResult, error)

View File

@@ -8,6 +8,9 @@ import (
"github.com/grafana/grafana/pkg/services/ldap"
"github.com/stretchr/testify/require"
//TODO(sh0rez): remove once import cycle resolved
_ "github.com/grafana/grafana/pkg/api/response"
)
func TestMultiLDAP(t *testing.T) {

View File

@@ -15,6 +15,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/services/serviceaccounts/database"
@@ -238,6 +239,9 @@ func setupTestServer(t *testing.T, svc *tests.ServiceAccountMock,
Logger: log.New("serviceaccounts-test"),
}
c.Map(ctx)
c.Req = c.Req.WithContext(ctxkey.Set(c.Req.Context(), ctx))
c.Map(c.Req)
})
a.RouterRegister.Register(m.Router)
return m, a