2014-12-29 13:36:08 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2020-04-22 10:30:06 +02:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2021-02-03 20:47:45 +01:00
|
|
|
"net/http"
|
2020-04-22 10:30:06 +02:00
|
|
|
|
2020-05-12 13:04:18 +02:00
|
|
|
"github.com/grafana/grafana/pkg/api/datasource"
|
2017-08-22 17:14:15 +02:00
|
|
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
2019-02-23 23:35:26 +01:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2020-03-04 12:57:20 +01:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2017-08-23 10:52:31 +02:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2017-01-16 12:16:41 +01:00
|
|
|
)
|
|
|
|
|
|
2020-03-04 12:57:20 +01:00
|
|
|
// ProxyDataSourceRequest proxies datasource requests
|
|
|
|
|
func (hs *HTTPServer) ProxyDataSourceRequest(c *models.ReqContext) {
|
2019-07-16 17:58:46 +03:00
|
|
|
c.TimeRequest(metrics.MDataSourceProxyReqTimer)
|
2016-06-03 09:17:36 +02:00
|
|
|
|
2020-03-04 12:57:20 +01:00
|
|
|
dsID := c.ParamsInt64(":id")
|
|
|
|
|
ds, err := hs.DatasourceCache.GetDatasource(dsID, c.SignedInUser, c.SkipCache)
|
2015-10-08 17:30:13 +02:00
|
|
|
if err != nil {
|
2020-11-19 13:34:28 +01:00
|
|
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
2021-02-03 20:47:45 +01:00
|
|
|
c.JsonApiErr(http.StatusForbidden, "Access denied to datasource", err)
|
2018-10-26 10:40:33 +02:00
|
|
|
return
|
|
|
|
|
}
|
2021-02-03 20:47:45 +01:00
|
|
|
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)
|
2015-03-11 17:34:11 +01:00
|
|
|
return
|
2014-12-29 13:36:08 +01:00
|
|
|
}
|
|
|
|
|
|
2017-08-23 10:52:31 +02:00
|
|
|
// find plugin
|
|
|
|
|
plugin, ok := plugins.DataSources[ds.Type]
|
|
|
|
|
if !ok {
|
2021-02-03 20:47:45 +01:00
|
|
|
c.JsonApiErr(http.StatusInternalServerError, "Unable to find datasource plugin", err)
|
2017-08-23 10:52:31 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 10:02:04 -07:00
|
|
|
// macaron does not include trailing slashes when resolving a wildcard path
|
2018-10-03 12:55:01 +02:00
|
|
|
proxyPath := ensureProxyPathTrailingSlash(c.Req.URL.Path, c.Params("*"))
|
|
|
|
|
|
2020-04-22 10:30:06 +02:00
|
|
|
proxy, err := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath, hs.Cfg)
|
|
|
|
|
if err != nil {
|
2020-05-12 13:04:18 +02:00
|
|
|
if errors.Is(err, datasource.URLValidationError{}) {
|
2021-02-03 20:47:45 +01:00
|
|
|
c.JsonApiErr(http.StatusBadRequest, fmt.Sprintf("Invalid data source URL: %q", ds.Url), err)
|
2020-04-22 10:30:06 +02:00
|
|
|
} else {
|
2021-02-03 20:47:45 +01:00
|
|
|
c.JsonApiErr(http.StatusInternalServerError, "Failed creating data source proxy", err)
|
2020-04-22 10:30:06 +02:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-10-03 12:55:01 +02:00
|
|
|
proxy.HandleRequest()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ensureProxyPathTrailingSlash Check for a trailing slash in original path and makes
|
|
|
|
|
// sure that a trailing slash is added to proxy path, if not already exists.
|
|
|
|
|
func ensureProxyPathTrailingSlash(originalPath, proxyPath string) string {
|
2018-09-18 11:16:09 -07:00
|
|
|
if len(proxyPath) > 1 {
|
2018-10-03 12:55:01 +02:00
|
|
|
if originalPath[len(originalPath)-1] == '/' && proxyPath[len(proxyPath)-1] != '/' {
|
|
|
|
|
return proxyPath + "/"
|
2018-09-18 11:16:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-03 12:55:01 +02:00
|
|
|
return proxyPath
|
2016-08-17 14:33:59 +09:00
|
|
|
}
|