mirror of
https://github.com/grafana/grafana.git
synced 2025-01-24 23:37:01 -06:00
2c5b72e844
* add a middleware that appens headers for IP range AC to data source proxy requests * update code * add tests * fix a mistake * add logging * refactor to reuse code * small cleanup * skip the plugins middleware if the header is already set * skip the plugins middleware if the header is already set
35 lines
1.3 KiB
Go
35 lines
1.3 KiB
Go
package httpclientprovider
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/clientmiddleware"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
const GrafanaRequestIDHeaderMiddlewareName = "grafana-request-id-header-middleware"
|
|
|
|
func GrafanaRequestIDHeaderMiddleware(cfg *setting.Cfg, logger log.Logger) sdkhttpclient.Middleware {
|
|
return sdkhttpclient.NamedMiddlewareFunc(GrafanaRequestIDHeaderMiddlewareName, func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
|
|
return sdkhttpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get(clientmiddleware.GrafanaRequestID) != "" {
|
|
logger.Debug("Request already has a Grafana request ID header", "request_id", req.Header.Get(clientmiddleware.GrafanaRequestID))
|
|
return next.RoundTrip(req)
|
|
}
|
|
|
|
if !clientmiddleware.IsRequestURLInAllowList(req.URL, cfg) {
|
|
logger.Debug("Data source URL not among the allow-listed URLs", "url", req.URL.String())
|
|
return next.RoundTrip(req)
|
|
}
|
|
|
|
for k, v := range clientmiddleware.GetGrafanaRequestIDHeaders(req, cfg, logger) {
|
|
req.Header.Set(k, v)
|
|
}
|
|
|
|
return next.RoundTrip(req)
|
|
})
|
|
})
|
|
}
|