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 4, 2024
1 parent 08f9c11 commit 7d96c3f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
23 changes: 23 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/onsi/gomega"
selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/sirupsen/logrus"
oscaps "github.com/syndtr/gocapability/capability"
)

var _ = Describe("Config", func() {
Expand Down Expand Up @@ -847,6 +848,28 @@ 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.Chmod(dir, 0o000)

Check failure on line 856 in pkg/config/config_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Chmod` is not checked (errcheck)
caps, err := oscaps.NewPid2(0)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
caps.Unset(oscaps.EFFECTIVE, oscaps.CAP_DAC_OVERRIDE)
defer caps.Set(oscaps.EFFECTIVE, oscaps.CAP_DAC_OVERRIDE)

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 7d96c3f

Please sign in to comment.