From 3726dca63facc027373b1930bfd255b67713c00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Wed, 22 May 2024 10:46:03 -1000 Subject: [PATCH 1/2] (#2173) Load plugins configuration from the system directory Some plugins need to load configuration that is managed system-wide by Puppet even when the user is using their own configuration file. Regardless of the configuration file loaded (system one or user one), we want to load these plugins configuration from the system. We previously inferred the plugins configuration directory relatively to the rest of the configuration, but ignored it if it was located in the user HOME directory. This was not correct, and even caused more trouble when the HOME environment variable was '/' as it is commonly on some systems for services, because plugins configuration was never loaded on these systems. Fix this by detecting the location of the system configuration and unconditionally loading plugins configuration from this directory. --- config/config.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 973d8459..2e01cf0d 100644 --- a/config/config.go +++ b/config/config.go @@ -96,6 +96,9 @@ type Config struct { // ConfigFile is the main configuration that got parsed ConfigFile string + // The system-wide configuration directory. Plugins are loaded from there + SystemConfigDirectory string + // ParsedFiles is a list of all files parsed to create the current config ParsedFiles []string @@ -408,23 +411,24 @@ func (c *Config) UnParsedOptions() map[string]string { } func (c *Config) dotdDir() string { - if !forceDotParse { - home, err := iu.HomeDir() - if err == nil { - if strings.HasPrefix(c.ConfigFile, home) { - return "" - } - } - } - - return filepath.Join(filepath.Dir(c.ConfigFile), "plugin.d") + return filepath.Join(c.SystemConfigDirectory, "plugin.d") } func newConfig() *Config { + scd := "" + if iu.FileExist("/etc/choria") { + scd = "/etc/choria" + } else if iu.FileExist("/usr/local/etc/choria") { + scd = "/usr/local/etc/choria" + } else if iu.FileExist("C:\\ProgramData\\choria\\etc") { + scd = "C:\\ProgramData\\choria\\etc" + } + m := &Config{ - Choria: newChoria(), - rawOpts: make(map[string]string), - Puppet: puppet.New(), + Choria: newChoria(), + rawOpts: make(map[string]string), + Puppet: puppet.New(), + SystemConfigDirectory: scd, } err := confkey.SetStructDefaults(m) From aeb6ae8671ee9dfa3bbde33c4c091db2915ef792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Wed, 22 May 2024 11:07:13 -1000 Subject: [PATCH 2/2] (misc) Get rid of unused variable --- config/config.go | 2 -- config/config_test.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/config/config.go b/config/config.go index 2e01cf0d..7207bb7f 100644 --- a/config/config.go +++ b/config/config.go @@ -21,8 +21,6 @@ import ( "github.com/choria-io/go-choria/puppet" ) -var forceDotParse bool - // Config represents Choria cofnfiguration // // NOTE: When adding or updating doc strings please run `go generate` in the root of the repository diff --git a/config/config_test.go b/config/config_test.go index db129204..3dc6c214 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -65,14 +65,12 @@ var _ = Describe("Choria/Config", func() { var c *Config var err error - forceDotParse = true if runtime.GOOS == "windows" { c, err = NewConfig("testdata/choria_windows.cfg") } else { c, err = NewConfig("testdata/choria.cfg") } Expect(err).ToNot(HaveOccurred()) - forceDotParse = false Expect(c.Choria.NetworkWriteDeadline).To(Equal(10 * time.Second)) Expect(c.Registration).To(Equal([]string{"foo"}))