-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.go
244 lines (208 loc) · 6.61 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
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
package function
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
var (
triggerToColour = map[string]string{
"run:created": "#595959",
"run:planning": "#13c2c2",
"run:needs_attention": "#fadb14",
"run:applying": "#1890ff",
"run:completed": "#a0d911",
"run:errored": "#f5222d",
}
)
type TFENotificationPayload struct {
PayloadVersion int `json:"payload_version"`
NotificationConfigurationID string `json:"notification_configuration_id"`
RunURL string `json:"run_url"`
RunID string `json:"run_id"`
RunMessage string `json:"run_message"`
RunCreatedAt time.Time `json:"run_created_at"`
RunCreatedBy string `json:"run_created_by"`
WorkspaceID string `json:"workspace_id"`
WorkspaceName string `json:"workspace_name"`
OrganizationName string `json:"organization_name"`
Notifications []TFENotification `json:"notifications"`
}
type TFENotification struct {
Message string `json:"message"`
Trigger string `json:"trigger"`
RunStatus string `json:"run_status"`
RunUpdatedAt time.Time `json:"run_updated_at"`
RunUpdatedBy string `json:"run_updated_by"`
}
type MessageCard struct {
Type string `json:"@type"`
Context string `json:"@context"`
Summary string `json:"summary,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
ThemeColor string `json:"themeColor,omitempty"`
Sections []Section `json:"sections,omitempty"`
PotentialActions []PotentialAction `json:"potentialAction,omitempty"`
}
type Section struct {
ActivityTitle string `json:"activityTitle,omitempty"`
ActivitySubtitle string `json:"activitySubtitle,omitempty"`
Facts []Fact `json:"facts,omitempty"`
}
type Fact struct {
Name string `json:"name"`
Value string `json:"value"`
}
type PotentialAction struct {
Type string `json:"@type"`
Name string `json:"name"`
Targets []map[string]string `json:"targets,omitempty"`
}
func toTeams(payload TFENotificationPayload) MessageCard {
notification := payload.Notifications[0]
title := notification.Message
if payload.WorkspaceName != "" {
title = fmt.Sprintf("%s in %s.", notification.Message, payload.WorkspaceName)
}
facts := []Fact{}
if payload.OrganizationName != "" {
facts = append(facts, Fact{
Name: "Organisation",
Value: payload.OrganizationName,
})
}
if payload.RunID != "" {
facts = append(facts, Fact{
Name: "Run ID",
Value: payload.RunID,
})
}
if payload.RunCreatedBy != "" {
facts = append(facts, Fact{
Name: "Run Created By",
Value: payload.RunCreatedBy,
})
}
if !payload.RunCreatedAt.IsZero() {
facts = append(facts, Fact{
Name: "Run Created At",
Value: payload.RunCreatedAt.Format("02/01/2006 15:04:05"),
})
}
colour := triggerToColour[notification.Trigger]
log.Println("unsupported trigger:", notification.Trigger)
section := Section{
Facts: facts,
}
if notification.RunUpdatedBy != "" {
section.ActivityTitle = notification.RunUpdatedBy
}
if !notification.RunUpdatedAt.IsZero() {
section.ActivitySubtitle = notification.RunUpdatedAt.Format("02/01/2006 15:04:05")
}
return MessageCard{
Type: "MessageCard",
Context: "https://schema.org/extensions",
Summary: title,
Title: title,
Text: payload.RunMessage,
ThemeColor: colour,
Sections: []Section{section},
PotentialActions: []PotentialAction{
{
Type: "OpenUri",
Name: "View Run",
Targets: []map[string]string{
{
"os": "default",
"uri": payload.RunURL,
},
},
},
},
}
}
func F(w http.ResponseWriter, r *http.Request) {
teamsWebhookURL := os.Getenv("TEAMS_WEBHOOK_URL")
if teamsWebhookURL == "" {
log.Fatalln("`TEAMS_WEBHOOK_URL` is not set in the environment")
}
if _, err := url.Parse(teamsWebhookURL); err != nil {
log.Fatalln(err)
}
if contentType := r.Header.Get("Content-Type"); r.Method != "POST" || contentType != "application/json" {
log.Printf("\ninvalid method / content-type: %s / %s \n", r.Method, contentType)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("invalid request"))
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalln(err)
}
tfeWebhookToken := os.Getenv("TFE_WEBHOOK_TOKEN")
// https://www.terraform.io/docs/cloud/api/notification-configurations.html#notification-authenticity
if tfeNotificationSignature := strings.TrimSpace(r.Header.Get("X-TFE-Notification-Signature")); tfeNotificationSignature != "" {
if tfeWebhookToken == "" {
log.Fatalln("received notification with signature, but `TFE_WEBHOOK_TOKEN` was not set in the environment")
}
mac := hmac.New(sha512.New, []byte(strings.TrimSpace(tfeWebhookToken)))
_, err = mac.Write(data)
if err != nil {
log.Fatalln(err)
}
expectedMAC := mac.Sum(nil)
tfeNotificationHexSignature, err := hex.DecodeString(tfeNotificationSignature)
if err != nil {
log.Fatalln(err)
}
if !hmac.Equal(tfeNotificationHexSignature, expectedMAC) {
log.Printf("\nsignature does not match: %s (got) != %s (want) \n", hex.EncodeToString(tfeNotificationHexSignature), hex.EncodeToString(expectedMAC))
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("invalid request"))
return
}
}
var payload TFENotificationPayload
err = json.Unmarshal(data, &payload)
if err != nil {
log.Printf("\nraw data received: %q \n", data)
log.Fatalln(err)
}
if payload.PayloadVersion != 1 {
log.Println("version not supported:", payload.PayloadVersion)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("invalid request"))
return
}
teamsWebhook := toTeams(payload)
teamsPayload, err := json.Marshal(teamsWebhook)
if err != nil {
log.Fatalln(err)
}
res, err := http.Post(teamsWebhookURL, "application/json", bytes.NewBuffer(teamsPayload))
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
log.Println("payload", string(teamsPayload))
log.Fatalln("unexpected status code", res.StatusCode)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(teamsWebhook)
if err != nil {
log.Fatalln(err)
}
}