Pass configured/auth headers to a Datasource.
In some setups (ex openshift), the Datasource will require Grafana to pass oauth token as header when sending queries. Also, this PR allow to send any header which is something Grafana currently does not support.
This commit is contained in:
@@ -109,6 +109,28 @@ func (proxy *DataSourceProxy) addTraceFromHeaderValue(span opentracing.Span, hea
|
||||
}
|
||||
}
|
||||
|
||||
func (proxy *DataSourceProxy) useCustomHeaders(req *http.Request) {
|
||||
decryptSdj := proxy.ds.SecureJsonData.Decrypt()
|
||||
index := 1
|
||||
for {
|
||||
headerNameSuffix := fmt.Sprintf("httpHeaderName%d", index)
|
||||
headerValueSuffix := fmt.Sprintf("httpHeaderValue%d", index)
|
||||
if key := proxy.ds.JsonData.Get(headerNameSuffix).MustString(); key != "" {
|
||||
if val, ok := decryptSdj[headerValueSuffix]; ok {
|
||||
// remove if exists
|
||||
if req.Header.Get(key) != "" {
|
||||
req.Header.Del(key)
|
||||
}
|
||||
req.Header.Add(key, val)
|
||||
logger.Debug("Using custom header ", "CustomHeaders", key)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
|
||||
func (proxy *DataSourceProxy) getDirector() func(req *http.Request) {
|
||||
return func(req *http.Request) {
|
||||
req.URL.Scheme = proxy.targetUrl.Scheme
|
||||
@@ -138,6 +160,11 @@ func (proxy *DataSourceProxy) getDirector() func(req *http.Request) {
|
||||
req.Header.Add("Authorization", util.GetBasicAuthHeader(proxy.ds.BasicAuthUser, proxy.ds.BasicAuthPassword))
|
||||
}
|
||||
|
||||
// Lookup and use custom headers
|
||||
if proxy.ds.SecureJsonData != nil {
|
||||
proxy.useCustomHeaders(req)
|
||||
}
|
||||
|
||||
dsAuth := req.Header.Get("X-DS-Authorization")
|
||||
if len(dsAuth) > 0 {
|
||||
req.Header.Del("X-DS-Authorization")
|
||||
|
||||
Reference in New Issue
Block a user