[v10.4.x] Chore: Update grafana-plugin-sdk (#85087)
* Chore: Update grafana-plugin-sdk (#84289)
(cherry picked from commit 265200799d)
* Re-generate openapi
---------
Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
Co-authored-by: Giuseppe Guerra <giuseppe@guerra.in>
This commit is contained in:
co-authored by
Andres Martinez Gotor
Giuseppe Guerra
parent
6a8944a76e
commit
e1fc1e9a13
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-azure-sdk-go/azsettings"
|
||||
@@ -21,7 +22,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))
|
||||
})
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user