mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): poll Kinesis event source in the mapping account (#3964)
This commit is contained in:
1 parent
2eadc106c5
commit
b0b191e0bc
2 files changed
+46
-8
No files matched your search
+8
-4
@@ -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()) {
|
||||
|
||||
+38
-4
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user