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 8a7b0a6 commit 2d0025d
Show file tree
Hide file tree
Showing 2 changed files with 41 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
39 changes: 24 additions & 15 deletions pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/containers/storage/types"
"github.com/opencontainers/selinux/go-selinux"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

const (
Expand Down Expand Up @@ -188,6 +189,25 @@ 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 {

Check failure on line 202 in pkg/config/default.go

View workflow job for this annotation

GitHub Actions / lint

elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
if !errors.Is(err, unix.ENOENT) {
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 +217,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 2d0025d

Please sign in to comment.