Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(lambda): poll Kinesis event source in the mapping account (#3964)

Daniël van Zielb0b191e0bc94
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java
index 4e1e8e8021..5986adbc40 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java
@@ -122,18 +122,22 @@ public class KinesisEventSourcePoller implements Resettable {
if (fn == null) return;
String streamName = streamNameFromArn(esm.getEventSourceArn());
- KinesisStream stream = kinesisService.describeStream(streamName, esm.getRegion());
+ KinesisStream stream = kinesisService.describeStreamForAccount(
+ esm.getAccountId(), streamName, esm.getRegion());
for (KinesisShard shard : stream.getShards()) {
String lastSeq = esm.getShardSequenceNumbers().get(shard.getShardId());
String iterator;
if (lastSeq == null) {
- iterator = kinesisService.getShardIterator(streamName, shard.getShardId(), "TRIM_HORIZON", null, esm.getRegion());
+ iterator = kinesisService.getShardIteratorForAccount(esm.getAccountId(),
+ streamName, shard.getShardId(), "TRIM_HORIZON", null, esm.getRegion());
} else {
- iterator = kinesisService.getShardIterator(streamName, shard.getShardId(), "AFTER_SEQUENCE_NUMBER", lastSeq, esm.getRegion());
+ iterator = kinesisService.getShardIteratorForAccount(esm.getAccountId(),
+ streamName, shard.getShardId(), "AFTER_SEQUENCE_NUMBER", lastSeq, esm.getRegion());
}
- Map<String, Object> result = kinesisService.getRecords(iterator, esm.getBatchSize(), esm.getRegion());
+ Map<String, Object> result = kinesisService.getRecordsForAccount(
+ esm.getAccountId(), iterator, esm.getBatchSize(), esm.getRegion());
List<KinesisRecord> records = (List<KinesisRecord>) result.get("Records");
if (records.isEmpty()) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java
index 0efaf217d0..823a96d345 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePollerTest.java
@@ -115,10 +115,10 @@ class KinesisEventSourcePollerTest {
private void stubStreamWith(List<KinesisRecord> records) {
KinesisStream stream = new KinesisStream("s", STREAM_ARN);
stream.setShards(List.of(new KinesisShard(SHARD, "0", "1", "0")));
- when(kinesisService.describeStream(anyString(), eq(REGION))).thenReturn(stream);
- when(kinesisService.getShardIterator(anyString(), eq(SHARD), anyString(), any(), eq(REGION)))
+ when(kinesisService.describeStreamForAccount(eq(ACCOUNT), anyString(), eq(REGION))).thenReturn(stream);
+ when(kinesisService.getShardIteratorForAccount(eq(ACCOUNT), anyString(), eq(SHARD), anyString(), any(), eq(REGION)))
.thenReturn("iter-0");
- when(kinesisService.getRecords(eq("iter-0"), anyInt(), eq(REGION)))
+ when(kinesisService.getRecordsForAccount(eq(ACCOUNT), eq("iter-0"), anyInt(), eq(REGION)))
.thenReturn(Map.of("Records", records));
}
@@ -147,7 +147,7 @@ class KinesisEventSourcePollerTest {
while (true) {
poller.pollAndInvoke(esm);
try {
- verify(kinesisService, atLeast(2)).getRecords(eq("iter-0"), anyInt(), eq(REGION));
+ verify(kinesisService, atLeast(2)).getRecordsForAccount(eq(ACCOUNT), eq("iter-0"), anyInt(), eq(REGION));
return;
} catch (AssertionError retry) {
if (System.currentTimeMillis() > deadline) {
@@ -435,4 +435,38 @@ class KinesisEventSourcePollerTest {
verify(esmStore, timeout(2000)).saveForAccount(eq(ACCOUNT), any());
assertEquals("s2", esm.getShardSequenceNumbers().get(SHARD));
}
+
+ /**
+ * Regression for the account leak: a non-default ESM must read its stream through the
+ * account-aware Kinesis overloads. The old request-context calls resolved against the ambient
+ * request account, which defaults outside a request, so a same-named stream in the default
+ * account could be read instead.
+ */
+ @Test
+ void readsTheStreamInTheEventSourceMappingsAccountNotTheRequestAccount() {
+ String esmAccount = "111111111111";
+ LambdaFunction fn = new LambdaFunction();
+ fn.setFunctionName("fn");
+ when(functionStore.getForAccount(esmAccount, REGION, "fn")).thenReturn(Optional.of(fn));
+
+ KinesisStream stream = new KinesisStream("s", STREAM_ARN);
+ stream.setShards(List.of(new KinesisShard(SHARD, "0", "1", "0")));
+ when(kinesisService.describeStreamForAccount(eq(esmAccount), anyString(), eq(REGION))).thenReturn(stream);
+ when(kinesisService.getShardIteratorForAccount(eq(esmAccount), anyString(), eq(SHARD), anyString(), any(), eq(REGION)))
+ .thenReturn("iter-0");
+ when(kinesisService.getRecordsForAccount(eq(esmAccount), eq("iter-0"), anyInt(), eq(REGION)))
+ .thenReturn(Map.of("Records", List.of(record("s1", "p1", "{\"type\":\"order\"}"))));
+ when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(new InvokeResult());
+
+ EventSourceMapping esm = esm();
+ esm.setAccountId(esmAccount);
+
+ poller.pollAndInvoke(esm);
+
+ verify(executorService, timeout(2000)).invoke(any(), any(), eq(InvocationType.RequestResponse));
+ verify(esmStore, timeout(2000)).saveForAccount(eq(esmAccount), any());
+ verify(kinesisService, never()).describeStream(anyString(), anyString());
+ verify(kinesisService, never()).getShardIterator(anyString(), anyString(), anyString(), any(), anyString());
+ verify(kinesisService, never()).getRecords(anyString(), anyInt(), anyString());
+ }
}