forked from argoproj/argo-events
/
start.go
316 lines (260 loc) · 9.82 KB
/
start.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright 2018 KompiTech GmbH
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 github
import (
"context"
"encoding/json"
"github.com/argoproj/argo-events/gateways/server"
"github.com/argoproj/argo-events/pkg/apis/events"
"net/http"
"net/url"
"time"
"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/gateways"
"github.com/argoproj/argo-events/gateways/server/common/webhook"
"github.com/argoproj/argo-events/pkg/apis/eventsources/v1alpha1"
"github.com/argoproj/argo-events/store"
"github.com/ghodss/yaml"
gh "github.com/google/go-github/github"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
)
// GitHub headers
const (
githubEventHeader = "X-GitHub-Event"
githubDeliveryHeader = "X-GitHub-Delivery"
)
// controller controls the webhook operations
var (
controller = webhook.NewController()
)
// set up the activation and inactivation channels to control the state of routes.
func init() {
go webhook.ProcessRouteStatus(controller)
}
// getCredentials for retrieves credentials for GitHub connection
func (router *Router) getCredentials(keySelector *corev1.SecretKeySelector, namespace string) (*cred, error) {
token, err := store.GetSecrets(router.k8sClient, namespace, keySelector.Name, keySelector.Key)
if err != nil {
return nil, err
}
return &cred{
secret: token,
}, nil
}
// Implement Router
// 1. GetRoute
// 2. HandleRoute
// 3. PostActivate
// 4. PostDeactivate
// GetRoute returns the route
func (router *Router) GetRoute() *webhook.Route {
return router.route
}
// HandleRoute handles incoming requests on the route
func (router *Router) HandleRoute(writer http.ResponseWriter, request *http.Request) {
route := router.route
logger := route.Logger.WithFields(
map[string]interface{}{
common.LabelEventSource: route.EventSource.Name,
common.LabelEndpoint: route.Context.Endpoint,
common.LabelPort: route.Context.Port,
})
logger.Info("received a request, processing it...")
if !route.Active {
logger.Info("endpoint is not active, won't process the request")
common.SendErrorResponse(writer, "endpoint is inactive")
return
}
hook := router.hook
secret := ""
if s, ok := hook.Config["secret"]; ok {
secret = s.(string)
}
body, err := parseValidateRequest(request, []byte(secret))
if err != nil {
logger.WithError(err).Error("request is not valid event notification, discarding it")
common.SendErrorResponse(writer, err.Error())
return
}
event := &events.GithubEventData{
Headers: request.Header,
Body: (*json.RawMessage)(&body),
}
eventBody, err := json.Marshal(event)
if err != nil {
logger.Info("failed to marshal event")
common.SendErrorResponse(writer, "invalid event")
return
}
logger.Infoln("dispatching event on route's data channel")
route.DataCh <- eventBody
logger.Info("request successfully processed")
common.SendSuccessResponse(writer, "success")
}
// PostActivate performs operations once the route is activated and ready to consume requests
func (router *Router) PostActivate() error {
// In order to successfully setup a GitHub hook for the given repository,
// 1. Get the API Token and Webhook secret from K8s secrets
// 2. Configure the hook with url, content type, ssl etc.
// 3. Set up a GitHub client
// 4. Set the base and upload url for the client
// 5. Create the hook if one doesn't exist already. If exists already, then use that one.
route := router.route
githubEventSource := router.githubEventSource
logger := route.Logger.WithFields(map[string]interface{}{
common.LabelEventSource: route.EventSource.Name,
"repository": githubEventSource.Repository,
})
logger.Infoln("retrieving api token credentials...")
apiTokenCreds, err := router.getCredentials(githubEventSource.APIToken, githubEventSource.Namespace)
if err != nil {
return errors.Errorf("failed to retrieve api token credentials. err: %+v", err)
}
logger.Infoln("setting up auth with api token...")
PATTransport := TokenAuthTransport{
Token: apiTokenCreds.secret,
}
logger.Infoln("configuring GitHub hook...")
formattedUrl := common.FormattedURL(githubEventSource.Webhook.URL, githubEventSource.Webhook.Endpoint)
hookConfig := map[string]interface{}{
"url": &formattedUrl,
}
if githubEventSource.ContentType != "" {
hookConfig["content_type"] = githubEventSource.ContentType
}
if githubEventSource.Insecure {
hookConfig["insecure_ssl"] = "1"
} else {
hookConfig["insecure_ssl"] = "0"
}
logger.Infoln("retrieving webhook secret credentials...")
if githubEventSource.WebhookSecret != nil {
webhookSecretCreds, err := router.getCredentials(githubEventSource.WebhookSecret, githubEventSource.Namespace)
if err != nil {
return errors.Errorf("failed to retrieve webhook secret. err: %+v", err)
}
hookConfig["secret"] = webhookSecretCreds.secret
}
router.hook = &gh.Hook{
Events: githubEventSource.Events,
Active: gh.Bool(githubEventSource.Active),
Config: hookConfig,
}
logger.Infoln("setting up client for GitHub...")
router.githubClient = gh.NewClient(PATTransport.Client())
logger.Infoln("setting up base url for GitHub client...")
if githubEventSource.GithubBaseURL != "" {
baseURL, err := url.Parse(githubEventSource.GithubBaseURL)
if err != nil {
return errors.Errorf("failed to parse github base url. err: %s", err)
}
router.githubClient.BaseURL = baseURL
}
logger.Infoln("setting up the upload url for GitHub client...")
if githubEventSource.GithubUploadURL != "" {
uploadURL, err := url.Parse(githubEventSource.GithubUploadURL)
if err != nil {
return errors.Errorf("failed to parse github upload url. err: %s", err)
}
router.githubClient.UploadURL = uploadURL
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
logger.Infoln("creating a GitHub hook for the repository...")
hook, _, err := router.githubClient.Repositories.CreateHook(ctx, githubEventSource.Owner, githubEventSource.Repository, router.hook)
if err != nil {
// Continue if error is because hook already exists
er, ok := err.(*gh.ErrorResponse)
if !ok || er.Response.StatusCode != http.StatusUnprocessableEntity {
return errors.Errorf("failed to create webhook. err: %+v", err)
}
}
// if hook alreay exists then CreateHook returns hook value as nil
if hook == nil {
logger.Infoln("GitHub hook for the repository already exists, trying to use the existing hook...")
ctx, cancel = context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
hooks, _, err := router.githubClient.Repositories.ListHooks(ctx, githubEventSource.Owner, githubEventSource.Repository, nil)
if err != nil {
return errors.Errorf("failed to list existing webhooks. err: %+v", err)
}
hook = getHook(hooks, formattedUrl, githubEventSource.Events)
if hook == nil {
return errors.New("failed to find existing webhook")
}
}
if githubEventSource.WebhookSecret != nil {
// As secret in hook config is masked with asterisk (*), replace it with unmasked secret.
hook.Config["secret"] = hookConfig["secret"]
}
router.hook = hook
logger.Infoln("GitHub hook has been successfully set for the repository")
return nil
}
// PostInactivate performs operations after the route is inactivated
func (router *Router) PostInactivate() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
githubEventSource := router.githubEventSource
if githubEventSource.DeleteHookOnFinish {
logger := router.route.Logger.WithFields(map[string]interface{}{
common.LabelEventSource: router.route.EventSource.Name,
"repository": githubEventSource.Repository,
"hook-id": *router.hook.ID,
})
logger.Infoln("deleting GitHub hook...")
if _, err := router.githubClient.Repositories.DeleteHook(ctx, githubEventSource.Owner, githubEventSource.Repository, *router.hook.ID); err != nil {
return errors.Errorf("failed to delete hook. err: %+v", err)
}
logger.Infoln("GitHub hook deleted")
}
return nil
}
// StartEventSource starts an event source
func (listener *EventListener) StartEventSource(eventSource *gateways.EventSource, eventStream gateways.Eventing_StartEventSourceServer) error {
defer server.Recover(eventSource.Name)
listener.Logger.WithField(common.LabelEventSource, eventSource.Name).Infoln("started processing the event source...")
var githubEventSource *v1alpha1.GithubEventSource
if err := yaml.Unmarshal(eventSource.Value, &githubEventSource); err != nil {
listener.Logger.WithError(err).WithField(common.LabelEventSource, eventSource.Name).Infoln("failed to parse the event source")
return err
}
if githubEventSource.Namespace == "" {
githubEventSource.Namespace = listener.Namespace
}
route := webhook.NewRoute(githubEventSource.Webhook, listener.Logger, eventSource)
return webhook.ManageRoute(&Router{
route: route,
k8sClient: listener.K8sClient,
githubEventSource: githubEventSource,
}, controller, eventStream)
}
// parseValidateRequest parses a http request and checks if it is valid GitHub notification
func parseValidateRequest(r *http.Request, secret []byte) ([]byte, error) {
body, err := gh.ValidatePayload(r, secret)
if err != nil {
return nil, err
}
payload := make(map[string]interface{})
if err := json.Unmarshal(body, &payload); err != nil {
return nil, err
}
for _, h := range []string{
githubEventHeader,
githubDeliveryHeader,
} {
payload[h] = r.Header.Get(h)
}
return json.Marshal(payload)
}