Share azureauth between prometheus clients (#58122)
* Move azureauth to upper package * Refactor http transport options
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth"
|
||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
|
||||
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
|
||||
"github.com/grafana/grafana/pkg/util/maputil"
|
||||
"github.com/prometheus/client_golang/api"
|
||||
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||
)
|
||||
|
||||
// CreateTransportOptions creates options for the http client. Probably should be shared and should not live in the
|
||||
// buffered package.
|
||||
func CreateTransportOptions(settings backend.DataSourceInstanceSettings, cfg *setting.Cfg, logger log.Logger) (*sdkhttpclient.Options, error) {
|
||||
opts, err := settings.HTTPClientOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jsonData, err := utils.GetJsonData(settings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading settings: %w", err)
|
||||
}
|
||||
httpMethod, _ := maputil.GetStringOptional(jsonData, "httpMethod")
|
||||
|
||||
opts.Middlewares = middlewares(logger, httpMethod)
|
||||
|
||||
// Set SigV4 service namespace
|
||||
if opts.SigV4 != nil {
|
||||
opts.SigV4.Service = "aps"
|
||||
}
|
||||
|
||||
// Set Azure authentication
|
||||
if cfg.AzureAuthEnabled {
|
||||
err = azureauth.ConfigureAzureAuthentication(settings, cfg.Azure, &opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error configuring Azure auth: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &opts, nil
|
||||
}
|
||||
|
||||
func CreateAPIClient(roundTripper http.RoundTripper, url string) (apiv1.API, error) {
|
||||
cfg := api.Config{
|
||||
Address: url,
|
||||
RoundTripper: roundTripper,
|
||||
}
|
||||
|
||||
client, err := api.NewClient(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return apiv1.NewAPI(client), nil
|
||||
}
|
||||
|
||||
func middlewares(logger log.Logger, httpMethod string) []sdkhttpclient.Middleware {
|
||||
middlewares := []sdkhttpclient.Middleware{
|
||||
// TODO: probably isn't needed anymore and should by done by http infra code
|
||||
middleware.CustomQueryParameters(logger),
|
||||
sdkhttpclient.CustomHeadersMiddleware(),
|
||||
}
|
||||
|
||||
// Needed to control GET vs POST method of the requests
|
||||
if strings.ToLower(httpMethod) == "get" {
|
||||
middlewares = append(middlewares, middleware.ForceHttpGet(logger))
|
||||
}
|
||||
|
||||
return middlewares
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-azure-sdk-go/azsettings"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/infra/log/logtest"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCreateTransportOptions(t *testing.T) {
|
||||
t.Run("creates correct options object", func(t *testing.T) {
|
||||
settings := backend.DataSourceInstanceSettings{
|
||||
BasicAuthEnabled: false,
|
||||
BasicAuthUser: "",
|
||||
JSONData: []byte(`{"httpHeaderName1": "foo"}`),
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"httpHeaderValue1": "bar",
|
||||
},
|
||||
}
|
||||
opts, err := CreateTransportOptions(settings, &setting.Cfg{}, &logtest.Fake{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, map[string]string{"foo": "bar"}, opts.Headers)
|
||||
require.Equal(t, 2, len(opts.Middlewares))
|
||||
})
|
||||
|
||||
t.Run("add azure credentials if configured", func(t *testing.T) {
|
||||
settings := backend.DataSourceInstanceSettings{
|
||||
BasicAuthEnabled: false,
|
||||
BasicAuthUser: "",
|
||||
JSONData: []byte(`{
|
||||
"azureCredentials": {
|
||||
"authType": "msi"
|
||||
}
|
||||
}`),
|
||||
DecryptedSecureJSONData: map[string]string{},
|
||||
}
|
||||
opts, err := CreateTransportOptions(settings, &setting.Cfg{AzureAuthEnabled: true, Azure: &azsettings.AzureSettings{}}, &logtest.Fake{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 3, len(opts.Middlewares))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user