Files
Valentin Rothberg b1ffa2324e 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>
2020-08-19 08:32:51 +02:00

69 lines
1.8 KiB
Go

package generate
import (
"strconv"
"strings"
"github.com/pkg/errors"
)
// EnvVariable "PODMAN_SYSTEMD_UNIT" is set in all generated systemd units and
// is set to the unit's (unique) name.
const EnvVariable = "PODMAN_SYSTEMD_UNIT"
// restartPolicies includes all valid restart policies to be used in a unit
// file.
var restartPolicies = []string{"no", "on-success", "on-failure", "on-abnormal", "on-watchdog", "on-abort", "always"}
// validateRestartPolicy checks that the user-provided policy is valid.
func validateRestartPolicy(restart string) error {
for _, i := range restartPolicies {
if i == restart {
return nil
}
}
return errors.Errorf("%s is not a valid restart policy", restart)
}
const headerTemplate = `# {{.ServiceName}}.service
# autogenerated by Podman {{.PodmanVersion}}
{{- if .TimeStamp}}
# {{.TimeStamp}}
{{- end}}
[Unit]
Description=Podman {{.ServiceName}}.service
Documentation=man:podman-generate-systemd(1)
Wants=network.target
After=network-online.target
`
// filterPodFlags removes --pod and --pod-id-file from the specified command.
func filterPodFlags(command []string) []string {
processed := []string{}
for i := 0; i < len(command); i++ {
s := command[i]
if s == "--pod" || s == "--pod-id-file" {
i++
continue
}
if strings.HasPrefix(s, "--pod=") || strings.HasPrefix(s, "--pod-id-file=") {
continue
}
processed = append(processed, s)
}
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
}