/
start.go
369 lines (309 loc) · 10.5 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright 2018 BlackRock, Inc.
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 awssns
import (
"bytes"
"context"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"regexp"
"time"
"github.com/aws/aws-sdk-go/aws"
snslib "github.com/aws/aws-sdk-go/service/sns"
"github.com/ghodss/yaml"
"go.uber.org/zap"
"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/common/logging"
eventsourcecommon "github.com/argoproj/argo-events/eventsources/common"
commonaws "github.com/argoproj/argo-events/eventsources/common/aws"
"github.com/argoproj/argo-events/eventsources/common/webhook"
"github.com/argoproj/argo-events/eventsources/sources"
metrics "github.com/argoproj/argo-events/metrics"
apicommon "github.com/argoproj/argo-events/pkg/apis/common"
"github.com/argoproj/argo-events/pkg/apis/events"
"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
)
var (
// controller controls the webhook operations
controller = webhook.NewController()
// used for SNS verification
snsSigKeys = map[string][]string{}
snsKeyRealNames = map[string]string{
"MessageID": "MessageId",
"TopicARN": "TopicArn",
}
)
// set up route activation and deactivation channels
func init() {
go webhook.ProcessRouteStatus(controller)
snsSigKeys[messageTypeNotification] = []string{
"Message",
"MessageID",
"Subject",
"Timestamp",
"TopicARN",
"Type",
}
snsSigKeys[messageTypeSubscriptionConfirmation] = []string{
"Message",
"MessageID",
"SubscribeURL",
"Timestamp",
"Token",
"TopicARN",
"Type",
}
}
// 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 new routes
func (router *Router) HandleRoute(writer http.ResponseWriter, request *http.Request) {
route := router.Route
logger := route.Logger.With(
logging.LabelEndpoint, route.Context.Endpoint,
logging.LabelPort, route.Context.Port,
logging.LabelHTTPMethod, route.Context.Method,
)
logger.Info("request received from event source")
if !route.Active {
logger.Info("endpoint is not active, won't process the request")
common.SendErrorResponse(writer, "inactive endpoint")
return
}
defer func(start time.Time) {
route.Metrics.EventProcessingDuration(route.EventSourceName, route.EventName, float64(time.Since(start)/time.Millisecond))
}(time.Now())
request.Body = http.MaxBytesReader(writer, request.Body, route.Context.GetMaxPayloadSize())
body, err := io.ReadAll(request.Body)
if err != nil {
logger.Errorw("failed to parse the request body", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
}
var notification *httpNotification
err = yaml.Unmarshal(body, ¬ification)
if err != nil {
logger.Errorw("failed to convert request payload into sns notification", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
}
if notification == nil {
common.SendErrorResponse(writer, "bad request, not a valid SNS notification")
return
}
// SNS Signature Verification
if router.eventSource.ValidateSignature {
err = notification.verify()
if err != nil {
logger.Errorw("failed to verify sns message", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
}
}
switch notification.Type {
case messageTypeSubscriptionConfirmation:
awsSession := router.session
response, err := awsSession.ConfirmSubscription(&snslib.ConfirmSubscriptionInput{
TopicArn: &router.eventSource.TopicArn,
Token: ¬ification.Token,
})
if err != nil {
logger.Errorw("failed to send confirmation response to aws sns", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
}
logger.Info("subscription successfully confirmed to aws sns")
router.subscriptionArn = response.SubscriptionArn
case messageTypeNotification:
logger.Info("dispatching notification on route's data channel")
eventData := &events.SNSEventData{
Header: request.Header,
Body: (*json.RawMessage)(&body),
Metadata: router.eventSource.Metadata,
}
eventBytes, err := json.Marshal(eventData)
if err != nil {
logger.Errorw("failed to marshal the event data", zap.Error(err))
common.SendErrorResponse(writer, err.Error())
route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName)
return
}
route.DataCh <- eventBytes
}
logger.Info("request has been successfully processed")
}
// PostActivate refers to operations performed after a route is successfully activated
func (router *Router) PostActivate() error {
route := router.Route
logger := route.Logger.With(
logging.LabelEndpoint, route.Context.Endpoint,
logging.LabelPort, route.Context.Port,
logging.LabelHTTPMethod, route.Context.Method,
"topic-arn", router.eventSource.TopicArn,
)
// In order to successfully subscribe to sns topic,
// 1. Fetch credentials if configured explicitly. Users can use something like https://github.com/jtblin/kube2iam
// which will help not configure creds explicitly.
// 2. Get AWS session
// 3. Subscribe to a topic
logger.Info("subscribing to sns topic...")
snsEventSource := router.eventSource
awsSession, err := commonaws.CreateAWSSessionWithCredsInVolume(snsEventSource.Region, snsEventSource.RoleARN, snsEventSource.AccessKey, snsEventSource.SecretKey, nil)
if err != nil {
return err
}
if snsEventSource.Endpoint == "" {
router.session = snslib.New(awsSession)
} else {
router.session = snslib.New(awsSession, &aws.Config{Endpoint: &snsEventSource.Endpoint, Region: &snsEventSource.Region})
}
formattedURL := common.FormattedURL(snsEventSource.Webhook.URL, snsEventSource.Webhook.Endpoint)
if _, err := router.session.Subscribe(&snslib.SubscribeInput{
Endpoint: &formattedURL,
Protocol: func(endpoint string) *string {
Protocol := "http"
if matched, _ := regexp.MatchString(`https://.*`, endpoint); matched {
Protocol = "https"
return &Protocol
}
return &Protocol
}(formattedURL),
TopicArn: &snsEventSource.TopicArn,
}); err != nil {
return err
}
return nil
}
// PostInactivate refers to operations performed after a route is successfully inactivated
func (router *Router) PostInactivate() error {
// After event source is removed, the subscription is cancelled.
if _, err := router.session.Unsubscribe(&snslib.UnsubscribeInput{
SubscriptionArn: router.subscriptionArn,
}); err != nil {
return err
}
return nil
}
// EventListener implements Eventing for aws sns event source
type EventListener struct {
EventSourceName string
EventName string
SNSEventSource v1alpha1.SNSEventSource
Metrics *metrics.Metrics
}
// GetEventSourceName returns name of event source
func (el *EventListener) GetEventSourceName() string {
return el.EventSourceName
}
// GetEventName returns name of event
func (el *EventListener) GetEventName() string {
return el.EventName
}
// GetEventSourceType return type of event server
func (el *EventListener) GetEventSourceType() apicommon.EventSourceType {
return apicommon.SNSEvent
}
// StartListening starts an SNS event source
func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byte, ...eventsourcecommon.Option) error) error {
logger := logging.FromContext(ctx).
With(logging.LabelEventSourceType, el.GetEventSourceType(), logging.LabelEventName, el.GetEventName())
defer sources.Recover(el.GetEventName())
logger.Info("started processing the AWS SNS event source...")
route := webhook.NewRoute(el.SNSEventSource.Webhook, logger, el.GetEventSourceName(), el.GetEventName(), el.Metrics)
logger.Info("operating on the route...")
return webhook.ManageRoute(ctx, &Router{
Route: route,
eventSource: &el.SNSEventSource,
}, controller, dispatch)
}
func (m *httpNotification) verifySigningCertUrl() error {
regexSigningCertHost := `^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$`
regex := regexp.MustCompile(regexSigningCertHost)
url, err := url.Parse(m.SigningCertURL)
if err != nil {
return fmt.Errorf("SigningCertURL is not a valid URL, %w", err)
}
if !regex.MatchString(url.Hostname()) {
return fmt.Errorf("SigningCertURL hostname `%s` does not match `%s`", url.Hostname(), regexSigningCertHost)
}
if url.Scheme != "https" {
return fmt.Errorf("SigningCertURL is not using https")
}
return nil
}
func (m *httpNotification) verify() error {
msgSig, err := base64.StdEncoding.DecodeString(m.Signature)
if err != nil {
return fmt.Errorf("failed to base64 decode signature, %w", err)
}
if err := m.verifySigningCertUrl(); err != nil {
return fmt.Errorf("failed to verify SigningCertURL, %w", err)
}
res, err := http.Get(m.SigningCertURL)
if err != nil {
return fmt.Errorf("failed to fetch signing cert, %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(io.LimitReader(res.Body, 65*1024))
if err != nil {
return fmt.Errorf("failed to read signing cert body, %w", err)
}
p, _ := pem.Decode(body)
if p == nil {
return fmt.Errorf("nothing found in pem encoded bytes")
}
cert, err := x509.ParseCertificate(p.Bytes)
if err != nil {
return fmt.Errorf("failed to parse signing cert, %w", err)
}
err = cert.CheckSignature(x509.SHA1WithRSA, m.sigSerialized(), msgSig)
if err != nil {
return fmt.Errorf("message signature check error, %w", err)
}
return nil
}
func (m *httpNotification) sigSerialized() []byte {
buf := &bytes.Buffer{}
v := reflect.ValueOf(m)
for _, key := range snsSigKeys[m.Type] {
field := reflect.Indirect(v).FieldByName(key)
val := field.String()
if !field.IsValid() || val == "" {
continue
}
if rn, ok := snsKeyRealNames[key]; ok {
key = rn
}
buf.WriteString(key + "\n")
buf.WriteString(val + "\n")
}
return buf.Bytes()
}