From 5c63cfc327f49a3d1dbfd2d29d56cb6c0fd8a5b3 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Tue, 3 Sep 2024 09:33:08 -0400 Subject: [PATCH] Do not fail on /home/dwalsh not containing .config directory Fixes: https://github.com/containers/podman/issues/23818 Signed-off-by: Daniel J Walsh --- pkg/config/config_test.go | 16 ++++++++++++++++ pkg/config/default.go | 39 ++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 6c6e722f4..1345d1ddd 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -847,6 +847,22 @@ 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()) + + 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 diff --git a/pkg/config/default.go b/pkg/config/default.go index 311b090b9..dae4bdd60 100644 --- a/pkg/config/default.go +++ b/pkg/config/default.go @@ -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 ( @@ -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 { + 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. @@ -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 {