Skip to content

Commit

Permalink
Do not fail on $HOME not containing .config directory
Browse files Browse the repository at this point in the history
Fixes: containers/podman#23818

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Sep 3, 2024
1 parent 08f9c11 commit a90e70e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
17 changes: 17 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,23 @@ env=["foo=bar"]`
gomega.Expect(config.Containers.EnableLabeledUsers).To(gomega.BeTrue())
})

It("HomeDirTest", func() {
oldHOMEDIR, set := os.LookupEnv("HOME")
dir, err := os.MkdirTemp("", "configTest")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
defer os.RemoveAll(dir)

os.Setenv("HOME", dir)
_, err = defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())

if set {
os.Setenv("HOME", oldHOMEDIR)
} else {
os.Unsetenv("HOME")
}
})

It("ParsePullPolicy", func() {
for _, test := range []struct {
value string
Expand Down
37 changes: 22 additions & 15 deletions pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -188,6 +189,23 @@ const (
DefaultVolumePluginTimeout = 5
)

func defaultSigPath() (string, error) {
// NOTE: For now we want Windows to use system locations.
// GetRootlessUID == -1 on Windows, so exclude negative range
if unshare.GetRootlessUID() > 0 {
configHome, err := homedir.GetConfigHome()
if err == nil {
sigPath := filepath.Join(configHome, DefaultRootlessSignaturePolicyPath)
if err := fileutils.Exists(sigPath); err == nil {
return sigPath, nil
}
} else if !errors.Is(err, fs.ErrNotExist) {
return "", err
}
}
return DefaultSignaturePolicyPath, nil
}

// defaultConfig returns Config with builtin defaults and minimal adjustments
// to the current host only. It does not read any config files from the host or
// the environment.
Expand All @@ -197,22 +215,11 @@ func defaultConfig() (*Config, error) {
return nil, err
}

defaultEngineConfig.SignaturePolicyPath = DefaultSignaturePolicyPath
// NOTE: For now we want Windows to use system locations.
// GetRootlessUID == -1 on Windows, so exclude negative range
if unshare.GetRootlessUID() > 0 {
configHome, err := homedir.GetConfigHome()
if err != nil {
return nil, err
}
sigPath := filepath.Join(configHome, DefaultRootlessSignaturePolicyPath)
defaultEngineConfig.SignaturePolicyPath = sigPath
if err := fileutils.Exists(sigPath); err != nil {
if err := fileutils.Exists(DefaultSignaturePolicyPath); err == nil {
defaultEngineConfig.SignaturePolicyPath = DefaultSignaturePolicyPath
}
}
sigPath, err := defaultSigPath()
if err != nil {
return nil, err
}
defaultEngineConfig.SignaturePolicyPath = sigPath

cgroupNS := "host"
if cgroup2, _ := cgroupv2.Enabled(); cgroup2 {
Expand Down

0 comments on commit a90e70e

Please sign in to comment.