Fix uppercased env (#3516)

closes #3515 

I think after this is fixed, we should publish a new release as this can
be quite important.

Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
This commit is contained in:
qwerty287 2024-03-20 15:53:33 +01:00 committed by GitHub
parent aaf11afef7
commit 75803dba41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 17 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, "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
}

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, 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

View file

@ -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"))
}