Skip to content

Commit

Permalink
Ensure DefaultEnvVariables is used in Specgen
Browse files Browse the repository at this point in the history
When we rewrote Podman's pkg/spec, one of the things that was
lost was our use of a set of default environment variables, that
ensure all containers have at least $PATH and $TERM set.

While we're in the process of re-adding it, change it from a
variable to a function, so we can ensure the Join function does
not overwrite it and corrupt the defaults.

Signed-off-by: Matthew Heon <[email protected]>
  • Loading branch information
mheon committed Aug 17, 2020
1 parent ff09631 commit d226603
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
5 changes: 2 additions & 3 deletions cmd/podman/common/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,8 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
//
// Precedence order (higher index wins):
// 1) env-host, 2) image data, 3) env-file, 4) env
env := map[string]string{
"container": "podman",
}
env := make(map[string]string)
env["container"] = "podman"

// First transform the os env into a map. We need it for the labels later in
// any case.
Expand Down
14 changes: 8 additions & 6 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import (
"github.com/pkg/errors"
)

// DefaultEnvVariables sets $PATH and $TERM.
var DefaultEnvVariables = map[string]string{
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM": "xterm",
}

const whiteSpaces = " \t"

// DefaultEnvVariables returns a default environment, with $PATH and $TERM set.
func DefaultEnvVariables() map[string]string {
return map[string]string{
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM": "xterm",
}
}

// Slice transforms the specified map of environment variables into a
// slice. If a value is non-empty, the key and value are joined with '='.
func Slice(m map[string]string) []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,13 @@ func (config *CreateConfig) createConfigToOCISpec(runtime *libpod.Runtime, userM
// config.
var defaultEnv map[string]string
if runtimeConfig == nil {
defaultEnv = env.DefaultEnvVariables
defaultEnv = env.DefaultEnvVariables()
} else {
defaultEnv, err = env.ParseSlice(runtimeConfig.Containers.Env)
if err != nil {
return nil, errors.Wrap(err, "Env fields in containers.conf failed ot parse")
}
defaultEnv = env.Join(env.DefaultEnvVariables, defaultEnv)
defaultEnv = env.Join(env.DefaultEnvVariables(), defaultEnv)
}

if err := addRlimits(config, &g); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/specgen/generate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat

s.Env = envLib.Join(envLib.Join(defaultEnvs, envs), s.Env)

// Ensure that default environment variables are populated.
// Container must have PATH and TERM set, even if nothing else set them.
baseEnv := envLib.DefaultEnvVariables()
for k, v := range baseEnv {
if _, ok := s.Env[k]; !ok {
s.Env[k] = v
}
}

// Labels and Annotations
annotations := make(map[string]string)
if newImage != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/specgen/generate/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt
for key, val := range s.Annotations {
g.AddAnnotation(key, val)
}
g.AddProcessEnv("container", "podman")

g.Config.Linux.Resources = s.ResourceLimits

Expand Down

0 comments on commit d226603

Please sign in to comment.