Skip to content

Commit

Permalink
Set the environment variables for the engine
Browse files Browse the repository at this point in the history
The Engine.Env needs to be set very early in the setup process
to make sure no one attempts to use the environment.

Fixes: containers/podman#12296

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Nov 15, 2021
1 parent cccc3f1 commit 7ae7bd1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ func NewConfig(userConfigPath string) (*Config, error) {
return nil, err
}

if err := config.setupEnv(); err != nil {
return nil, err
}

return config, nil
}

Expand Down Expand Up @@ -1187,3 +1191,23 @@ func (c *Config) ImageCopyTmpDir() (string, error) {

return "", errors.Errorf("invalid image_copy_tmp_dir value %q (relative paths are not accepted)", c.Engine.ImageCopyTmpDir)
}

// setupEnv sets the environment variables for the engine
func (c *Config) setupEnv() error {
for _, env := range c.Engine.Env {
splitEnv := strings.SplitN(env, "=", 2)
if len(splitEnv) != 2 {
logrus.Warnf("invalid environment variable for engine %s, valid configuration is KEY=value pair", env)
continue
}
// skip if the env is already defined
if _, ok := os.LookupEnv(splitEnv[0]); ok {
logrus.Debugf("environment variable %s is already defined, skip the settings from containers.conf", splitEnv[0])
continue
}
if err := os.Setenv(splitEnv[0], splitEnv[1]); err != nil {
return err
}
}
return nil
}
5 changes: 4 additions & 1 deletion pkg/config/config_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,15 @@ var _ = Describe("Config Local", func() {

It("should return containers engine env", func() {
// Given
expectedEnv := []string{"http_proxy=internal.proxy.company.com", "foo=bar"}
expectedEnv := []string{"super=duper", "foo=bar"}
// When
config, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(config.Engine.Env).To(gomega.BeEquivalentTo(expectedEnv))
gomega.Expect(os.Getenv("super")).To(gomega.BeEquivalentTo("duper"))
gomega.Expect(os.Getenv("foo")).To(gomega.BeEquivalentTo("bar"))

})

It("Expect Remote to be False", func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/testdata/containers_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ conmon_path = [
# For example "http_proxy=internal.proxy.company.com".
# Note these environment variables will not be used within the container.
# Set the env section under [containers] table, if you want to set environment variables for the container.
env = ["http_proxy=internal.proxy.company.com", "foo=bar"]
env = ["super=duper", "foo=bar"]

# Container init binary
#init_path = "/usr/libexec/podman/catatonit"
Expand Down

0 comments on commit 7ae7bd1

Please sign in to comment.