Merge pull request #15434 from rhatdan/manifest1

Allow podman to run in an environment with keys containing spaces
This commit is contained in:
OpenShift Merge Robot
2022-08-24 13:29:20 -04:00
committed by GitHub
4 changed files with 26 additions and 12 deletions

20
pkg/env/env.go vendored
View File

@ -37,6 +37,22 @@ func Slice(m map[string]string) []string {
return env return env
} }
// Map transforms the specified slice of environment variables into a
// map.
func Map(slice []string) map[string]string {
envmap := make(map[string]string, len(slice))
for _, val := range slice {
data := strings.SplitN(val, "=", 2)
if len(data) > 1 {
envmap[data[0]] = data[1]
} else {
envmap[data[0]] = ""
}
}
return envmap
}
// Join joins the two environment maps with override overriding base. // Join joins the two environment maps with override overriding base.
func Join(base map[string]string, override map[string]string) map[string]string { func Join(base map[string]string, override map[string]string) map[string]string {
if len(base) == 0 { if len(base) == 0 {
@ -87,10 +103,6 @@ func parseEnv(env map[string]string, line string) error {
} }
// trim the front of a variable, but nothing else // trim the front of a variable, but nothing else
name := strings.TrimLeft(data[0], whiteSpaces) name := strings.TrimLeft(data[0], whiteSpaces)
if strings.ContainsAny(name, whiteSpaces) {
return fmt.Errorf("name %q has white spaces, poorly formatted name", name)
}
if len(data) > 1 { if len(data) > 1 {
env[name] = data[1] env[name] = data[1]
} else { } else {

View File

@ -152,10 +152,8 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
} }
// First transform the os env into a map. We need it for the labels later in // First transform the os env into a map. We need it for the labels later in
// any case. // any case.
osEnv, err := envLib.ParseSlice(os.Environ()) osEnv := envLib.Map(os.Environ())
if err != nil {
return nil, fmt.Errorf("error parsing host environment variables: %w", err)
}
// Caller Specified defaults // Caller Specified defaults
if s.EnvHost { if s.EnvHost {
defaultEnvs = envLib.Join(defaultEnvs, osEnv) defaultEnvs = envLib.Join(defaultEnvs, osEnv)

View File

@ -362,10 +362,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
// First transform the os env into a map. We need it for the labels later in // First transform the os env into a map. We need it for the labels later in
// any case. // any case.
osEnv, err := envLib.ParseSlice(os.Environ()) osEnv := envLib.Map(os.Environ())
if err != nil {
return fmt.Errorf("error parsing host environment variables: %w", err)
}
if !s.EnvHost { if !s.EnvHost {
s.EnvHost = c.EnvHost s.EnvHost = c.EnvHost

View File

@ -58,6 +58,13 @@ var _ = Describe("Podman run", func() {
Expect(session).Should(Exit(0)) Expect(session).Should(Exit(0))
Expect(session.OutputToString()).To(ContainSubstring("/bin")) Expect(session.OutputToString()).To(ContainSubstring("/bin"))
// Verify environ keys with spaces do not blow up podman command
os.Setenv("FOO BAR", "BAZ")
session = podmanTest.Podman([]string{"run", "--rm", ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
os.Unsetenv("FOO BAR")
os.Setenv("FOO", "BAR") os.Setenv("FOO", "BAR")
session = podmanTest.Podman([]string{"run", "--rm", "--env", "FOO", ALPINE, "printenv", "FOO"}) session = podmanTest.Podman([]string{"run", "--rm", "--env", "FOO", ALPINE, "printenv", "FOO"})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()