Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Panato <ctadeu@gmail.com>
  • Loading branch information
cpanato committed Nov 15, 2020
1 parent 325cbca commit 17a8a8d
Show file tree
Hide file tree
Showing 33 changed files with 1,077 additions and 957 deletions.
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func getConfig() *types.Configuration {
log.Printf("[ERROR] : Error when reading config file : %v\n", err)
}
}

v.GetStringMapString("customfields")
v.GetStringMapString("Webhook.CustomHeaders")
v.Unmarshal(c)
Expand All @@ -138,6 +139,7 @@ func getConfig() *types.Configuration {
}
}
}

if value, present := os.LookupEnv("WEBHOOK_CUSTOMHEADERS"); present {
customfields := strings.Split(value, ",")
for _, label := range customfields {
Expand Down Expand Up @@ -182,6 +184,7 @@ func checkPriority(prio string) string {
if match {
return prio
}

return ""
}

Expand All @@ -192,7 +195,9 @@ func getMessageFormatTemplate(output, temp string) *template.Template {
if err != nil {
log.Fatalf("[ERROR] : Error compiling %v message template : %v\n", output, err)
}

return t
}

return nil
}
61 changes: 41 additions & 20 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/falcosecurity/falcosidekick/types"
)

const TestRule string = "Test rule"

func getPriorityMap() map[string]int {
return map[string]int{
"emergency": 8,
Expand All @@ -37,6 +39,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
stats.Requests.Add("rejected", 1)
promStats.Inputs.With(map[string]string{"source": "requests", "status": "rejected"}).Inc()
nullClient.CountMetric("inputs.requests.rejected", 1, []string{"error:nobody"})

return
}

Expand All @@ -46,6 +49,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
stats.Requests.Add("rejected", 1)
promStats.Inputs.With(map[string]string{"source": "requests", "status": "rejected"}).Inc()
nullClient.CountMetric("inputs.requests.rejected", 1, []string{"error:invalidjson"})

return
}

Expand Down Expand Up @@ -125,61 +129,78 @@ func newFalcoPayload(payload io.Reader) (types.FalcoPayload, error) {
}

func forwardEvent(falcopayload types.FalcoPayload) {
if config.Slack.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Slack.MinimumPriority)] || falcopayload.Rule == "Test rule") {
if config.Slack.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Slack.MinimumPriority)] || falcopayload.Rule == TestRule) {
go slackClient.SlackPost(falcopayload)
}
if config.Rocketchat.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Rocketchat.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Rocketchat.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Rocketchat.MinimumPriority)] || falcopayload.Rule == TestRule) {
go rocketchatClient.RocketchatPost(falcopayload)
}
if config.Mattermost.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Mattermost.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Mattermost.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Mattermost.MinimumPriority)] || falcopayload.Rule == TestRule) {
go mattermostClient.MattermostPost(falcopayload)
}
if config.Teams.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Teams.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Teams.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Teams.MinimumPriority)] || falcopayload.Rule == TestRule) {
go teamsClient.TeamsPost(falcopayload)
}
if config.Datadog.APIKey != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Datadog.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Datadog.APIKey != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Datadog.MinimumPriority)] || falcopayload.Rule == TestRule) {
go datadogClient.DatadogPost(falcopayload)
}
if config.Discord.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Discord.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Discord.WebhookURL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Discord.MinimumPriority)] || falcopayload.Rule == TestRule) {
go discordClient.DiscordPost(falcopayload)
}
if config.Alertmanager.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Alertmanager.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Alertmanager.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Alertmanager.MinimumPriority)] || falcopayload.Rule == TestRule) {
go alertmanagerClient.AlertmanagerPost(falcopayload)
}
if config.Elasticsearch.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Elasticsearch.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Elasticsearch.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Elasticsearch.MinimumPriority)] || falcopayload.Rule == TestRule) {
go elasticsearchClient.ElasticsearchPost(falcopayload)
}
if config.Influxdb.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Influxdb.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Influxdb.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Influxdb.MinimumPriority)] || falcopayload.Rule == TestRule) {
go influxdbClient.InfluxdbPost(falcopayload)
}
if config.Loki.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Loki.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Loki.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Loki.MinimumPriority)] || falcopayload.Rule == TestRule) {
go lokiClient.LokiPost(falcopayload)
}
if config.Nats.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Nats.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Nats.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Nats.MinimumPriority)] || falcopayload.Rule == TestRule) {
go natsClient.NatsPublish(falcopayload)
}
if config.AWS.Lambda.FunctionName != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.AWS.Lambda.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.AWS.Lambda.FunctionName != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.AWS.Lambda.MinimumPriority)] || falcopayload.Rule == TestRule) {
go awsClient.InvokeLambda(falcopayload)
}
if config.AWS.SQS.URL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.AWS.SQS.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.AWS.SQS.URL != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.AWS.SQS.MinimumPriority)] || falcopayload.Rule == TestRule) {
go awsClient.SendMessage(falcopayload)
}
if config.AWS.SNS.TopicArn != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.AWS.SNS.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.AWS.SNS.TopicArn != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.AWS.SNS.MinimumPriority)] || falcopayload.Rule == TestRule) {
go awsClient.PublishTopic(falcopayload)
}
if config.SMTP.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.SMTP.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.SMTP.HostPort != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.SMTP.MinimumPriority)] || falcopayload.Rule == TestRule) {
go smtpClient.SendMail(falcopayload)
}
if config.Opsgenie.APIKey != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Opsgenie.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Opsgenie.APIKey != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Opsgenie.MinimumPriority)] || falcopayload.Rule == TestRule) {
go opsgenieClient.OpsgeniePost(falcopayload)
}
if config.Webhook.Address != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Webhook.MinimumPriority)] || falcopayload.Rule == "Test rule") {
if config.Webhook.Address != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Webhook.MinimumPriority)] || falcopayload.Rule == TestRule) {
go webhookClient.WebhookPost(falcopayload)
}
if config.Azure.EventHub.Name != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Azure.EventHub.MinimumPriority)] || falcopayload.Rule == "Test rule") {

if config.Azure.EventHub.Name != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.Azure.EventHub.MinimumPriority)] || falcopayload.Rule == TestRule) {
go azureClient.EventHubPost(falcopayload)
}
if config.GCP.PubSub.Topic != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.GCP.PubSub.MinimumPriority)] || falcopayload.Rule == "Test rule") {
go gcpClient.GCPPublishTopic(falcopayload)

if config.GCP.PubSub.Topic != "" && (priorityMap[strings.ToLower(falcopayload.Priority)] >= priorityMap[strings.ToLower(config.GCP.PubSub.MinimumPriority)] || falcopayload.Rule == TestRule) {
go gcpClient.GCPPublishTopic(falcopayload)
}
}
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
enabledOutputsText += "StatsD "
}
}

if config.Dogstatsd.Forwarder != "" {
var err error
dogstatsdClient, err = outputs.NewStatsdClient("DogStatsD", config, stats)
Expand Down Expand Up @@ -85,6 +86,7 @@ func init() {
enabledOutputsText += "Slack "
}
}

if config.Rocketchat.WebhookURL != "" {
var err error
rocketchatClient, err = outputs.NewClient("Rocketchat", config.Rocketchat.WebhookURL, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -94,6 +96,7 @@ func init() {
enabledOutputsText += "Rocketchat "
}
}

if config.Mattermost.WebhookURL != "" {
var err error
mattermostClient, err = outputs.NewClient("Mattermost", config.Mattermost.WebhookURL, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -103,6 +106,7 @@ func init() {
enabledOutputsText += "Mattermost "
}
}

if config.Teams.WebhookURL != "" {
var err error
teamsClient, err = outputs.NewClient("Teams", config.Teams.WebhookURL, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -112,6 +116,7 @@ func init() {
enabledOutputsText += "Teams "
}
}

if config.Datadog.APIKey != "" {
var err error
datadogClient, err = outputs.NewClient("Datadog", config.Datadog.Host+outputs.DatadogPath+"?api_key="+config.Datadog.APIKey, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -121,6 +126,7 @@ func init() {
enabledOutputsText += "Datadog "
}
}

if config.Discord.WebhookURL != "" {
var err error
discordClient, err = outputs.NewClient("Discord", config.Discord.WebhookURL, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -130,6 +136,7 @@ func init() {
enabledOutputsText += "Discord "
}
}

if config.Alertmanager.HostPort != "" {
var err error
alertmanagerClient, err = outputs.NewClient("AlertManager", config.Alertmanager.HostPort+outputs.AlertmanagerURI, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -139,6 +146,7 @@ func init() {
enabledOutputsText += "AlertManager "
}
}

if config.Elasticsearch.HostPort != "" {
var err error
elasticsearchClient, err = outputs.NewClient("Elasticsearch", config.Elasticsearch.HostPort+"/"+config.Elasticsearch.Index+"/"+config.Elasticsearch.Type, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -148,6 +156,7 @@ func init() {
enabledOutputsText += "Elasticsearch "
}
}

if config.Loki.HostPort != "" {
var err error
lokiClient, err = outputs.NewClient("Loki", config.Loki.HostPort+"/api/prom/push", config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -157,6 +166,7 @@ func init() {
enabledOutputsText += "Loki "
}
}

if config.Nats.HostPort != "" {
var err error
natsClient, err = outputs.NewClient("NATS", config.Nats.HostPort, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -166,11 +176,13 @@ func init() {
enabledOutputsText += "NATS "
}
}

if config.Influxdb.HostPort != "" {
var credentials string
if config.Influxdb.User != "" && config.Influxdb.Password != "" {
credentials = "&u=" + config.Influxdb.User + "&p=" + config.Influxdb.Password
}

var err error
influxdbClient, err = outputs.NewClient("Influxdb", config.Influxdb.HostPort+"/write?db="+config.Influxdb.Database+credentials, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
Expand All @@ -179,6 +191,7 @@ func init() {
enabledOutputsText += "Influxdb "
}
}

if config.AWS.Lambda.FunctionName != "" || config.AWS.SQS.URL != "" || config.AWS.SNS.TopicArn != "" {
var err error
awsClient, err = outputs.NewAWSClient(config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -201,6 +214,7 @@ func init() {
}
}
}

if config.SMTP.HostPort != "" && config.SMTP.From != "" && config.SMTP.To != "" {
var err error
smtpClient, err = outputs.NewSMTPClient(config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -210,6 +224,7 @@ func init() {
enabledOutputsText += "SMTP "
}
}

if config.Opsgenie.APIKey != "" {
var err error
url := "https://api.opsgenie.com/v2/alerts"
Expand All @@ -223,6 +238,7 @@ func init() {
enabledOutputsText += "Opsgenie "
}
}

if config.Webhook.Address != "" {
var err error
webhookClient, err = outputs.NewClient("Webhook", config.Webhook.Address, config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -232,6 +248,7 @@ func init() {
enabledOutputsText += "Webhook "
}
}

if config.Azure.EventHub.Name != "" {
var err error
azureClient, err = outputs.NewEventHubClient(config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -244,6 +261,7 @@ func init() {
}
}
}

if config.GCP.PubSub.ProjectID != "" && config.GCP.PubSub.Topic != "" && config.GCP.Credentials != "" {
var err error
gcpClient, err = outputs.NewGCPClient(config, stats, promStats, statsdClient, dogstatsdClient)
Expand All @@ -267,6 +285,7 @@ func main() {
if config.Debug {
log.Printf("[INFO] : Debug mode : %v\n", config.Debug)
}

if err := http.ListenAndServe(":"+strconv.Itoa(config.ListenPort), nil); err != nil {
log.Fatalf("[ERROR] : %v\n", err.Error())
}
Expand Down
24 changes: 12 additions & 12 deletions outputs/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ func newAlertmanagerPayload(falcopayload types.FalcoPayload) []alertmanagerPaylo
switch {
case d == 0:
jj = "0"
falcopayload.Priority = "warning"
falcopayload.Priority = Warning
case d < 10:
jj = "<10"
falcopayload.Priority = "warning"
falcopayload.Priority = Warning
case d > 10000:
jj = ">10000"
falcopayload.Priority = "critical"
falcopayload.Priority = Critical
case d > 1000:
jj = ">1000"
falcopayload.Priority = "critical"
falcopayload.Priority = Critical
case d > 100:
jj = ">100"
falcopayload.Priority = "critical"
falcopayload.Priority = Critical
case d > 10:
jj = ">10"
falcopayload.Priority = "warning"
falcopayload.Priority = Warning
default:
jj = j.(string)
falcopayload.Priority = "critical"
falcopayload.Priority = Critical
}

amPayload.Labels[i] = jj
Expand Down Expand Up @@ -86,11 +86,11 @@ func newAlertmanagerPayload(falcopayload types.FalcoPayload) []alertmanagerPaylo
func (c *Client) AlertmanagerPost(falcopayload types.FalcoPayload) {
err := c.Post(newAlertmanagerPayload(falcopayload))
if err != nil {
c.Stats.Alertmanager.Add("error", 1)
c.PromStats.Outputs.With(map[string]string{"destination": "alertmanager", "status": "error"}).Inc()
c.Stats.Alertmanager.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "alertmanager", "status": Error}).Inc()
} else {
c.Stats.Alertmanager.Add("ok", 1)
c.PromStats.Outputs.With(map[string]string{"destination": "alertmanager", "status": "ok"}).Inc()
c.Stats.Alertmanager.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "alertmanager", "status": OK}).Inc()
}
c.Stats.Alertmanager.Add("total", 1)
c.Stats.Alertmanager.Add(Total, 1)
}
2 changes: 1 addition & 1 deletion outputs/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewAlertmanagerPayload(t *testing.T) {

var o1, o2 alertmanagerPayload
json.Unmarshal([]byte(expectedOutput), &o1)
json.Unmarshal([]byte(s), &o2)
json.Unmarshal(s, &o2)

if !reflect.DeepEqual(o1, o2) {
// t.Fatalf("\nexpected payload: \n%v\ngot: \n%v\n", o1, o2)
Expand Down
Loading

0 comments on commit 17a8a8d

Please sign in to comment.