-
Notifications
You must be signed in to change notification settings - Fork 165
/
googlechat.go
117 lines (97 loc) · 2.96 KB
/
googlechat.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
// SPDX-License-Identifier: MIT OR Apache-2.0
package outputs
import (
"bytes"
"log"
"sort"
"strings"
"github.com/falcosecurity/falcosidekick/types"
)
type header struct {
Title string `json:"title"`
SubTitle string `json:"subtitle"`
}
type keyValue struct {
TopLabel string `json:"topLabel"`
Content string `json:"content"`
}
type widget struct {
KeyValue keyValue `json:"keyValue,omitempty"`
}
type section struct {
Widgets []widget `json:"widgets"`
}
type card struct {
Header header `json:"header,omitempty"`
Sections []section `json:"sections,omitempty"`
}
type googlechatPayload struct {
Text string `json:"text,omitempty"`
Cards []card `json:"cards,omitempty"`
}
func newGooglechatPayload(falcopayload types.FalcoPayload, config *types.Configuration) googlechatPayload {
var messageText string
widgets := []widget{}
if config.Googlechat.MessageFormatTemplate != nil {
buf := &bytes.Buffer{}
if err := config.Googlechat.MessageFormatTemplate.Execute(buf, falcopayload); err != nil {
log.Printf("[ERROR] : GoogleChat - Error expanding Google Chat message %v", err)
} else {
messageText = buf.String()
}
}
if config.Googlechat.OutputFormat == Text {
return googlechatPayload{
Text: messageText,
}
}
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{
TopLabel: i,
Content: falcopayload.OutputFields[i].(string),
},
})
}
if len(falcopayload.Tags) != 0 {
sort.Strings(falcopayload.Tags)
widgets = append(widgets, widget{
KeyValue: keyValue{
TopLabel: "tags",
Content: strings.Join(falcopayload.Tags, ", "),
},
})
}
widgets = append(widgets, widget{KeyValue: keyValue{"time", falcopayload.Time.String()}})
return googlechatPayload{
Text: messageText,
Cards: []card{
{
Sections: []section{
{Widgets: widgets},
},
},
},
}
}
// GooglechatPost posts event to Google Chat
func (c *Client) GooglechatPost(falcopayload types.FalcoPayload) {
c.Stats.GoogleChat.Add(Total, 1)
err := c.Post(newGooglechatPayload(falcopayload, c.Config))
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:googlechat", "status:error"})
c.Stats.GoogleChat.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "googlechat", "status": Error}).Inc()
log.Printf("[ERROR] : GoogleChat - %v\n", err)
return
}
go c.CountMetric(Outputs, 1, []string{"output:googlechat", "status:ok"})
c.Stats.GoogleChat.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "googlechat", "status": OK}).Inc()
}