2017-08-22 10:14:15 -05:00
|
|
|
package pluginproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-02-18 06:26:01 -06:00
|
|
|
"log"
|
2017-08-22 10:14:15 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2017-08-23 10:18:43 -05:00
|
|
|
"strconv"
|
2017-08-22 10:14:15 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-05-12 06:04:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/datasource"
|
2021-05-19 16:53:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
2020-02-18 06:26:01 -06:00
|
|
|
glog "github.com/grafana/grafana/pkg/infra/log"
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2017-08-22 10:14:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2020-10-23 18:34:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/oauthtoken"
|
2017-08-22 10:14:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2020-03-03 04:45:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/proxyutil"
|
2022-01-20 04:10:12 -06:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2017-08-22 10:14:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-02-18 06:26:01 -06:00
|
|
|
logger = glog.New("data-proxy-log")
|
2018-09-06 08:50:16 -05:00
|
|
|
client = newHTTPClient()
|
2017-08-22 10:14:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DataSourceProxy struct {
|
2021-10-07 09:33:50 -05:00
|
|
|
ds *models.DataSource
|
|
|
|
ctx *models.ReqContext
|
|
|
|
targetUrl *url.URL
|
|
|
|
proxyPath string
|
2021-11-01 04:53:33 -05:00
|
|
|
matchedRoute *plugins.Route
|
|
|
|
pluginRoutes []*plugins.Route
|
2021-10-07 09:33:50 -05:00
|
|
|
cfg *setting.Cfg
|
|
|
|
clientProvider httpclient.Provider
|
|
|
|
oAuthTokenService oauthtoken.OAuthTokenService
|
|
|
|
dataSourcesService *datasources.Service
|
2022-01-20 04:10:12 -06:00
|
|
|
tracer tracing.Tracer
|
2018-06-12 10:39:38 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 06:03:12 -06:00
|
|
|
type handleResponseTransport struct {
|
|
|
|
transport http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *handleResponseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
res, err := t.transport.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Header.Del("Set-Cookie")
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-06-14 06:39:46 -05:00
|
|
|
type httpClient interface {
|
2018-06-12 10:39:38 -05:00
|
|
|
Do(req *http.Request) (*http.Response, error)
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 06:26:01 -06:00
|
|
|
type logWrapper struct {
|
|
|
|
logger glog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes log messages as bytes from proxy
|
|
|
|
func (lw *logWrapper) Write(p []byte) (n int, err error) {
|
|
|
|
withoutNewline := strings.TrimSuffix(string(p), "\n")
|
|
|
|
lw.logger.Error("Data proxy error", "error", withoutNewline)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDataSourceProxy creates a new Datasource proxy
|
2021-11-01 04:53:33 -05:00
|
|
|
func NewDataSourceProxy(ds *models.DataSource, pluginRoutes []*plugins.Route, ctx *models.ReqContext,
|
2021-10-07 09:33:50 -05:00
|
|
|
proxyPath string, cfg *setting.Cfg, clientProvider httpclient.Provider,
|
2022-01-20 04:10:12 -06:00
|
|
|
oAuthTokenService oauthtoken.OAuthTokenService, dsService *datasources.Service,
|
|
|
|
tracer tracing.Tracer) (*DataSourceProxy, error) {
|
2020-06-17 04:17:11 -05:00
|
|
|
targetURL, err := datasource.ValidateURL(ds.Type, ds.Url)
|
2020-04-22 03:30:06 -05:00
|
|
|
if err != nil {
|
2020-05-12 06:04:18 -05:00
|
|
|
return nil, err
|
2020-04-22 03:30:06 -05:00
|
|
|
}
|
2017-08-23 03:52:31 -05:00
|
|
|
|
2017-08-22 10:14:15 -05:00
|
|
|
return &DataSourceProxy{
|
2021-10-07 09:33:50 -05:00
|
|
|
ds: ds,
|
2021-11-01 04:53:33 -05:00
|
|
|
pluginRoutes: pluginRoutes,
|
2021-10-07 09:33:50 -05:00
|
|
|
ctx: ctx,
|
|
|
|
proxyPath: proxyPath,
|
|
|
|
targetUrl: targetURL,
|
|
|
|
cfg: cfg,
|
|
|
|
clientProvider: clientProvider,
|
|
|
|
oAuthTokenService: oAuthTokenService,
|
|
|
|
dataSourcesService: dsService,
|
2022-01-20 04:10:12 -06:00
|
|
|
tracer: tracer,
|
2020-04-22 03:30:06 -05:00
|
|
|
}, nil
|
2018-06-14 06:39:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newHTTPClient() httpClient {
|
|
|
|
return &http.Client{
|
2019-02-11 06:42:05 -06:00
|
|
|
Timeout: 30 * time.Second,
|
2018-06-14 06:39:46 -05:00
|
|
|
Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *DataSourceProxy) HandleRequest() {
|
|
|
|
if err := proxy.validateRequest(); err != nil {
|
|
|
|
proxy.ctx.JsonApiErr(403, err.Error(), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
proxyErrorLogger := logger.New("userId", proxy.ctx.UserId, "orgId", proxy.ctx.OrgId, "uname", proxy.ctx.Login,
|
|
|
|
"path", proxy.ctx.Req.URL.Path, "remote_addr", proxy.ctx.RemoteAddr(), "referer", proxy.ctx.Req.Referer())
|
2017-08-22 10:14:15 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
transport, err := proxy.dataSourcesService.GetHTTPTransport(proxy.ds, proxy.clientProvider)
|
2017-08-22 10:14:15 -05:00
|
|
|
if err != nil {
|
|
|
|
proxy.ctx.JsonApiErr(400, "Unable to load TLS certificate", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
reverseProxy := &httputil.ReverseProxy{
|
|
|
|
Director: proxy.director,
|
|
|
|
FlushInterval: time.Millisecond * 200,
|
|
|
|
ErrorLog: log.New(&logWrapper{logger: proxyErrorLogger}, "", 0),
|
|
|
|
Transport: &handleResponseTransport{
|
|
|
|
transport: transport,
|
|
|
|
},
|
|
|
|
ModifyResponse: func(resp *http.Response) error {
|
|
|
|
if resp.StatusCode == 401 {
|
|
|
|
// The data source rejected the request as unauthorized, convert to 400 (bad request)
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read data source response body: %w", err)
|
|
|
|
}
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
|
|
|
|
proxyErrorLogger.Info("Authentication to data source failed", "body", string(body), "statusCode",
|
|
|
|
resp.StatusCode)
|
|
|
|
msg := "Authentication to data source failed"
|
|
|
|
*resp = http.Response{
|
|
|
|
StatusCode: 400,
|
|
|
|
Status: "Bad Request",
|
|
|
|
Body: ioutil.NopCloser(strings.NewReader(msg)),
|
|
|
|
ContentLength: int64(len(msg)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2020-01-15 06:03:12 -06:00
|
|
|
}
|
|
|
|
|
2017-08-22 10:14:15 -05:00
|
|
|
proxy.logRequest()
|
2022-01-20 04:10:12 -06:00
|
|
|
ctx, span := proxy.tracer.Start(proxy.ctx.Req.Context(), "datasource reverse proxy")
|
|
|
|
defer span.End()
|
2020-11-13 06:21:43 -06:00
|
|
|
|
2021-09-01 04:18:30 -05:00
|
|
|
proxy.ctx.Req = proxy.ctx.Req.WithContext(ctx)
|
2017-09-12 03:25:40 -05:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes("datasource_name", proxy.ds.Name, attribute.Key("datasource_name").String(proxy.ds.Name))
|
|
|
|
span.SetAttributes("datasource_type", proxy.ds.Type, attribute.Key("datasource_type").String(proxy.ds.Type))
|
2022-01-24 11:17:39 -06:00
|
|
|
span.SetAttributes("user", proxy.ctx.SignedInUser.Login, attribute.Key("user").String(proxy.ctx.SignedInUser.Login))
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes("org_id", proxy.ctx.SignedInUser.OrgId, attribute.Key("org_id").Int64(proxy.ctx.SignedInUser.OrgId))
|
2017-09-12 03:25:40 -05:00
|
|
|
|
2018-03-20 16:21:24 -05:00
|
|
|
proxy.addTraceFromHeaderValue(span, "X-Panel-Id", "panel_id")
|
|
|
|
proxy.addTraceFromHeaderValue(span, "X-Dashboard-Id", "dashboard_id")
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
proxy.tracer.Inject(ctx, proxy.ctx.Req.Header, span)
|
2017-09-12 04:06:37 -05:00
|
|
|
|
2021-09-01 04:18:30 -05:00
|
|
|
reverseProxy.ServeHTTP(proxy.ctx.Resp, proxy.ctx.Req)
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
func (proxy *DataSourceProxy) addTraceFromHeaderValue(span tracing.Span, headerName string, tagName string) {
|
2018-03-20 16:21:24 -05:00
|
|
|
panelId := proxy.ctx.Req.Header.Get(headerName)
|
|
|
|
dashId, err := strconv.Atoi(panelId)
|
|
|
|
if err == nil {
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes(tagName, dashId, attribute.Key(tagName).Int(dashId))
|
2018-03-20 16:21:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
func (proxy *DataSourceProxy) director(req *http.Request) {
|
|
|
|
req.URL.Scheme = proxy.targetUrl.Scheme
|
|
|
|
req.URL.Host = proxy.targetUrl.Host
|
|
|
|
req.Host = proxy.targetUrl.Host
|
|
|
|
|
|
|
|
reqQueryVals := req.URL.Query()
|
|
|
|
|
|
|
|
switch proxy.ds.Type {
|
|
|
|
case models.DS_INFLUXDB_08:
|
2021-03-17 06:17:41 -05:00
|
|
|
req.URL.RawPath = util.JoinURLFragments(proxy.targetUrl.Path, "db/"+proxy.ds.Database+"/"+proxy.proxyPath)
|
2020-11-13 06:21:43 -06:00
|
|
|
reqQueryVals.Add("u", proxy.ds.User)
|
2021-10-07 09:33:50 -05:00
|
|
|
reqQueryVals.Add("p", proxy.dataSourcesService.DecryptedPassword(proxy.ds))
|
2020-11-13 06:21:43 -06:00
|
|
|
req.URL.RawQuery = reqQueryVals.Encode()
|
|
|
|
case models.DS_INFLUXDB:
|
2021-03-17 06:17:41 -05:00
|
|
|
req.URL.RawPath = util.JoinURLFragments(proxy.targetUrl.Path, proxy.proxyPath)
|
2020-11-13 06:21:43 -06:00
|
|
|
req.URL.RawQuery = reqQueryVals.Encode()
|
|
|
|
if !proxy.ds.BasicAuth {
|
2021-10-07 09:33:50 -05:00
|
|
|
req.Header.Set(
|
|
|
|
"Authorization",
|
|
|
|
util.GetBasicAuthHeader(proxy.ds.User, proxy.dataSourcesService.DecryptedPassword(proxy.ds)),
|
|
|
|
)
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
2020-11-13 06:21:43 -06:00
|
|
|
default:
|
2021-03-17 06:17:41 -05:00
|
|
|
req.URL.RawPath = util.JoinURLFragments(proxy.targetUrl.Path, proxy.proxyPath)
|
2020-11-13 06:21:43 -06:00
|
|
|
}
|
2020-04-24 03:32:13 -05:00
|
|
|
|
2021-03-17 06:17:41 -05:00
|
|
|
unescapedPath, err := url.PathUnescape(req.URL.RawPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to unescape raw path", "rawPath", req.URL.RawPath, "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.Path = unescapedPath
|
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
if proxy.ds.BasicAuth {
|
|
|
|
req.Header.Set("Authorization", util.GetBasicAuthHeader(proxy.ds.BasicAuthUser,
|
2021-10-07 09:33:50 -05:00
|
|
|
proxy.dataSourcesService.DecryptedBasicAuthPassword(proxy.ds)))
|
2020-11-13 06:21:43 -06:00
|
|
|
}
|
2017-08-22 10:14:15 -05:00
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
dsAuth := req.Header.Get("X-DS-Authorization")
|
|
|
|
if len(dsAuth) > 0 {
|
|
|
|
req.Header.Del("X-DS-Authorization")
|
|
|
|
req.Header.Set("Authorization", dsAuth)
|
|
|
|
}
|
2017-08-22 10:14:15 -05:00
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
applyUserHeader(proxy.cfg.SendUserHeader, req, proxy.ctx.SignedInUser)
|
2019-03-14 07:04:47 -05:00
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
keepCookieNames := []string{}
|
|
|
|
if proxy.ds.JsonData != nil {
|
|
|
|
if keepCookies := proxy.ds.JsonData.Get("keepCookies"); keepCookies != nil {
|
|
|
|
keepCookieNames = keepCookies.MustStringArray()
|
2017-10-17 17:47:15 -05:00
|
|
|
}
|
2020-11-13 06:21:43 -06:00
|
|
|
}
|
2017-08-22 10:14:15 -05:00
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
proxyutil.ClearCookieHeader(req, keepCookieNames)
|
|
|
|
proxyutil.PrepareProxyRequest(req)
|
2020-03-03 04:45:16 -06:00
|
|
|
|
2020-11-13 06:21:43 -06:00
|
|
|
req.Header.Set("User-Agent", fmt.Sprintf("Grafana/%s", setting.BuildVersion))
|
2017-08-22 10:14:15 -05:00
|
|
|
|
2021-10-08 07:46:35 -05:00
|
|
|
// Clear Origin and Referer to avoid CORS issues
|
2020-11-13 06:21:43 -06:00
|
|
|
req.Header.Del("Origin")
|
|
|
|
req.Header.Del("Referer")
|
2018-11-08 05:30:10 -06:00
|
|
|
|
2021-10-08 07:46:35 -05:00
|
|
|
jsonData := make(map[string]interface{})
|
|
|
|
if proxy.ds.JsonData != nil {
|
|
|
|
jsonData, err = proxy.ds.JsonData.Map()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to get json data as map", "jsonData", proxy.ds.JsonData, "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 11:47:21 -05:00
|
|
|
secureJsonData, err := proxy.dataSourcesService.SecretsService.DecryptJsonData(req.Context(), proxy.ds.SecureJsonData)
|
2021-10-08 07:46:35 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error interpolating proxy url", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
if proxy.matchedRoute != nil {
|
|
|
|
ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.matchedRoute, DSInfo{
|
2021-10-08 07:46:35 -05:00
|
|
|
ID: proxy.ds.Id,
|
|
|
|
Updated: proxy.ds.Updated,
|
|
|
|
JSONData: jsonData,
|
|
|
|
DecryptedSecureJSONData: secureJsonData,
|
|
|
|
}, proxy.cfg)
|
2020-11-13 06:21:43 -06:00
|
|
|
}
|
2019-02-01 18:40:57 -06:00
|
|
|
|
2021-07-07 01:54:17 -05:00
|
|
|
if proxy.oAuthTokenService.IsOAuthPassThruEnabled(proxy.ds) {
|
|
|
|
if token := proxy.oAuthTokenService.GetCurrentOAuthToken(proxy.ctx.Req.Context(), proxy.ctx.SignedInUser); token != nil {
|
2020-11-13 06:21:43 -06:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("%s %s", token.Type(), token.AccessToken))
|
2021-11-29 08:40:05 -06:00
|
|
|
|
|
|
|
idToken, ok := token.Extra("id_token").(string)
|
|
|
|
if ok && idToken != "" {
|
|
|
|
req.Header.Set("X-ID-Token", idToken)
|
|
|
|
}
|
2019-02-01 18:40:57 -06:00
|
|
|
}
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *DataSourceProxy) validateRequest() error {
|
2017-08-23 03:52:31 -05:00
|
|
|
if !checkWhiteList(proxy.ctx, proxy.targetUrl.Host) {
|
2020-11-05 06:07:06 -06:00
|
|
|
return errors.New("target URL is not a valid target")
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if proxy.ds.Type == models.DS_ES {
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Method == "DELETE" {
|
2020-11-05 06:07:06 -06:00
|
|
|
return errors.New("deletes not allowed on proxied Elasticsearch datasource")
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Method == "PUT" {
|
2020-11-05 06:07:06 -06:00
|
|
|
return errors.New("puts not allowed on proxied Elasticsearch datasource")
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Method == "POST" && proxy.proxyPath != "_msearch" {
|
2020-11-05 06:07:06 -06:00
|
|
|
return errors.New("posts not allowed on proxied Elasticsearch datasource except on /_msearch")
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// found route if there are any
|
2021-11-01 04:53:33 -05:00
|
|
|
for _, route := range proxy.pluginRoutes {
|
|
|
|
// method match
|
|
|
|
if route.Method != "" && route.Method != "*" && route.Method != proxy.ctx.Req.Method {
|
|
|
|
continue
|
|
|
|
}
|
2017-08-22 10:14:15 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
// route match
|
|
|
|
if !strings.HasPrefix(proxy.proxyPath, route.Path) {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-14 12:06:20 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
if route.ReqRole.IsValid() {
|
|
|
|
if !proxy.ctx.HasUserRole(route.ReqRole) {
|
|
|
|
return errors.New("plugin proxy route access denied")
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
2021-04-14 12:06:20 -05:00
|
|
|
}
|
2021-11-01 04:53:33 -05:00
|
|
|
|
|
|
|
proxy.matchedRoute = route
|
|
|
|
return nil
|
2021-04-14 12:06:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trailing validation below this point for routes that were not matched
|
|
|
|
if proxy.ds.Type == models.DS_PROMETHEUS {
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Method == "DELETE" {
|
2021-04-14 12:06:20 -05:00
|
|
|
return errors.New("non allow-listed DELETEs not allowed on proxied Prometheus datasource")
|
|
|
|
}
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Method == "PUT" {
|
2021-04-14 12:06:20 -05:00
|
|
|
return errors.New("non allow-listed PUTs not allowed on proxied Prometheus datasource")
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Method == "POST" {
|
2021-04-29 02:20:51 -05:00
|
|
|
return errors.New("non allow-listed POSTs not allowed on proxied Prometheus datasource")
|
|
|
|
}
|
2017-08-22 10:14:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *DataSourceProxy) logRequest() {
|
2021-07-15 07:30:06 -05:00
|
|
|
if !proxy.cfg.DataProxyLogging {
|
2017-08-22 10:14:15 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var body string
|
2021-09-01 04:18:30 -05:00
|
|
|
if proxy.ctx.Req.Body != nil {
|
|
|
|
buffer, err := ioutil.ReadAll(proxy.ctx.Req.Body)
|
2017-08-22 10:14:15 -05:00
|
|
|
if err == nil {
|
2021-09-01 04:18:30 -05:00
|
|
|
proxy.ctx.Req.Body = ioutil.NopCloser(bytes.NewBuffer(buffer))
|
2017-08-22 10:14:15 -05:00
|
|
|
body = string(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("Proxying incoming request",
|
|
|
|
"userid", proxy.ctx.UserId,
|
|
|
|
"orgid", proxy.ctx.OrgId,
|
|
|
|
"username", proxy.ctx.Login,
|
|
|
|
"datasource", proxy.ds.Type,
|
|
|
|
"uri", proxy.ctx.Req.RequestURI,
|
2021-09-01 04:18:30 -05:00
|
|
|
"method", proxy.ctx.Req.Method,
|
2017-08-22 10:14:15 -05:00
|
|
|
"body", body)
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func checkWhiteList(c *models.ReqContext, host string) bool {
|
2017-08-22 10:14:15 -05:00
|
|
|
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)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|