Skip to content

Commit

Permalink
add Error in notifiers
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Labarussias <issif+github@gadz.org>
  • Loading branch information
Issif committed Jun 14, 2023
1 parent 8d15efa commit 2c119ab
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 65 deletions.
6 changes: 3 additions & 3 deletions actionners/actionners.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Init() {
for _, j := range *rules {
if i.CheckParameters != nil {
if err := i.CheckParameters(j); err != nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: err, Rule: j.GetName(), Message: "rules"})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: err.Error(), Rule: j.GetName(), Message: "rules"})
}
}
if i.Category == j.GetActionCategory() {
Expand All @@ -105,7 +105,7 @@ func Init() {
if i.Init != nil {
utils.PrintLog("info", config.LogFormat, utils.LogLine{Message: "init", ActionCategory: i.Category})
if err := i.Init(); err != nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, ActionCategory: i.Category})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), ActionCategory: i.Category})
continue
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func Trigger(rule *rules.Rule, event *events.Event) {
if len(i.Checks) != 0 {
for _, j := range i.Checks {
if err := j(event); err != nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, Rule: ruleName, Action: action, TraceID: event.TraceID, Message: "action"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), Rule: ruleName, Action: action, TraceID: event.TraceID, Message: "action"})
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions actionners/kubernetes/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var Exec = func(rule *rules.Rule, event *events.Event) (utils.LogLine, error) {
return utils.LogLine{
Pod: pod,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand All @@ -65,7 +65,7 @@ var Exec = func(rule *rules.Rule, event *events.Event) (utils.LogLine, error) {
return utils.LogLine{
Pod: pod,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand Down
4 changes: 2 additions & 2 deletions actionners/kubernetes/labelize/labelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var Labelize = func(rule *rules.Rule, event *events.Event) (utils.LogLine, error
return utils.LogLine{
Pod: pod,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand All @@ -70,7 +70,7 @@ var Labelize = func(rule *rules.Rule, event *events.Event) (utils.LogLine, error
return utils.LogLine{
Pod: pod,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand Down
32 changes: 16 additions & 16 deletions actionners/kubernetes/networkpolicy/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand All @@ -45,7 +45,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: errG,
Error: errG.Error(),
Status: "failure",
},
errG
Expand All @@ -54,7 +54,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the daemonset for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the daemonset for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the daemonset for the pod %v in namespace %v", podName, namespace)
Expand All @@ -65,7 +65,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace)
Expand All @@ -76,7 +76,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: errG,
Error: errG.Error(),
Status: "failure",
},
errG
Expand All @@ -85,7 +85,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the statefulset for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the statefulset for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the statefulset for the pod %v in namespace %v", podName, namespace)
Expand All @@ -96,7 +96,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace)
Expand All @@ -107,7 +107,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: errG,
Error: errG.Error(),
Status: "failure",
},
errG
Expand All @@ -116,7 +116,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the replicaset for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the replicaset for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the replicaset for the pod %v in namespace %v", podName, namespace)
Expand All @@ -127,7 +127,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: errG,
Error: errG.Error(),
Status: "failure",
},
errG
Expand All @@ -136,7 +136,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the deployment for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the deployment for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the deployment for the pod %v in namespace %v", podName, namespace)
Expand All @@ -147,7 +147,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace)
Expand All @@ -160,7 +160,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Error: fmt.Sprintf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace),
Status: "failure",
},
fmt.Errorf("can't find the owner and/or labels for the pod %v in namespace %v", podName, namespace)
Expand All @@ -174,7 +174,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand Down Expand Up @@ -202,7 +202,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand All @@ -215,7 +215,7 @@ var NetworkPolicy = func(rule *rules.Rule, event *events.Event) (utils.LogLine,
return utils.LogLine{
Pod: podName,
Namespace: namespace,
Error: err,
Error: err.Error(),
Status: "failure",
},
err
Expand Down
2 changes: 1 addition & 1 deletion actionners/kubernetes/terminate/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Terminate = func(rule *rules.Rule, event *events.Event) (utils.LogLine, erro
Pod: pod,
Namespace: namespace,
Status: "failure",
Error: err,
Error: err.Error(),
},
err
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"errors"

"github.com/Issif/falco-talon/actionners"
"github.com/Issif/falco-talon/configuration"
ruleengine "github.com/Issif/falco-talon/internal/rules"
Expand All @@ -24,15 +22,15 @@ var checkCmd = &cobra.Command{
}
rules := ruleengine.ParseRules(config.RulesFile)
if rules == nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: errors.New("invalid rules"), Message: "rules"})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: "invalid rules", Message: "rules"})
}
actionners.Init()
actions := actionners.GetActionners()
for _, i := range *actions {
for _, j := range *rules {
if i.CheckParameters != nil {
if err := i.CheckParameters(j); err != nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, Rule: j.GetName(), Message: "rules"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), Rule: j.GetName(), Message: "rules"})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Execute() {
config := configuration.GetConfiguration()
err := RootCmd.Execute()
if err != nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: err})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: err.Error()})
}
}

Expand Down
15 changes: 7 additions & 8 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -31,7 +30,7 @@ var serverCmd = &cobra.Command{
}
rules := ruleengine.ParseRules(config.RulesFile)
if rules == nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: errors.New("invalid rules"), Message: "rules"})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: "invalid rules", Message: "rules"})
}
actionners.Init()
notifiers.Init()
Expand Down Expand Up @@ -60,12 +59,12 @@ var serverCmd = &cobra.Command{
ignore := false
watcher, err := fsnotify.NewWatcher()
if err != nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, Message: "rules"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), Message: "rules"})
return
}
defer watcher.Close()
if err := watcher.Add(config.RulesFile); err != nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, Message: "rules"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), Message: "rules"})
return
}
for {
Expand All @@ -80,7 +79,7 @@ var serverCmd = &cobra.Command{
utils.PrintLog("info", config.LogFormat, utils.LogLine{Result: "changes detected", Message: "rules"})
newRules := ruleengine.ParseRules(config.RulesFile)
if newRules == nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: errors.New("invalid rules"), Message: "rules"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: "invalid rules", Message: "rules"})
break
}
actions := actionners.GetActionners()
Expand All @@ -89,7 +88,7 @@ var serverCmd = &cobra.Command{
for _, j := range *newRules {
if i.CheckParameters != nil {
if err := i.CheckParameters(j); err != nil {
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, Rule: j.GetName(), Message: "rules"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), Rule: j.GetName(), Message: "rules"})
parametersOk = false
}
}
Expand All @@ -102,14 +101,14 @@ var serverCmd = &cobra.Command{

}
case err := <-watcher.Errors:
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err, Message: "rules"})
utils.PrintLog("error", config.LogFormat, utils.LogLine{Error: err.Error(), Message: "rules"})
}
}
}()
}

if err := srv.ListenAndServe(); err != nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: err})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: err.Error()})
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func CreateConfiguration(configFile string) *Configuration {
v.AddConfigPath(d)
err := v.ReadInConfig()
if err != nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: fmt.Errorf("error when reading config file: '%v'", err.Error())})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: fmt.Sprintf("error when reading config file: '%v'", err.Error())})
}
}

if err := v.Unmarshal(config); err != nil {
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: fmt.Errorf("error unmarshalling config file: '%v'", err.Error())})
utils.PrintLog("fatal", config.LogFormat, utils.LogLine{Error: fmt.Sprintf("error unmarshalling config file: '%v'", err.Error())})
}

// fmt.Printf("%#v\n", config)
Expand Down
Loading

0 comments on commit 2c119ab

Please sign in to comment.