mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(pipes): build flat Kinesis record shape with correct field values (#7554)
buildKinesisRecordNodes previously used valueToTree + PascalCase key reads, losing all values and producing a nested kinesis object. Now reads KinesisRecord getters directly and builds the flat Pipes shape, with eventVersion, kinesisSchemaVersion, invokeIdentityArn and a shardId-000000000000:<sequence> eventID. - Fix PipesTargetInvoker base64 decoding for flat shape - Decode base64 Kinesis data in PipesFilterMatcher before JSON parsing, through a helper shared with PipesTargetInvoker Refs #7481 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
baa834101d
commit
8d95688eec
6 files changed
+101
-34
No files matched your search
@@ -106,15 +106,25 @@ public class PipesFilterMatcher {
|
||||
}
|
||||
JsonNode value = actual == null || !actual.isObject() ? null : actual.get(field.getKey());
|
||||
JsonNode expected = field.getValue();
|
||||
// Kinesis data arrives base64-encoded; decode it for both object and array patterns.
|
||||
JsonNode decodedValue = value;
|
||||
if (value != null && value.isTextual() && "data".equals(field.getKey())
|
||||
&& actual != null && "aws:kinesis".equals(actual.path("eventSource").asText())) {
|
||||
String decoded = PipesTargetInvoker.decodedSourceText(field.getKey(), value.asText(),
|
||||
actual);
|
||||
if (!decoded.equals(value.asText()) && decoder != null) {
|
||||
decodedValue = decoder.getNodeFactory().textNode(decoded);
|
||||
}
|
||||
}
|
||||
if (expected.isObject()) {
|
||||
boolean nestedMatches = value == null
|
||||
? matches(expected, null, decoder)
|
||||
: matchesNested(expected, decoded(value, decoder), decoder);
|
||||
: matchesNested(expected, decoded(decodedValue, decoder), decoder);
|
||||
if (!nestedMatches) {
|
||||
return false;
|
||||
}
|
||||
} else if (expected.isArray()) {
|
||||
if (!matchesAlternatives(field.getKey(), expected, value)) {
|
||||
if (!matchesAlternatives(field.getKey(), expected, decodedValue)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -749,22 +749,27 @@ public class PipesPoller implements Resettable {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
private List<ObjectNode> buildKinesisRecordNodes(List<?> records, Pipe pipe, String region) {
|
||||
List<ObjectNode> buildKinesisRecordNodes(List<?> records, Pipe pipe, String region) {
|
||||
List<ObjectNode> nodes = new ArrayList<>();
|
||||
for (Object record : records) {
|
||||
ObjectNode node = objectMapper.valueToTree(record);
|
||||
KinesisRecord kr = (KinesisRecord) record;
|
||||
ObjectNode eventRecord = objectMapper.createObjectNode();
|
||||
eventRecord.put("eventSource", "aws:kinesis");
|
||||
eventRecord.put("eventSourceARN", pipe.getSource());
|
||||
eventRecord.put("eventVersion", "1.0");
|
||||
eventRecord.put("eventID", "shardId-000000000000:" + kr.getSequenceNumber());
|
||||
eventRecord.put("eventName", "aws:kinesis:record");
|
||||
eventRecord.put("invokeIdentityArn", pipe.getRoleArn());
|
||||
eventRecord.put("awsRegion", region);
|
||||
eventRecord.put("eventID", pipe.getSource() + ":" +
|
||||
node.path("SequenceNumber").asText());
|
||||
ObjectNode kinesis = eventRecord.putObject("kinesis");
|
||||
kinesis.put("partitionKey", node.path("PartitionKey").asText());
|
||||
kinesis.put("sequenceNumber", node.path("SequenceNumber").asText());
|
||||
kinesis.put("approximateArrivalTimestamp",
|
||||
node.path("ApproximateArrivalTimestamp").asDouble());
|
||||
kinesis.put("data", node.path("Data").asText());
|
||||
eventRecord.put("eventSourceARN", pipe.getSource());
|
||||
eventRecord.put("kinesisSchemaVersion", "1.0");
|
||||
eventRecord.put("partitionKey", kr.getPartitionKey());
|
||||
eventRecord.put("sequenceNumber", kr.getSequenceNumber());
|
||||
eventRecord.put("approximateArrivalTimestamp",
|
||||
kr.getApproximateArrivalTimestamp() != null
|
||||
? kr.getApproximateArrivalTimestamp().toEpochMilli() / 1000.0 : 0.0);
|
||||
String data = kr.getData() != null
|
||||
? Base64.getEncoder().encodeToString(kr.getData()) : "";
|
||||
eventRecord.put("data", data);
|
||||
nodes.add(eventRecord);
|
||||
}
|
||||
return nodes;
|
||||
|
||||
@@ -379,14 +379,13 @@ public class PipesTargetInvoker {
|
||||
JsonNode current = objectMapper.readTree(json);
|
||||
String previousSegment = null;
|
||||
JsonNode container = null;
|
||||
JsonNode outer = null;
|
||||
boolean sourceFieldDecoded = false;
|
||||
for (String segment : segments) {
|
||||
// AWS Pipes parses an encoded source body when traversing into it.
|
||||
if (!sourceFieldDecoded && isEncodedSourceField(previousSegment) && current.isTextual()) {
|
||||
try {
|
||||
JsonNode decoded = objectMapper.readTree(
|
||||
decodedSourceText(previousSegment, current.asText(), container, outer));
|
||||
decodedSourceText(previousSegment, current.asText(), container));
|
||||
if (decoded != null && (decoded.isObject() || decoded.isArray())) {
|
||||
current = decoded;
|
||||
}
|
||||
@@ -401,7 +400,6 @@ public class PipesTargetInvoker {
|
||||
if (next.isMissingNode() || next.isNull()) {
|
||||
return null;
|
||||
}
|
||||
outer = container;
|
||||
container = current;
|
||||
current = next;
|
||||
previousSegment = segment;
|
||||
@@ -409,7 +407,7 @@ public class PipesTargetInvoker {
|
||||
if (!sourceFieldDecoded && isEncodedSourceField(previousSegment) && current.isTextual()) {
|
||||
// SQS bodies commonly contain a JSON document as a string. Pipes input
|
||||
// transformation resolves that document when the body itself is selected.
|
||||
String text = decodedSourceText(previousSegment, current.asText(), container, outer);
|
||||
String text = decodedSourceText(previousSegment, current.asText(), container);
|
||||
try {
|
||||
JsonNode decoded = objectMapper.readTree(text);
|
||||
if (decoded != null && (decoded.isObject() || decoded.isArray())) {
|
||||
@@ -439,10 +437,9 @@ public class PipesTargetInvoker {
|
||||
* is not base64.
|
||||
*
|
||||
* @param container the object that holds {@code field}
|
||||
* @param outer the object that holds {@code container}
|
||||
*/
|
||||
private static String decodedSourceText(String field, String text, JsonNode container, JsonNode outer) {
|
||||
if (!isBase64SourceField(field, container, outer)) {
|
||||
public static String decodedSourceText(String field, String text, JsonNode container) {
|
||||
if (!isBase64SourceField(field, container)) {
|
||||
return text;
|
||||
}
|
||||
try {
|
||||
@@ -453,10 +450,9 @@ public class PipesTargetInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBase64SourceField(String field, JsonNode container, JsonNode outer) {
|
||||
private static boolean isBase64SourceField(String field, JsonNode container) {
|
||||
if ("data".equals(field)) {
|
||||
return outer != null && "aws:kinesis".equals(outer.path("eventSource").asText())
|
||||
&& outer.path("kinesis") == container;
|
||||
return container != null && "aws:kinesis".equals(container.path("eventSource").asText());
|
||||
}
|
||||
if ("key".equals(field) || "value".equals(field)) {
|
||||
return container != null && isKafkaEventSource(container.path("eventSource").asText());
|
||||
|
||||
@@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -358,4 +360,23 @@ class PipesFilterMatcherTest {
|
||||
private static JsonNode json(String value) throws Exception {
|
||||
return MAPPER.readTree(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
void kinesisDataFilter_matchesDecodedJsonField() throws Exception {
|
||||
String data = Base64.getEncoder().encodeToString(
|
||||
"{\"id\": 7, \"status\": \"NEW\"}".getBytes(StandardCharsets.UTF_8));
|
||||
JsonNode record = MAPPER.readTree(
|
||||
"{\"eventSource\": \"aws:kinesis\", \"data\": \"" + data + "\"}");
|
||||
assertTrue(matcher.matchesRecord(record, "{\"data\": {\"id\": [7]}}"));
|
||||
assertFalse(matcher.matchesRecord(record, "{\"data\": {\"id\": [99]}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void kinesisDataFilter_matchesDecodedStringField() throws Exception {
|
||||
String data = Base64.getEncoder().encodeToString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
JsonNode record = MAPPER.readTree(
|
||||
"{\"eventSource\": \"aws:kinesis\", \"data\": \"" + data + "\"}");
|
||||
assertTrue(matcher.matchesRecord(record, "{\"data\": [\"hello\"]}"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.dynamodb.backend.DynamoDbStreamReader;
|
||||
import io.floci.aws.services.kinesis.KinesisService;
|
||||
import io.floci.aws.services.kinesis.model.KinesisRecord;
|
||||
import io.floci.aws.services.pipes.model.DesiredState;
|
||||
import io.floci.aws.services.pipes.model.Pipe;
|
||||
import io.floci.aws.services.sqs.SqsService;
|
||||
@@ -26,6 +27,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -776,4 +778,37 @@ class PipesPollerTest {
|
||||
"arn:aws-cn:kinesis:cn-north-1:111111111111:stream/orders/consumer/reader:1700000000"));
|
||||
assertEquals("orders", PipesPoller.kinesisStreamName("orders"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildKinesisRecordNodes_carriesGuideFields() {
|
||||
Pipe pipe = new Pipe();
|
||||
pipe.setName("kinesis-pipe");
|
||||
pipe.setSource("arn:aws:kinesis:us-east-1:000000000000:stream/orders");
|
||||
pipe.setRoleArn("arn:aws:iam::000000000000:role/pipe-role");
|
||||
|
||||
KinesisRecord kr = new KinesisRecord(
|
||||
"hello".getBytes(StandardCharsets.UTF_8),
|
||||
"partition-1",
|
||||
"49545115243490985018280067714973144582180062593244413954",
|
||||
Instant.ofEpochMilli(1545084650987L));
|
||||
|
||||
List<ObjectNode> nodes = poller.buildKinesisRecordNodes(List.of(kr), pipe, "us-east-1");
|
||||
assertEquals(1, nodes.size());
|
||||
ObjectNode node = nodes.get(0);
|
||||
assertEquals("aws:kinesis", node.get("eventSource").asText());
|
||||
assertEquals("1.0", node.get("eventVersion").asText());
|
||||
assertEquals("shardId-000000000000:49545115243490985018280067714973144582180062593244413954",
|
||||
node.get("eventID").asText());
|
||||
assertEquals("aws:kinesis:record", node.get("eventName").asText());
|
||||
assertEquals("arn:aws:iam::000000000000:role/pipe-role", node.get("invokeIdentityArn").asText());
|
||||
assertEquals("us-east-1", node.get("awsRegion").asText());
|
||||
assertEquals("arn:aws:kinesis:us-east-1:000000000000:stream/orders", node.get("eventSourceARN").asText());
|
||||
assertEquals("1.0", node.get("kinesisSchemaVersion").asText());
|
||||
assertEquals("partition-1", node.get("partitionKey").asText());
|
||||
assertEquals("49545115243490985018280067714973144582180062593244413954",
|
||||
node.get("sequenceNumber").asText());
|
||||
assertEquals(Base64.getEncoder().encodeToString("hello".getBytes(StandardCharsets.UTF_8)),
|
||||
node.get("data").asText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -137,38 +137,38 @@ class PipesTargetInvokerTest {
|
||||
void redshiftTarget_readsPathsFromAKinesisDataDeliveredAsBase64() throws Exception {
|
||||
when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(true);
|
||||
Pipe pipe = redshiftPipe("{\"RedshiftDataParameters\":{\"Database\":\"dev\","
|
||||
+ "\"Sqls\":[\"$[0].kinesis.data.statement\",\"$[0].kinesis.data\"]}}");
|
||||
+ "\"Sqls\":[\"$[0].data.statement\",\"$[0].data\"]}}");
|
||||
String data = Base64.getEncoder().encodeToString(
|
||||
"{\"statement\":\"INSERT INTO t VALUES (1)\"}".getBytes(StandardCharsets.UTF_8));
|
||||
String event = "[{\"eventSource\":\"aws:kinesis\",\"kinesis\":{\"partitionKey\":\"p\",\"data\":\""
|
||||
+ data + "\"}}]";
|
||||
String event = "[{\"eventSource\":\"aws:kinesis\",\"partitionKey\":\"p\",\"data\":\""
|
||||
+ data + "\"}]";
|
||||
|
||||
invoker.invoke(pipe, event, "us-east-1");
|
||||
|
||||
ArgumentCaptor<UnaryOperator<String>> resolver = ArgumentCaptor.captor();
|
||||
verify(redshiftSubmitter).submit(any(), any(), any(), any(), any(), resolver.capture(), any());
|
||||
assertEquals("INSERT INTO t VALUES (1)", resolver.getValue().apply("$[0].kinesis.data.statement"));
|
||||
assertEquals("{\"statement\":\"INSERT INTO t VALUES (1)\"}", resolver.getValue().apply("$[0].kinesis.data"));
|
||||
assertEquals("INSERT INTO t VALUES (1)", resolver.getValue().apply("$[0].data.statement"));
|
||||
assertEquals("{\"statement\":\"INSERT INTO t VALUES (1)\"}", resolver.getValue().apply("$[0].data"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractJsonPath_decodesBase64OnlyInKafkaAndKinesisSourceRecords() {
|
||||
String encoded = Base64.getEncoder().encodeToString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
String kafka = "{\"eventSource\":\"aws:kafka\",\"key\":\"" + encoded + "\",\"value\":\"" + encoded + "\"}";
|
||||
String kinesis = "{\"eventSource\":\"aws:kinesis\",\"kinesis\":{\"data\":\"" + encoded + "\"}}";
|
||||
String kinesis = "{\"eventSource\":\"aws:kinesis\",\"data\":\"" + encoded + "\"}";
|
||||
|
||||
assertEquals("\"hello\"", invoker.extractJsonPath("$.key", kafka));
|
||||
assertEquals("\"hello\"", invoker.extractJsonPath("$.value", kafka));
|
||||
assertEquals("\"hello\"", invoker.extractJsonPath("$.kinesis.data", kinesis));
|
||||
assertEquals("\"hello\"", invoker.extractJsonPath("$.data", kinesis));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractJsonPath_leavesFieldsOfAnEnrichmentResponseOrAnSqsBodyUnchanged() {
|
||||
String encoded = Base64.getEncoder().encodeToString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
String enriched = "{\"data\":\"" + encoded + "\",\"key\":\"" + encoded + "\",\"value\":\"" + encoded
|
||||
+ "\",\"body\":\"" + encoded + "\",\"kinesis\":{\"data\":\"" + encoded + "\"}}";
|
||||
+ "\",\"body\":\"" + encoded + "\"}";
|
||||
|
||||
for (String path : List.of("$.data", "$.key", "$.value", "$.body", "$.kinesis.data")) {
|
||||
for (String path : List.of("$.data", "$.key", "$.value", "$.body")) {
|
||||
assertEquals("\"" + encoded + "\"", invoker.extractJsonPath(path, enriched), path);
|
||||
}
|
||||
assertEquals("\"" + encoded + "\"", invoker.extractJsonPath("$.key", "{\"key\":\"" + encoded + "\"}"));
|
||||
@@ -177,9 +177,9 @@ class PipesTargetInvokerTest {
|
||||
@Test
|
||||
void inputTemplate_readsAFieldOfBase64KinesisData() {
|
||||
String data = Base64.getEncoder().encodeToString("{\"id\":7}".getBytes(StandardCharsets.UTF_8));
|
||||
String event = "{\"eventSource\":\"aws:kinesis\",\"kinesis\":{\"data\":\"" + data + "\"}}";
|
||||
String event = "{\"eventSource\":\"aws:kinesis\",\"data\":\"" + data + "\"}";
|
||||
|
||||
assertEquals("{\"id\": 7}", invoker.applyInputTemplate("{\"id\": <$.kinesis.data.id>}", event));
|
||||
assertEquals("{\"id\": 7}", invoker.applyInputTemplate("{\"id\": <$.data.id>}", event));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user