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:
@@ -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)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user