Merge remote-tracking branch 'origin/master'

# Conflicts:
#	wazuh-notify-go/wazuh-notify-config.yaml
This commit is contained in:
Rudi klein 2024-05-10 13:22:35 +02:00
commit bff328cfcd
4 changed files with 42 additions and 19 deletions

View File

@ -20,6 +20,18 @@ func OpenLogFile(BasePath string) {
}
}
func CloseLogFile() {
_, err := logFile.WriteString(
"\n\n#######################################\n## CLOSE ##" +
"\n" + time.Now().String() +
"\n#######################################\n",
)
if err != nil {
panic(err)
}
logFile.Close()
}
func Log(message string) {
if _, err := logFile.WriteString("\n" + message + ": " + time.Now().String()); err != nil {
panic(err)

View File

@ -20,4 +20,5 @@ func main() {
notification.SendNtfy(inputParams)
}
}
log.CloseLogFile()
}

View File

@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v2"
"os"
"path"
"runtime"
"strings"
"wazuh-notify/log"
"wazuh-notify/types"
)
@ -16,33 +16,31 @@ import (
var inputParams types.Params
var configParams types.Params
var wazuhData types.WazuhMessage
var BasePath string
func InitNotify() types.Params {
_, currentFile, _, _ := runtime.Caller(1)
BaseFilePath, _ := os.Executable()
BaseDirPath := path.Dir(BaseFilePath)
BasePath = path.Dir(currentFile)
log.OpenLogFile(BaseDirPath)
log.OpenLogFile(BasePath)
err := godotenv.Load(path.Join(BasePath, "../../etc/.env"))
err := godotenv.Load(path.Join(BaseDirPath, "../../etc/.env"))
if err != nil {
log.Log("env failed to load")
godotenv.Load(path.Join(BasePath, ".env"))
godotenv.Load(path.Join(BaseDirPath, ".env"))
} else {
log.Log("env loaded")
}
wazuhInput()
yamlFile, err := os.ReadFile(path.Join(BasePath, "../../etc/wazuh-notify-config.yaml"))
yamlFile, err := os.ReadFile(path.Join(BaseDirPath, "../../etc/wazuh-notify-config.yaml"))
if err != nil {
log.Log("yaml failed to load")
yamlFile, err = os.ReadFile(path.Join(BasePath, "wazuh-notify-config.yaml"))
yamlFile, err = os.ReadFile(path.Join(BaseDirPath, "wazuh-notify-config.yaml"))
}
yaml.Unmarshal(yamlFile, &configParams)
log.Log("yaml loaded")
configParamString, _ := json.Marshal(configParams)
log.Log(string(configParamString))
flag.StringVar(&inputParams.Url, "url", "", "is the webhook URL of the Discord server. It is stored in .env.")
flag.StringVar(&inputParams.Click, "click", configParams.Click, "is a link (URL) that can be followed by tapping/clicking inside the message. Default is https://google.com.")
@ -54,8 +52,13 @@ func InitNotify() types.Params {
flag.Parse()
log.Log("params loaded")
inputParamString, _ := json.Marshal(inputParams)
log.Log(string(inputParamString))
inputParams.Targets = configParams.Targets
wazuhInput()
return inputParams
}
@ -64,7 +67,13 @@ func wazuhInput() {
json.NewDecoder(reader).Decode(&wazuhData)
mapPriority()
inputParams.Priority = mapPriority()
inputParams.Tags += strings.Join(wazuhData.Parameters.Alert.Rule.Groups, ",")
inputParams.WazuhMessage = wazuhData
log.Log("Wazuh data loaded")
inputParamString, _ := json.Marshal(inputParams)
log.Log(string(inputParamString))
}

View File

@ -2,20 +2,21 @@ package services
import "slices"
func mapPriority() {
func mapPriority() int {
if slices.Contains(configParams.Priority1, wazuhData.Parameters.Alert.Rule.Level) {
inputParams.Priority = wazuhData.Parameters.Alert.Rule.Level
return 1
}
if slices.Contains(configParams.Priority2, wazuhData.Parameters.Alert.Rule.Level) {
inputParams.Priority = wazuhData.Parameters.Alert.Rule.Level
return 2
}
if slices.Contains(configParams.Priority3, wazuhData.Parameters.Alert.Rule.Level) {
inputParams.Priority = wazuhData.Parameters.Alert.Rule.Level
return 3
}
if slices.Contains(configParams.Priority4, wazuhData.Parameters.Alert.Rule.Level) {
inputParams.Priority = wazuhData.Parameters.Alert.Rule.Level
return 4
}
if slices.Contains(configParams.Priority5, wazuhData.Parameters.Alert.Rule.Level) {
inputParams.Priority = wazuhData.Parameters.Alert.Rule.Level
return 5
}
return 0
}