mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Data Source Proxy: Migrate proxy to its own service and make more extensible (#31927)
* datasource proxy extensions * revert normalresponse extensions * data proxy service impl * lint * api datasource proxy method * moves datasource proxy tests into correct new pkg
This commit is contained in:
parent
e935e4979f
commit
36614b03f7
@ -1,64 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import "github.com/grafana/grafana/pkg/models"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/datasource"
|
|
||||||
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ProxyDataSourceRequest proxies datasource requests
|
|
||||||
func (hs *HTTPServer) ProxyDataSourceRequest(c *models.ReqContext) {
|
func (hs *HTTPServer) ProxyDataSourceRequest(c *models.ReqContext) {
|
||||||
c.TimeRequest(metrics.MDataSourceProxyReqTimer)
|
hs.DataProxy.ProxyDataSourceRequest(c)
|
||||||
|
|
||||||
dsID := c.ParamsInt64(":id")
|
|
||||||
ds, err := hs.DatasourceCache.GetDatasource(dsID, c.SignedInUser, c.SkipCache)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
|
||||||
c.JsonApiErr(http.StatusForbidden, "Access denied to datasource", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JsonApiErr(http.StatusInternalServerError, "Unable to load datasource meta data", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = hs.PluginRequestValidator.Validate(ds.Url, c.Req.Request)
|
|
||||||
if err != nil {
|
|
||||||
c.JsonApiErr(http.StatusForbidden, "Access denied", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// find plugin
|
|
||||||
plugin := hs.PluginManager.GetDataSource(ds.Type)
|
|
||||||
if plugin == nil {
|
|
||||||
c.JsonApiErr(http.StatusInternalServerError, "Unable to find datasource plugin", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
proxyPath := getProxyPath(c)
|
|
||||||
proxy, err := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath, hs.Cfg)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, datasource.URLValidationError{}) {
|
|
||||||
c.JsonApiErr(http.StatusBadRequest, fmt.Sprintf("Invalid data source URL: %q", ds.Url), err)
|
|
||||||
} else {
|
|
||||||
c.JsonApiErr(http.StatusInternalServerError, "Failed creating data source proxy", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
proxy.HandleRequest()
|
|
||||||
}
|
|
||||||
|
|
||||||
var proxyPathRegexp = regexp.MustCompile(`^\/api\/datasources\/proxy\/[\d]+\/?`)
|
|
||||||
|
|
||||||
func extractProxyPath(originalRawPath string) string {
|
|
||||||
return proxyPathRegexp.ReplaceAllString(originalRawPath, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func getProxyPath(c *models.ReqContext) string {
|
|
||||||
return extractProxyPath(c.Req.URL.EscapedPath())
|
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins/plugindashboards"
|
"github.com/grafana/grafana/pkg/plugins/plugindashboards"
|
||||||
"github.com/grafana/grafana/pkg/registry"
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||||
|
"github.com/grafana/grafana/pkg/services/datasourceproxy"
|
||||||
"github.com/grafana/grafana/pkg/services/datasources"
|
"github.com/grafana/grafana/pkg/services/datasources"
|
||||||
"github.com/grafana/grafana/pkg/services/hooks"
|
"github.com/grafana/grafana/pkg/services/hooks"
|
||||||
"github.com/grafana/grafana/pkg/services/librarypanels"
|
"github.com/grafana/grafana/pkg/services/librarypanels"
|
||||||
@ -65,31 +66,32 @@ type HTTPServer struct {
|
|||||||
httpSrv *http.Server
|
httpSrv *http.Server
|
||||||
middlewares []macaron.Handler
|
middlewares []macaron.Handler
|
||||||
|
|
||||||
RouteRegister routing.RouteRegister `inject:""`
|
RouteRegister routing.RouteRegister `inject:""`
|
||||||
Bus bus.Bus `inject:""`
|
Bus bus.Bus `inject:""`
|
||||||
RenderService rendering.Service `inject:""`
|
RenderService rendering.Service `inject:""`
|
||||||
Cfg *setting.Cfg `inject:""`
|
Cfg *setting.Cfg `inject:""`
|
||||||
HooksService *hooks.HooksService `inject:""`
|
HooksService *hooks.HooksService `inject:""`
|
||||||
CacheService *localcache.CacheService `inject:""`
|
CacheService *localcache.CacheService `inject:""`
|
||||||
DatasourceCache datasources.CacheService `inject:""`
|
DatasourceCache datasources.CacheService `inject:""`
|
||||||
AuthTokenService models.UserTokenService `inject:""`
|
AuthTokenService models.UserTokenService `inject:""`
|
||||||
QuotaService *quota.QuotaService `inject:""`
|
QuotaService *quota.QuotaService `inject:""`
|
||||||
RemoteCacheService *remotecache.RemoteCache `inject:""`
|
RemoteCacheService *remotecache.RemoteCache `inject:""`
|
||||||
ProvisioningService provisioning.ProvisioningService `inject:""`
|
ProvisioningService provisioning.ProvisioningService `inject:""`
|
||||||
Login *login.LoginService `inject:""`
|
Login *login.LoginService `inject:""`
|
||||||
License models.Licensing `inject:""`
|
License models.Licensing `inject:""`
|
||||||
BackendPluginManager backendplugin.Manager `inject:""`
|
BackendPluginManager backendplugin.Manager `inject:""`
|
||||||
PluginRequestValidator models.PluginRequestValidator `inject:""`
|
DataProxy *datasourceproxy.DatasourceProxyService `inject:""`
|
||||||
PluginManager plugins.Manager `inject:""`
|
PluginRequestValidator models.PluginRequestValidator `inject:""`
|
||||||
SearchService *search.SearchService `inject:""`
|
PluginManager plugins.Manager `inject:""`
|
||||||
ShortURLService *shorturls.ShortURLService `inject:""`
|
SearchService *search.SearchService `inject:""`
|
||||||
Live *live.GrafanaLive `inject:""`
|
ShortURLService *shorturls.ShortURLService `inject:""`
|
||||||
ContextHandler *contexthandler.ContextHandler `inject:""`
|
Live *live.GrafanaLive `inject:""`
|
||||||
SQLStore *sqlstore.SQLStore `inject:""`
|
ContextHandler *contexthandler.ContextHandler `inject:""`
|
||||||
LibraryPanelService *librarypanels.LibraryPanelService `inject:""`
|
SQLStore *sqlstore.SQLStore `inject:""`
|
||||||
DataService *tsdb.Service `inject:""`
|
LibraryPanelService *librarypanels.LibraryPanelService `inject:""`
|
||||||
PluginDashboardService *plugindashboards.Service `inject:""`
|
DataService *tsdb.Service `inject:""`
|
||||||
AlertEngine *alerting.AlertEngine `inject:""`
|
PluginDashboardService *plugindashboards.Service `inject:""`
|
||||||
|
AlertEngine *alerting.AlertEngine `inject:""`
|
||||||
Listener net.Listener
|
Listener net.Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
85
pkg/services/datasourceproxy/datasourceproxy.go
Normal file
85
pkg/services/datasourceproxy/datasourceproxy.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package datasourceproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/api/datasource"
|
||||||
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
||||||
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
|
"github.com/grafana/grafana/pkg/services/datasources"
|
||||||
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registry.RegisterService(&DatasourceProxyService{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type DatasourceProxyService struct {
|
||||||
|
DatasourceCache datasources.CacheService `inject:""`
|
||||||
|
PluginRequestValidator models.PluginRequestValidator `inject:""`
|
||||||
|
PluginManager plugins.Manager `inject:""`
|
||||||
|
Cfg *setting.Cfg `inject:""`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DatasourceProxyService) Init() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DatasourceProxyService) ProxyDataSourceRequest(c *models.ReqContext) {
|
||||||
|
p.ProxyDatasourceRequestWithID(c, c.ParamsInt64(":id"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DatasourceProxyService) ProxyDatasourceRequestWithID(c *models.ReqContext, dsID int64) {
|
||||||
|
c.TimeRequest(metrics.MDataSourceProxyReqTimer)
|
||||||
|
|
||||||
|
ds, err := p.DatasourceCache.GetDatasource(dsID, c.SignedInUser, c.SkipCache)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
|
c.JsonApiErr(http.StatusForbidden, "Access denied to datasource", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JsonApiErr(http.StatusInternalServerError, "Unable to load datasource meta data", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.PluginRequestValidator.Validate(ds.Url, c.Req.Request)
|
||||||
|
if err != nil {
|
||||||
|
c.JsonApiErr(http.StatusForbidden, "Access denied", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// find plugin
|
||||||
|
plugin := p.PluginManager.GetDataSource(ds.Type)
|
||||||
|
if plugin == nil {
|
||||||
|
c.JsonApiErr(http.StatusInternalServerError, "Unable to find datasource plugin", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyPath := getProxyPath(c)
|
||||||
|
proxy, err := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath, p.Cfg)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, datasource.URLValidationError{}) {
|
||||||
|
c.JsonApiErr(http.StatusBadRequest, fmt.Sprintf("Invalid data source URL: %q", ds.Url), err)
|
||||||
|
} else {
|
||||||
|
c.JsonApiErr(http.StatusInternalServerError, "Failed creating data source proxy", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
proxy.HandleRequest()
|
||||||
|
}
|
||||||
|
|
||||||
|
var proxyPathRegexp = regexp.MustCompile(`^\/api\/datasources\/proxy\/[\d]+\/?`)
|
||||||
|
|
||||||
|
func extractProxyPath(originalRawPath string) string {
|
||||||
|
return proxyPathRegexp.ReplaceAllString(originalRawPath, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProxyPath(c *models.ReqContext) string {
|
||||||
|
return extractProxyPath(c.Req.URL.EscapedPath())
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package api
|
package datasourceproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
Loading…
Reference in New Issue
Block a user