Chore: Move ReqContext to contexthandler service (#62102)

* Chore: Move ReqContext to contexthandler service

* Rename package to contextmodel

* Generate ngalert files

* Remove unused imports
This commit is contained in:
idafurjes
2023-01-27 08:50:36 +01:00
committed by GitHub
parent 8379a29b53
commit 6c5a573772
180 changed files with 1208 additions and 1182 deletions
+4 -4
View File
@@ -17,8 +17,8 @@ import (
"github.com/grafana/grafana/pkg/infra/httpclient"
glog "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/oauthtoken"
"github.com/grafana/grafana/pkg/setting"
@@ -33,7 +33,7 @@ var (
type DataSourceProxy struct {
ds *datasources.DataSource
ctx *models.ReqContext
ctx *contextmodel.ReqContext
targetUrl *url.URL
proxyPath string
matchedRoute *plugins.Route
@@ -50,7 +50,7 @@ type httpClient interface {
}
// NewDataSourceProxy creates a new Datasource proxy
func NewDataSourceProxy(ds *datasources.DataSource, pluginRoutes []*plugins.Route, ctx *models.ReqContext,
func NewDataSourceProxy(ds *datasources.DataSource, pluginRoutes []*plugins.Route, ctx *contextmodel.ReqContext,
proxyPath string, cfg *setting.Cfg, clientProvider httpclient.Provider,
oAuthTokenService oauthtoken.OAuthTokenService, dsService datasources.DataSourceService,
tracer tracing.Tracer) (*DataSourceProxy, error) {
@@ -343,7 +343,7 @@ func (proxy *DataSourceProxy) logRequest() {
"body", body)
}
func checkWhiteList(c *models.ReqContext, host string) bool {
func checkWhiteList(c *contextmodel.ReqContext, host string) bool {
if host != "" && len(setting.DataProxyWhiteList) > 0 {
if _, exists := setting.DataProxyWhiteList[host]; !exists {
c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil)
+22 -21
View File
@@ -28,6 +28,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/datasources"
datasourceservice "github.com/grafana/grafana/pkg/services/datasources/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
@@ -128,10 +129,10 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
},
}
setUp := func() (*models.ReqContext, *http.Request) {
setUp := func() (*contextmodel.ReqContext, *http.Request) {
req, err := http.NewRequest("GET", "http://localhost/asd", nil)
require.NoError(t, err)
ctx := &models.ReqContext{
ctx := &contextmodel.ReqContext{
Context: &web.Context{Req: req},
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor},
}
@@ -286,7 +287,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost/asd", nil)
require.NoError(t, err)
ctx := &models.ReqContext{
ctx := &contextmodel.ReqContext{
Context: &web.Context{Req: req},
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor},
}
@@ -372,7 +373,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
t.Run("When proxying graphite", func(t *testing.T) {
var routes []*plugins.Route
ds := &datasources.DataSource{Url: "htttp://graphite:8080", Type: datasources.DS_GRAPHITE}
ctx := &models.ReqContext{}
ctx := &contextmodel.ReqContext{}
sqlStore := db.InitTestDB(t)
secretsService := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
@@ -401,7 +402,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
User: "user",
}
ctx := &models.ReqContext{}
ctx := &contextmodel.ReqContext{}
var routes []*plugins.Route
sqlStore := db.InitTestDB(t)
secretsService := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
@@ -429,7 +430,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
JsonData: json,
}
ctx := &models.ReqContext{}
ctx := &contextmodel.ReqContext{}
var routes []*plugins.Route
sqlStore := db.InitTestDB(t)
secretsService := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
@@ -461,7 +462,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
JsonData: json,
}
ctx := &models.ReqContext{}
ctx := &contextmodel.ReqContext{}
var pluginRoutes []*plugins.Route
sqlStore := db.InitTestDB(t)
secretsService := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
@@ -488,7 +489,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
Type: "custom-datasource",
Url: "http://host/root/",
}
ctx := &models.ReqContext{}
ctx := &contextmodel.ReqContext{}
var routes []*plugins.Route
sqlStore := db.InitTestDB(t)
secretsService := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
@@ -522,7 +523,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost/asd", nil)
require.NoError(t, err)
ctx := &models.ReqContext{
ctx := &contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{UserID: 1},
Context: &web.Context{Req: req},
}
@@ -563,7 +564,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
t.Run("When SendUserHeader config is enabled", func(t *testing.T) {
req := getDatasourceProxiedRequest(
t,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -576,7 +577,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
t.Run("When SendUserHeader config is disabled", func(t *testing.T) {
req := getDatasourceProxiedRequest(
t,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -590,7 +591,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
t.Run("When SendUserHeader config is enabled but user is anonymous", func(t *testing.T) {
req := getDatasourceProxiedRequest(
t,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{IsAnonymous: true},
},
&setting.Cfg{SendUserHeader: true},
@@ -635,7 +636,7 @@ func TestDataSourceProxy_requestHandling(t *testing.T) {
writeCb func(w http.ResponseWriter, r *http.Request)
}
setUp := func(t *testing.T, cfgs ...setUpCfg) (*models.ReqContext, *datasources.DataSource) {
setUp := func(t *testing.T, cfgs ...setUpCfg) (*contextmodel.ReqContext, *datasources.DataSource) {
writeErr = nil
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -668,7 +669,7 @@ func TestDataSourceProxy_requestHandling(t *testing.T) {
}
}
return &models.ReqContext{
return &contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{},
Context: &web.Context{
Req: httptest.NewRequest("GET", "/render", nil),
@@ -822,7 +823,7 @@ func TestDataSourceProxy_requestHandling(t *testing.T) {
}
func TestNewDataSourceProxy_InvalidURL(t *testing.T) {
ctx := models.ReqContext{
ctx := contextmodel.ReqContext{
Context: &web.Context{},
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor},
}
@@ -846,7 +847,7 @@ func TestNewDataSourceProxy_InvalidURL(t *testing.T) {
}
func TestNewDataSourceProxy_ProtocolLessURL(t *testing.T) {
ctx := models.ReqContext{
ctx := contextmodel.ReqContext{
Context: &web.Context{},
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor},
}
@@ -871,7 +872,7 @@ func TestNewDataSourceProxy_ProtocolLessURL(t *testing.T) {
// Test wth MSSQL type data sources.
func TestNewDataSourceProxy_MSSQL(t *testing.T) {
ctx := models.ReqContext{
ctx := contextmodel.ReqContext{
Context: &web.Context{},
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor},
}
@@ -926,7 +927,7 @@ func TestNewDataSourceProxy_MSSQL(t *testing.T) {
}
// getDatasourceProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
func getDatasourceProxiedRequest(t *testing.T, ctx *models.ReqContext, cfg *setting.Cfg) *http.Request {
func getDatasourceProxiedRequest(t *testing.T, ctx *contextmodel.ReqContext, cfg *setting.Cfg) *http.Request {
ds := &datasources.DataSource{
Type: "custom",
Url: "http://host/root/",
@@ -1052,7 +1053,7 @@ func createAuthTest(t *testing.T, secretsStore secretskvs.SecretsKVStore, dsType
}
func runDatasourceAuthTest(t *testing.T, secretsService secrets.Service, secretsStore secretskvs.SecretsKVStore, cfg *setting.Cfg, test *testCase) {
ctx := &models.ReqContext{}
ctx := &contextmodel.ReqContext{}
tracer := tracing.InitializeTracerForTest()
var routes []*plugins.Route
@@ -1089,10 +1090,10 @@ func Test_PathCheck(t *testing.T) {
}
tracer := tracing.InitializeTracerForTest()
setUp := func() (*models.ReqContext, *http.Request) {
setUp := func() (*contextmodel.ReqContext, *http.Request) {
req, err := http.NewRequest("GET", "http://localhost/asd", nil)
require.NoError(t, err)
ctx := &models.ReqContext{
ctx := &contextmodel.ReqContext{
Context: &web.Context{Req: req},
SignedInUser: &user.SignedInUser{OrgRole: org.RoleViewer},
}
+3 -3
View File
@@ -8,8 +8,8 @@ import (
"net/url"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/pluginsettings"
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/setting"
@@ -22,7 +22,7 @@ import (
type PluginProxy struct {
ps *pluginsettings.DTO
pluginRoutes []*plugins.Route
ctx *models.ReqContext
ctx *contextmodel.ReqContext
proxyPath string
matchedRoute *plugins.Route
cfg *setting.Cfg
@@ -32,7 +32,7 @@ type PluginProxy struct {
}
// NewPluginProxy creates a plugin proxy.
func NewPluginProxy(ps *pluginsettings.DTO, routes []*plugins.Route, ctx *models.ReqContext,
func NewPluginProxy(ps *pluginsettings.DTO, routes []*plugins.Route, ctx *contextmodel.ReqContext,
proxyPath string, cfg *setting.Cfg, secretsService secrets.Service, tracer tracing.Tracer,
transport *http.Transport) (*PluginProxy, error) {
return &PluginProxy{
+11 -11
View File
@@ -10,8 +10,8 @@ import (
"testing"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/pluginsettings"
"github.com/grafana/grafana/pkg/services/secrets"
@@ -49,7 +49,7 @@ func TestPluginProxy(t *testing.T) {
},
},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -72,7 +72,7 @@ func TestPluginProxy(t *testing.T) {
t,
&pluginsettings.DTO{},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -96,7 +96,7 @@ func TestPluginProxy(t *testing.T) {
t,
&pluginsettings.DTO{},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -119,7 +119,7 @@ func TestPluginProxy(t *testing.T) {
t,
&pluginsettings.DTO{},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{IsAnonymous: true},
Context: &web.Context{
Req: httpReq,
@@ -150,7 +150,7 @@ func TestPluginProxy(t *testing.T) {
},
},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -178,7 +178,7 @@ func TestPluginProxy(t *testing.T) {
t,
&pluginsettings.DTO{},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -216,7 +216,7 @@ func TestPluginProxy(t *testing.T) {
SecureJSONData: encryptedJsonData,
},
secretsService,
&models.ReqContext{
&contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{
Login: "test_user",
},
@@ -250,7 +250,7 @@ func TestPluginProxy(t *testing.T) {
},
}
ctx := &models.ReqContext{
ctx := &contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{},
Context: &web.Context{
Req: httptest.NewRequest("GET", "/", nil),
@@ -388,7 +388,7 @@ func TestPluginProxyRoutes(t *testing.T) {
responseWriter := web.NewResponseWriter("GET", httptest.NewRecorder())
ctx := &models.ReqContext{
ctx := &contextmodel.ReqContext{
SignedInUser: &user.SignedInUser{},
Context: &web.Context{
Req: httptest.NewRequest("GET", tc.proxyPath, nil),
@@ -420,7 +420,7 @@ func TestPluginProxyRoutes(t *testing.T) {
}
// getPluginProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
func getPluginProxiedRequest(t *testing.T, ps *pluginsettings.DTO, secretsService secrets.Service, ctx *models.ReqContext, cfg *setting.Cfg, route *plugins.Route) *http.Request {
func getPluginProxiedRequest(t *testing.T, ps *pluginsettings.DTO, secretsService secrets.Service, ctx *contextmodel.ReqContext, cfg *setting.Cfg, route *plugins.Route) *http.Request {
// insert dummy route if none is specified
if route == nil {
route = &plugins.Route{