Skip to content

Commit

Permalink
generate systemd: support entrypoint JSON strings
Browse files Browse the repository at this point in the history
Make sure to preserve the quoting of entrypoint JSON strings.

Fixes: containers#12477
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
  • Loading branch information
vrothberg committed Dec 8, 2021
1 parent 31be3a9 commit 2d517b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/systemd/generate/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func escapeSystemdArguments(command []string) []string {
func escapeSystemdArg(arg string) string {
arg = strings.ReplaceAll(arg, "$", "$$")
arg = strings.ReplaceAll(arg, "%", "%%")
if strings.ContainsAny(arg, " \t") {
if strings.ContainsAny(arg, " \t\"") {
arg = strconv.Quote(arg)
} else if strings.Contains(arg, `\`) {
// strconv.Quote also escapes backslashes so
Expand Down
18 changes: 17 additions & 1 deletion pkg/systemd/generate/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestEscapeSystemdArguments(t *testing.T) {
}{
{
[]string{"foo", "bar=\"arg\""},
[]string{"foo", "bar=\"arg\""},
[]string{"foo", "\"bar=\\\"arg\\\"\""},
},
{
[]string{"foo", "bar=\"arg with space\""},
Expand Down Expand Up @@ -192,6 +192,22 @@ func TestEscapeSystemdArguments(t *testing.T) {
[]string{"foo", `command with two backslashes \\`},
[]string{"foo", `"command with two backslashes \\\\"`},
},
{
[]string{"podman", "create", "--entrypoint", "foo"},
[]string{"podman", "create", "--entrypoint", "foo"},
},
{
[]string{"podman", "create", "--entrypoint=foo"},
[]string{"podman", "create", "--entrypoint=foo"},
},
{
[]string{"podman", "create", "--entrypoint", "[\"foo\"]"},
[]string{"podman", "create", "--entrypoint", "\"[\\\"foo\\\"]\""},
},
{
[]string{"podman", "create", "--entrypoint", "[\"sh\", \"-c\", \"date '+%s'\"]"},
[]string{"podman", "create", "--entrypoint", "\"[\\\"sh\\\", \\\"-c\\\", \\\"date '+%%s'\\\"]\""},
},
}

for _, test := range tests {
Expand Down
3 changes: 2 additions & 1 deletion test/system/250-systemd.bats
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ function service_cleanup() {
}

@test "podman autoupdate local" {
# Note that the entrypoint may be a JSON string which requires preserving the quotes (see #12477)
cname=$(random_string)
run_podman create --name $cname --label "io.containers.autoupdate=local" $IMAGE top
run_podman create --name $cname --label "io.containers.autoupdate=local" --entrypoint '["top"]' $IMAGE

# Start systemd service to run this container
service_setup
Expand Down

0 comments on commit 2d517b6

Please sign in to comment.