Fix env var naming (#3438)

closes #3436
This commit is contained in:
qwerty287 2024-02-25 10:12:40 +01:00 committed by GitHub
parent 5b0430ab4a
commit 9b0c4e4e3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 14 deletions

View file

@ -106,12 +106,12 @@ func (c *Compiler) createProcess(container *yaml_types.Container, stepType backe
// TODO: why don't we pass secrets to detached steps?
if !detached {
if err := settings.ParamsToEnv(container.Settings, environment, getSecretValue); err != nil {
if err := settings.ParamsToEnv(container.Settings, environment, "PLUGIN_", getSecretValue); err != nil {
return nil, err
}
}
if err := settings.ParamsToEnv(container.Environment, environment, getSecretValue); err != nil {
if err := settings.ParamsToEnv(container.Environment, environment, "", getSecretValue); err != nil {
return nil, err
}

View file

@ -26,7 +26,7 @@ import (
// ParamsToEnv uses reflection to convert a map[string]interface to a list
// of environment variables.
func ParamsToEnv(from map[string]any, to map[string]string, getSecretValue func(name string) (string, error)) (err error) {
func ParamsToEnv(from map[string]any, to map[string]string, prefix string, getSecretValue func(name string) (string, error)) (err error) {
if to == nil {
return fmt.Errorf("no map to write to")
}
@ -34,7 +34,7 @@ func ParamsToEnv(from map[string]any, to map[string]string, getSecretValue func(
if v == nil || len(k) == 0 {
continue
}
to[sanitizeParamKey(k)], err = sanitizeParamValue(v, getSecretValue)
to[sanitizeParamKey(prefix, k)], err = sanitizeParamValue(v, getSecretValue)
if err != nil {
return err
}
@ -43,8 +43,8 @@ func ParamsToEnv(from map[string]any, to map[string]string, getSecretValue func(
}
// format the environment variable key
func sanitizeParamKey(k string) string {
return "PLUGIN_" + strings.ToUpper(
func sanitizeParamKey(prefix, k string) string {
return prefix + strings.ToUpper(
strings.ReplaceAll(strings.ReplaceAll(k, ".", "_"), "-", "_"))
}

View file

@ -69,19 +69,47 @@ func TestParamsToEnv(t *testing.T) {
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
}
assert.NoError(t, ParamsToEnv(from, got, getSecretValue))
assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue))
assert.EqualValues(t, want, got, "Problem converting plugin parameters to environment variables")
// handle edge cases (#1609)
got = map[string]string{}
assert.NoError(t, ParamsToEnv(map[string]any{"a": []any{"a", nil}}, got, nil))
assert.NoError(t, ParamsToEnv(map[string]any{"a": []any{"a", nil}}, got, "PLUGIN_", nil))
assert.EqualValues(t, map[string]string{"PLUGIN_A": "a,"}, got)
}
func TestParamsToEnvPrefix(t *testing.T) {
from := map[string]any{
"string": "stringz",
"int": 1,
}
wantPrefixPlugin := map[string]string{
"PLUGIN_STRING": "stringz",
"PLUGIN_INT": "1",
}
got := map[string]string{}
getSecretValue := func(name string) (string, error) {
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
}
assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue))
assert.EqualValues(t, wantPrefixPlugin, got, "Problem converting plugin parameters to environment variables")
wantNoPrefix := map[string]string{
"STRING": "stringz",
"INT": "1",
}
// handle edge cases (#1609)
got = map[string]string{}
assert.NoError(t, ParamsToEnv(from, got, "", getSecretValue))
assert.EqualValues(t, wantNoPrefix, got, "Problem converting plugin parameters to environment variables")
}
func TestSanitizeParamKey(t *testing.T) {
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("dry-run"))
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("dry_Run"))
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("dry.run"))
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", "dry-run"))
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", "dry_Run"))
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", "dry.run"))
}
func TestYAMLToParamsToEnv(t *testing.T) {
@ -136,7 +164,7 @@ list.map:
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
}
assert.NoError(t, ParamsToEnv(from, got, getSecretValue))
assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue))
assert.EqualValues(t, want, got, "Problem converting plugin parameters to environment variables")
}
@ -160,7 +188,7 @@ func TestYAMLToParamsToEnvError(t *testing.T) {
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
}
assert.Error(t, ParamsToEnv(from, make(map[string]string), getSecretValue))
assert.Error(t, ParamsToEnv(from, make(map[string]string), "PLUGIN_", getSecretValue))
}
func stringsToInterface(val ...string) []any {
@ -191,6 +219,6 @@ func TestSecretNotFound(t *testing.T) {
got := map[string]string{}
assert.ErrorContains(t,
ParamsToEnv(from, got, getSecretValue),
ParamsToEnv(from, got, "PLUGIN_", getSecretValue),
fmt.Sprintf("secret %q not found or not allowed to be used", "secret_token"))
}