-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
242 lines (214 loc) · 6.32 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
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
/*
Copyright 2024 Sourav Patnaik.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/go-logr/logr"
securityv1beta1 "github.com/sourav977/stale-secrets-watch/api/v1beta1"
)
type SlackPayload struct {
Channel string `json:"channel"`
Blocks []Block `json:"blocks"`
}
type Block struct {
Type string `json:"type"`
Text *TextElement `json:"text,omitempty"`
Elements []Element `json:"elements,omitempty"`
Divider *Divider `json:"divider,omitempty"`
}
type TextElement struct {
Type string `json:"type"`
Text string `json:"text"`
}
type Element struct {
Type string `json:"type"`
Elements []Markup `json:"elements,omitempty"`
Text string `json:"text,omitempty"`
Border int `json:"border,omitempty"`
Style *Style `json:"style,omitempty"`
}
type Markup struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Name string `json:"name,omitempty"`
Style *Style `json:"style,omitempty"`
}
const (
RT = "rich_text"
RTF = "rich_text_preformatted"
)
type Style struct {
Bold bool `json:"bold,omitempty"`
}
// Updated Divider struct with explicit type field
type Divider struct {
Type string `json:"type"`
}
// NotifySlack prepares Blocks to send msg over slack
func (r *StaleSecretWatchReconciler) NotifySlack(ctx context.Context, logger logr.Logger, staleSecretWatch *securityv1beta1.StaleSecretWatch) error {
token := os.Getenv("SLACK_BOT_TOKEN")
channelID := os.Getenv("SLACK_CHANNEL_ID")
if token == "" || channelID == "" {
return fmt.Errorf("SLACK_BOT_TOKEN or SLACK_CHANNEL_ID is not set")
}
clusterName := GetClusterName()
if len(staleSecretWatch.Status.SecretStatus) <= 0 {
successText := "All secrets are up to date. Good work!\t"
payload := prepareSlackMessage("success", successText, channelID, nil)
logger.Info(successText)
return sendSlackNotification(ctx, logger, payload, token)
}
warningText := fmt.Sprintf("Below is a list of secret resources where secret data has not been modified for StaleThresholdInDays %d days in %s Cluster!! \n Total %d number of stale secrets found.\n", staleSecretWatch.Spec.StaleThresholdInDays, clusterName, staleSecretWatch.Status.StaleSecretsCount)
payload := prepareSlackMessage("warning", warningText, channelID, staleSecretWatch.Status.SecretStatus)
// Marshal the complete payload
payloadBytes, err := json.Marshal(payload)
if err != nil {
logger.Error(err, "Failed to encode ConfigData to JSON")
return err
}
logger.Info("This Info will send to Slack: ", "payload", string(payloadBytes))
return sendSlackNotification(ctx, logger, payload, token)
}
// prepareSlackMessage prepares slack msg
func prepareSlackMessage(msgType, text, channelID string, statuses []securityv1beta1.SecretStatus) SlackPayload {
var blocks []Block
// Initial common blocks
blocks = append(blocks, Block{
Type: "section",
Text: &TextElement{
Type: "mrkdwn",
Text: "*Daily check completed successfully.*\n\n",
},
}, Block{
Type: "divider",
})
if msgType != "warning" {
blocks = append(blocks, generateSuccessBlock(text))
} else {
blocks = append(blocks, generateWarningBlock(text))
// Add secret details if present
for _, status := range statuses {
info := fmt.Sprintf("\"secret_name\": \"%s\",\n\"namespace\": \"%s\",\n\"type\": \"%s\",\n\"created\": \"%s\",\n\"last_modified\": \"%s\"",
status.Name, status.Namespace, status.SecretType, status.Created.Format("2006-01-02T15:04:05Z"), status.LastModified.Format("2006-01-02T15:04:05Z"))
blocks = append(blocks, Block{
Type: RT,
Elements: []Element{
{
Type: RTF,
Border: 1,
Elements: []Markup{
{
Type: "text",
Text: info,
},
},
},
},
})
}
}
// Final divider
blocks = append(blocks, Block{Type: "divider"})
return SlackPayload{
//Channel: "C06UV9S4DC0", // Your Slack channel ID
Channel: channelID,
Blocks: blocks,
}
}
// generateWarningBlock appends warning Block to main Block
func generateWarningBlock(text string) Block {
return Block{
Type: "rich_text",
Elements: []Element{
{
Type: "rich_text_section",
Elements: []Markup{
{
Type: "emoji",
Name: "warning",
},
{
Type: "text",
Text: "Stale Secret Detected !!!",
Style: &Style{Bold: true},
},
},
},
{
Type: "rich_text_section",
Elements: []Markup{
{
Type: "text",
Text: text,
Style: &Style{Bold: true},
},
{
Type: "text",
Text: "\n\n",
},
},
},
},
}
}
// generateSuccessBlock adds success Block to main Block
func generateSuccessBlock(text string) Block {
return Block{
Type: "rich_text",
Elements: []Element{
{
Type: "rich_text_section",
Elements: []Markup{
{
Type: "text",
Text: text,
Style: &Style{Bold: true},
},
{
Type: "emoji",
Name: "smiley",
},
},
},
},
}
}
// sendSlackNotification actually sends message to slack channel
// using curl/http to send msg to slack rather using slack golang library
func sendSlackNotification(ctx context.Context, logger logr.Logger, payload SlackPayload, token string) error {
payloadBytes, err := json.Marshal(payload)
if err != nil {
logger.Error(err, "Failed to encode ConfigData to JSON")
return err
}
logger.Info("This Info will send to Slack", "payload", string(payloadBytes))
reqBody := bytes.NewReader(payloadBytes)
req, err := http.NewRequestWithContext(ctx, "POST", "https://slack.com/api/chat.postMessage", reqBody)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
logger.Info("Message sent to Slack successfully")
return nil
}