-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLegacyEventMapper.php
281 lines (246 loc) · 11.6 KB
/
LegacyEventMapper.php
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
<?php
/**
* Copyright 2020 Google LLC.
*
* 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.
*/
namespace Google\CloudFunctions;
use DateTime;
use DateTimeInterface;
use RuntimeException;
class LegacyEventMapper
{
// Maps background/legacy event types to their equivalent CloudEvent types.
// For more info on event mappings see
// https://github.com/GoogleCloudPlatform/functions-framework-conformance/blob/main/docs/mapping.md
private static $ceTypeMap = [
'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',
];
// Constants for Legacy Pubsub Conversion
private const PUBSUB_CE_EVENT_TYPE = 'google.pubsub.topic.publish';
private const LEGACY_PUBSUB_MESSAGE_TYPE = 'type.googleapis.com/google.pubsub.v1.PubsubMessage';
// CloudEvent service names.
private const FIREBASE_AUTH_CE_SERVICE = 'firebaseauth.googleapis.com';
private const FIREBASE_CE_SERVICE = 'firebase.googleapis.com';
private const FIREBASE_DB_CE_SERVICE = 'firebasedatabase.googleapis.com';
private const FIRESTORE_CE_SERVICE = 'firestore.googleapis.com';
private const PUBSUB_CE_SERVICE = 'pubsub.googleapis.com';
private const STORAGE_CE_SERVICE = 'storage.googleapis.com';
// Maps background event services to their equivalent CloudEvent services.
private static $ceServiceMap = [
'providers/cloud.firestore/' => self::FIRESTORE_CE_SERVICE,
'providers/google.firebase.analytics/' => self::FIREBASE_CE_SERVICE,
'providers/firebase.auth/' => self::FIREBASE_AUTH_CE_SERVICE,
'providers/google.firebase.database/' => self::FIREBASE_DB_CE_SERVICE,
'providers/cloud.pubsub/' => self::PUBSUB_CE_SERVICE,
'providers/cloud.storage/' => self::STORAGE_CE_SERVICE,
];
// Maps CloudEvent service strings to regular expressions used to split a background
// event resource string into CloudEvent resource and subject strings. Each regex
// must have exactly two capture groups: the first for the resource and the second
// for the subject.
private static $ceResourceRegexMap = [
self::FIREBASE_CE_SERVICE => '#^(projects/[^/]+)/(events/[^/]+)$#',
self::FIREBASE_DB_CE_SERVICE => '#^projects/_/(instances/[^/]+)/(refs/.+)$#',
self::FIRESTORE_CE_SERVICE => '#^(projects/[^/]+/databases/\(default\))/(documents/.+)$#',
self::STORAGE_CE_SERVICE => '#^(projects/_/buckets/[^/]+)/(objects/.+)$#',
];
// Maps Firebase Auth background event metadata field names to their equivalent
// CloudEvent field names.
private static $firebaseAuthMetadataFieldMap = [
'createdAt' => 'createTime',
'lastSignedInAt' => 'lastSignInTime',
];
public function fromJsonData(array $jsonData, string $requestUriPath): CloudEvent
{
[$context, $data] = $this->getLegacyEventContextAndData($jsonData, $requestUriPath);
$eventType = $context->getEventType();
$resourceName = $context->getResourceName();
$ceId = $context->getEventId();
// Mapped from eventType.
$ceType = $this->ceType($eventType);
// From context/resource/service, or mapped from eventType.
$ceService = $context->getService() ?: $this->ceService($eventType);
// Split the background event resource into a CloudEvent resource and subject.
[$ceResource, $ceSubject] = $this->ceResourceAndSubject(
$ceService,
$resourceName,
$context->getDomain()
);
$ceTime = $context->getTimestamp();
if ($ceService === self::PUBSUB_CE_SERVICE) {
// Handle Pub/Sub events.
if (!is_array($data)) {
$data = ['data' => $data];
}
$data['messageId'] = $context->getEventId();
$data['publishTime'] = $context->getTimestamp();
$data = ['message' => $data];
} elseif ($ceService === self::FIREBASE_AUTH_CE_SERVICE) {
// Handle Firebase Auth events.
if (array_key_exists('metadata', $data)) {
foreach (self::$firebaseAuthMetadataFieldMap as $old => $new) {
if (array_key_exists($old, $data['metadata'])) {
$data['metadata'][$new] = $data['metadata'][$old];
unset($data['metadata'][$old]);
}
}
}
if (array_key_exists('uid', $data)) {
$ceSubject = sprintf('users/%s', $data['uid']);
}
}
return CloudEvent::fromArray([
'id' => $ceId,
'source' => sprintf('//%s/%s', $ceService, $ceResource),
'specversion' => '1.0',
'type' => $ceType,
'datacontenttype' => 'application/json',
'dataschema' => null,
'subject' => $ceSubject,
'time' => $ceTime,
'data' => $data,
]);
}
private function getLegacyEventContextAndData(array $jsonData, string $requestUriPath): array
{
if ($this->isRawPubsubPayload($jsonData)) {
$jsonData = $this->convertRawPubsubPayload($jsonData, $requestUriPath);
}
$data = $jsonData['data'] ?? null;
if (array_key_exists('context', $jsonData)) {
$context = $jsonData['context'];
} else {
unset($jsonData['data']);
$context = $jsonData;
}
$context = Context::fromArray($context);
return [$context, $data];
}
private function isRawPubsubPayload(array $jsonData): bool
{
return (!is_null($jsonData) &&
!array_key_exists('context', $jsonData) &&
array_key_exists('subscription', $jsonData) &&
array_key_exists('message', $jsonData) &&
array_key_exists('data', $jsonData['message']) &&
array_key_exists('messageId', $jsonData['message']));
}
private function convertRawPubsubPayload(array $jsonData, string $requestUriPath): array
{
$path_match = preg_match('#projects/[^/?]+/topics/[^/?]+#', $requestUriPath, $matches);
if ($path_match) {
$topic = $matches[0];
} else {
$topic = 'UNKNOWN_PUBSUB_TOPIC';
$this->stderrStructuredWarn('Failed to extract the topic name from the URL path.');
$this->stderrStructuredWarn(
'Configure your subscription\'s push endpoint to use the following path: ' .
'projects/PROJECT_NAME/topics/TOPIC_NAME'
);
}
if (array_key_exists('publishTime', $jsonData['message'])) {
$timestamp = $jsonData['message']['publishTime'];
} else {
$timestamp = gmdate('Y-m-d\TH:i:s.v\Z');
}
return [
'context' => [
'eventId' => $jsonData['message']['messageId'],
'timestamp' => $timestamp,
'eventType' => self::PUBSUB_CE_EVENT_TYPE,
'resource' => [
'service' => self::PUBSUB_CE_SERVICE,
'type' => self::LEGACY_PUBSUB_MESSAGE_TYPE,
'name' => $topic,
],
],
'data' => [
'@type' => self::LEGACY_PUBSUB_MESSAGE_TYPE,
'data' => $jsonData['message']['data'],
'attributes' => $jsonData['message']['attributes'],
],
];
}
private function ceType(string $eventType): string
{
if (isset(self::$ceTypeMap[$eventType])) {
return self::$ceTypeMap[$eventType];
}
// Default to the legacy event type if no mapping is found.
return $eventType;
}
private function ceService(string $eventType): string
{
foreach (self::$ceServiceMap as $prefix => $ceService) {
if (0 === strpos($eventType, $prefix)) {
return $ceService;
}
}
// Default to the legacy event type if no service mapping is found.
return $eventType;
}
private function ceResourceAndSubject(string $ceService, string $resource, ?string $domain): array
{
if (!array_key_exists($ceService, self::$ceResourceRegexMap)) {
return [$resource, null];
}
$ret = preg_match(self::$ceResourceRegexMap[$ceService], $resource, $matches);
if (!$ret) {
throw new RuntimeException(
$ret === 0 ? 'Resource regex did not match' : 'Failed while matching resource regex'
);
}
if (self::FIREBASE_DB_CE_SERVICE === $ceService) {
if (null === $domain) {
return [null, null];
}
$location = 'us-central1';
if ($domain !== 'firebaseio.com') {
preg_match('#^([\w-]+)\.#', $domain, $locationMatches);
if (!$locationMatches) {
return [null, null];
}
$location = $locationMatches[1];
}
return ["projects/_/locations/$location/$matches[1]", $matches[2]];
}
return [$matches[1], $matches[2]];
}
private function stderrStructuredWarn(string $msg)
{
$stderr = fopen('php://stderr', 'wb');
fwrite($stderr, json_encode([
'message' => $msg,
'severity' => 'WARNING'
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
fclose($stderr);
}
}