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

Remove leading and trailing double quotes from environment variable values in docker run commands #5434

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 14 additions & 7 deletions cli/command/container/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ func TestParseWithVolumes(t *testing.T) {
}

// Two bind mounts.
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/hostVar:/containerVar`}, []string{os.Getenv("ProgramData") + `:c:\ContainerPD`, os.Getenv("TEMP") + `:c:\containerTmp`})
arr, tryit = setupPlatformVolume(
[]string{`/hostTmp:/containerTmp`, `/hostVar:/containerVar`},
[]string{os.Getenv("ProgramData") + `:c:\ContainerPD`, os.Getenv("TEMP") + `:c:\containerTmp`},
)
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
}
Expand All @@ -218,7 +221,8 @@ func TestParseWithVolumes(t *testing.T) {
// TODO Windows: The Windows version uses read-write as that's the only mode it supports. Can change this post TP4
arr, tryit = setupPlatformVolume(
[]string{`/hostTmp:/containerTmp:ro`, `/hostVar:/containerVar:rw`},
[]string{os.Getenv("TEMP") + `:c:\containerTmp:rw`, os.Getenv("ProgramData") + `:c:\ContainerPD:rw`})
[]string{os.Getenv("TEMP") + `:c:\containerTmp:rw`, os.Getenv("ProgramData") + `:c:\ContainerPD:rw`},
)
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
}
Expand Down Expand Up @@ -892,15 +896,18 @@ func TestParseEnvfileVariables(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(config.Env) != 1 || config.Env[0] != "ENV1=value1" {
t.Fatalf("Expected a config with [ENV1=value1], got %v", config.Env)

if len(config.Env) != 2 || config.Env[0] != "ENV1=value1" || config.Env[1] != "ENV2=value2" {
t.Fatalf("Expected a config with [ENV1=value1], got %+v", config.Env)
}
config, _, _, err = parseRun([]string{"--env-file=testdata/valid.env", "--env=ENV2=value2", "img", "cmd"})

config, _, _, err = parseRun([]string{"--env-file=testdata/valid.env", "--env=ENV3=value3", "--env=ENV4=\"value4\"", "img", "cmd"})
if err != nil {
t.Fatal(err)
}
if len(config.Env) != 2 || config.Env[0] != "ENV1=value1" || config.Env[1] != "ENV2=value2" {
t.Fatalf("Expected a config with [ENV1=value1 ENV2=value2], got %v", config.Env)

if len(config.Env) != 4 || config.Env[0] != "ENV1=value1" || config.Env[1] != "ENV2=value2" || config.Env[2] != "ENV3=value3" || config.Env[3] != "ENV4=value4" {
t.Fatalf("Expected a config with [ENV1=value1 ENV2=value2, ENV3=value3, ENV4=value4], got %v", config.Env)
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/container/testdata/valid.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ENV1=value1
ENV2="value2"
4 changes: 4 additions & 0 deletions opts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]
if len(line) > 0 && !strings.HasPrefix(line, "#") {
variable, value, hasValue := strings.Cut(line, "=")

if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
value = strings.Trim(value, "\"")
}

// trim the front of a variable, but nothing else
variable = strings.TrimLeft(variable, whiteSpaces)
if strings.ContainsAny(variable, whiteSpaces) {
Expand Down
14 changes: 14 additions & 0 deletions opts/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opts

import (
"errors"
"fmt"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -31,6 +32,19 @@ func readKVStrings(files []string, override []string, emptyFn func(string) (stri
}
variables = append(variables, parsedVars...)
}

for i, value := range override {
key, value, ok := strings.Cut(value, "=")
if !ok {
continue
}

if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
value = strings.Trim(value, "\"")
override[i] = fmt.Sprintf("%s=%s", key, value)
}
}

// parse the '-e' and '--env' after, to allow override
variables = append(variables, override...)

Expand Down