diff --git a/pkg/services/pluginsintegration/plugins_integration_test.go b/pkg/services/pluginsintegration/plugins_integration_test.go index c10bdb00662..f42099c05da 100644 --- a/pkg/services/pluginsintegration/plugins_integration_test.go +++ b/pkg/services/pluginsintegration/plugins_integration_test.go @@ -78,7 +78,7 @@ func TestIntegrationPluginManager(t *testing.T) { idb := influxdb.ProvideService(hcp, features) lk := loki.ProvideService(hcp, features, tracer) otsdb := opentsdb.ProvideService(hcp) - pr := prometheus.ProvideService(hcp, cfg) + pr := prometheus.ProvideService(hcp) tmpo := tempo.ProvideService(hcp) td := testdatasource.ProvideService() pg := postgres.ProvideService(cfg) diff --git a/pkg/tsdb/prometheus/azureauth/azure_test.go b/pkg/tsdb/prometheus/azureauth/azure_test.go index 7a35300d183..bda541f8acb 100644 --- a/pkg/tsdb/prometheus/azureauth/azure_test.go +++ b/pkg/tsdb/prometheus/azureauth/azure_test.go @@ -8,14 +8,10 @@ import ( sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "github.com/grafana/grafana/pkg/setting" ) func TestConfigureAzureAuthentication(t *testing.T) { - cfg := &setting.Cfg{ - Azure: &azsettings.AzureSettings{}, - } + cfgAzure := &azsettings.AzureSettings{} t.Run("should set Azure middleware when JsonData contains valid credentials", func(t *testing.T) { settings := backend.DataSourceInstanceSettings{ @@ -29,7 +25,7 @@ func TestConfigureAzureAuthentication(t *testing.T) { var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}} - err := ConfigureAzureAuthentication(settings, cfg.Azure, opts) + err := ConfigureAzureAuthentication(settings, cfgAzure, opts) require.NoError(t, err) require.NotNil(t, opts.Middlewares) @@ -43,7 +39,7 @@ func TestConfigureAzureAuthentication(t *testing.T) { var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}} - err := ConfigureAzureAuthentication(settings, cfg.Azure, opts) + err := ConfigureAzureAuthentication(settings, cfgAzure, opts) require.NoError(t, err) assert.NotContains(t, opts.CustomOptions, "_azureCredentials") @@ -58,7 +54,7 @@ func TestConfigureAzureAuthentication(t *testing.T) { } var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}} - err := ConfigureAzureAuthentication(settings, cfg.Azure, opts) + err := ConfigureAzureAuthentication(settings, cfgAzure, opts) assert.Error(t, err) }) @@ -74,7 +70,7 @@ func TestConfigureAzureAuthentication(t *testing.T) { } var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}} - err := ConfigureAzureAuthentication(settings, cfg.Azure, opts) + err := ConfigureAzureAuthentication(settings, cfgAzure, opts) require.NoError(t, err) require.NotNil(t, opts.Middlewares) @@ -90,7 +86,7 @@ func TestConfigureAzureAuthentication(t *testing.T) { } var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}} - err := ConfigureAzureAuthentication(settings, cfg.Azure, opts) + err := ConfigureAzureAuthentication(settings, cfgAzure, opts) require.NoError(t, err) if opts.Middlewares != nil { @@ -111,7 +107,7 @@ func TestConfigureAzureAuthentication(t *testing.T) { var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}} - err := ConfigureAzureAuthentication(settings, cfg.Azure, opts) + err := ConfigureAzureAuthentication(settings, cfgAzure, opts) assert.Error(t, err) }) } diff --git a/pkg/tsdb/prometheus/client/transport.go b/pkg/tsdb/prometheus/client/transport.go index ce7db24e81b..a333e04fafb 100644 --- a/pkg/tsdb/prometheus/client/transport.go +++ b/pkg/tsdb/prometheus/client/transport.go @@ -5,13 +5,13 @@ import ( "fmt" "strings" + "github.com/grafana/grafana-azure-sdk-go/azsettings" "github.com/grafana/grafana-azure-sdk-go/util/maputil" "github.com/grafana/grafana-plugin-sdk-go/backend" sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana-plugin-sdk-go/backend/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" @@ -19,7 +19,7 @@ import ( // CreateTransportOptions creates options for the http client. Probably should be shared and should not live in the // buffered package. -func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, cfg *setting.Cfg, logger log.Logger) (*sdkhttpclient.Options, error) { +func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, logger log.Logger) (*sdkhttpclient.Options, error) { opts, err := settings.HTTPClientOptions(ctx) if err != nil { return nil, fmt.Errorf("error getting HTTP options: %w", err) @@ -38,9 +38,15 @@ func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInst opts.SigV4.Service = "aps" } + azureSettings, err := azsettings.ReadSettings(ctx) + if err != nil { + logger.Error("failed to read Azure settings from Grafana", "error", err.Error()) + return nil, fmt.Errorf("failed to read Azure settings from Grafana: %v", err) + } + // Set Azure authentication - if cfg.AzureAuthEnabled { - err = azureauth.ConfigureAzureAuthentication(settings, cfg.Azure, &opts) + if azureSettings.AzureAuthEnabled { + err = azureauth.ConfigureAzureAuthentication(settings, azureSettings, &opts) if err != nil { return nil, fmt.Errorf("error configuring Azure auth: %v", err) } diff --git a/pkg/tsdb/prometheus/client/transport_test.go b/pkg/tsdb/prometheus/client/transport_test.go index f609ec0af7d..de7b165c6f6 100644 --- a/pkg/tsdb/prometheus/client/transport_test.go +++ b/pkg/tsdb/prometheus/client/transport_test.go @@ -7,8 +7,6 @@ import ( "github.com/grafana/grafana-azure-sdk-go/azsettings" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/stretchr/testify/require" - - "github.com/grafana/grafana/pkg/setting" ) func TestCreateTransportOptions(t *testing.T) { @@ -21,13 +19,17 @@ func TestCreateTransportOptions(t *testing.T) { "httpHeaderValue1": "bar", }, } - opts, err := CreateTransportOptions(context.Background(), settings, &setting.Cfg{}, backend.NewLoggerWith("logger", "test")) + 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, 2, len(opts.Middlewares)) }) t.Run("add azure credentials if configured", func(t *testing.T) { + cfg := backend.NewGrafanaCfg(map[string]string{ + azsettings.AzureCloud: azsettings.AzurePublic, + azsettings.AzureAuthEnabled: "true", + }) settings := backend.DataSourceInstanceSettings{ BasicAuthEnabled: false, BasicAuthUser: "", @@ -38,7 +40,8 @@ func TestCreateTransportOptions(t *testing.T) { }`), DecryptedSecureJSONData: map[string]string{}, } - opts, err := CreateTransportOptions(context.Background(), settings, &setting.Cfg{AzureAuthEnabled: true, Azure: &azsettings.AzureSettings{}}, backend.NewLoggerWith("logger", "test")) + ctx := backend.WithGrafanaConfig(context.Background(), cfg) + opts, err := CreateTransportOptions(ctx, settings, backend.NewLoggerWith("logger", "test")) require.NoError(t, err) require.Equal(t, 3, len(opts.Middlewares)) }) diff --git a/pkg/tsdb/prometheus/healthcheck_test.go b/pkg/tsdb/prometheus/healthcheck_test.go index 88d55c25ca3..b50c6c649f7 100644 --- a/pkg/tsdb/prometheus/healthcheck_test.go +++ b/pkg/tsdb/prometheus/healthcheck_test.go @@ -12,8 +12,6 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/stretchr/testify/assert" - - "github.com/grafana/grafana/pkg/setting" ) type healthCheckProvider[T http.RoundTripper] struct { @@ -84,7 +82,7 @@ func Test_healthcheck(t *testing.T) { t.Run("should do a successful health check", func(t *testing.T) { httpProvider := getMockProvider[*healthCheckSuccessRoundTripper]() s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), } req := &backend.CheckHealthRequest{ @@ -100,7 +98,7 @@ func Test_healthcheck(t *testing.T) { t.Run("should return an error for an unsuccessful health check", func(t *testing.T) { httpProvider := getMockProvider[*healthCheckFailRoundTripper]() s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), } req := &backend.CheckHealthRequest{ diff --git a/pkg/tsdb/prometheus/heuristics_test.go b/pkg/tsdb/prometheus/heuristics_test.go index 89f1d5b9a88..58a9befc683 100644 --- a/pkg/tsdb/prometheus/heuristics_test.go +++ b/pkg/tsdb/prometheus/heuristics_test.go @@ -14,8 +14,6 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" - - "github.com/grafana/grafana/pkg/setting" ) type heuristicsSuccessRoundTripper struct { @@ -52,7 +50,7 @@ func Test_GetHeuristics(t *testing.T) { //httpProvider := getHeuristicsMockProvider(&rt) httpProvider := newHeuristicsSDKProvider(rt) s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), } req := HeuristicsRequest{ @@ -72,7 +70,7 @@ func Test_GetHeuristics(t *testing.T) { } httpProvider := newHeuristicsSDKProvider(rt) s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), } req := HeuristicsRequest{ diff --git a/pkg/tsdb/prometheus/prometheus.go b/pkg/tsdb/prometheus/prometheus.go index ab91aaf7394..a6b92b87000 100644 --- a/pkg/tsdb/prometheus/prometheus.go +++ b/pkg/tsdb/prometheus/prometheus.go @@ -16,7 +16,6 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" - "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/tsdb/prometheus/client" "github.com/grafana/grafana/pkg/tsdb/prometheus/instrumentation" "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata" @@ -34,19 +33,19 @@ type instance struct { versionCache *cache.Cache } -func ProvideService(httpClientProvider *httpclient.Provider, cfg *setting.Cfg) *Service { +func ProvideService(httpClientProvider *httpclient.Provider) *Service { plog := backend.NewLoggerWith("logger", "tsdb.prometheus") plog.Debug("Initializing") return &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, cfg, plog)), + im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, plog)), logger: plog, } } -func newInstanceSettings(httpClientProvider *httpclient.Provider, cfg *setting.Cfg, log log.Logger) datasource.InstanceFactoryFunc { +func newInstanceSettings(httpClientProvider *httpclient.Provider, log log.Logger) datasource.InstanceFactoryFunc { return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { // Creates a http roundTripper. - opts, err := client.CreateTransportOptions(ctx, settings, cfg, log) + opts, err := client.CreateTransportOptions(ctx, settings, log) if err != nil { return nil, fmt.Errorf("error creating transport options: %v", err) } diff --git a/pkg/tsdb/prometheus/prometheus_test.go b/pkg/tsdb/prometheus/prometheus_test.go index 02307493664..1d21212a18f 100644 --- a/pkg/tsdb/prometheus/prometheus_test.go +++ b/pkg/tsdb/prometheus/prometheus_test.go @@ -11,8 +11,6 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/stretchr/testify/require" - - "github.com/grafana/grafana/pkg/setting" ) type fakeSender struct{} @@ -69,7 +67,7 @@ func TestService(t *testing.T) { f := &fakeHTTPClientProvider{} httpProvider := getMockPromTestSDKProvider(f) service := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), } req := &backend.CallResourceRequest{ diff --git a/pkg/tsdb/prometheus/querydata/request_test.go b/pkg/tsdb/prometheus/querydata/request_test.go index 488d676f25e..79163a59905 100644 --- a/pkg/tsdb/prometheus/querydata/request_test.go +++ b/pkg/tsdb/prometheus/querydata/request_test.go @@ -24,7 +24,6 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/data" - "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/tsdb/prometheus/client" "github.com/grafana/grafana/pkg/tsdb/prometheus/models" "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata" @@ -441,7 +440,7 @@ func setup() (*testContext, error) { JSONData: json.RawMessage(`{"timeInterval": "15s"}`), } - opts, err := client.CreateTransportOptions(context.Background(), settings, &setting.Cfg{}, log.New()) + opts, err := client.CreateTransportOptions(context.Background(), settings, log.New()) if err != nil { return nil, err }