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
This commit is contained in:
Ieva
2024-03-06 12:40:48 +00:00
committed by GitHub
parent 401265522e
commit 2c5b72e844
5 changed files with 197 additions and 24 deletions
@@ -0,0 +1,34 @@
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)
})
})
}