mirror of
https://github.com/containers/podman.git
synced 2025-06-22 09:58:10 +08:00
generate systemd: quote arguments with whitespace
Make sure that arguments with whitespace are properly quoted so they are interpreted as one (and not multiple ones) by systemd. Now `-e tz="america/new york"` will be generated as `-e "tz=america/new york"`. The quotes are moving but the argument is still correct. Fixes: #7285 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -53,3 +54,15 @@ func filterPodFlags(command []string) []string {
|
|||||||
}
|
}
|
||||||
return processed
|
return processed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// quoteArguments makes sure that all arguments with at least one whitespace
|
||||||
|
// are quoted to make sure those are interpreted as one argument instead of
|
||||||
|
// multiple ones.
|
||||||
|
func quoteArguments(command []string) []string {
|
||||||
|
for i := range command {
|
||||||
|
if strings.ContainsAny(command[i], " \t") {
|
||||||
|
command[i] = strconv.Quote(command[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return command
|
||||||
|
}
|
||||||
|
@ -28,3 +28,28 @@ func TestFilterPodFlags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQuoteArguments(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input []string
|
||||||
|
output []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
[]string{"foo", "bar=\"arg\""},
|
||||||
|
[]string{"foo", "bar=\"arg\""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]string{"foo", "bar=\"arg with space\""},
|
||||||
|
[]string{"foo", "\"bar=\\\"arg with space\\\"\""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]string{"foo", "bar=\"arg with\ttab\""},
|
||||||
|
[]string{"foo", "\"bar=\\\"arg with\\ttab\\\"\""},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
quoted := quoteArguments(test.input)
|
||||||
|
assert.Equal(t, test.output, quoted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -241,6 +241,7 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
|
|||||||
startCommand = append(startCommand, "--replace")
|
startCommand = append(startCommand, "--replace")
|
||||||
}
|
}
|
||||||
startCommand = append(startCommand, info.CreateCommand[index:]...)
|
startCommand = append(startCommand, info.CreateCommand[index:]...)
|
||||||
|
startCommand = quoteArguments(startCommand)
|
||||||
|
|
||||||
info.ExecStartPre = "/bin/rm -f {{.PIDFile}} {{.ContainerIDFile}}"
|
info.ExecStartPre = "/bin/rm -f {{.PIDFile}} {{.ContainerIDFile}}"
|
||||||
info.ExecStart = strings.Join(startCommand, " ")
|
info.ExecStart = strings.Join(startCommand, " ")
|
||||||
|
@ -117,7 +117,7 @@ After=network-online.target
|
|||||||
Environment=PODMAN_SYSTEMD_UNIT=%n
|
Environment=PODMAN_SYSTEMD_UNIT=%n
|
||||||
Restart=always
|
Restart=always
|
||||||
ExecStartPre=/bin/rm -f %t/jadda-jadda.pid %t/jadda-jadda.ctr-id
|
ExecStartPre=/bin/rm -f %t/jadda-jadda.pid %t/jadda-jadda.ctr-id
|
||||||
ExecStart=/usr/bin/podman run --conmon-pidfile %t/jadda-jadda.pid --cidfile %t/jadda-jadda.ctr-id --cgroups=no-conmon -d --replace --name jadda-jadda --hostname hello-world awesome-image:latest command arg1 ... argN
|
ExecStart=/usr/bin/podman run --conmon-pidfile %t/jadda-jadda.pid --cidfile %t/jadda-jadda.ctr-id --cgroups=no-conmon -d --replace --name jadda-jadda --hostname hello-world awesome-image:latest command arg1 ... argN "foo=arg \"with \" space"
|
||||||
ExecStop=/usr/bin/podman stop --ignore --cidfile %t/jadda-jadda.ctr-id -t 42
|
ExecStop=/usr/bin/podman stop --ignore --cidfile %t/jadda-jadda.ctr-id -t 42
|
||||||
ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/jadda-jadda.ctr-id
|
ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/jadda-jadda.ctr-id
|
||||||
PIDFile=%t/jadda-jadda.pid
|
PIDFile=%t/jadda-jadda.pid
|
||||||
@ -296,7 +296,7 @@ WantedBy=multi-user.target default.target`
|
|||||||
PIDFile: "/var/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid",
|
PIDFile: "/var/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid",
|
||||||
StopTimeout: 42,
|
StopTimeout: 42,
|
||||||
PodmanVersion: "CI",
|
PodmanVersion: "CI",
|
||||||
CreateCommand: []string{"I'll get stripped", "container", "run", "--name", "jadda-jadda", "--hostname", "hello-world", "awesome-image:latest", "command", "arg1", "...", "argN"},
|
CreateCommand: []string{"I'll get stripped", "container", "run", "--name", "jadda-jadda", "--hostname", "hello-world", "awesome-image:latest", "command", "arg1", "...", "argN", "foo=arg \"with \" space"},
|
||||||
EnvVariable: EnvVariable,
|
EnvVariable: EnvVariable,
|
||||||
},
|
},
|
||||||
goodWithNameAndGeneric,
|
goodWithNameAndGeneric,
|
||||||
|
@ -292,6 +292,7 @@ func executePodTemplate(info *podInfo, options entities.GenerateSystemdOptions)
|
|||||||
}
|
}
|
||||||
|
|
||||||
startCommand = append(startCommand, podCreateArgs...)
|
startCommand = append(startCommand, podCreateArgs...)
|
||||||
|
startCommand = quoteArguments(startCommand)
|
||||||
|
|
||||||
info.ExecStartPre1 = "/bin/rm -f {{.PIDFile}} {{.PodIDFile}}"
|
info.ExecStartPre1 = "/bin/rm -f {{.PIDFile}} {{.PodIDFile}}"
|
||||||
info.ExecStartPre2 = strings.Join(startCommand, " ")
|
info.ExecStartPre2 = strings.Join(startCommand, " ")
|
||||||
|
@ -75,7 +75,7 @@ Before=container-1.service container-2.service
|
|||||||
Environment=PODMAN_SYSTEMD_UNIT=%n
|
Environment=PODMAN_SYSTEMD_UNIT=%n
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
ExecStartPre=/bin/rm -f %t/pod-123abc.pid %t/pod-123abc.pod-id
|
ExecStartPre=/bin/rm -f %t/pod-123abc.pid %t/pod-123abc.pod-id
|
||||||
ExecStartPre=/usr/bin/podman pod create --infra-conmon-pidfile %t/pod-123abc.pid --pod-id-file %t/pod-123abc.pod-id --name foo --replace
|
ExecStartPre=/usr/bin/podman pod create --infra-conmon-pidfile %t/pod-123abc.pid --pod-id-file %t/pod-123abc.pod-id --name foo "bar=arg with space" --replace
|
||||||
ExecStart=/usr/bin/podman pod start --pod-id-file %t/pod-123abc.pod-id
|
ExecStart=/usr/bin/podman pod start --pod-id-file %t/pod-123abc.pod-id
|
||||||
ExecStop=/usr/bin/podman pod stop --ignore --pod-id-file %t/pod-123abc.pod-id -t 10
|
ExecStop=/usr/bin/podman pod stop --ignore --pod-id-file %t/pod-123abc.pod-id -t 10
|
||||||
ExecStopPost=/usr/bin/podman pod rm --ignore -f --pod-id-file %t/pod-123abc.pod-id
|
ExecStopPost=/usr/bin/podman pod rm --ignore -f --pod-id-file %t/pod-123abc.pod-id
|
||||||
@ -118,7 +118,7 @@ WantedBy=multi-user.target default.target`
|
|||||||
StopTimeout: 10,
|
StopTimeout: 10,
|
||||||
PodmanVersion: "CI",
|
PodmanVersion: "CI",
|
||||||
RequiredServices: []string{"container-1", "container-2"},
|
RequiredServices: []string{"container-1", "container-2"},
|
||||||
CreateCommand: []string{"podman", "pod", "create", "--name", "foo"},
|
CreateCommand: []string{"podman", "pod", "create", "--name", "foo", "bar=arg with space"},
|
||||||
},
|
},
|
||||||
podGoodNamedNew,
|
podGoodNamedNew,
|
||||||
true,
|
true,
|
||||||
|
Reference in New Issue
Block a user