6dbe3b555f
Adding support for backend plugin client middlewares. This allows headers in outgoing backend plugin and HTTP requests to be modified using client middlewares. The following client middlewares added: Forward cookies: Will forward incoming HTTP request Cookies to outgoing plugins.Client and HTTP requests if the datasource has enabled forwarding of cookies (keepCookies). Forward OAuth token: Will set OAuth token headers on outgoing plugins.Client and HTTP requests if the datasource has enabled Forward OAuth Identity (oauthPassThru). Clear auth headers: Will clear any outgoing HTTP headers that was part of the incoming HTTP request and used when authenticating to Grafana. The current suggested way to register client middlewares is to have a separate package, pluginsintegration, responsible for bootstrap/instantiate the backend plugin client with middlewares and/or longer term bootstrap/instantiate plugin management. Fixes #54135 Related to #47734 Related to #57870 Related to #41623 Related to #57065
32 lines
884 B
Go
32 lines
884 B
Go
package httpclientprovider
|
|
|
|
import (
|
|
"net/http"
|
|
"net/textproto"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
)
|
|
|
|
const SetHeadersMiddlewareName = "set-headers"
|
|
|
|
// SetHeadersMiddleware middleware that sets headers on the outgoing
|
|
// request if headers provided.
|
|
// If the request already contains any of the headers provided, they
|
|
// will be overwritten.
|
|
func SetHeadersMiddleware(headers http.Header) httpclient.Middleware {
|
|
return httpclient.NamedMiddlewareFunc(SetHeadersMiddlewareName, func(opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
|
|
if len(headers) == 0 {
|
|
return next
|
|
}
|
|
|
|
return httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
for k, v := range headers {
|
|
canonicalKey := textproto.CanonicalMIMEHeaderKey(k)
|
|
req.Header[canonicalKey] = v
|
|
}
|
|
|
|
return next.RoundTrip(req)
|
|
})
|
|
})
|
|
}
|