-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathEventDeserializer.java
More file actions
431 lines (395 loc) · 20.6 KB
/
EventDeserializer.java
File metadata and controls
431 lines (395 loc) · 20.6 KB
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
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates.
* 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 software.amazon.lambda.powertools.utilities;
import org.crac.Context;
import org.crac.Core;
import org.crac.Resource;
import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
import static java.nio.charset.StandardCharsets.UTF_8;
import static software.amazon.lambda.powertools.utilities.jmespath.Base64Function.decode;
import static software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction.decompress;
import java.nio.ByteBuffer;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
import com.amazonaws.services.lambda.runtime.events.ActiveMQEvent;
import com.amazonaws.services.lambda.runtime.events.ApplicationLoadBalancerRequestEvent;
import com.amazonaws.services.lambda.runtime.events.CloudFormationCustomResourceEvent;
import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent;
import com.amazonaws.services.lambda.runtime.events.KafkaEvent;
import com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsFirehoseInputPreprocessingEvent;
import com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsStreamsInputPreprocessingEvent;
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
import com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent;
import com.amazonaws.services.lambda.runtime.events.RabbitMQEvent;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Class that can be used to extract the meaningful part of an event and deserialize it into a Java object.<br/>
* For example, extract the body of an API Gateway event, or messages from an SQS event.
*/
public class EventDeserializer implements Resource{
private static final Logger LOG = LoggerFactory.getLogger(EventDeserializer.class);
private static final EventDeserializer INSTANCE = new EventDeserializer();
/*
* Base64 encoding of: {"id":1234,"name":"product","price":42}
* Used in priming of Kafka, ActiveMQ, RabbitMQ events
*/
public static final String DATA = "eyJpZCI6MTIzNCwgIm5hbWUiOiJwcm9kdWN0IiwgInByaWNlIjo0Mn0=";
static {
Core.getGlobalContext().register(INSTANCE);
}
public static void init() {
// Placeholder method used to enable SnapStart priming. Users need a direct reference to this class in order for the CRaC hooks to execute.
new EventDeserializer();
}
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
init();
primeAllEventTypes();
ClassPreLoader.preloadClasses();
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception {
// This is a no-op, as we don't need to do anything after restore
}
private void primeAllEventTypes() {
try {
// API Gateway v1
APIGatewayProxyRequestEvent apiV1 = new APIGatewayProxyRequestEvent();
apiV1.setBody("{}");
extractDataFrom(apiV1);
// API Gateway v2
APIGatewayV2HTTPEvent apiV2 = new APIGatewayV2HTTPEvent();
apiV2.setBody("{}");
extractDataFrom(apiV2);
// SNS
SNSEvent snsEvent = new SNSEvent();
SNSEvent.SNS sns = new SNSEvent.SNS();
sns.setMessage("{}");
SNSEvent.SNSRecord snsRecord = new SNSEvent.SNSRecord();
snsRecord.setSns(sns);
snsEvent.setRecords(List.of(snsRecord));
extractDataFrom(snsEvent);
// SQS
SQSEvent sqsEvent = new SQSEvent();
SQSEvent.SQSMessage sqsMessage = new SQSEvent.SQSMessage();
sqsMessage.setBody("{}");
sqsEvent.setRecords(List.of(sqsMessage));
extractDataFrom(sqsEvent);
// Single SQS message
extractDataFrom(sqsMessage);
// Scheduled Event
ScheduledEvent scheduledEvent = new ScheduledEvent();
scheduledEvent.setDetail(Map.of("key", "value"));
extractDataFrom(scheduledEvent);
// ALB
ApplicationLoadBalancerRequestEvent albEvent =
new ApplicationLoadBalancerRequestEvent();
albEvent.setBody("{}");
extractDataFrom(albEvent);
// CloudWatch Logs
CloudWatchLogsEvent cwEvent = new CloudWatchLogsEvent();
CloudWatchLogsEvent.AWSLogs logs = new CloudWatchLogsEvent.AWSLogs();
logs.setData("ewogICJpZCI6IDEyMzQsCiAgIm5hbWUiOiAicHJvZHVjdCIsCiAgInByaWNlIjogNDIKfQ");
cwEvent.setAwsLogs(logs);
extractDataFrom(cwEvent);
// CloudFormation
CloudFormationCustomResourceEvent cfEvent =
new CloudFormationCustomResourceEvent();
cfEvent.setResourceProperties(Map.of("key", "value"));
extractDataFrom(cfEvent);
// Kinesis
KinesisEvent kinesisEvent = new KinesisEvent();
KinesisEvent.Record kinesisRecord = new KinesisEvent.Record();
kinesisRecord.setData(ByteBuffer.allocate(0));
KinesisEvent.KinesisEventRecord kinesisEventRecord =
new KinesisEvent.KinesisEventRecord();
kinesisEventRecord.setKinesis(kinesisRecord);
kinesisEvent.setRecords(List.of(kinesisEventRecord));
extractDataFrom(kinesisEvent);
// Single Kinesis record
extractDataFrom(kinesisEventRecord);
// Firehose
KinesisFirehoseEvent firehoseEvent = new KinesisFirehoseEvent();
KinesisFirehoseEvent.Record firehoseRecord =
new KinesisFirehoseEvent.Record();
firehoseRecord.setData(ByteBuffer.allocate(0));
firehoseEvent.setRecords(List.of(firehoseRecord));
extractDataFrom(firehoseEvent);
// Kafka
KafkaEvent kafkaEvent = new KafkaEvent();
KafkaEvent.KafkaEventRecord kafkaRecord =
new KafkaEvent.KafkaEventRecord();
kafkaRecord.setValue(DATA);
kafkaEvent.setRecords(
Map.of("topic", List.of(kafkaRecord))
);
extractDataFrom(kafkaEvent);
// ActiveMQ
ActiveMQEvent activeMQEvent = new ActiveMQEvent();
ActiveMQEvent.ActiveMQMessage activeMQMessage =
new ActiveMQEvent.ActiveMQMessage();
activeMQMessage.setData(DATA);
activeMQEvent.setMessages(List.of(activeMQMessage));
extractDataFrom(activeMQEvent);
// RabbitMQ
RabbitMQEvent rabbitMQEvent = new RabbitMQEvent();
RabbitMQEvent.RabbitMessage rabbitMessage =
new RabbitMQEvent.RabbitMessage();
rabbitMessage.setData(DATA);
rabbitMQEvent.setRmqMessagesByQueue(
Map.of("queue", List.of(rabbitMessage))
);
extractDataFrom(rabbitMQEvent);
// Kinesis Analytics Firehose preprocessing
KinesisAnalyticsFirehoseInputPreprocessingEvent kaFirehoseEvent =
new KinesisAnalyticsFirehoseInputPreprocessingEvent();
KinesisAnalyticsFirehoseInputPreprocessingEvent.Record kaFirehoseRecord =
new KinesisAnalyticsFirehoseInputPreprocessingEvent.Record();
kaFirehoseRecord.setData(ByteBuffer.allocate(0));
kaFirehoseEvent.setRecords(List.of(kaFirehoseRecord));
extractDataFrom(kaFirehoseEvent);
// Kinesis Analytics Streams preprocessing
KinesisAnalyticsStreamsInputPreprocessingEvent kaStreamsEvent =
new KinesisAnalyticsStreamsInputPreprocessingEvent();
KinesisAnalyticsStreamsInputPreprocessingEvent.Record kaStreamsRecord =
new KinesisAnalyticsStreamsInputPreprocessingEvent.Record();
kaStreamsRecord.setData(ByteBuffer.allocate(0));
kaStreamsEvent.setRecords(List.of(kaStreamsRecord));
extractDataFrom(kaStreamsEvent);
} catch (Exception e) {
// Best-effort priming only — never fail checkpointing
throw new PrimingException("Failed to prime event ", e);
}
}
/**
* Extract the meaningful part of a Lambda Event object. Main events are built-in:
* <ul>
* <li>{@link APIGatewayProxyRequestEvent} -> body</li>
* <li>{@link APIGatewayV2HTTPEvent} -> body</li>
* <li>{@link SNSEvent} -> Records[0].Sns.Message</li>
* <li>{@link SQSEvent} -> Records[*].body <i>(list)</i></li>
* <li>{@link ScheduledEvent} -> detail</li>
* <li>{@link ApplicationLoadBalancerRequestEvent} -> body</li>
* <li>{@link CloudWatchLogsEvent} -> powertools_base64_gzip(data)</li>
* <li>{@link CloudFormationCustomResourceEvent} -> resourceProperties</li>
* <li>{@link KinesisEvent} -> Records[*].kinesis.powertools_base64(data) <i>(list)</i></li>
* <li>{@link KinesisFirehoseEvent} -> Records[*].powertools_base64(data) <i>(list)</i></li>
* <li>{@link KafkaEvent} -> records[*].values[*].powertools_base64(value) <i>(list)</i></li>
* <li>{@link ActiveMQEvent} -> messages[*].powertools_base64(data) <i>(list)</i></li>
* <li>{@link RabbitMQEvent} -> rmqMessagesByQueue[*].values[*].powertools_base64(data) <i>(list)</i></li>
* <li>{@link KinesisAnalyticsFirehoseInputPreprocessingEvent} -> Records[*].kinesis.powertools_base64(data) <i>(list)</i></li>
* <li>{@link KinesisAnalyticsStreamsInputPreprocessingEvent} > Records[*].kinesis.powertools_base64(data) <i>(list)</i></li>
* <li>{@link String}</li>
* <li>{@link Map}</li>
* </ul>
* To be used in conjunction with {@link EventPart#as(Class)} or {@link EventPart#asListOf(Class)}
* for the deserialization.
*
* @param object the event of your Lambda function handler method
* @return the part of the event which is meaningful (ex: body of the API Gateway).<br/>
*/
public static EventPart extractDataFrom(Object object) {
if (object instanceof String) {
return new EventPart((String) object);
} else if (object instanceof Map) {
return new EventPart((Map<String, Object>) object);
} else if (object instanceof APIGatewayProxyRequestEvent) {
APIGatewayProxyRequestEvent event = (APIGatewayProxyRequestEvent) object;
return new EventPart(event.getBody());
} else if (object instanceof APIGatewayV2HTTPEvent) {
APIGatewayV2HTTPEvent event = (APIGatewayV2HTTPEvent) object;
return new EventPart(event.getBody());
} else if (object instanceof SNSEvent) {
SNSEvent event = (SNSEvent) object;
return new EventPart(event.getRecords().get(0).getSNS().getMessage());
} else if (object instanceof SQSEvent) {
SQSEvent event = (SQSEvent) object;
return new EventPart(event.getRecords().stream()
.map(SQSEvent.SQSMessage::getBody)
.collect(Collectors.toList()));
} else if (object instanceof SQSEvent.SQSMessage) {
return new EventPart(((SQSEvent.SQSMessage) object).getBody());
} else if (object instanceof ScheduledEvent) {
ScheduledEvent event = (ScheduledEvent) object;
return new EventPart(event.getDetail());
} else if (object instanceof ApplicationLoadBalancerRequestEvent) {
ApplicationLoadBalancerRequestEvent event = (ApplicationLoadBalancerRequestEvent) object;
return new EventPart(event.getBody());
} else if (object instanceof CloudWatchLogsEvent) {
CloudWatchLogsEvent event = (CloudWatchLogsEvent) object;
return new EventPart(decompress(decode(event.getAwsLogs().getData().getBytes(UTF_8))));
} else if (object instanceof CloudFormationCustomResourceEvent) {
CloudFormationCustomResourceEvent event = (CloudFormationCustomResourceEvent) object;
return new EventPart(event.getResourceProperties());
} else if (object instanceof KinesisEvent) {
KinesisEvent event = (KinesisEvent) object;
return new EventPart(event.getRecords().stream()
.map(r -> decode(r.getKinesis().getData()))
.collect(Collectors.toList()));
} else if (object instanceof KinesisEvent.KinesisEventRecord) {
return new EventPart(decode(((KinesisEvent.KinesisEventRecord)object).getKinesis().getData()));
} else if (object instanceof KinesisFirehoseEvent) {
KinesisFirehoseEvent event = (KinesisFirehoseEvent) object;
return new EventPart(event.getRecords().stream()
.map(r -> decode(r.getData()))
.collect(Collectors.toList()));
} else if (object instanceof KafkaEvent) {
KafkaEvent event = (KafkaEvent) object;
return new EventPart(event.getRecords().values().stream()
.flatMap(List::stream)
.map(r -> decode(r.getValue()))
.collect(Collectors.toList()));
} else if (object instanceof ActiveMQEvent) {
ActiveMQEvent event = (ActiveMQEvent) object;
return new EventPart(event.getMessages().stream()
.map(m -> decode(m.getData()))
.collect(Collectors.toList()));
} else if (object instanceof RabbitMQEvent) {
RabbitMQEvent event = (RabbitMQEvent) object;
return new EventPart(event.getRmqMessagesByQueue().values().stream()
.flatMap(List::stream)
.map(r -> decode(r.getData()))
.collect(Collectors.toList()));
} else if (object instanceof KinesisAnalyticsFirehoseInputPreprocessingEvent) {
KinesisAnalyticsFirehoseInputPreprocessingEvent event =
(KinesisAnalyticsFirehoseInputPreprocessingEvent) object;
return new EventPart(event.getRecords().stream()
.map(r -> decode(r.getData()))
.collect(Collectors.toList()));
} else if (object instanceof KinesisAnalyticsStreamsInputPreprocessingEvent) {
KinesisAnalyticsStreamsInputPreprocessingEvent event =
(KinesisAnalyticsStreamsInputPreprocessingEvent) object;
return new EventPart(event.getRecords().stream()
.map(r -> decode(r.getData()))
.collect(Collectors.toList()));
} else {
// does not really make sense to use this EventDeserializer when you already have a typed object
// just not to throw an exception
LOG.warn("Consider using your object directly instead of using EventDeserializer");
return new EventPart(object);
}
}
/**
* Meaningful part of a Lambda event.<br/>
* Use {@link #extractDataFrom(Object)} to retrieve an instance of this class.
*/
public static class EventPart {
private Map<String, Object> contentMap;
private String content;
private List<String> contentList;
private Object contentObject;
private EventPart(List<String> contentList) {
this.contentList = contentList;
}
private EventPart(String content) {
this.content = content;
}
private EventPart(Map<String, Object> contentMap) {
this.contentMap = contentMap;
}
private EventPart(Object content) {
this.contentObject = content;
}
/**
* Deserialize this part of event from JSON to an object of type T
*
* @param clazz the target type for deserialization
* @param <T> type of object to return
* @return an Object of type T (deserialized from the content)
*/
public <T> T as(Class<T> clazz) {
try {
if (content != null) {
if (content.getClass().equals(clazz)) {
// do not read json when returning String, just return the String
return (T) content;
}
return JsonConfig.get().getObjectMapper().reader().readValue(content, clazz);
}
if (contentMap != null) {
return JsonConfig.get().getObjectMapper().convertValue(contentMap, clazz);
}
if (contentObject != null) {
return (T) contentObject;
}
if (contentList != null) {
throw new EventDeserializationException(
"The content of this event is a list, consider using 'asListOf' instead");
}
// should not occur, except if the event is malformed (missing fields)
throw new IllegalStateException("Event content is null: the event may be malformed (missing fields)");
} catch (IOException e) {
throw new EventDeserializationException("Cannot load the event as " + clazz.getSimpleName(), e);
}
}
public <M> M as() {
TypeReference<M> typeRef = new TypeReference<M>() {};
try {
JsonParser parser = JsonConfig.get().getObjectMapper().createParser(content);
return JsonConfig.get().getObjectMapper().reader().readValue(parser, typeRef);
} catch (IOException e) {
throw new EventDeserializationException("Cannot load the event as " + typeRef, e);
}
};
/**
* Deserialize this part of event from JSON to a list of objects of type T
*
* @param clazz the target type for deserialization
* @param <T> type of object to return
* @return a list of objects of type T (deserialized from the content)
*/
public <T> List<T> asListOf(Class<T> clazz) {
if (contentList == null && content == null) {
if (contentMap != null || contentObject != null) {
throw new EventDeserializationException(
"The content of this event is not a list, consider using 'as' instead");
}
// should not occur, except if the event is really malformed
throw new IllegalStateException("Event content is null: the event may be malformed (missing fields)");
}
if (content != null) {
ObjectReader reader = JsonConfig.get().getObjectMapper().readerForListOf(clazz);
try {
return reader.readValue(content);
} catch (JsonProcessingException e) {
throw new EventDeserializationException(
"Cannot load the event as a list of " + clazz.getSimpleName() +
", consider using 'as' instead", e);
}
} else {
return contentList.stream().map(s ->
{
try {
return s == null ? null : JsonConfig.get().getObjectMapper().reader().readValue(s, clazz);
} catch (IOException e) {
throw new EventDeserializationException(
"Cannot load the event as a list of " + clazz.getSimpleName(), e);
}
}).collect(Collectors.toList());
}
}
}
}