-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_slack.go
302 lines (281 loc) · 7.23 KB
/
alert_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package alert
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/ca-risken/core/pkg/model"
projectproto "github.com/ca-risken/core/proto/project"
"github.com/slack-go/slack"
)
type slackNotifySetting struct {
WebhookURL string `json:"webhook_url"`
Data slackNotifyOption `json:"data"`
Locale string `json:"locale"`
}
type slackNotifyOption struct {
Channel string `json:"channel,omitempty"`
Message string `json:"message,omitempty"`
}
const (
LocaleJa = "ja"
LocaleEn = "en"
slackNotificationMessageJa = `%v問題を検知しました。内容を確認し以下のいずれかの対応を行ってください。
- 問題の根本原因を取り除く
- 意図的な設定・操作であり、リスクが小さい場合はアーカイブする
- 問題の性質上緊急性はなく、すぐに対応が難しい場合は目標期限を設定してPENDする`
slackNotificationMessageEn = `%vDetected alerts. Please review the contents and take one of the following actions
- Remove the root cause of the problem
- If it is an intentional setup/operation and the risk is small, archive it
- If the nature of the problem is not urgent and immediate action is difficult, set a target deadline and PEND`
slackNotificationAttachmentJa = "その他、%d件すべてのFindingは <%s/alert/alert?project_id=%d&from=slack|アラート画面> からご確認ください。"
slackNotificationAttachmentEn = "Please check all %d Findings from <%s/alert/alert?project_id=%d&from=slack|Alert screen>."
slackNotificationTestMessageJa = "RISKENからのテスト通知です"
slackNotificationTestMessageEn = "This is a test notification from RISKEN"
)
func sendSlackNotification(
ctx context.Context, url, notifySetting string,
alert *model.Alert,
project *projectproto.Project,
rules *[]model.AlertRule,
findings *findingDetail,
defaultLocale string,
) error {
var setting slackNotifySetting
if err := json.Unmarshal([]byte(notifySetting), &setting); err != nil {
return err
}
if setting.WebhookURL == "" {
return nil
}
var locale string
switch setting.Locale {
case LocaleJa:
locale = LocaleJa
case LocaleEn:
locale = LocaleEn
default:
locale = defaultLocale
}
payload := getPayload(ctx, setting.Data.Channel, setting.Data.Message, url, alert, project, rules, findings, locale)
// TODO http tracing
if err := slack.PostWebhook(setting.WebhookURL, payload); err != nil {
return fmt.Errorf("failed to send slack: %w", err)
}
return nil
}
func sendSlackTestNotification(ctx context.Context, url, notifySetting, defaultLocale string) error {
var setting slackNotifySetting
if err := json.Unmarshal([]byte(notifySetting), &setting); err != nil {
return err
}
if setting.WebhookURL == "" {
return nil
}
var locale string
switch setting.Locale {
case LocaleJa:
locale = LocaleJa
case LocaleEn:
locale = LocaleEn
default:
locale = defaultLocale
}
payload := getTestPayload(setting.Data.Channel, locale)
// TODO http tracing
if err := slack.PostWebhook(setting.WebhookURL, payload); err != nil {
return fmt.Errorf("failed to send slack: %w", err)
}
return nil
}
func getPayload(
ctx context.Context,
channel string,
message string,
url string,
alert *model.Alert,
project *projectproto.Project,
rules *[]model.AlertRule,
findings *findingDetail,
locale string,
) *slack.WebhookMessage {
// attachments
attachment := slack.Attachment{
Color: getColor(alert.Severity),
Fields: []slack.AttachmentField{
{
Value: fmt.Sprintf("<%s/alert/alert?project_id=%d&from=slack|%s>", url, project.ProjectId, alert.Description),
},
{
Title: "Rules",
Value: generateRuleList(rules),
},
{
Title: "Project",
Value: project.Name,
Short: true,
},
{
Title: "Findings",
Value: fmt.Sprint(findings.FindingCount),
Short: true,
},
},
}
var msgText string
switch locale {
case LocaleJa:
msgText = slackNotificationMessageJa
default:
msgText = slackNotificationMessageEn
}
msg := slack.WebhookMessage{
Text: fmt.Sprintf(msgText, getMention(alert.Severity)),
Attachments: []slack.Attachment{attachment},
}
msg.Attachments = append(msg.Attachments, *getFindingAttachment(url, project.ProjectId, findings, locale)...)
// override message
if message != "" {
msg.Text = message // update text
}
if channel != "" {
msg.Channel = channel // add channel
}
return &msg
}
func getTestPayload(channel, locale string) *slack.WebhookMessage {
var msgText string
switch locale {
case LocaleJa:
msgText = slackNotificationTestMessageJa
default:
msgText = slackNotificationTestMessageEn
}
msg := slack.WebhookMessage{
Text: msgText,
}
// override message
if channel != "" {
msg.Channel = channel
}
return &msg
}
func getColor(severity string) string {
switch severity {
case "high":
return "danger"
case "medium":
return "warning"
case "low":
return "good"
default:
return "good"
}
}
func getMention(severity string) string {
switch severity {
case "high":
return "<!channel> "
case "medium":
return "<!here> "
case "low":
return ""
default:
return ""
}
}
const (
MAX_NOTIFY_RULE_NUM = 3
)
func generateRuleList(rules *[]model.AlertRule) string {
if rules == nil {
return ""
}
list := ""
for idx, rule := range *rules {
if idx >= MAX_NOTIFY_RULE_NUM {
list = fmt.Sprintf("%s\n- %s", list, "...")
return list
}
if idx != 0 {
list += "\n"
}
list += fmt.Sprintf("- %s", rule.Name)
}
return list
}
func getFindingAttachment(url string, projectID uint32, findings *findingDetail, locale string) *[]slack.Attachment {
attachments := []slack.Attachment{}
for _, f := range findings.Exampls {
a := slack.Attachment{
Color: getColorByScore(f.Score),
Fields: []slack.AttachmentField{
{
Value: fmt.Sprintf("<%s/finding/finding?project_id=%d&finding_id=%d&from_score=0&status=1&from=slack|%s>", url, projectID, f.FindingID, f.Description),
},
{
Title: "DataSource",
Value: f.DataSource,
Short: true,
},
{
Title: "ResourceName",
Value: f.ResourceName,
Short: true,
},
{
Title: "Tags",
Value: generateTagContentByFinding(f.Tags),
},
},
}
attachments = append(attachments, a)
}
if findings.FindingCount > len(findings.Exampls) {
var attachmentText string
switch locale {
case LocaleJa:
attachmentText = slackNotificationAttachmentJa
default:
attachmentText = slackNotificationAttachmentEn
}
attachments = append(attachments, slack.Attachment{
Color: "grey",
Fields: []slack.AttachmentField{
{
Value: fmt.Sprintf(attachmentText, findings.FindingCount, url, projectID),
},
},
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
})
}
return &attachments
}
func getColorByScore(score float32) string {
switch {
case score >= 0.8:
return "danger"
case score >= 0.6:
return "warning"
default:
return "good"
}
}
const (
MAX_NOTIFY_FINDING_TAG_NUM = 15
)
func generateTagContentByFinding(tags []string) string {
content := ""
for idx, t := range tags {
if content != "" {
content += " "
}
content += fmt.Sprintf("`%s`", t)
if idx+1 >= MAX_NOTIFY_FINDING_TAG_NUM {
content += " ..."
return content
}
}
return content
}