Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uppercased env #3516

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 8 additions & 5 deletions pipeline/frontend/yaml/compiler/settings/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ 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) {
xoxys marked this conversation as resolved.
Show resolved Hide resolved
if to == nil {
return fmt.Errorf("no map to write to")
}
for k, v := range from {
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
}
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions pipeline/frontend/yaml/compiler/settings/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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{
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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"))
}