101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package config
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"wazuh-notify/log"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
//go:embed default-config.toml
|
|
var DefaultConfigFile []byte
|
|
var File Config
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
tomlFile, err = os.ReadFile(LocalConfigPath)
|
|
}
|
|
|
|
if err != nil {
|
|
log.Log("Config not found. Attempting to create default.")
|
|
|
|
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.")
|
|
} else {
|
|
log.Log(fmt.Sprintf("Successfully created default config at %s", SystemConfigPath))
|
|
}
|
|
|
|
tomlFile = DefaultConfigFile
|
|
err = nil
|
|
}
|
|
|
|
parseErr := toml.Unmarshal(tomlFile, &File)
|
|
if parseErr != nil {
|
|
log.Log(parseErr.Error())
|
|
return parseErr
|
|
}
|
|
|
|
log.Log("TOML configuration loaded successfully")
|
|
return nil
|
|
}
|
|
|
|
// Config holds the entire configuration structure defined in the TOML file.
|
|
type Config struct {
|
|
General General `toml:"general"`
|
|
PriorityMaps []PriorityMap `toml:"priority_map"`
|
|
Discord Discord `toml:"discord"`
|
|
Ntfy Ntfy `toml:"ntfy"`
|
|
Slack Slack `toml:"slack"`
|
|
}
|
|
|
|
// General maps the values within the [general] TOML table.
|
|
type General struct {
|
|
Targets string `toml:"targets"`
|
|
FullAlert string `toml:"full_alert"`
|
|
ExcludedRules string `toml:"excluded_rules"`
|
|
ExcludedAgents string `toml:"excluded_agents"`
|
|
ExcludeDescriptions []string `toml:"exclude_descriptions"`
|
|
Sender string `toml:"sender"`
|
|
Click string `toml:"click"`
|
|
}
|
|
|
|
// PriorityMap maps the values within a single [[priority_map]] TOML table entry.
|
|
type PriorityMap struct {
|
|
ThreatMap []int `toml:"threat_map"`
|
|
MentionThreshold int `toml:"mention_threshold"`
|
|
NotifyThreshold int `toml:"notify_threshold"`
|
|
Color int `toml:"color"`
|
|
}
|
|
|
|
type Discord struct {
|
|
Webhook string `toml:"webhook"`
|
|
}
|
|
|
|
// Ntfy maps the values within the [ntfy] TOML table.
|
|
type Ntfy struct {
|
|
Webhook string `toml:"webhook"`
|
|
}
|
|
|
|
// Slack maps the values within the [slack] TOML table.
|
|
type Slack struct {
|
|
Webhook string `toml:"webhook"`
|
|
}
|