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

Add test for discord and refactor #117

Merged
merged 2 commits into from
Nov 20, 2020
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
25 changes: 8 additions & 17 deletions outputs/discord.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package outputs

import (
"fmt"
"strings"

"github.com/falcosecurity/falcosidekick/types"
Expand Down Expand Up @@ -62,26 +63,17 @@ func newDiscordPayload(falcopayload types.FalcoPayload, config *types.Configurat
for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
embedField.Name = i
embedField.Inline = true
embedField.Value = "```" + v + "```"
embedField = discordEmbedFieldPayload{i, fmt.Sprintf("```%s```", v), true}
default:
continue
}

embedFields = append(embedFields, embedField)
}
embedField.Name = Rule
embedField.Value = falcopayload.Rule
embedField.Inline = true
embedFields = append(embedFields, embedField)
embedField.Name = Priority
embedField.Value = falcopayload.Priority
embedField.Inline = true
embedFields = append(embedFields, embedField)
embedField.Name = Time
embedField.Value = falcopayload.Time.String()
embedField.Inline = true
embedFields = append(embedFields, embedField)

embedFields = append(embedFields, discordEmbedFieldPayload{Rule, falcopayload.Rule, true})
embedFields = append(embedFields, discordEmbedFieldPayload{Priority, falcopayload.Priority, true})
embedFields = append(embedFields, discordEmbedFieldPayload{Time, falcopayload.Time.String(), true})

embed := discordEmbedPayload{
Title: "",
Expand All @@ -91,12 +83,11 @@ func newDiscordPayload(falcopayload types.FalcoPayload, config *types.Configurat
}
embeds = append(embeds, embed)

ds := discordPayload{
return discordPayload{
Content: "",
AvatarURL: iconURL,
Embeds: embeds,
}
return ds
}

// DiscordPost posts events to discord
Expand Down
55 changes: 55 additions & 0 deletions outputs/discord_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package outputs

import (
"encoding/json"
"fmt"
"testing"

"github.com/falcosecurity/falcosidekick/types"
"github.com/stretchr/testify/require"
)

func TestNewDiscordPayload(t *testing.T) {
expectedOutput := discordPayload{
Content: "",
AvatarURL: DefaultIconURL,
Embeds: []discordEmbedPayload{
{
Title: "",
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",
Inline: true,
},
{
Name: "priority",
Value: "Debug",
Inline: true,
},
{
Name: "time",
Value: "2001-01-01 01:10:00 +0000 UTC",
Inline: true,
},
},
},
},
}

var f types.FalcoPayload
require.Nil(t, json.Unmarshal([]byte(falcoTestInput), &f))
config := &types.Configuration{
Discord: types.DiscordOutputConfig{},
}

output := newDiscordPayload(f, config)
require.Equal(t, output, expectedOutput)
}
4 changes: 2 additions & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Configuration struct {
Rocketchat rocketchatOutputConfig
Teams teamsOutputConfig
Datadog datadogOutputConfig
Discord discordOutputConfig
Discord DiscordOutputConfig
Alertmanager alertmanagerOutputConfig
Elasticsearch elasticsearchOutputConfig
Influxdb influxdbOutputConfig
Expand Down Expand Up @@ -93,7 +93,7 @@ type datadogOutputConfig struct {
MinimumPriority string
}

type discordOutputConfig struct {
type DiscordOutputConfig struct {
WebhookURL string
MinimumPriority string
Icon string
Expand Down