Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to use a consistent order for the output_fields and tags for more outputs #802

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TOOLS_BIN_DIR := $(abspath $(TOOLS_DIR)/bin)
GO_INSTALL = ./hack/go_install.sh

# Binaries.
GOLANGCI_LINT_VER := v1.52.2
GOLANGCI_LINT_VER := v1.56.2
GOLANGCI_LINT_BIN := golangci-lint
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)

Expand Down
5 changes: 5 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"log"
"net/http"
"sort"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -152,6 +153,10 @@ func newFalcoPayload(payload io.Reader) (types.FalcoPayload, error) {
}
}

if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
}

nullClient.CountMetric("falco.accepted", 1, []string{"priority:" + falcopayload.Priority.String()})
stats.Falco.Add(strings.ToLower(falcopayload.Priority.String()), 1)
promLabels := map[string]string{"rule": falcopayload.Rule, "priority": falcopayload.Priority.String(), "source": falcopayload.Source, "k8s_ns_name": kn, "k8s_pod_name": kp}
Expand Down
2 changes: 2 additions & 0 deletions outputs/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"encoding/json"
"log"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -103,6 +104,7 @@ func newAlertmanagerPayload(falcopayload types.FalcoPayload, config *types.Confi
amPayload.Labels[Hostname] = falcopayload.Hostname
}
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
amPayload.Labels["tags"] = strings.Join(falcopayload.Tags, ",")
}

Expand Down
2 changes: 1 addition & 1 deletion outputs/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
const defaultThresholds = `[{"priority":"critical", "value":10000}, {"priority":"critical", "value":1000}, {"priority":"critical", "value":100} ,{"priority":"warning", "value":10}, {"priority":"warning", "value":1}]`

func TestNewAlertmanagerPayloadO(t *testing.T) {
expectedOutput := `[{"labels":{"proc_name":"falcosidekick","priority":"Debug","severity": "information","proc_tty":"1234","eventsource":"syscalls","hostname":"test-host","rule":"Test rule","source":"falco","tags":"test,example"},"annotations":{"info":"This is a test from falcosidekick","description":"This is a test from falcosidekick","summary":"Test rule"}}]`
expectedOutput := `[{"labels":{"proc_name":"falcosidekick","priority":"Debug","severity": "information","proc_tty":"1234","eventsource":"syscalls","hostname":"test-host","rule":"Test rule","source":"falco","tags":"example,test"},"annotations":{"info":"This is a test from falcosidekick","description":"This is a test from falcosidekick","summary":"Test rule"}}]`
var f types.FalcoPayload
d := json.NewDecoder(strings.NewReader(falcoTestInput))
d.UseNumber()
Expand Down
18 changes: 9 additions & 9 deletions outputs/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ limitations under the License.
package outputs

import (
"fmt"
"log"
"sort"

"github.com/falcosecurity/falcosidekick/types"
)
Expand All @@ -38,21 +40,19 @@ type datadogPayload struct {

func newDatadogPayload(falcopayload types.FalcoPayload) datadogPayload {
var d datadogPayload
var tags []string

for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
tags = append(tags, i+":"+v)
default:
continue
}
tags := make([]string, 0)

for _, i := range getSortedStringKeys(falcopayload.OutputFields) {
tags = append(tags, fmt.Sprintf("%v:%v", i, falcopayload.OutputFields[i]))

}
tags = append(tags, "source:"+falcopayload.Source)
if falcopayload.Hostname != "" {
tags = append(tags, Hostname+":"+falcopayload.Hostname)
}

if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
tags = append(tags, falcopayload.Tags...)
}
d.Tags = tags
Expand Down
2 changes: 1 addition & 1 deletion outputs/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func TestNewDatadogPayload(t *testing.T) {
expectedOutput := `{"title":"Test rule","text":"This is a test from falcosidekick","alert_type":"info","source_type_name":"falco","tags":["proc.name:falcosidekick", "source:syscalls", "hostname:test-host", "test", "example"]}`
expectedOutput := `{"title":"Test rule","text":"This is a test from falcosidekick","alert_type":"info","source_type_name":"falco","tags":["proc.name:falcosidekick", "source:syscalls", "hostname:test-host", "example", "test"]}`
var f types.FalcoPayload
json.Unmarshal([]byte(falcoTestInput), &f)
s, _ := json.Marshal(newDatadogPayload(f))
Expand Down
17 changes: 7 additions & 10 deletions outputs/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"fmt"
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -78,23 +79,19 @@ func newDiscordPayload(falcopayload types.FalcoPayload, config *types.Configurat
embedFields := make([]discordEmbedFieldPayload, 0)
var embedField discordEmbedFieldPayload

for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
embedField = discordEmbedFieldPayload{i, fmt.Sprintf("```%s```", v), true}
default:
continue
}
embedFields = append(embedFields, embedField)
}

embedFields = append(embedFields, discordEmbedFieldPayload{Rule, falcopayload.Rule, true})
embedFields = append(embedFields, discordEmbedFieldPayload{Priority, falcopayload.Priority.String(), true})
embedFields = append(embedFields, discordEmbedFieldPayload{Source, falcopayload.Source, true})
if falcopayload.Hostname != "" {
embedFields = append(embedFields, discordEmbedFieldPayload{Hostname, falcopayload.Hostname, true})
}

for _, i := range getSortedStringKeys(falcopayload.OutputFields) {
embedField = discordEmbedFieldPayload{i, fmt.Sprintf("```%v```", falcopayload.OutputFields[i]), true}
embedFields = append(embedFields, embedField)
}
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
embedFields = append(embedFields, discordEmbedFieldPayload{Tags, strings.Join(falcopayload.Tags, ", "), true})
}
embedFields = append(embedFields, discordEmbedFieldPayload{Time, falcopayload.Time.String(), true})
Expand Down
12 changes: 6 additions & 6 deletions outputs/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func TestNewDiscordPayload(t *testing.T) {
Description: "This is a test from falcosidekick",
Color: "12370112", // light grey
Fields: []discordEmbedFieldPayload{
{
Name: "proc.name",
Value: fmt.Sprintf("```%s```", "falcosidekick"),
Inline: true,
},
{
Name: "rule",
Value: "Test rule",
Expand All @@ -62,9 +57,14 @@ func TestNewDiscordPayload(t *testing.T) {
Value: "test-host",
Inline: true,
},
{
Name: "proc.name",
Value: fmt.Sprintf("```%s```", "falcosidekick"),
Inline: true,
},
{
Name: "tags",
Value: "test, example",
Value: "example, test",
Inline: true,
},
{
Expand Down
17 changes: 9 additions & 8 deletions outputs/googlechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"bytes"
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -72,6 +73,13 @@ func newGooglechatPayload(falcopayload types.FalcoPayload, config *types.Configu
}
}

widgets = append(widgets, widget{KeyValue: keyValue{"rule", falcopayload.Rule}})
widgets = append(widgets, widget{KeyValue: keyValue{"priority", falcopayload.Priority.String()}})
widgets = append(widgets, widget{KeyValue: keyValue{"source", falcopayload.Source}})
if falcopayload.Hostname != "" {
widgets = append(widgets, widget{KeyValue: keyValue{Hostname, falcopayload.Hostname}})
}

for _, i := range getSortedStringKeys(falcopayload.OutputFields) {
widgets = append(widgets, widget{
KeyValue: keyValue{
Expand All @@ -81,15 +89,8 @@ func newGooglechatPayload(falcopayload types.FalcoPayload, config *types.Configu
})
}

widgets = append(widgets, widget{KeyValue: keyValue{"rule", falcopayload.Rule}})
widgets = append(widgets, widget{KeyValue: keyValue{"priority", falcopayload.Priority.String()}})
widgets = append(widgets, widget{KeyValue: keyValue{"source", falcopayload.Source}})

if falcopayload.Hostname != "" {
widgets = append(widgets, widget{KeyValue: keyValue{Hostname, falcopayload.Hostname}})
}

if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
widgets = append(widgets, widget{
KeyValue: keyValue{
TopLabel: "tags",
Expand Down
14 changes: 7 additions & 7 deletions outputs/googlechat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ func TestNewGoogleChatPayload(t *testing.T) {
Sections: []section{
{
Widgets: []widget{
{
keyValue{
TopLabel: "proc.name",
Content: "falcosidekick",
},
},
{
keyValue{
TopLabel: "rule",
Expand All @@ -65,10 +59,16 @@ func TestNewGoogleChatPayload(t *testing.T) {
Content: "test-host",
},
},
{
keyValue{
TopLabel: "proc.name",
Content: "falcosidekick",
},
},
{
keyValue{
TopLabel: "tags",
Content: "test, example",
Content: "example, test",
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions outputs/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"fmt"
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -68,6 +69,7 @@ func newLokiPayload(falcopayload types.FalcoPayload, config *types.Configuration
}

if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
s["tags"] = strings.Join(falcopayload.Tags, ",")
}

Expand Down
2 changes: 1 addition & 1 deletion outputs/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestNewLokiPayload(t *testing.T) {
{
Stream: map[string]string{
"hostname": "test-host",
"tags": "test,example",
"tags": "example,test",
"rule": "Test rule",
"source": "syscalls",
"priority": "Debug",
Expand Down
2 changes: 2 additions & 0 deletions outputs/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"bytes"
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -54,6 +55,7 @@ func newMattermostPayload(falcopayload types.FalcoPayload, config *types.Configu
field.Short = true
fields = append(fields, field)
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
field.Title = Tags
field.Value = strings.Join(falcopayload.Tags, ", ")
field.Short = true
Expand Down
2 changes: 1 addition & 1 deletion outputs/mattermost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestMattermostPayload(t *testing.T) {
},
{
Title: "tags",
Value: "test, example",
Value: "example, test",
Short: true,
},
{
Expand Down
2 changes: 2 additions & 0 deletions outputs/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"context"
"log"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -68,6 +69,7 @@ func createPagerdutyEvent(falcopayload types.FalcoPayload, config types.Pagerdut
falcopayload.OutputFields[Hostname] = falcopayload.Hostname
}
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
details["tags"] = strings.Join(falcopayload.Tags, ", ")
}
event := pagerduty.V2Event{
Expand Down
2 changes: 2 additions & 0 deletions outputs/rocketchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"bytes"
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -48,6 +49,7 @@ func newRocketchatPayload(falcopayload types.FalcoPayload, config *types.Configu
field.Short = true
fields = append(fields, field)
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
field.Title = Tags
field.Value = strings.Join(falcopayload.Tags, ", ")
field.Short = true
Expand Down
2 changes: 1 addition & 1 deletion outputs/rocketchat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestNewRocketchatPayload(t *testing.T) {
},
{
Title: "tags",
Value: "test, example",
Value: "example, test",
Short: true,
},
{
Expand Down
2 changes: 2 additions & 0 deletions outputs/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package outputs
import (
"bytes"
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -79,6 +80,7 @@ func newSlackPayload(falcopayload types.FalcoPayload, config *types.Configuratio
fields = append(fields, field)
}
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
field.Title = Tags
field.Value = strings.Join(falcopayload.Tags, ", ")
field.Short = true
Expand Down
2 changes: 1 addition & 1 deletion outputs/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestNewSlackPayload(t *testing.T) {
},
{
Title: "tags",
Value: "test, example",
Value: "example, test",
Short: true,
},
{
Expand Down
21 changes: 9 additions & 12 deletions outputs/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package outputs

import (
"log"
"sort"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -65,18 +66,6 @@ func newTeamsPayload(falcopayload types.FalcoPayload, config *types.Configuratio
}

if config.Teams.OutputFormat == All || config.Teams.OutputFormat == "facts" || config.Teams.OutputFormat == "" {
for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
fact.Name = i
fact.Value = v
default:
continue
}

facts = append(facts, fact)
}

fact.Name = Rule
fact.Value = falcopayload.Rule
facts = append(facts, fact)
Expand All @@ -91,7 +80,15 @@ func newTeamsPayload(falcopayload types.FalcoPayload, config *types.Configuratio
fact.Value = falcopayload.Hostname
facts = append(facts, fact)
}

for _, i := range getSortedStringKeys(falcopayload.OutputFields) {
fact.Name = i
fact.Value = falcopayload.OutputFields[i].(string)
facts = append(facts, fact)
}

if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
fact.Name = Tags
fact.Value = strings.Join(falcopayload.Tags, ", ")
facts = append(facts, fact)
Expand Down
Loading
Loading