This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
function.go
164 lines (140 loc) · 3.57 KB
/
function.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
package function
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/url"
"os"
"time"
)
type Notification struct {
Incident Incident `json:"incident"`
Version string `json:"version"`
}
type Incident struct {
IncidentID string `json:"incident_id"`
ResourceID string `json:"resource_id"`
ResourceName string `json:"resource_name"`
State string `json:"state"`
StartedAt int64 `json:"started_at"`
EndedAt int64 `json:"ended_at,omitempty"`
PolicyName string `json:"policy_name"`
ConditionName string `json:"condition_name"`
URL string `json:"url"`
Summary string `json:"summary"`
}
type DiscordWebhook struct {
Content string `json:"content"`
Embeds []Embed `json:"embeds,omitempty"`
}
type Embed struct {
Title string `json:"title"`
URL string `json:"url"`
Description string `json:"description"`
Color int `json:"color"`
Fields []Field `json:"fields,omitempty"`
}
type Field struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}
func toDiscord(notification Notification) DiscordWebhook {
startedAt := "-"
endedAt := "-"
if st := notification.Incident.StartedAt; st > 0 {
startedAt = time.Unix(st, 0).String()
}
if et := notification.Incident.EndedAt; et > 0 {
endedAt = time.Unix(et, 0).String()
}
policyName := notification.Incident.PolicyName
if policyName == "" {
policyName = "-"
}
conditionName := notification.Incident.ConditionName
if conditionName == "" {
conditionName = "-"
}
colour := 1609983
if notification.Incident.State == "open" {
colour = 16065069
}
return DiscordWebhook{
Embeds: []Embed{
Embed{
Title: notification.Incident.Summary,
URL: notification.Incident.URL,
Color: colour,
Fields: []Field{
Field{
Name: "Incident ID",
Value: notification.Incident.IncidentID,
},
Field{
Name: "Policy",
Value: policyName,
Inline: true,
},
Field{
Name: "Condition",
Value: conditionName,
Inline: true,
},
Field{
Name: "Started At",
Value: startedAt,
},
Field{
Name: "Ended At",
Value: endedAt,
},
},
},
},
}
}
func F(w http.ResponseWriter, r *http.Request) {
authToken := os.Getenv("AUTH_TOKEN")
if authToken == "" {
log.Fatalln("`AUTH_TOKEN` is not set in the environment")
}
if r.URL.Query().Get("auth_token") != authToken {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("invalid request"))
return
}
discordWebhookURL := os.Getenv("DISCORD_WEBHOOK_URL")
if discordWebhookURL == "" {
log.Fatalln("`DISCORD_WEBHOOK_URL` is not set in the environment")
}
if _, err := url.Parse(discordWebhookURL); err != nil {
log.Fatalln(err)
}
if r.Method != "POST" || r.Header.Get("Content-Type") != "application/json" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("invalid request"))
return
}
var notification Notification
if err := json.NewDecoder(r.Body).Decode(¬ification); err != nil {
log.Fatalln(err)
}
discordWebhook := toDiscord(notification)
payload, err := json.Marshal(discordWebhook)
if err != nil {
log.Fatalln(err)
}
res, err := http.Post(discordWebhookURL, "application/json", bytes.NewBuffer(payload))
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
log.Println("payload", string(payload))
log.Fatalln("unexpected status code", res.StatusCode)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(discordWebhook)
}