Skip to content

Commit

Permalink
Respect XDG_CONFIG_HOME for policy.json
Browse files Browse the repository at this point in the history
The default path for (rootless) policy.json should respect
XDG_CONFIG_HOME if present, as the paths for other configuration
files like storage.conf and containers.conf do.

Signed-off-by: David Scherer <david.scherer@antithesis.com>
  • Loading branch information
David Scherer authored and davidscherer committed Aug 8, 2020
1 parent a0bdc7f commit ed0c7be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions signature/policy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ var systemDefaultPolicyPath = builtinDefaultPolicyPath
const builtinDefaultPolicyPath = "/etc/containers/policy.json"

// userPolicyFile is the path to the per user policy path.
var userPolicyFile = filepath.FromSlash(".config/containers/policy.json")
var userPolicyFile = filepath.FromSlash("containers/policy.json")

const defaultConfigHomeDir = ".config"

// InvalidPolicyFormatError is returned when parsing an invalid policy configuration.
type InvalidPolicyFormatError string
Expand All @@ -61,7 +63,11 @@ func defaultPolicyPath(sys *types.SystemContext) string {
if sys != nil && sys.SignaturePolicyPath != "" {
return sys.SignaturePolicyPath
}
userPolicyFilePath := filepath.Join(homedir.Get(), userPolicyFile)
configHomeDir := os.Getenv("XDG_CONFIG_HOME")
if configHomeDir == "" {
configHomeDir = filepath.Join( homedir.Get(), defaultConfigHomeDir )
}
userPolicyFilePath := filepath.Join(configHomeDir, userPolicyFile)
if _, err := os.Stat(userPolicyFilePath); err == nil {
return userPolicyFilePath
}
Expand Down
2 changes: 1 addition & 1 deletion signature/policy_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestDefaultPolicyPath(t *testing.T) {
os.Unsetenv("HOME")
}
}()
userDefaultPolicyPath := filepath.Join(tempHome, userPolicyFile)
userDefaultPolicyPath := filepath.Join(tempHome, defaultConfigHomeDir, userPolicyFile)

for _, c := range []struct {
sys *types.SystemContext
Expand Down

0 comments on commit ed0c7be

Please sign in to comment.