grafana/pkg/infra/httpclient/httpclientprovider/grafana_request_id_header_middleware.go
Ieva 2c5b72e844
AuthZ: add headers for IP range AC checks for data source proxy requests (#81662)
* 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
2024-03-06 12:40:48 +00:00

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)
})
})
}