From 59f84be035204a8406a3fcc8f8ab59dcf6652555 Mon Sep 17 00:00:00 2001 From: Richard LT Date: Fri, 28 Jan 2022 09:44:15 +0100 Subject: [PATCH] feat(worker): multiline job variable to one line environment variable (#6071) --- engine/worker/internal/types.go | 8 ++++---- sdk/blur_test.go | 2 ++ sdk/variable.go | 17 +++++++++++++---- sdk/variable_test.go | 12 ++++++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/engine/worker/internal/types.go b/engine/worker/internal/types.go index 60e8e0bed7..88e96a5eb4 100644 --- a/engine/worker/internal/types.go +++ b/engine/worker/internal/types.go @@ -295,7 +295,7 @@ func (wk *CurrentWorker) Environ() []string { if v == "" { continue } - newEnv = append(newEnv, k+"="+v) + newEnv = append(newEnv, k+"="+sdk.OneLineValue(v)) } //set up environment variables from pipeline build job parameters @@ -313,19 +313,19 @@ func (wk *CurrentWorker) Environ() []string { envName := strings.Replace(p.Name, ".", "_", -1) envName = strings.Replace(envName, "-", "_", -1) envName = strings.ToUpper(envName) - newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, p.Value)) + newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, sdk.OneLineValue(p.Value))) } for _, p := range wk.currentJob.newVariables { envName := strings.Replace(p.Name, ".", "_", -1) envName = strings.Replace(envName, "-", "_", -1) envName = strings.ToUpper(envName) - newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, p.Value)) + newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, sdk.OneLineValue(p.Value))) } //Set env variables from hooks for k, v := range wk.currentJob.envFromHooks { - newEnv = append(newEnv, k+"="+v) + newEnv = append(newEnv, k+"="+sdk.OneLineValue(v)) } return newEnv } diff --git a/sdk/blur_test.go b/sdk/blur_test.go index e7734a8842..098e938071 100644 --- a/sdk/blur_test.go +++ b/sdk/blur_test.go @@ -19,6 +19,7 @@ func TestBlur(t *testing.T) { `"1234567890`, "12345", "123456", + "123\n456", }) require.NoError(t, err) @@ -28,6 +29,7 @@ func TestBlur(t *testing.T) { require.Equal(t, sdk.PasswordPlaceholder, b.String(`&é'"'"'(§è!çà`)) require.Equal(t, sdk.PasswordPlaceholder, b.String(url.QueryEscape("&é'(§è!çà"))) require.Equal(t, sdk.PasswordPlaceholder, b.String(base64.StdEncoding.EncodeToString([]byte("&é'(§è!çà")))) + require.Equal(t, sdk.PasswordPlaceholder, b.String("123\\n456")) buf, err := json.Marshal(`"1234567890`) require.NoError(t, err) diff --git a/sdk/variable.go b/sdk/variable.go index ea0f205d10..8127f4559b 100644 --- a/sdk/variable.go +++ b/sdk/variable.go @@ -232,11 +232,20 @@ func EnvVartoENV(p Parameter) []string { pName := strings.TrimPrefix(p.Name, "cds.env.") + oneLineValue := OneLineValue(p.Value) + envName := strings.Replace(pName, ".", "_", -1) envName = strings.Replace(envName, "-", "_", -1) - env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", strings.ToUpper(envName), p.Value)) // CDS_ENV_MYSTRINGVARIABLE - env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", pName, p.Value)) //CDS_ENV_MyStringVariable - env = append(env, fmt.Sprintf("%s=%s", pName, p.Value)) // MyStringVariable - env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(envName), p.Value)) // MYSTRINGVARIABLE + env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", strings.ToUpper(envName), oneLineValue)) // CDS_ENV_MYSTRINGVARIABLE + env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", pName, oneLineValue)) //CDS_ENV_MyStringVariable + env = append(env, fmt.Sprintf("%s=%s", pName, oneLineValue)) // MyStringVariable + env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(envName), oneLineValue)) // MYSTRINGVARIABLE return env } + +func OneLineValue(v string) string { + if strings.Contains(v, "\n") { + return strings.Replace(v, "\n", "\\n", -1) + } + return v +} diff --git a/sdk/variable_test.go b/sdk/variable_test.go index 0ac36ad3d7..63ada03ca8 100644 --- a/sdk/variable_test.go +++ b/sdk/variable_test.go @@ -49,6 +49,18 @@ func Test_EnvVartoENV(t *testing.T) { "MY_STRING_VARIABLE=value", }, }, + { + args: sdk.Parameter{ + Name: "cds.env.MyTextVariable", + Value: "one=one\ntwo=two\nthree=three", + }, + want: []string{ + "CDS_ENV_MYTEXTVARIABLE=one=one\\ntwo=two\\nthree=three", + "CDS_ENV_MyTextVariable=one=one\\ntwo=two\\nthree=three", + "MyTextVariable=one=one\\ntwo=two\\nthree=three", + "MYTEXTVARIABLE=one=one\\ntwo=two\\nthree=three", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {