From ca9874ab7d6b72074861d049b986ebe70821ed51 Mon Sep 17 00:00:00 2001 From: Ismael Arias Date: Mon, 17 Jul 2023 14:12:01 +0200 Subject: [PATCH] Use pkg/homedir to get the home config directory Also, log a warning if there is an error when reading the local policy.json file, if the error is other than ENOEXIST [NO NEW TESTS NEEDED] Signed-off-by: Ismael Arias --- pkg/trust/policy.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/trust/policy.go b/pkg/trust/policy.go index 6befb01676..21222c3570 100644 --- a/pkg/trust/policy.go +++ b/pkg/trust/policy.go @@ -18,9 +18,6 @@ import ( "github.com/sirupsen/logrus" ) -// userPolicyFile is the path to the per user policy path. -var userPolicyFile = filepath.FromSlash(".config/containers/policy.json") - // policyContent is the overall structure of a policy.json file (= c/image/v5/signature.Policy) type policyContent struct { Default []repoContent `json:"default"` @@ -61,10 +58,17 @@ func DefaultPolicyPath(sys *types.SystemContext) string { if sys != nil && sys.SignaturePolicyPath != "" { return sys.SignaturePolicyPath } - userPolicyFilePath := filepath.Join(homedir.Get(), userPolicyFile) - if _, err := os.Stat(userPolicyFilePath); err == nil { + + confDir, _ := homedir.GetConfigHome() + userPolicyFilePath := filepath.Join(confDir, filepath.FromSlash("containers/policy.json")) + _, err := os.Stat(userPolicyFilePath) + if err == nil { return userPolicyFilePath } + if !os.IsNotExist(err) { + logrus.Warnf("Error trying to read local config file: %s", err.Error()) + } + systemDefaultPolicyPath := config.DefaultSignaturePolicyPath if sys != nil && sys.RootForImplicitAbsolutePaths != "" { return filepath.Join(sys.RootForImplicitAbsolutePaths, systemDefaultPolicyPath)