mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Fix #58598 X-ID-Token header missing on Loki Datasource * Remove unecessary continue statements * Add getAuthHeadersForCallResource unit tests * Fix test and switch statement issues introduced during merge
This commit is contained in:
parent
054d87e52e
commit
f1ef63791a
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -122,22 +123,24 @@ func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceReq
|
||||
func getHeadersForCallResource(headers map[string][]string) map[string]string {
|
||||
data := make(map[string]string)
|
||||
|
||||
if auth := arrayHeaderFirstValue(headers["Authorization"]); auth != "" {
|
||||
data["Authorization"] = auth
|
||||
}
|
||||
for k, values := range headers {
|
||||
k = textproto.CanonicalMIMEHeaderKey(k)
|
||||
firstValue := arrayHeaderFirstValue(values)
|
||||
|
||||
if cookie := arrayHeaderFirstValue(headers["Cookie"]); cookie != "" {
|
||||
data["Cookie"] = cookie
|
||||
if firstValue == "" {
|
||||
continue
|
||||
}
|
||||
switch k {
|
||||
case "Authorization":
|
||||
data["Authorization"] = firstValue
|
||||
case "X-Id-Token":
|
||||
data["X-ID-Token"] = firstValue
|
||||
case "Cookie":
|
||||
data["Cookie"] = firstValue
|
||||
case "Accept-Encoding":
|
||||
data["Accept-Encoding"] = firstValue
|
||||
}
|
||||
}
|
||||
|
||||
if idToken := arrayHeaderFirstValue(headers["X-ID-Token"]); idToken != "" {
|
||||
data["X-ID-Token"] = idToken
|
||||
}
|
||||
|
||||
if encType := arrayHeaderFirstValue(headers["Accept-Encoding"]); encType != "" {
|
||||
data["Accept-Encoding"] = encType
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
|
74
pkg/tsdb/loki/loki_test.go
Normal file
74
pkg/tsdb/loki/loki_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
package loki
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetHeadersForCallResource(t *testing.T) {
|
||||
const idTokn1 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
|
||||
const idTokn2 = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJleHAiOjE2Njg2MjExODQsImlhdCI6MTY2ODYyMTE4NH0.bg0Y0S245DeANhNnnLBCfGYBseTld29O0xynhQwZZlU"
|
||||
const authTokn1 = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
|
||||
const authTokn2 = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJleHAiOjE2Njg2MjExODQsImlhdCI6MTY2ODYyMTE4NH0.bg0Y0S245DeANhNnnLBCfGYBseTld29O0xynhQwZZlU"
|
||||
|
||||
testCases := map[string]struct {
|
||||
headers map[string][]string
|
||||
expectedHeaders map[string]string
|
||||
}{
|
||||
"Headers with empty value": {
|
||||
headers: map[string][]string{
|
||||
"X-Grafana-Org-Id": {"1"},
|
||||
"Cookie": {""},
|
||||
"X-Id-Token": {""},
|
||||
"Accept-Encoding": {""},
|
||||
"Authorization": {""},
|
||||
},
|
||||
expectedHeaders: map[string]string{},
|
||||
},
|
||||
"Headers with multiple values": {
|
||||
headers: map[string][]string{
|
||||
"Authorization": {authTokn1, authTokn2},
|
||||
"Cookie": {"a=1"},
|
||||
"X-Grafana-Org-Id": {"1"},
|
||||
"Accept-Encoding": {"gzip", "compress"},
|
||||
"X-Id-Token": {idTokn1, idTokn2},
|
||||
},
|
||||
expectedHeaders: map[string]string{
|
||||
"Authorization": authTokn1,
|
||||
"Cookie": "a=1",
|
||||
"Accept-Encoding": "gzip",
|
||||
"X-ID-Token": idTokn1,
|
||||
},
|
||||
},
|
||||
"Headers with single value": {
|
||||
headers: map[string][]string{
|
||||
"Authorization": {authTokn1},
|
||||
"X-Grafana-Org-Id": {"1"},
|
||||
"Cookie": {"a=1"},
|
||||
"Accept-Encoding": {"gzip"},
|
||||
"X-Id-Token": {idTokn1},
|
||||
},
|
||||
expectedHeaders: map[string]string{
|
||||
"Authorization": authTokn1,
|
||||
"Cookie": "a=1",
|
||||
"Accept-Encoding": "gzip",
|
||||
"X-ID-Token": idTokn1,
|
||||
},
|
||||
},
|
||||
"Non Canonical 'X-Id-Token' header key": {
|
||||
headers: map[string][]string{
|
||||
"X-ID-TOKEN": {idTokn1},
|
||||
},
|
||||
expectedHeaders: map[string]string{
|
||||
"X-ID-Token": idTokn1,
|
||||
},
|
||||
},
|
||||
}
|
||||
for name, test := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
headers := getHeadersForCallResource(test.headers)
|
||||
assert.Equal(t, test.expectedHeaders, headers)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user