From 2117fcc7832cb3f1d6f97cb5568aeea166cc07c7 Mon Sep 17 00:00:00 2001 From: Darius klein Date: Mon, 17 Nov 2025 21:27:53 +0100 Subject: [PATCH] updated config logic --- wazuh-notify-go-v2/config/config.go | 50 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/wazuh-notify-go-v2/config/config.go b/wazuh-notify-go-v2/config/config.go index 70eb5bf..c3bffa9 100644 --- a/wazuh-notify-go-v2/config/config.go +++ b/wazuh-notify-go-v2/config/config.go @@ -14,46 +14,56 @@ import ( var DefaultConfigFile []byte var File Config +// WARNING: this code is ai generated func Read() error { const SystemConfigPath = "/etc/wazuh-notify/wazuh-notify-config.toml" - execPath, _ := os.Executable() LocalConfigPath := path.Join(path.Dir(execPath), "wazuh-notify-config.toml") - var tomlFile []byte - var err error - - tomlFile, err = os.ReadFile(SystemConfigPath) - + err := toml.Unmarshal(DefaultConfigFile, &File) if err != nil { - tomlFile, err = os.ReadFile(LocalConfigPath) + log.Log(fmt.Sprintf("CRITICAL: Failed to parse embedded default config: %v", err)) + return err } - if err != nil { - log.Log("Config not found. Attempting to create default.") + var userTomlFile []byte + var readErr error + var configPath string + + userTomlFile, readErr = os.ReadFile(SystemConfigPath) + if readErr == nil { + configPath = SystemConfigPath + } else { + userTomlFile, readErr = os.ReadFile(LocalConfigPath) + if readErr == nil { + configPath = LocalConfigPath + } + } + + if readErr != nil { + log.Log("No user config file found. Attempting to create default on disk.") errMkdir := os.MkdirAll(path.Dir(SystemConfigPath), os.ModePerm) - errWrite := os.WriteFile(SystemConfigPath, DefaultConfigFile, 0600) if errMkdir != nil || errWrite != nil { - log.Log(fmt.Sprintf("Warning: Could not write config to disk (%v).", errWrite)) - log.Log("Falling back to embedded memory config.") + log.Log(fmt.Sprintf("Warning: Could not write default config to %s (%v).", SystemConfigPath, errWrite)) + log.Log("Using embedded default configuration only.") } else { - log.Log(fmt.Sprintf("Successfully created default config at %s", SystemConfigPath)) + log.Log(fmt.Sprintf("Successfully created default config at %s.", SystemConfigPath)) } - tomlFile = DefaultConfigFile - err = nil + log.Log("TOML configuration loaded successfully from Embedded Default") + return nil } - parseErr := toml.Unmarshal(tomlFile, &File) - if parseErr != nil { - log.Log(parseErr.Error()) - return parseErr + overrideErr := toml.Unmarshal(userTomlFile, &File) + if overrideErr != nil { + log.Log(fmt.Sprintf("Error parsing user configuration from %s: %v", configPath, overrideErr)) + return overrideErr } - log.Log("TOML configuration loaded successfully") + log.Log(fmt.Sprintf("TOML configuration loaded successfully. Defaults merged with %s", configPath)) return nil }