diff --git a/pipeline/frontend/yaml/compiler/convert.go b/pipeline/frontend/yaml/compiler/convert.go index f1cafa872..c36ef9734 100644 --- a/pipeline/frontend/yaml/compiler/convert.go +++ b/pipeline/frontend/yaml/compiler/convert.go @@ -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, "PLUGIN_", getSecretValue); err != nil { + if err := settings.ParamsToEnv(container.Settings, environment, "PLUGIN_", true, getSecretValue); err != nil { return nil, err } } - if err := settings.ParamsToEnv(container.Environment, environment, "", getSecretValue); err != nil { + if err := settings.ParamsToEnv(container.Environment, environment, "", false, getSecretValue); err != nil { return nil, err } diff --git a/pipeline/frontend/yaml/compiler/settings/params.go b/pipeline/frontend/yaml/compiler/settings/params.go index 2c0d36406..73505e206 100644 --- a/pipeline/frontend/yaml/compiler/settings/params.go +++ b/pipeline/frontend/yaml/compiler/settings/params.go @@ -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, prefix string, getSecretValue func(name string) (string, error)) (err error) { +func ParamsToEnv(from map[string]any, to map[string]string, prefix string, upper bool, 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, prefix string, getSe if v == nil || len(k) == 0 { continue } - to[sanitizeParamKey(prefix, k)], err = sanitizeParamValue(v, getSecretValue) + to[sanitizeParamKey(prefix, upper, k)], err = sanitizeParamValue(v, getSecretValue) if err != nil { return err } @@ -43,9 +43,12 @@ func ParamsToEnv(from map[string]any, to map[string]string, prefix string, getSe } // format the environment variable key -func sanitizeParamKey(prefix, k string) string { - return prefix + strings.ToUpper( - strings.ReplaceAll(strings.ReplaceAll(k, ".", "_"), "-", "_")) +func sanitizeParamKey(prefix string, upper bool, k string) string { + r := strings.ReplaceAll(strings.ReplaceAll(k, ".", "_"), "-", "_") + if upper { + r = strings.ToUpper(r) + } + return prefix + r } // indicate if a data type can be turned into string without encoding as json diff --git a/pipeline/frontend/yaml/compiler/settings/params_test.go b/pipeline/frontend/yaml/compiler/settings/params_test.go index 63b5df99c..5e3e6da12 100644 --- a/pipeline/frontend/yaml/compiler/settings/params_test.go +++ b/pipeline/frontend/yaml/compiler/settings/params_test.go @@ -69,12 +69,12 @@ 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, "PLUGIN_", getSecretValue)) + assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", true, 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, "PLUGIN_", nil)) + assert.NoError(t, ParamsToEnv(map[string]any{"a": []any{"a", nil}}, got, "PLUGIN_", true, nil)) assert.EqualValues(t, map[string]string{"PLUGIN_A": "a,"}, got) } @@ -92,7 +92,7 @@ func TestParamsToEnvPrefix(t *testing.T) { return "", fmt.Errorf("secret %q not found or not allowed to be used", name) } - assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue)) + assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", true, getSecretValue)) assert.EqualValues(t, wantPrefixPlugin, got, "Problem converting plugin parameters to environment variables") wantNoPrefix := map[string]string{ @@ -102,14 +102,16 @@ func TestParamsToEnvPrefix(t *testing.T) { // handle edge cases (#1609) got = map[string]string{} - assert.NoError(t, ParamsToEnv(from, got, "", getSecretValue)) + assert.NoError(t, ParamsToEnv(from, got, "", true, 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("PLUGIN_", "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_", true, "dry-run")) + assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", true, "dry_Run")) + assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", true, "dry.run")) + assert.EqualValues(t, "PLUGIN_dry_run", sanitizeParamKey("PLUGIN_", false, "dry-run")) + assert.EqualValues(t, "PLUGIN_dry_Run", sanitizeParamKey("PLUGIN_", false, "dry_Run")) } func TestYAMLToParamsToEnv(t *testing.T) { @@ -164,7 +166,7 @@ list.map: return "", fmt.Errorf("secret %q not found or not allowed to be used", name) } - assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue)) + assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", true, getSecretValue)) assert.EqualValues(t, want, got, "Problem converting plugin parameters to environment variables") } @@ -188,7 +190,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), "PLUGIN_", getSecretValue)) + assert.Error(t, ParamsToEnv(from, make(map[string]string), "PLUGIN_", true, getSecretValue)) } func stringsToInterface(val ...string) []any { @@ -219,6 +221,6 @@ func TestSecretNotFound(t *testing.T) { got := map[string]string{} assert.ErrorContains(t, - ParamsToEnv(from, got, "PLUGIN_", getSecretValue), + ParamsToEnv(from, got, "PLUGIN_", true, getSecretValue), fmt.Sprintf("secret %q not found or not allowed to be used", "secret_token")) }