mirror of https://github.com/grafana/grafana
[WIP] Plugins: Introduce Plugins specific config (#54854)
parent
8b38f9408d
commit
0571d98bba
@ -1,53 +0,0 @@ |
||||
package plugins |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana-azure-sdk-go/azsettings" |
||||
|
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
type Cfg struct { |
||||
DevMode bool |
||||
|
||||
PluginsPath string |
||||
|
||||
PluginSettings setting.PluginSettings |
||||
PluginsAllowUnsigned []string |
||||
|
||||
EnterpriseLicensePath string |
||||
|
||||
// AWS Plugin Auth
|
||||
AWSAllowedAuthProviders []string |
||||
AWSAssumeRoleEnabled bool |
||||
|
||||
// Azure Cloud settings
|
||||
Azure *azsettings.AzureSettings |
||||
|
||||
BuildVersion string // TODO Remove
|
||||
} |
||||
|
||||
func NewCfg() *Cfg { |
||||
return &Cfg{} |
||||
} |
||||
|
||||
func FromGrafanaCfg(grafanaCfg *setting.Cfg) *Cfg { |
||||
cfg := &Cfg{} |
||||
|
||||
cfg.DevMode = grafanaCfg.Env == setting.Dev |
||||
cfg.PluginsPath = grafanaCfg.PluginsPath |
||||
|
||||
cfg.PluginSettings = grafanaCfg.PluginSettings |
||||
cfg.PluginsAllowUnsigned = grafanaCfg.PluginsAllowUnsigned |
||||
cfg.EnterpriseLicensePath = grafanaCfg.EnterpriseLicensePath |
||||
|
||||
// AWS
|
||||
cfg.AWSAllowedAuthProviders = grafanaCfg.AWSAllowedAuthProviders |
||||
cfg.AWSAssumeRoleEnabled = grafanaCfg.AWSAssumeRoleEnabled |
||||
|
||||
// Azure
|
||||
cfg.Azure = grafanaCfg.Azure |
||||
|
||||
cfg.BuildVersion = grafanaCfg.BuildVersion |
||||
|
||||
return cfg |
||||
} |
||||
@ -0,0 +1,86 @@ |
||||
package config |
||||
|
||||
import ( |
||||
"strings" |
||||
|
||||
"github.com/grafana/grafana-azure-sdk-go/azsettings" |
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
type Cfg struct { |
||||
log log.Logger |
||||
|
||||
DevMode bool |
||||
|
||||
PluginSettings setting.PluginSettings |
||||
PluginsAllowUnsigned []string |
||||
|
||||
EnterpriseLicensePath string |
||||
|
||||
// AWS Plugin Auth
|
||||
AWSAllowedAuthProviders []string |
||||
AWSAssumeRoleEnabled bool |
||||
|
||||
// Azure Cloud settings
|
||||
Azure *azsettings.AzureSettings |
||||
|
||||
BuildVersion string // TODO Remove
|
||||
} |
||||
|
||||
func ProvideConfig(settingProvider setting.Provider, grafanaCfg *setting.Cfg) *Cfg { |
||||
return NewCfg(settingProvider, grafanaCfg) |
||||
} |
||||
|
||||
func NewCfg(settingProvider setting.Provider, grafanaCfg *setting.Cfg) *Cfg { |
||||
logger := log.New("plugin.cfg") |
||||
|
||||
azure := settingProvider.Section("azure") |
||||
aws := settingProvider.Section("aws") |
||||
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(), ",") |
||||
} |
||||
|
||||
allowedAuth := grafanaCfg.AWSAllowedAuthProviders |
||||
if len(aws.KeyValue("allowed_auth_providers").Value()) > 0 { |
||||
allowedUnsigned = strings.Split(settingProvider.KeyValue("plugins", "allow_loading_unsigned_plugins").Value(), ",") |
||||
} |
||||
|
||||
return &Cfg{ |
||||
log: logger, |
||||
BuildVersion: grafanaCfg.BuildVersion, |
||||
DevMode: settingProvider.KeyValue("", "app_mode").MustBool(grafanaCfg.Env == setting.Dev), |
||||
EnterpriseLicensePath: settingProvider.KeyValue("enterprise", "license_path").MustString(grafanaCfg.EnterpriseLicensePath), |
||||
PluginSettings: extractPluginSettings(settingProvider), |
||||
PluginsAllowUnsigned: allowedUnsigned, |
||||
AWSAllowedAuthProviders: allowedAuth, |
||||
AWSAssumeRoleEnabled: aws.KeyValue("assume_role_enabled").MustBool(grafanaCfg.AWSAssumeRoleEnabled), |
||||
Azure: &azsettings.AzureSettings{ |
||||
Cloud: azure.KeyValue("cloud").MustString(grafanaCfg.Azure.Cloud), |
||||
ManagedIdentityEnabled: azure.KeyValue("managed_identity_enabled").MustBool(grafanaCfg.Azure.ManagedIdentityEnabled), |
||||
ManagedIdentityClientId: azure.KeyValue("managed_identity_client_id").MustString(grafanaCfg.Azure.ManagedIdentityClientId), |
||||
}, |
||||
} |
||||
} |
||||
|
||||
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") |
||||
} |
||||
Loading…
Reference in new issue