-
Notifications
You must be signed in to change notification settings - Fork 165
/
slack.go
165 lines (145 loc) · 4.4 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: MIT OR Apache-2.0
package outputs
import (
"bytes"
"log"
"sort"
"strings"
"github.com/falcosecurity/falcosidekick/types"
)
// Field
type slackAttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
// Attachment
type slackAttachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Text string `json:"text,omitempty"`
Fields []slackAttachmentField `json:"fields"`
Footer string `json:"footer,omitempty"`
FooterIcon string `json:"footer_icon,omitempty"`
}
// Payload
type slackPayload struct {
Text string `json:"text,omitempty"`
Username string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Channel string `json:"channel,omitempty"`
Attachments []slackAttachment `json:"attachments,omitempty"`
}
func newSlackPayload(falcopayload types.FalcoPayload, config *types.Configuration) slackPayload {
var (
messageText string
attachments []slackAttachment
attachment slackAttachment
fields []slackAttachmentField
field slackAttachmentField
)
if config.Slack.OutputFormat == All || config.Slack.OutputFormat == Fields || config.Slack.OutputFormat == "" {
field.Title = Rule
field.Value = falcopayload.Rule
field.Short = true
fields = append(fields, field)
field.Title = Priority
field.Value = falcopayload.Priority.String()
field.Short = true
fields = append(fields, field)
field.Title = Source
field.Value = falcopayload.Source
field.Short = true
fields = append(fields, field)
if falcopayload.Hostname != "" {
field.Title = Hostname
field.Value = falcopayload.Hostname
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
fields = append(fields, field)
}
for _, i := range getSortedStringKeys(falcopayload.OutputFields) {
field.Title = i
field.Value = falcopayload.OutputFields[i].(string)
if len([]rune(falcopayload.OutputFields[i].(string))) < 36 {
field.Short = true
} else {
field.Short = false
}
fields = append(fields, field)
}
field.Title = Time
field.Short = false
field.Value = falcopayload.Time.String()
fields = append(fields, field)
attachment.Footer = DefaultFooter
if config.Slack.Footer != "" {
attachment.Footer = config.Slack.Footer
}
}
attachment.Fallback = falcopayload.Output
attachment.Fields = fields
if config.Slack.OutputFormat == All || config.Slack.OutputFormat == Text || config.Slack.OutputFormat == "" {
attachment.Text = falcopayload.Output
}
if config.Slack.MessageFormatTemplate != nil {
buf := &bytes.Buffer{}
if err := config.Slack.MessageFormatTemplate.Execute(buf, falcopayload); err != nil {
log.Printf("[ERROR] : Slack - Error expanding Slack message %v", err)
} else {
messageText = buf.String()
}
}
var color string
switch falcopayload.Priority {
case types.Emergency:
color = Red
case types.Alert:
color = Orange
case types.Critical:
color = Orange
case types.Error:
color = Red
case types.Warning:
color = Yellow
case types.Notice:
color = Lightcyan
case types.Informational:
color = LigthBlue
case types.Debug:
color = PaleCyan
}
attachment.Color = color
attachments = append(attachments, attachment)
s := slackPayload{
Text: messageText,
Username: config.Slack.Username,
IconURL: config.Slack.Icon,
Attachments: attachments}
if config.Slack.Channel != "" {
s.Channel = config.Slack.Channel
}
return s
}
// SlackPost posts event to Slack
func (c *Client) SlackPost(falcopayload types.FalcoPayload) {
c.Stats.Slack.Add(Total, 1)
err := c.Post(newSlackPayload(falcopayload, c.Config))
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:slack", "status:error"})
c.Stats.Slack.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "slack", "status": Error}).Inc()
log.Printf("[ERROR] : Slack - %v\n", err)
return
}
// Setting the success status
go c.CountMetric(Outputs, 1, []string{"output:slack", "status:ok"})
c.Stats.Slack.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "slack", "status": OK}).Inc()
}