-
Notifications
You must be signed in to change notification settings - Fork 60
/
events.go
542 lines (471 loc) · 18.6 KB
/
events.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package funcframework
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"strings"
"cloud.google.com/go/functions/metadata"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/events/pubsub"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/fftypes"
)
const (
ceIDHeader = "Ce-Id"
contentTypeHeader = "Content-Type"
contentLengthHeader = "Content-Length"
ceSpecVersion = "1.0"
jsonContentType = "application/cloudevents+json"
firebaseAuthCEService = "firebaseauth.googleapis.com"
firebaseCEService = "firebase.googleapis.com"
firebaseDBCEService = "firebasedatabase.googleapis.com"
firestoreCEService = "firestore.googleapis.com"
pubSubCEService = "pubsub.googleapis.com"
storageCEService = "storage.googleapis.com"
pubsubMessageType = "type.googleapis.com/google.pubsub.v1.PubsubMessage"
)
var (
typeBackgroundToCloudEvent = map[string]string{
"google.pubsub.topic.publish": "google.cloud.pubsub.topic.v1.messagePublished",
"providers/cloud.pubsub/eventTypes/topic.publish": "google.cloud.pubsub.topic.v1.messagePublished",
"google.storage.object.finalize": "google.cloud.storage.object.v1.finalized",
"google.storage.object.delete": "google.cloud.storage.object.v1.deleted",
"google.storage.object.archive": "google.cloud.storage.object.v1.archived",
"google.storage.object.metadataUpdate": "google.cloud.storage.object.v1.metadataUpdated",
"providers/cloud.firestore/eventTypes/document.write": "google.cloud.firestore.document.v1.written",
"providers/cloud.firestore/eventTypes/document.create": "google.cloud.firestore.document.v1.created",
"providers/cloud.firestore/eventTypes/document.update": "google.cloud.firestore.document.v1.updated",
"providers/cloud.firestore/eventTypes/document.delete": "google.cloud.firestore.document.v1.deleted",
"providers/firebase.auth/eventTypes/user.create": "google.firebase.auth.user.v1.created",
"providers/firebase.auth/eventTypes/user.delete": "google.firebase.auth.user.v1.deleted",
"providers/google.firebase.analytics/eventTypes/event.log": "google.firebase.analytics.log.v1.written",
"providers/google.firebase.database/eventTypes/ref.create": "google.firebase.database.ref.v1.created",
"providers/google.firebase.database/eventTypes/ref.write": "google.firebase.database.ref.v1.written",
"providers/google.firebase.database/eventTypes/ref.update": "google.firebase.database.ref.v1.updated",
"providers/google.firebase.database/eventTypes/ref.delete": "google.firebase.database.ref.v1.deleted",
"providers/cloud.storage/eventTypes/object.change": "google.cloud.storage.object.v1.finalized",
}
typeCloudToBackgroundEvent = map[string]string{
"google.cloud.pubsub.topic.v1.messagePublished": "google.pubsub.topic.publish",
"google.cloud.storage.object.v1.finalized": "google.storage.object.finalize",
"google.cloud.storage.object.v1.deleted": "google.storage.object.delete",
"google.cloud.storage.object.v1.archived": "google.storage.object.archive",
"google.cloud.storage.object.v1.metadataUpdated": "google.storage.object.metadataUpdate",
"google.cloud.firestore.document.v1.written": "providers/cloud.firestore/eventTypes/document.write",
"google.cloud.firestore.document.v1.created": "providers/cloud.firestore/eventTypes/document.create",
"google.cloud.firestore.document.v1.updated": "providers/cloud.firestore/eventTypes/document.update",
"google.cloud.firestore.document.v1.deleted": "providers/cloud.firestore/eventTypes/document.delete",
"google.firebase.auth.user.v1.created": "providers/firebase.auth/eventTypes/user.create",
"google.firebase.auth.user.v1.deleted": "providers/firebase.auth/eventTypes/user.delete",
"google.firebase.analytics.log.v1.written": "providers/google.firebase.analytics/eventTypes/event.log",
"google.firebase.database.ref.v1.created": "providers/google.firebase.database/eventTypes/ref.create",
"google.firebase.database.ref.v1.written": "providers/google.firebase.database/eventTypes/ref.write",
"google.firebase.database.ref.v1.updated": "providers/google.firebase.database/eventTypes/ref.update",
"google.firebase.database.ref.v1.deleted": "providers/google.firebase.database/eventTypes/ref.delete",
}
serviceBackgroundToCloudEvent = map[string]string{
"providers/cloud.firestore/": firestoreCEService,
"providers/google.firebase.analytics/": firebaseCEService,
"providers/firebase.auth/": firebaseAuthCEService,
"providers/google.firebase.database/": firebaseDBCEService,
"providers/cloud.pubsub/": pubSubCEService,
"providers/cloud.storage/": storageCEService,
"google.pubsub": pubSubCEService,
"google.storage": storageCEService,
}
// ceServiceToResourceRe maps CloudEvent service strings to regexps used to split
// a background event resource string into CloudEvent resource and subject strings.
// Each regexp must have exactly two submatches (a.k.a. capture groups): the first
// for the resource and the second for the subject. See splitResource for more info.
ceServiceToResourceRe = map[string]*regexp.Regexp{
firebaseCEService: regexp.MustCompile("^(projects/[^/]+)/(events/[^/]+)$"),
firebaseDBCEService: regexp.MustCompile("^projects/_/(instances/[^/]+)/(refs/.+)$"),
firestoreCEService: regexp.MustCompile("^(projects/[^/]+/databases/\\(default\\))/(documents/.+)$"),
storageCEService: regexp.MustCompile("^(projects/_/buckets/[^/]+)/(objects/.+)$"),
}
// firebaseAuthMetadataFieldsBackgroundToCloudEvent maps Firebase Auth background event metadata field
// names to their equivalent CloudEvent field names.
firebaseAuthMetadataFieldsBackgroundToCloudEvent = map[string]string{
"createdAt": "createTime",
"lastSignedInAt": "lastSignInTime",
}
)
func getBackgroundEvent(body []byte, path string) (*metadata.Metadata, interface{}, error) {
// Known background event types that the incoming request could represent.
// Event types are mutually exclusive. During unmarshalling, only the field
// for the matching type is populated.
type possibleEvents struct {
*pubsub.LegacyPushSubscriptionEvent
*fftypes.BackgroundEvent
}
// Attempt to unmarshal into one of the known background event types.
possible := possibleEvents{}
if err := json.Unmarshal(body, &possible); err != nil {
return nil, nil, err
}
event := possible.BackgroundEvent
// If the background event payload is missing, check if it's a legacy
// Pub/Sub event.
if possible.BackgroundEvent == nil && possible.LegacyPushSubscriptionEvent != nil {
topic, err := pubsub.ExtractTopicFromRequestPath(path)
if err != nil {
fmt.Printf("WARNING: %s", err)
}
event = possible.LegacyPushSubscriptionEvent.ToBackgroundEvent(topic)
}
// If there is no "data" payload, this isn't a background event, but that's okay.
if event == nil || event.Data == nil {
return nil, nil, nil
}
// If the "context" field was present, we have a complete event and so return.
if event.Metadata != nil {
return event.Metadata, event.Data, nil
}
// Otherwise, try to directly populate a metadata object.
m := &metadata.Metadata{}
if err := json.Unmarshal(body, m); err != nil {
return nil, nil, err
}
// Check for event ID to see if this is a background event, but if not that's okay.
if m.EventID == "" {
return nil, nil, nil
}
return m, event.Data, nil
}
func runBackgroundEvent(w http.ResponseWriter, r *http.Request, m *metadata.Metadata, data, fn interface{}) {
b, err := encodeData(data)
if err != nil {
writeHTTPErrorResponse(w, http.StatusBadRequest, crashStatus, fmt.Sprintf("Unable to encode data %v: %s", data, err.Error()))
return
}
ctx := metadata.NewContext(r.Context(), m)
runUserFunctionWithContext(ctx, w, r, b, fn)
}
func validateEventFunction(fn interface{}) error {
ft := reflect.TypeOf(fn)
if ft.NumIn() != 2 {
return fmt.Errorf("expected function to have two parameters, found %d", ft.NumIn())
}
var err error
errorType := reflect.TypeOf(&err).Elem()
if ft.NumOut() != 1 || !ft.Out(0).AssignableTo(errorType) {
return fmt.Errorf("expected function to return only an error")
}
var ctx context.Context
ctxType := reflect.TypeOf(&ctx).Elem()
if !ctxType.AssignableTo(ft.In(0)) {
return fmt.Errorf("expected first parameter to be context.Context")
}
return nil
}
func convertBackgroundToCloudEvent(ceHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// If the incoming request is not CloudEvent, make it so.
if r.Header.Get(ceIDHeader) == "" && !strings.Contains(r.Header.Get(contentTypeHeader), "cloudevents") {
if err := convertBackgroundToCloudEventRequest(r); err != nil {
writeHTTPErrorResponse(w, http.StatusBadRequest, crashStatus, fmt.Sprintf("%v", err))
return
}
}
ceHandler.ServeHTTP(w, r)
})
}
func encodeData(d interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(d); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// splitResource takes a background event resource string, which contains the full path to
// a resource, for example:
//
// - Cloud Storage bucket and object within it
// - Datastore database and entry within it
//
// and splits those two elements into separate strings; in CloudEvents the former is the
// "resource" and the latter is the "subject". Splitting is performed based on a regexp
// associated with the given CloudEvent service. See ceServiceToResourceRe for the regexp
// mapping. For example,
//
// "projects/_/buckets/some-bucket/objects/folder/test.txt"
//
// would be split to create the strings "projects/_/buckets/some-bucket"
// and "objects/folder/test.txt". This function returns the resource string, the
// subject string, and an error, which will be non-nil if a regexp failed to match.
// If there is no regexp for the given service then the resource is returned unchanged
// along with a nil error.
func splitResource(service, resource string) (string, string, error) {
re, ok := ceServiceToResourceRe[service]
if !ok {
return resource, "", nil
}
match := re.FindStringSubmatch(resource)
if match == nil {
return resource, "", fmt.Errorf("resource regexp did not match")
}
if len(match) != 3 {
return resource, "", fmt.Errorf("expected 2 match groups, got %v", len(match)-1)
}
return match[1], match[2], nil
}
// convertBackgroundFirebaseAuthMetadata converts Firebase Auth background event metadata to CloudEvent metadata.
// The given data is only modified if it is a map with the requisite keys, so modifications occur in place.
func convertBackgroundFirebaseAuthMetadata(data interface{}) {
d, ok := data.(map[string]interface{})
if !ok {
return
}
if _, ok := d["metadata"]; !ok {
return
}
metadata, ok := d["metadata"].(map[string]interface{})
if !ok {
return
}
for old, new := range firebaseAuthMetadataFieldsBackgroundToCloudEvent {
if _, ok := metadata[old]; ok {
metadata[new] = metadata[old]
delete(metadata, old)
}
}
}
// firebaseAuthSubject creates the CloudEvent subject from the "uid" field in the data.
func firebaseAuthSubject(data interface{}) (string, error) {
d, ok := data.(map[string]interface{})
if !ok {
return "", fmt.Errorf("data is not a map from string to interface")
}
if _, ok := d["uid"]; !ok {
return "", fmt.Errorf("data does not contain field \"uid\"")
}
return fmt.Sprintf("users/%v", d["uid"]), nil
}
func convertBackgroundToCloudEventRequest(r *http.Request) error {
body, err := readHTTPRequestBody(r)
if err != nil {
return err
}
md, d, err := getBackgroundEvent(body, r.URL.Path)
if err != nil {
return fmt.Errorf("parsing background event body %s: %v", string(body), err)
}
if md == nil || d == nil {
return fmt.Errorf("unable to extract background event from %s", string(body))
}
r.Header.Set(contentTypeHeader, jsonContentType)
t, ok := typeBackgroundToCloudEvent[md.EventType]
if !ok {
return fmt.Errorf("unable to find CloudEvent equivalent event type for %s", md.EventType)
}
service := md.Resource.Service
if service == "" {
for bService, ceService := range serviceBackgroundToCloudEvent {
if strings.HasPrefix(md.EventType, bService) {
service = ceService
}
}
// If service is still empty, we didn't find a match in the map. Return the error.
if service == "" {
return fmt.Errorf("unable to find CloudEvent equivalent service for %s", md.EventType)
}
}
resource := md.Resource.Name
if resource == "" {
resource = md.Resource.RawPath
}
var subject string
resource, subject, err = splitResource(service, resource)
if err != nil {
return err
}
ce := map[string]interface{}{
"id": md.EventID,
"time": md.Timestamp,
"specversion": ceSpecVersion,
"datacontenttype": "application/json",
"type": t,
"source": fmt.Sprintf("//%s/%s", service, resource),
"data": d,
}
if subject != "" {
ce["subject"] = subject
}
switch service {
case pubSubCEService:
data, ok := d.(map[string]interface{})
if !ok {
return fmt.Errorf(`invalid "data" field in event payload, "data": %q`, d)
}
data["publishTime"] = md.Timestamp
data["messageId"] = md.EventID
// In a Pub/Sub CloudEvent "data" is wrapped by "message".
ce["data"] = struct {
Message interface{} `json:"message"`
}{
Message: data,
}
case firebaseAuthCEService:
convertBackgroundFirebaseAuthMetadata(d)
if s, err := firebaseAuthSubject(d); err == nil && s != "" {
ce["subject"] = s
}
case firebaseDBCEService:
var dbDomain struct {
Domain string `json:"domain"`
}
if err := json.Unmarshal(body, &dbDomain); err != nil {
return fmt.Errorf("unable to unmarshal %q domain from event payload %q: %v", firebaseDBCEService, string(body), err)
}
location := "us-central1"
if dbDomain.Domain != "firebaseio.com" {
domainSplit := strings.SplitN(dbDomain.Domain, ".", 2)
if len(domainSplit) != 2 {
return fmt.Errorf("invalid %q domain: %q", firebaseDBCEService, dbDomain.Domain)
}
location = domainSplit[0]
}
ce["source"] = fmt.Sprintf("//%s/projects/_/locations/%s/%s", service, location, resource)
}
encoded, err := json.Marshal(ce)
if err != nil {
return fmt.Errorf("unable to marshal CloudEvent %v: %v", ce, err)
}
r.Body = ioutil.NopCloser(bytes.NewReader(encoded))
r.Header.Set(contentLengthHeader, fmt.Sprint(len(encoded)))
return nil
}
func shouldConvertCloudEventToBackgroundRequest(r *http.Request) bool {
_, ok := typeCloudToBackgroundEvent[r.Header.Get("ce-type")]
return ok &&
r.Header.Get("ce-source") != "" &&
r.Header.Get("ce-specversion") != "" &&
r.Header.Get("ce-id") != ""
}
func convertCloudEventToBackgroundRequest(r *http.Request) error {
body, err := readHTTPRequestBody(r)
if err != nil {
return err
}
var data map[string]interface{}
if err := json.Unmarshal(body, &data); err != nil {
return fmt.Errorf("unable to unmarshal CloudEvent data: %s, error: %v", string(body), err)
}
ceCtx := struct {
Type string
Source string
Subject string
Id string
Time string
}{
Type: r.Header.Get("ce-type"),
Source: r.Header.Get("ce-source"),
Subject: r.Header.Get("ce-subject"),
Id: r.Header.Get("ce-id"),
Time: r.Header.Get("ce-time"),
}
eventType, ok := typeCloudToBackgroundEvent[ceCtx.Type]
if !ok {
return fmt.Errorf("incoming event has unsupported event type: %q", ceCtx.Type)
}
/*
Ex 1: "//firebaseauth.googleapis.com/projects/my-project-id"
matches: ["//firebaseauth.googleapis.com/projects/my-project-id",
"firebaseauth.googleapis.com", "projects/my-project-id"]
Ex 2: "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test"
matches: ["//pubsub.googleapis.com/projects/sample-project/topics/gcf-test",
"pubsub.googleapis.com", "projects/sample-project/topics/gcf-test"]
*/
matches := regexp.MustCompile(`//([^/]+)/(.+)`).FindStringSubmatch(ceCtx.Source)
if len(matches) != 3 {
return fmt.Errorf("unable to parse CloudEvent source into resource service and name: %q", ceCtx.Source)
}
// 0th match is the entire input string
service := matches[1]
name := matches[2]
resource := fmt.Sprintf("%s/%s", name, ceCtx.Subject)
// Use custom metadata struct to control the exact formatting when
// fields are serialized to JSON.
type Metadata struct {
EventID string `json:"eventId"`
Timestamp string `json:"timestamp"`
EventType string `json:"eventType"`
// Resource can be a single string or a struct, depending on the
// event type.
Resource interface{} `json:"resource"`
}
type backgroundEvent struct {
Data map[string]interface{} `json:"data"`
Metadata `json:"context"`
}
be := backgroundEvent{
Data: data,
Metadata: Metadata{
EventID: ceCtx.Id,
Timestamp: ceCtx.Time,
EventType: eventType,
Resource: resource,
},
}
type splitResource struct {
Name string `json:"name"`
Service string `json:"service"`
Type interface{} `json:"type"`
}
switch service {
case pubSubCEService:
be.Resource = splitResource{
Name: name,
Service: service,
Type: pubsubMessageType,
}
// Lift the "message" field into the main "data" field.
if message, ok := be.Data["message"]; ok {
if md, ok := message.(map[string]interface{}); ok {
be.Data = md
}
}
delete(be.Data, "messageId")
delete(be.Data, "publishTime")
case firebaseAuthCEService:
be.Metadata.Resource = name
// Some keys in the metadata are inconsistent between CloudEvents
// and Background Events.
if metadata, ok := be.Data["metadata"]; ok {
if md, ok := metadata.(map[string]interface{}); ok {
if createTime, ok := md["createTime"]; ok {
md["createdAt"] = createTime
delete(md, "createTime")
}
if lastSignInTime, ok := md["lastSignInTime"]; ok {
md["lastSignedInAt"] = lastSignInTime
delete(md, "lastSignInTime")
}
}
}
case firebaseDBCEService:
be.Resource = regexp.MustCompile(`/locations/[^/]+`).ReplaceAllString(resource, "")
case storageCEService:
splitRes := splitResource{
Name: resource,
Service: service,
}
if dataKind, ok := be.Data["kind"]; ok {
splitRes.Type = dataKind
}
be.Resource = splitRes
}
encoded, err := json.Marshal(be)
if err != nil {
return fmt.Errorf("unable to marshal Background event %v: %v", be, err)
}
r.Body = ioutil.NopCloser(bytes.NewReader(encoded))
r.Header.Set(contentTypeHeader, jsonContentType)
r.Header.Set(contentLengthHeader, fmt.Sprint(len(encoded)))
return nil
}