Plugins: Add token to gcom requests (#96261)

This commit is contained in:
Hugo Kiyodi Oshiro
2024-11-26 11:42:55 +01:00
committed by GitHub
parent 3b8499eaee
commit a8174f9285
6 changed files with 53 additions and 12 deletions
+6 -2
View File
@@ -23,7 +23,7 @@ var grafanaComProxyTransport = &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
}
func ReverseProxyGnetReq(logger log.Logger, proxyPath string, version string, grafanaComAPIUrl string) *httputil.ReverseProxy {
func ReverseProxyGnetReq(logger log.Logger, proxyPath, version, grafanaComAPIUrl, grafanaComAPIToken string) *httputil.ReverseProxy {
url, _ := url.Parse(grafanaComAPIUrl)
director := func(req *http.Request) {
@@ -40,6 +40,10 @@ func ReverseProxyGnetReq(logger log.Logger, proxyPath string, version string, gr
// send the current Grafana version for each request proxied to GCOM
req.Header.Add("grafana-version", version)
if grafanaComAPIToken != "" {
req.Header.Set("Authorization", "Bearer "+grafanaComAPIToken)
}
}
return proxyutil.NewReverseProxy(logger, director)
@@ -47,7 +51,7 @@ func ReverseProxyGnetReq(logger log.Logger, proxyPath string, version string, gr
func (hs *HTTPServer) ProxyGnetRequest(c *contextmodel.ReqContext) {
proxyPath := web.Params(c.Req)["*"]
proxy := ReverseProxyGnetReq(c.Logger, proxyPath, hs.Cfg.BuildVersion, hs.Cfg.GrafanaComAPIURL)
proxy := ReverseProxyGnetReq(c.Logger, proxyPath, hs.Cfg.BuildVersion, hs.Cfg.GrafanaComAPIURL, hs.Cfg.PluginInstallToken)
proxy.Transport = grafanaComProxyTransport
proxy.ServeHTTP(c.Resp, c.Req)
}