Fix #58598 X-ID-Token header missing on Loki Datasource (#58784)

* 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:
Yann Vigara 2022-11-23 10:31:50 +00:00 committed by GitHub
parent 054d87e52e
commit f1ef63791a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 14 deletions

View File

@ -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
}
if idToken := arrayHeaderFirstValue(headers["X-ID-Token"]); idToken != "" {
data["X-ID-Token"] = idToken
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 encType := arrayHeaderFirstValue(headers["Accept-Encoding"]); encType != "" {
data["Accept-Encoding"] = encType
}
return data
}

View 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)
})
}
}