Plugins: Remove various custom headers logic (#54146) (#54284)

Removes various custom headers logic sprinkled around in the backend.
It should automatically be applied to outgoing HTTP requests via the
CustomHeadersMiddleware.
This also removes decryption of SecureJSONData to populate custom
headers in ngalert which seemed to have caused a ton of CPU usage.

(cherry picked from commit 87afd9cadc)
This commit is contained in:
Marcus Efraimsson
2022-08-29 10:21:34 +02:00
committed by GitHub
parent 200a901829
commit c8a313c289
9 changed files with 228 additions and 169 deletions
-32
View File
@@ -3,12 +3,10 @@ package service
import (
"context"
"fmt"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/adapters"
"github.com/grafana/grafana/pkg/services/datasources"
@@ -16,11 +14,6 @@ import (
"github.com/grafana/grafana/pkg/tsdb/legacydata"
)
const (
headerName = "httpHeaderName"
headerValue = "httpHeaderValue"
)
var oAuthIsOAuthPassThruEnabledFunc = func(oAuthTokenService oauthtoken.OAuthTokenService, ds *datasources.DataSource) bool {
return oAuthTokenService.IsOAuthPassThruEnabled(ds)
}
@@ -126,11 +119,6 @@ func generateRequest(ctx context.Context, ds *datasources.DataSource, decryptedJ
Headers: query.Headers,
}
// Apply Configured Custom Headers to query request.
for k, v := range customHeaders(ds.JsonData, instanceSettings.DecryptedSecureJSONData) {
req.Headers[k] = v
}
for _, q := range query.Queries {
modelJSON, err := q.Model.MarshalJSON()
if err != nil {
@@ -151,24 +139,4 @@ func generateRequest(ctx context.Context, ds *datasources.DataSource, decryptedJ
return req, nil
}
func customHeaders(jsonData *simplejson.Json, decryptedJsonData map[string]string) map[string]string {
if jsonData == nil {
return nil
}
data := jsonData.MustMap()
headers := map[string]string{}
for k := range data {
if strings.HasPrefix(k, headerName) {
if header, ok := data[k].(string); ok {
valueKey := strings.ReplaceAll(k, headerName, headerValue)
headers[header] = decryptedJsonData[valueKey]
}
}
}
return headers
}
var _ legacydata.RequestHandler = &Service{}
@@ -61,39 +61,6 @@ func TestHandleRequest(t *testing.T) {
})
}
func Test_generateRequest(t *testing.T) {
t.Run("Should attach custom headers to request if present", func(t *testing.T) {
jsonData := simplejson.New()
jsonData.Set(headerName+"testOne", "x-test-one")
jsonData.Set("testOne", "x-test-wrong")
jsonData.Set(headerName+"testTwo", "x-test-two")
decryptedJsonData := map[string]string{
headerValue + "testOne": "secret-value-one",
headerValue + "testTwo": "secret-value-two",
"something": "else",
}
ds := &datasources.DataSource{Id: 12, Type: "unregisteredType", JsonData: jsonData}
query := legacydata.DataQuery{
TimeRange: &legacydata.DataTimeRange{},
Queries: []legacydata.DataSubQuery{
{RefID: "A", DataSource: &datasources.DataSource{Id: 1, Type: "test"}, Model: simplejson.New()},
{RefID: "B", DataSource: &datasources.DataSource{Id: 1, Type: "test"}, Model: simplejson.New()},
},
}
req, err := generateRequest(context.Background(), ds, decryptedJsonData, query)
require.NoError(t, err)
require.NotNil(t, req)
require.EqualValues(t,
map[string]string{
"x-test-one": "secret-value-one",
"x-test-two": "secret-value-two",
}, req.Headers)
})
}
type fakePluginsClient struct {
plugins.Client
backend.QueryDataHandlerFunc