Skip to content

Commit 1bfd4cb

Browse files
authored
Fix healthcheck argument with spaces split in Docker API (#27818)
Fixes: #26519 Signed-off-by: MayorFaj <mayorfaj@gmail.com>
1 parent cadc74b commit 1bfd4cb

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

pkg/api/handlers/compat/containers_create.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,13 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C
604604
cliOpts.OOMKillDisable = *cc.HostConfig.OomKillDisable
605605
}
606606
if cc.Config.Healthcheck != nil {
607-
finCmd := ""
608-
for _, str := range cc.Config.Healthcheck.Test {
609-
finCmd = finCmd + str + " "
610-
}
611-
if len(finCmd) > 1 {
612-
finCmd = finCmd[:len(finCmd)-1]
607+
// Encode healthcheck test as JSON to preserve arguments with spaces.
608+
// MakeHealthCheckFromCli will unmarshal this back to the original array.
609+
cmdJSON, err := json.Marshal(cc.Config.Healthcheck.Test)
610+
if err != nil {
611+
return nil, nil, err
613612
}
614-
cliOpts.HealthCmd = finCmd
613+
cliOpts.HealthCmd = string(cmdJSON)
615614
if cc.Config.Healthcheck.Interval > 0 {
616615
cliOpts.HealthInterval = cc.Config.Healthcheck.Interval.String()
617616
}

pkg/specgenutil/specgen.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,10 @@ func MakeHealthCheckFromCli(inCmd, interval string, retries uint, timeout, start
985985

986986
var concat string
987987
if strings.ToUpper(cmdArr[0]) == define.HealthConfigTestCmd || strings.ToUpper(cmdArr[0]) == define.HealthConfigTestNone { // this is for compat, we are already split properly for most compat cases
988-
cmdArr = strings.Fields(inCmd)
988+
// Only re-split if the input was not already a JSON array (isArr == false); otherwise preserve the unmarshaled array structure
989+
if !isArr {
990+
cmdArr = strings.Fields(inCmd)
991+
}
989992
} else if strings.ToUpper(cmdArr[0]) != define.HealthConfigTestCmdShell { // this is for podman side of things, won't contain the keywords
990993
if isArr && len(cmdArr) > 1 { // an array of consecutive commands
991994
cmdArr = append([]string{define.HealthConfigTestCmd}, cmdArr...)

test/apiv2/20-containers.at

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,31 @@ t GET containers/$cid/json 200 \
594594
.Config.Healthcheck.Timeout=30000000000 \
595595
.Config.Healthcheck.Retries=3
596596

597+
t DELETE containers/$cid?v=true 204
598+
599+
# Test Compat Create with healthcheck preserving arguments with spaces
600+
HEALTHCHECK_TMPD=$(mktemp -d podman-apiv2-test.healthcheck.XXXXXXXX)
601+
cat >$HEALTHCHECK_TMPD/create.json <<EOF
602+
{
603+
"Image": "$IMAGE",
604+
"Cmd": ["top"],
605+
"Healthcheck": {
606+
"Test": ["CMD", "/usr/bin/test", "--arg=value with spaces", "another arg"]
607+
}
608+
}
609+
EOF
610+
t POST containers/create $HEALTHCHECK_TMPD/create.json 201 \
611+
.Id~[0-9a-f]\\{64\\}
612+
cid=$(jq -r '.Id' <<<"$output")
613+
t GET containers/$cid/json 200 \
614+
.Config.Healthcheck.Test[0]="CMD" \
615+
.Config.Healthcheck.Test[1]="/usr/bin/test" \
616+
.Config.Healthcheck.Test[2]="--arg=value with spaces" \
617+
.Config.Healthcheck.Test[3]="another arg"
618+
619+
t DELETE containers/$cid?v=true 204
620+
rm -rf $HEALTHCHECK_TMPD
621+
597622
# compat api: Test for mount options support
598623
# Sigh, JSON can't handle octal. 0755(octal) = 493(decimal)
599624
payload='{"Mounts":[{"Type":"tmpfs","Target":"/mnt/scratch","TmpfsOptions":{"SizeBytes":1024,"Mode":493}}]}'

0 commit comments

Comments
 (0)