diff --git a/pkg/api/pluginproxy/ds_proxy.go b/pkg/api/pluginproxy/ds_proxy.go index 8b20fb81b1f..975f7b3b1f7 100644 --- a/pkg/api/pluginproxy/ds_proxy.go +++ b/pkg/api/pluginproxy/ds_proxy.go @@ -187,9 +187,7 @@ func (proxy *DataSourceProxy) getDirector() func(req *http.Request) { req.Header.Add("Authorization", dsAuth) } - if proxy.cfg.SendUserHeader && !proxy.ctx.SignedInUser.IsAnonymous { - req.Header.Add("X-Grafana-User", proxy.ctx.SignedInUser.Login) - } + applyUserHeader(proxy.cfg.SendUserHeader, req, proxy.ctx.SignedInUser) keepCookieNames := []string{} if proxy.ds.JsonData != nil { diff --git a/pkg/api/pluginproxy/pluginproxy.go b/pkg/api/pluginproxy/pluginproxy.go index 68535dbfe13..85415ea2f6c 100644 --- a/pkg/api/pluginproxy/pluginproxy.go +++ b/pkg/api/pluginproxy/pluginproxy.go @@ -79,11 +79,9 @@ func NewApiPluginProxy(ctx *models.ReqContext, proxyPath string, route *plugins. return } - req.Header.Add("X-Grafana-Context", string(ctxJSON)) + req.Header.Set("X-Grafana-Context", string(ctxJSON)) - if cfg.SendUserHeader && !ctx.SignedInUser.IsAnonymous { - req.Header.Add("X-Grafana-User", ctx.SignedInUser.Login) - } + applyUserHeader(cfg.SendUserHeader, req, ctx.SignedInUser) if len(route.Headers) > 0 { headers, err := getHeaders(route, ctx.OrgId, appID) diff --git a/pkg/api/pluginproxy/utils.go b/pkg/api/pluginproxy/utils.go index 462f1d96169..e9fd7facde9 100644 --- a/pkg/api/pluginproxy/utils.go +++ b/pkg/api/pluginproxy/utils.go @@ -3,6 +3,7 @@ package pluginproxy import ( "bytes" "fmt" + "net/http" "net/url" "text/template" @@ -47,3 +48,11 @@ func InterpolateURL(anURL *url.URL, route *plugins.AppPluginRoute, orgID int64, return result, err } + +// Set the X-Grafana-User header if needed (and remove if not) +func applyUserHeader(sendUserHeader bool, req *http.Request, user *models.SignedInUser) { + req.Header.Del("X-Grafana-User") + if sendUserHeader && !user.IsAnonymous { + req.Header.Set("X-Grafana-User", user.Login) + } +}