From 265200799d9f142622a99f3561e1864f84216ecd Mon Sep 17 00:00:00 2001 From: Andres Martinez Gotor Date: Tue, 12 Mar 2024 17:13:23 +0100 Subject: [PATCH] Chore: Update grafana-plugin-sdk (#84289) --- go.mod | 9 +++--- go.sum | 9 ++++-- pkg/apiserver/go.mod | 2 +- pkg/apiserver/go.sum | 2 +- pkg/promlib/client/transport_test.go | 3 +- pkg/promlib/go.mod | 2 +- pkg/promlib/go.sum | 2 +- pkg/services/datasources/datasources.go | 2 +- .../fakes/fake_datasource_service.go | 2 +- .../datasources/service/datasource.go | 10 +++---- .../datasources/service/datasource_test.go | 28 ++++++++++++++----- pkg/services/ngalert/sender/notifier_ext.go | 12 ++++---- pkg/services/ngalert/sender/sender.go | 6 ++-- pkg/tsdb/azuremonitor/httpclient.go | 2 +- pkg/tsdb/azuremonitor/httpclient_test.go | 12 ++++---- pkg/tsdb/tempo/grpc.go | 8 ++++-- 16 files changed, 67 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index 01fb04827c7..b6e92d64d67 100644 --- a/go.mod +++ b/go.mod @@ -63,7 +63,7 @@ require ( github.com/grafana/cuetsy v0.1.11 // @grafana/grafana-as-code github.com/grafana/grafana-aws-sdk v0.24.0 // @grafana/aws-datasources github.com/grafana/grafana-azure-sdk-go v1.12.0 // @grafana/partner-datasources - github.com/grafana/grafana-plugin-sdk-go v0.214.0 // @grafana/plugins-platform-backend + github.com/grafana/grafana-plugin-sdk-go v0.215.0 // @grafana/plugins-platform-backend github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // @grafana/backend-platform github.com/hashicorp/go-hclog v1.6.2 // @grafana/plugins-platform-backend github.com/hashicorp/go-plugin v1.6.0 // @grafana/plugins-platform-backend @@ -122,7 +122,7 @@ require ( gopkg.in/mail.v2 v2.3.1 // @grafana/backend-platform gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // @grafana/alerting-squad-backend - xorm.io/builder v0.3.6 // indirect; @grafana/backend-platform + xorm.io/builder v0.3.6 // @grafana/backend-platform xorm.io/core v0.7.3 // @grafana/backend-platform xorm.io/xorm v0.8.2 // @grafana/alerting-squad-backend ) @@ -174,7 +174,7 @@ require ( github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-msgpack v0.5.5 // indirect - github.com/hashicorp/go-multierror v1.1.1 // indirect; @grafana/alerting-squad + github.com/hashicorp/go-multierror v1.1.1 // @grafana/alerting-squad github.com/hashicorp/go-sockaddr v1.0.6 // indirect github.com/hashicorp/golang-lru v0.6.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect @@ -337,7 +337,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect - github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect; @grafana/alerting-squad-backend + github.com/hashicorp/golang-lru/v2 v2.0.7 // @grafana/alerting-squad-backend github.com/hashicorp/memberlist v0.5.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/invopop/yaml v0.2.0 // indirect @@ -518,4 +518,3 @@ exclude github.com/mattn/go-sqlite3 v2.0.3+incompatible // Use our fork xorm. go.work currently overrides this and points to the local ./pkg/util/xorm directory. replace xorm.io/xorm => github.com/grafana/grafana/pkg/util/xorm v0.0.1 - diff --git a/go.sum b/go.sum index dd2aa1b4505..76f5048564b 100644 --- a/go.sum +++ b/go.sum @@ -1449,6 +1449,7 @@ github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f h1:y06x6vGnFYf github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f/go.mod h1:2stgcRjl6QmW+gU2h5E7BQXg4HU0gzxKWDuT5HviN9s= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/beevik/etree v1.2.0 h1:l7WETslUG/T+xOPs47dtd6jov2Ii/8/OjCldk5fYfQw= github.com/beevik/etree v1.2.0/go.mod h1:aiPf89g/1k3AShMVAzriilpcE4R/Vuor90y83zVZWFc= @@ -1501,6 +1502,7 @@ github.com/bufbuild/connect-go v1.10.0/go.mod h1:CAIePUgkDR5pAFaylSMtNK45ANQjp9J github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/buildkite/yaml v2.1.0+incompatible h1:xirI+ql5GzfikVNDmt+yeiXpf/v1Gt03qXTtT5WXdr8= github.com/buildkite/yaml v2.1.0+incompatible/go.mod h1:UoU8vbcwu1+vjZq01+KrpSeLBgQQIjL/H7Y6KwikUrI= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= @@ -2193,13 +2195,14 @@ github.com/grafana/grafana-google-sdk-go v0.1.0/go.mod h1:Vo2TKWfDVmNTELBUM+3lkr github.com/grafana/grafana-openapi-client-go v0.0.0-20231213163343-bd475d63fb79 h1:r+mU5bGMzcXCRVAuOrTn54S80qbfVkvTdUJZfSfTNbs= github.com/grafana/grafana-openapi-client-go v0.0.0-20231213163343-bd475d63fb79/go.mod h1:wc6Hbh3K2TgCUSfBC/BOzabItujtHMESZeFk5ZhdxhQ= github.com/grafana/grafana-plugin-sdk-go v0.114.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk= -github.com/grafana/grafana-plugin-sdk-go v0.214.0 h1:09AoomxfsMdKmS4bc5tF81f7fvI9HjHckGFAmu/UQls= -github.com/grafana/grafana-plugin-sdk-go v0.214.0/go.mod h1:nBsh3jRItKQUXDF2BQkiQCPxqrsSQeb+7hiFyJTO1RE= +github.com/grafana/grafana-plugin-sdk-go v0.215.0 h1:02gwVsqYi1I+U48/MQR61eOMxiXE7KNKC8QsiMJ//qA= +github.com/grafana/grafana-plugin-sdk-go v0.215.0/go.mod h1:nBsh3jRItKQUXDF2BQkiQCPxqrsSQeb+7hiFyJTO1RE= github.com/grafana/grafana/pkg/apimachinery v0.0.0-20240226124929-648abdbd0ea4 h1:hpyusz8c3yRFoJPlA0o34rWnsLbaOOBZleqRhFBi5Lg= github.com/grafana/grafana/pkg/apimachinery v0.0.0-20240226124929-648abdbd0ea4/go.mod h1:vrRQJuNprTWqwm6JPxHf3BoTJhvO15QMEjQ7Q/YUOnI= github.com/grafana/grafana/pkg/apiserver v0.0.0-20240226124929-648abdbd0ea4 h1:tIbI5zgos92vwJ8lV3zwHwuxkV03GR3FGLkFW9V5LxY= github.com/grafana/grafana/pkg/apiserver v0.0.0-20240226124929-648abdbd0ea4/go.mod h1:vpYI6DHvFO595rpQGooUjcyicjt9rOevldDdW79peV0= github.com/grafana/grafana/pkg/promlib v0.0.1 h1:R+z+xC4vhG3jgMjIJ2cbUjMQdc1HSJHFQWdIWUPCG4Q= +github.com/grafana/grafana/pkg/promlib v0.0.1/go.mod h1:zkmF4NeKJs/y65xc3/XXc53LY0J8E7YUsZnn+FSrcq4= github.com/grafana/grafana/pkg/util/xorm v0.0.1 h1:72QZjxWIWpSeOF8ob4aMV058kfgZyeetkAB8dmeti2o= github.com/grafana/grafana/pkg/util/xorm v0.0.1/go.mod h1:eNfbB9f2jM8o9RfwqwjY8SYm5tvowJ8Ly+iE4P9rXII= github.com/grafana/kindsys v0.0.0-20230508162304-452481b63482 h1:1YNoeIhii4UIIQpCPU+EXidnqf449d0C3ZntAEt4KSo= @@ -2355,6 +2358,7 @@ github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab/go.mod github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097 h1:vilfsDSy7TDxedi9gyBkMvAirat/oRcL0lFdJBf6tdM= github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI= +github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY= github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q= github.com/ionos-cloud/sdk-go/v6 v6.1.10 h1:3815Q2Hw/wc4cJ8wD7bwfsmDsdfIEp80B7BQMj0YP2w= @@ -3090,6 +3094,7 @@ github.com/vultr/govultr/v2 v2.17.2/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49 github.com/wk8/go-ordered-map v1.0.0 h1:BV7z+2PaK8LTSd/mWgY12HyMAo5CEgkHqbkVq2thqr8= github.com/wk8/go-ordered-map v1.0.0/go.mod h1:9ZIbRunKbuvfPKyBP1SIKLcXNlv74YCOZ3t3VTS6gRk= github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/xanzy/go-gitlab v0.15.0/go.mod h1:8zdQa/ri1dfn8eS3Ir1SyfvOKlw7WBJ8DVThkpGiXrs= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= diff --git a/pkg/apiserver/go.mod b/pkg/apiserver/go.mod index a806fd79c90..77fab0818a9 100644 --- a/pkg/apiserver/go.mod +++ b/pkg/apiserver/go.mod @@ -5,7 +5,7 @@ go 1.21.0 require ( github.com/bwmarrin/snowflake v0.3.0 github.com/gorilla/mux v1.8.0 - github.com/grafana/grafana-plugin-sdk-go v0.214.0 + github.com/grafana/grafana-plugin-sdk-go v0.215.0 github.com/stretchr/testify v1.8.4 golang.org/x/mod v0.14.0 k8s.io/apimachinery v0.29.2 diff --git a/pkg/apiserver/go.sum b/pkg/apiserver/go.sum index d25e7989bf5..3196c458b74 100644 --- a/pkg/apiserver/go.sum +++ b/pkg/apiserver/go.sum @@ -77,7 +77,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1 github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/grafana-plugin-sdk-go v0.214.0 h1:09AoomxfsMdKmS4bc5tF81f7fvI9HjHckGFAmu/UQls= +github.com/grafana/grafana-plugin-sdk-go v0.215.0 h1:02gwVsqYi1I+U48/MQR61eOMxiXE7KNKC8QsiMJ//qA= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 h1:uGoIog/wiQHI9GAxXO5TJbT0wWKH3O9HhOJW1F9c3fY= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= diff --git a/pkg/promlib/client/transport_test.go b/pkg/promlib/client/transport_test.go index 61c481adca2..a0d23aa2f2f 100644 --- a/pkg/promlib/client/transport_test.go +++ b/pkg/promlib/client/transport_test.go @@ -2,6 +2,7 @@ package client import ( "context" + "net/http" "testing" "github.com/grafana/grafana-plugin-sdk-go/backend" @@ -20,7 +21,7 @@ func TestCreateTransportOptions(t *testing.T) { } opts, err := CreateTransportOptions(context.Background(), settings, backend.NewLoggerWith("logger", "test")) require.NoError(t, err) - require.Equal(t, map[string]string{"foo": "bar"}, opts.Headers) + require.Equal(t, http.Header{"Foo": []string{"bar"}}, opts.Header) require.Equal(t, 2, len(opts.Middlewares)) }) } diff --git a/pkg/promlib/go.mod b/pkg/promlib/go.mod index b6a16bfab7a..49c4ea67244 100644 --- a/pkg/promlib/go.mod +++ b/pkg/promlib/go.mod @@ -3,7 +3,7 @@ module github.com/grafana/grafana/pkg/promlib go 1.21.0 require ( - github.com/grafana/grafana-plugin-sdk-go v0.214.0 + github.com/grafana/grafana-plugin-sdk-go v0.215.0 github.com/json-iterator/go v1.1.12 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/prometheus/client_golang v1.18.0 diff --git a/pkg/promlib/go.sum b/pkg/promlib/go.sum index 8f3fa371931..9d05b2cf1b4 100644 --- a/pkg/promlib/go.sum +++ b/pkg/promlib/go.sum @@ -33,7 +33,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/grafana/grafana-plugin-sdk-go v0.214.0 h1:09AoomxfsMdKmS4bc5tF81f7fvI9HjHckGFAmu/UQls= +github.com/grafana/grafana-plugin-sdk-go v0.215.0 h1:02gwVsqYi1I+U48/MQR61eOMxiXE7KNKC8QsiMJ//qA= github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db h1:7aN5cccjIqCLTzedH7MZzRZt5/lsAHch6Z3L2ZGn5FA= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 h1:uGoIog/wiQHI9GAxXO5TJbT0wWKH3O9HhOJW1F9c3fY= diff --git a/pkg/services/datasources/datasources.go b/pkg/services/datasources/datasources.go index accc2075887..cc4573fb21c 100644 --- a/pkg/services/datasources/datasources.go +++ b/pkg/services/datasources/datasources.go @@ -58,7 +58,7 @@ type DataSourceService interface { // CustomHeaders returns a map of custom headers the user might have // configured for this Datasource. Not every datasource can has the option // to configure those. - CustomHeaders(ctx context.Context, ds *DataSource) (map[string]string, error) + CustomHeaders(ctx context.Context, ds *DataSource) (http.Header, error) } // CacheService interface for retrieving a cached datasource. diff --git a/pkg/services/datasources/fakes/fake_datasource_service.go b/pkg/services/datasources/fakes/fake_datasource_service.go index 69959cc6d57..3f9fcf4d560 100644 --- a/pkg/services/datasources/fakes/fake_datasource_service.go +++ b/pkg/services/datasources/fakes/fake_datasource_service.go @@ -134,6 +134,6 @@ func (s *FakeDataSourceService) DecryptedPassword(ctx context.Context, ds *datas return "", nil } -func (s *FakeDataSourceService) CustomHeaders(ctx context.Context, ds *datasources.DataSource) (map[string]string, error) { +func (s *FakeDataSourceService) CustomHeaders(ctx context.Context, ds *datasources.DataSource) (http.Header, error) { return nil, nil } diff --git a/pkg/services/datasources/service/datasource.go b/pkg/services/datasources/service/datasource.go index 43bf6f0a6fe..d110042d5a5 100644 --- a/pkg/services/datasources/service/datasource.go +++ b/pkg/services/datasources/service/datasource.go @@ -484,7 +484,7 @@ func (s *Service) httpClientOptions(ctx context.Context, ds *datasources.DataSou opts := &sdkhttpclient.Options{ Timeouts: timeouts, - Headers: s.getCustomHeaders(ds.JsonData, decryptedValues), + Header: s.getCustomHeaders(ds.JsonData, decryptedValues), Labels: map[string]string{ "datasource_type": ds.Type, "datasource_name": ds.Name, @@ -654,8 +654,8 @@ func (s *Service) getTimeout(ds *datasources.DataSource) time.Duration { // getCustomHeaders returns a map with all the to be set headers // The map key represents the HeaderName and the value represents this header's value -func (s *Service) getCustomHeaders(jsonData *simplejson.Json, decryptedValues map[string]string) map[string]string { - headers := make(map[string]string) +func (s *Service) getCustomHeaders(jsonData *simplejson.Json, decryptedValues map[string]string) http.Header { + headers := make(http.Header) if jsonData == nil { return headers } @@ -680,7 +680,7 @@ func (s *Service) getCustomHeaders(jsonData *simplejson.Json, decryptedValues ma } if val, ok := decryptedValues[headerValueSuffix]; ok { - headers[key] = val + headers.Add(key, val) } } @@ -769,7 +769,7 @@ func readQuotaConfig(cfg *setting.Cfg) (*quota.Map, error) { } // CustomerHeaders returns the custom headers specified in the datasource. The context is used for the decryption operation that might use the store, so consider setting an acceptable timeout for your use case. -func (s *Service) CustomHeaders(ctx context.Context, ds *datasources.DataSource) (map[string]string, error) { +func (s *Service) CustomHeaders(ctx context.Context, ds *datasources.DataSource) (http.Header, error) { values, err := s.SecretsService.DecryptJsonData(ctx, ds.SecureJsonData) if err != nil { return nil, fmt.Errorf("failed to get custom headers: %w", err) diff --git a/pkg/services/datasources/service/datasource_test.go b/pkg/services/datasources/service/datasource_test.go index c9818f4bcb4..fab5b2b47d7 100644 --- a/pkg/services/datasources/service/datasource_test.go +++ b/pkg/services/datasources/service/datasource_test.go @@ -833,7 +833,7 @@ func TestService_GetHttpTransport(t *testing.T) { require.NoError(t, err) headers := dsService.getCustomHeaders(sjson, map[string]string{"httpHeaderValue1": "Bearer xf5yhfkpsnmgo"}) - require.Equal(t, "Bearer xf5yhfkpsnmgo", headers["Authorization"]) + require.Equal(t, "Bearer xf5yhfkpsnmgo", headers.Get("Authorization")) // 1. Start HTTP test server which checks the request headers backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -902,7 +902,7 @@ func TestService_GetHttpTransport(t *testing.T) { require.NoError(t, err) headers := dsService.getCustomHeaders(sjson, map[string]string{"httpHeaderValue1": "example.com"}) - require.Equal(t, "example.com", headers["Host"]) + require.Equal(t, "example.com", headers.Get("Host")) // 1. Start HTTP test server which checks the request headers backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -1192,7 +1192,7 @@ func TestDataSource_CustomHeaders(t *testing.T) { name string jsonData *simplejson.Json secureJsonData map[string][]byte - expectedHeaders map[string]string + expectedHeaders http.Header expectedErrorMsg string }{ { @@ -1203,8 +1203,8 @@ func TestDataSource_CustomHeaders(t *testing.T) { secureJsonData: map[string][]byte{ "httpHeaderValue1": encryptedValue, }, - expectedHeaders: map[string]string{ - "X-Test-Header1": testValue, + expectedHeaders: http.Header{ + "X-Test-Header1": []string{testValue}, }, }, { @@ -1213,7 +1213,7 @@ func TestDataSource_CustomHeaders(t *testing.T) { "httpHeaderName1": "X-Test-Header1", }), secureJsonData: map[string][]byte{}, - expectedHeaders: map[string]string{}, + expectedHeaders: http.Header{}, }, { name: "non customer header value", @@ -1221,7 +1221,21 @@ func TestDataSource_CustomHeaders(t *testing.T) { "someotherheader": "X-Test-Header1", }), secureJsonData: map[string][]byte{}, - expectedHeaders: map[string]string{}, + expectedHeaders: http.Header{}, + }, + { + name: "add multiple header value", + jsonData: simplejson.NewFromAny(map[string]any{ + "httpHeaderName1": "X-Test-Header1", + "httpHeaderName2": "X-Test-Header1", + }), + secureJsonData: map[string][]byte{ + "httpHeaderValue1": encryptedValue, + "httpHeaderValue2": encryptedValue, + }, + expectedHeaders: http.Header{ + "X-Test-Header1": []string{testValue, testValue}, + }, }, } diff --git a/pkg/services/ngalert/sender/notifier_ext.go b/pkg/services/ngalert/sender/notifier_ext.go index 27effd18913..544ed4ae072 100644 --- a/pkg/services/ngalert/sender/notifier_ext.go +++ b/pkg/services/ngalert/sender/notifier_ext.go @@ -24,7 +24,7 @@ import ( // ApplyConfig updates the status state as the new config requires. // Extension: add new parameter headers. -func (n *Manager) ApplyConfig(conf *config.Config, headers map[string]map[string]string) error { +func (n *Manager) ApplyConfig(conf *config.Config, headers map[string]http.Header) error { n.mtx.Lock() defer n.mtx.Unlock() @@ -57,7 +57,7 @@ type alertmanagerSet struct { client *http.Client // Extension: headers that should be used for the http requests to the alertmanagers. - headers map[string]string + headers http.Header metrics *alertMetrics @@ -144,7 +144,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool { defer cancel() // Extension: added headers parameter. - go func(client *http.Client, url string, headers map[string]string) { + go func(client *http.Client, url string, headers http.Header) { if err := n.sendOne(ctx, client, url, payload, headers); err != nil { level.Error(n.logger).Log("alertmanager", url, "count", len(alerts), "msg", "Error sending alert", "err", err) n.metrics.errors.WithLabelValues(url).Inc() @@ -167,7 +167,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool { } // Extension: added headers parameter. -func (n *Manager) sendOne(ctx context.Context, c *http.Client, url string, b []byte, headers map[string]string) error { +func (n *Manager) sendOne(ctx context.Context, c *http.Client, url string, b []byte, headers http.Header) error { req, err := http.NewRequest("POST", url, bytes.NewReader(b)) if err != nil { return err @@ -176,7 +176,9 @@ func (n *Manager) sendOne(ctx context.Context, c *http.Client, url string, b []b req.Header.Set("Content-Type", contentTypeJSON) // Extension: set headers. for k, v := range headers { - req.Header.Set(k, v) + for _, vv := range v { + req.Header.Set(k, vv) + } } resp, err := n.opts.Do(ctx, c, req) if err != nil { diff --git a/pkg/services/ngalert/sender/sender.go b/pkg/services/ngalert/sender/sender.go index 18f16d194c1..60d60924381 100644 --- a/pkg/services/ngalert/sender/sender.go +++ b/pkg/services/ngalert/sender/sender.go @@ -43,7 +43,7 @@ type ExternalAlertmanager struct { type ExternalAMcfg struct { URL string - Headers map[string]string + Headers http.Header } type Option func(*ExternalAlertmanager) @@ -177,9 +177,9 @@ func (s *ExternalAlertmanager) DroppedAlertmanagers() []*url.URL { return s.manager.DroppedAlertmanagers() } -func buildNotifierConfig(alertmanagers []ExternalAMcfg) (*config.Config, map[string]map[string]string, error) { +func buildNotifierConfig(alertmanagers []ExternalAMcfg) (*config.Config, map[string]http.Header, error) { amConfigs := make([]*config.AlertmanagerConfig, 0, len(alertmanagers)) - headers := map[string]map[string]string{} + headers := map[string]http.Header{} for i, am := range alertmanagers { u, err := url.Parse(am.URL) if err != nil { diff --git a/pkg/tsdb/azuremonitor/httpclient.go b/pkg/tsdb/azuremonitor/httpclient.go index 77ae5779579..74cb2826981 100644 --- a/pkg/tsdb/azuremonitor/httpclient.go +++ b/pkg/tsdb/azuremonitor/httpclient.go @@ -28,7 +28,7 @@ func newHTTPClient(ctx context.Context, route types.AzRoute, model types.Datasou } for header, value := range route.Headers { - clientOpts.Headers[header] = value + clientOpts.Header.Add(header, value) } // Use Azure credentials if the route has OAuth scopes configured diff --git a/pkg/tsdb/azuremonitor/httpclient_test.go b/pkg/tsdb/azuremonitor/httpclient_test.go index d6fe1d4840c..4bd22114488 100644 --- a/pkg/tsdb/azuremonitor/httpclient_test.go +++ b/pkg/tsdb/azuremonitor/httpclient_test.go @@ -72,18 +72,18 @@ func TestHttpClient_AzureCredentials(t *testing.T) { }, } - res := map[string]string{ - "GrafanaHeader": "GrafanaValue", - "AzureHeader": "AzureValue", + res := http.Header{ + "Grafanaheader": {"GrafanaValue"}, + "Azureheader": {"AzureValue"}, } _, err := newHTTPClient(context.Background(), route, model, settings, azureSettings, provider) require.NoError(t, err) assert.NotNil(t, provider.opts) - if provider.opts.Headers != nil { - assert.Len(t, provider.opts.Headers, 2) - assert.Equal(t, res, provider.opts.Headers) + if provider.opts.Header != nil { + assert.Len(t, provider.opts.Header, 2) + assert.Equal(t, res, provider.opts.Header) } }) } diff --git a/pkg/tsdb/tempo/grpc.go b/pkg/tsdb/tempo/grpc.go index 061713f989e..12ff0155471 100644 --- a/pkg/tsdb/tempo/grpc.go +++ b/pkg/tsdb/tempo/grpc.go @@ -81,9 +81,11 @@ func getDialOpts(settings backend.DataSourceInstanceSettings, opts httpclient.Op // to the CustomHeadersMiddleware in the HTTP client provider. func CustomHeadersStreamInterceptor(httpOpts httpclient.Options) grpc.StreamClientInterceptor { return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { - if len(httpOpts.Headers) != 0 { - for key, value := range httpOpts.Headers { - ctx = metadata.AppendToOutgoingContext(ctx, key, value) + if len(httpOpts.Header) != 0 { + for key, value := range httpOpts.Header { + for _, v := range value { + ctx = metadata.AppendToOutgoingContext(ctx, key, v) + } } }