Plugins: Backend: Skip host environment variables (#77858)
* Add pluginsSkipHostEnvVars feature flag * Set go-plugin's SkipHostEnvVar depending on feature flags * add missing file * Re-generate feature flags * Add allowedHostEnvVarNames * Fix feature toggles not being passed to plugin context service's plugin env vars * Fix tests * PR review feedback: Use cfg.Features * Fix tests * PR review feedback: removed DefaultProviderWithFeatures * merge with master * fix tests * use features.IsEnabledGlobally
This commit is contained in:
@@ -38,6 +38,7 @@ func TestInitializer_envVars(t *testing.T) {
|
||||
"custom_env_var": "customVal",
|
||||
},
|
||||
},
|
||||
Features: featuremgmt.WithFeatures(),
|
||||
}, licensing)
|
||||
|
||||
envVars := envVarsProvider.Get(context.Background(), p)
|
||||
@@ -51,6 +52,76 @@ func TestInitializer_envVars(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestInitializer_skipHostEnvVars(t *testing.T) {
|
||||
const (
|
||||
envVarName = "HTTP_PROXY"
|
||||
envVarValue = "lorem ipsum"
|
||||
)
|
||||
|
||||
t.Setenv(envVarName, envVarValue)
|
||||
|
||||
p := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "test",
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("without FlagPluginsSkipHostEnvVars should not populate host env vars", func(t *testing.T) {
|
||||
envVarsProvider := NewProvider(&config.Cfg{Features: featuremgmt.WithFeatures()}, nil)
|
||||
envVars := envVarsProvider.Get(context.Background(), p)
|
||||
|
||||
// We want to test that the envvars.Provider does not add any of the host env vars.
|
||||
// When starting the plugin via go-plugin, ALL host env vars will be added by go-plugin,
|
||||
// but we are testing the envvars.Provider here, so that's outside the scope of this test.
|
||||
_, ok := getEnvVarWithExists(envVars, envVarName)
|
||||
require.False(t, ok, "host env var should not be present")
|
||||
})
|
||||
|
||||
t.Run("with FlagPluginsSkipHostEnvVars", func(t *testing.T) {
|
||||
envVarsProvider := NewProvider(&config.Cfg{
|
||||
Features: featuremgmt.WithFeatures(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
}, nil)
|
||||
|
||||
t.Run("should populate allowed host env vars", func(t *testing.T) {
|
||||
// Set all allowed variables
|
||||
for _, ev := range allowedHostEnvVarNames {
|
||||
t.Setenv(ev, envVarValue)
|
||||
}
|
||||
envVars := envVarsProvider.Get(context.Background(), p)
|
||||
|
||||
// Test against each variable
|
||||
for _, expEvName := range allowedHostEnvVarNames {
|
||||
gotEvValue, ok := getEnvVarWithExists(envVars, expEvName)
|
||||
require.True(t, ok, "host env var should be present")
|
||||
require.Equal(t, envVarValue, gotEvValue)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should not populate host env vars that aren't allowed", func(t *testing.T) {
|
||||
// Set all allowed variables
|
||||
for _, ev := range allowedHostEnvVarNames {
|
||||
t.Setenv(ev, envVarValue)
|
||||
}
|
||||
// ...and an extra one, which should not leak
|
||||
const superSecretEnvVariableName = "SUPER_SECRET_VALUE"
|
||||
t.Setenv(superSecretEnvVariableName, "01189998819991197253")
|
||||
envVars := envVarsProvider.Get(context.Background(), p)
|
||||
|
||||
// Super secret should not leak
|
||||
_, ok := getEnvVarWithExists(envVars, superSecretEnvVariableName)
|
||||
require.False(t, ok, "super secret env var should not be leaked")
|
||||
|
||||
// Everything else should be present
|
||||
for _, expEvName := range allowedHostEnvVarNames {
|
||||
var gotEvValue string
|
||||
gotEvValue, ok = getEnvVarWithExists(envVars, expEvName)
|
||||
require.True(t, ok, "host env var should be present")
|
||||
require.Equal(t, envVarValue, gotEvValue)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestInitializer_tracingEnvironmentVariables(t *testing.T) {
|
||||
const pluginID = "plugin_id"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user