Plugins: Move config factory to pluginsintegration package (#65716)

* move config to pluginsintegration package

* change to all plugin toggle

* fixes

* fixes

* fix lerna

* fix test
This commit is contained in:
Will Browne
2023-04-05 14:40:08 +02:00
committed by GitHub
parent c9288868f4
commit bff9f4c890
12 changed files with 175 additions and 126 deletions
@@ -0,0 +1,59 @@
package config
import (
"fmt"
"strings"
pCfg "github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/setting"
)
func ProvideConfig(settingProvider setting.Provider, grafanaCfg *setting.Cfg) (*pCfg.Cfg, error) {
plugins := settingProvider.Section("plugins")
allowedUnsigned := grafanaCfg.PluginsAllowUnsigned
if len(plugins.KeyValue("allow_loading_unsigned_plugins").Value()) > 0 {
allowedUnsigned = strings.Split(plugins.KeyValue("allow_loading_unsigned_plugins").Value(), ",")
}
aws := settingProvider.Section("aws")
allowedAuth := grafanaCfg.AWSAllowedAuthProviders
if len(aws.KeyValue("allowed_auth_providers").Value()) > 0 {
allowedUnsigned = strings.Split(settingProvider.KeyValue("plugins", "allow_loading_unsigned_plugins").Value(), ",")
}
tracingCfg, err := newTracingCfg(grafanaCfg)
if err != nil {
return nil, fmt.Errorf("new opentelemetry cfg: %w", err)
}
return pCfg.NewCfg(
settingProvider.KeyValue("", "app_mode").MustBool(grafanaCfg.Env == setting.Dev),
grafanaCfg.PluginsPath,
extractPluginSettings(settingProvider),
allowedUnsigned,
allowedAuth,
aws.KeyValue("assume_role_enabled").MustBool(grafanaCfg.AWSAssumeRoleEnabled),
grafanaCfg.Azure,
grafanaCfg.BuildVersion,
grafanaCfg.PluginLogBackendRequests,
grafanaCfg.PluginsCDNURLTemplate,
tracingCfg,
), nil
}
func extractPluginSettings(settingProvider setting.Provider) setting.PluginSettings {
ps := setting.PluginSettings{}
for sectionName, sectionCopy := range settingProvider.Current() {
if !strings.HasPrefix(sectionName, "plugin.") {
continue
}
// Calling Current() returns a redacted version of section. We need to replace the map values with the unredacted values.
section := settingProvider.Section(sectionName)
for k := range sectionCopy {
sectionCopy[k] = section.KeyValue(k).MustString("")
}
pluginID := strings.Replace(sectionName, "plugin.", "", 1)
ps[pluginID] = sectionCopy
}
return ps
}
@@ -0,0 +1,38 @@
package config
import (
"testing"
"gopkg.in/ini.v1"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/require"
)
func TestPluginSettings(t *testing.T) {
raw, err := ini.Load([]byte(`
[plugins]
test_key = 123
[plugin.test-datasource]
foo = 5m
bar = something
[plugin.secret-plugin]
secret_key = secret
normal_key = not a secret`))
require.NoError(t, err)
cfg := &setting.Cfg{Raw: raw}
settings := &setting.OSSImpl{Cfg: cfg}
ps := extractPluginSettings(settings)
require.Len(t, ps, 2)
require.Len(t, ps["test-datasource"], 2)
require.Equal(t, ps["test-datasource"]["foo"], "5m")
require.Equal(t, ps["test-datasource"]["bar"], "something")
require.Len(t, ps["secret-plugin"], 2)
require.Equal(t, ps["secret-plugin"]["secret_key"], "secret")
require.Equal(t, ps["secret-plugin"]["normal_key"], "not a secret")
}
@@ -0,0 +1,27 @@
package config
import (
"fmt"
"github.com/grafana/grafana/pkg/infra/tracing"
pCfg "github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/setting"
)
// newTracingCfg creates a plugins tracing configuration based on the provided Grafana tracing config.
// If OpenTelemetry (OTLP) is disabled, a zero-value OpenTelemetryCfg is returned.
func newTracingCfg(grafanaCfg *setting.Cfg) (pCfg.Tracing, error) {
ots, err := tracing.ParseSettingsOpentelemetry(grafanaCfg)
if err != nil {
return pCfg.Tracing{}, fmt.Errorf("parse settings: %w", err)
}
if !ots.OTelExporterEnabled() {
return pCfg.Tracing{}, nil
}
return pCfg.Tracing{
OpenTelemetry: pCfg.OpenTelemetryCfg{
Address: ots.Address,
Propagation: ots.Propagation,
},
}, nil
}
@@ -0,0 +1,50 @@
package config
import (
"testing"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewTracingCfg(t *testing.T) {
t.Run("empty", func(t *testing.T) {
cfg := setting.NewCfg()
tracingCfg, err := newTracingCfg(cfg)
require.NoError(t, err)
assert.False(t, tracingCfg.IsEnabled(), "tracing should be disabled")
assert.Empty(t, tracingCfg.OpenTelemetry.Address)
assert.Empty(t, tracingCfg.OpenTelemetry.Propagation)
})
t.Run("enabled", func(t *testing.T) {
for _, tc := range []struct {
name string
propagation string
}{
{"empty", ""},
{"jaeger", "jaeger"},
{"w3c", "w3c"},
{"multiple", "jaeger,w3c"},
} {
t.Run(tc.name, func(t *testing.T) {
const address = "127.0.0.1:4317"
cfg := setting.NewCfg()
otlpSect := cfg.Raw.Section("tracing.opentelemetry.otlp")
otlpSect.Key("address").SetValue(address)
if tc.propagation != "" {
otlpSect.Key("propagation").SetValue(tc.propagation)
}
tracingCfg, err := newTracingCfg(cfg)
require.NoError(t, err)
assert.True(t, tracingCfg.IsEnabled(), "tracing should be enabled")
assert.Equal(t, address, tracingCfg.OpenTelemetry.Address)
assert.Equal(t, tc.propagation, tracingCfg.OpenTelemetry.Propagation)
})
}
})
}
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
"github.com/grafana/grafana/pkg/plugins/backendplugin/provider"
"github.com/grafana/grafana/pkg/plugins/config"
pCfg "github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/plugins/manager"
"github.com/grafana/grafana/pkg/plugins/manager/client"
"github.com/grafana/grafana/pkg/plugins/manager/filestore"
@@ -23,6 +23,7 @@ import (
"github.com/grafana/grafana/pkg/plugins/repo"
"github.com/grafana/grafana/pkg/services/oauthtoken"
"github.com/grafana/grafana/pkg/services/pluginsintegration/clientmiddleware"
"github.com/grafana/grafana/pkg/services/pluginsintegration/config"
"github.com/grafana/grafana/pkg/services/pluginsintegration/licensing"
"github.com/grafana/grafana/pkg/services/pluginsintegration/plugincontext"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
@@ -76,14 +77,14 @@ var WireExtensionSet = wire.NewSet(
finder.NewLocalFinder,
)
func ProvideClientDecorator(cfg *setting.Cfg, pCfg *config.Cfg,
func ProvideClientDecorator(cfg *setting.Cfg, pCfg *pCfg.Cfg,
pluginRegistry registry.Service,
oAuthTokenService oauthtoken.OAuthTokenService,
tracer tracing.Tracer) (*client.Decorator, error) {
return NewClientDecorator(cfg, pCfg, pluginRegistry, oAuthTokenService, tracer)
}
func NewClientDecorator(cfg *setting.Cfg, pCfg *config.Cfg,
func NewClientDecorator(cfg *setting.Cfg, pCfg *pCfg.Cfg,
pluginRegistry registry.Service,
oAuthTokenService oauthtoken.OAuthTokenService,
tracer tracing.Tracer) (*client.Decorator, error) {