Plugins: Add forward_host_env_vars setting (#79333)
* Plugins: Add forward_host_env_vars_plugins setting * Renamed forward_host_env_vars_plugins to forward_host_env_vars * Add readPluginIDsList * refactoring * lint * Use util.SplitString
This commit is contained in:
@@ -18,6 +18,7 @@ type Cfg struct {
|
||||
PluginSettings setting.PluginSettings
|
||||
PluginsAllowUnsigned []string
|
||||
DisablePlugins []string
|
||||
ForwardHostEnvVars []string
|
||||
|
||||
// AWS Plugin Auth
|
||||
AWSAllowedAuthProviders []string
|
||||
@@ -52,7 +53,7 @@ type Cfg struct {
|
||||
func NewCfg(devMode bool, pluginsPath string, pluginSettings setting.PluginSettings, pluginsAllowUnsigned []string,
|
||||
awsAllowedAuthProviders []string, awsAssumeRoleEnabled bool, awsExternalId string, azure *azsettings.AzureSettings, secureSocksDSProxy setting.SecureSocksDSProxySettings,
|
||||
grafanaVersion string, logDatasourceRequests bool, pluginsCDNURLTemplate string, appURL string, appSubURL string, tracing Tracing, features plugins.FeatureToggles, angularSupportEnabled bool,
|
||||
grafanaComURL string, disablePlugins []string, hideAngularDeprecation []string) *Cfg {
|
||||
grafanaComURL string, disablePlugins []string, hideAngularDeprecation []string, forwardHostEnvVars []string) *Cfg {
|
||||
return &Cfg{
|
||||
log: log.New("plugin.cfg"),
|
||||
PluginsPath: pluginsPath,
|
||||
@@ -75,5 +76,6 @@ func NewCfg(devMode bool, pluginsPath string, pluginSettings setting.PluginSetti
|
||||
Features: features,
|
||||
AngularSupportEnabled: angularSupportEnabled,
|
||||
HideAngularDeprecation: hideAngularDeprecation,
|
||||
ForwardHostEnvVars: forwardHostEnvVars,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package bootstrap
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
@@ -157,12 +158,12 @@ func configureAppChildPlugin(cfg *config.Cfg, parent *plugins.Plugin, child *plu
|
||||
}
|
||||
|
||||
// SkipHostEnvVarsDecorateFunc returns a DecorateFunc that configures the SkipHostEnvVars field of the plugin.
|
||||
// It will be set to true if the FlagPluginsSkipHostEnvVars feature flag is set, and the plugin does not have
|
||||
// forward_host_env_vars = true in its plugin settings.
|
||||
// It will be set to true if the FlagPluginsSkipHostEnvVars feature flag is set, and the plugin is not present in the
|
||||
// ForwardHostEnvVars plugin ids list.
|
||||
func SkipHostEnvVarsDecorateFunc(cfg *config.Cfg) DecorateFunc {
|
||||
return func(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
p.SkipHostEnvVars = cfg.Features.IsEnabledGlobally(featuremgmt.FlagPluginsSkipHostEnvVars) &&
|
||||
cfg.PluginSettings[p.ID]["forward_host_env_vars"] != "true"
|
||||
!slices.Contains(cfg.ForwardHostEnvVars, p.ID)
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func TestSetDefaultNavURL(t *testing.T) {
|
||||
@@ -172,34 +171,34 @@ func TestSkipEnvVarsDecorateFunc(t *testing.T) {
|
||||
t.Run("plugin setting", func(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
pluginSettings setting.PluginSettings
|
||||
forwardHostEnvVars []string
|
||||
expSkipHostEnvVars bool
|
||||
}{
|
||||
{
|
||||
name: "forward_host_env_vars = false should set SkipHostEnvVars to true",
|
||||
pluginSettings: setting.PluginSettings{pluginID: map[string]string{"forward_host_env_vars": "false"}},
|
||||
name: "plugin id not present in forwardHostEnvVars should set SkipHostEnvVars to true (empty)",
|
||||
forwardHostEnvVars: []string{},
|
||||
expSkipHostEnvVars: true,
|
||||
},
|
||||
{
|
||||
name: "forward_host_env_vars = true should set SkipHostEnvVars to false",
|
||||
pluginSettings: setting.PluginSettings{pluginID: map[string]string{"forward_host_env_vars": "true"}},
|
||||
name: "plugin id not present in forwardHostEnvVars should set SkipHostEnvVars to true (other id)",
|
||||
forwardHostEnvVars: []string{"other-id", "yet-another-id"},
|
||||
expSkipHostEnvVars: true,
|
||||
},
|
||||
{
|
||||
name: "plugin id in forwardHostEnvVars should set SkipHostEnvVars to false (only)",
|
||||
forwardHostEnvVars: []string{pluginID},
|
||||
expSkipHostEnvVars: false,
|
||||
},
|
||||
{
|
||||
name: "invalid forward_host_env_vars should set SkipHostEnvVars to true",
|
||||
pluginSettings: setting.PluginSettings{pluginID: map[string]string{"forward_host_env_vars": "grilled cheese sandwich with bacon"}},
|
||||
expSkipHostEnvVars: true,
|
||||
},
|
||||
{
|
||||
name: "forward_host_env_vars absent should set SkipHostEnvVars to true",
|
||||
pluginSettings: setting.PluginSettings{pluginID: nil},
|
||||
expSkipHostEnvVars: true,
|
||||
name: "plugin id in forwardHostEnvVars should set SkipHostEnvVars to false (with other)",
|
||||
forwardHostEnvVars: []string{"a-plugin", pluginID, "other-id"},
|
||||
expSkipHostEnvVars: false,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.Cfg{
|
||||
Features: featuremgmt.WithFeatures(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
PluginSettings: tc.pluginSettings,
|
||||
Features: featuremgmt.WithFeatures(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
ForwardHostEnvVars: tc.forwardHostEnvVars,
|
||||
})
|
||||
p, err := f(context.Background(), &plugins.Plugin{JSONData: plugins.JSONData{ID: pluginID}})
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user