mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ssm): bound direct-execution output to the GetCommandInvocation limits (#4257)
The direct executor buffered a command's full stdout and stderr in memory until the command finished, and only then truncated them to 24,000 and 8,000 characters. Collect each stream with a bounded collector that keeps the first N characters, keeps draining the Docker stream after the cap, decodes UTF-8 characters split across frames, and never ends on half a surrogate pair.
This commit is contained in:
1 parent
ed627a6ce7
commit
1027f0a602
5 files changed
+263
-27
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package io.github.hectorvent.floci.services.ssm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class BoundedOutputCollectorTest {
|
||||
|
||||
@Test
|
||||
void feedingMoreThanCapYieldsExactlyFirstNChars() {
|
||||
BoundedOutputCollector collector = new BoundedOutputCollector(10);
|
||||
|
||||
collector.write("abcde".getBytes(StandardCharsets.UTF_8));
|
||||
collector.write("fghijKLMNOP".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals("abcdefghij", collector.content());
|
||||
}
|
||||
|
||||
@Test
|
||||
void additionalWritesAfterCapAreDiscardedWithoutGrowingContent() {
|
||||
BoundedOutputCollector collector = new BoundedOutputCollector(5);
|
||||
|
||||
collector.write("abcde".getBytes(StandardCharsets.UTF_8));
|
||||
collector.write("this is a lot more text that should be fully discarded".getBytes(StandardCharsets.UTF_8));
|
||||
collector.write("even more after that".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals("abcde", collector.content());
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiByteUtf8CharacterSplitAcrossWritesDecodesCorrectly() {
|
||||
BoundedOutputCollector collector = new BoundedOutputCollector(10);
|
||||
byte[] euroSign = "€".getBytes(StandardCharsets.UTF_8); // 0xE2 0x82 0xAC
|
||||
|
||||
collector.write(new byte[] {euroSign[0], euroSign[1]});
|
||||
collector.write(new byte[] {euroSign[2]});
|
||||
collector.write("X".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals("€X", collector.content());
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiByteCharacterCompletingExactlyAtCapIsIncludedWithoutCorruption() {
|
||||
BoundedOutputCollector collector = new BoundedOutputCollector(3);
|
||||
byte[] euroSign = "€".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
collector.write("ab".getBytes(StandardCharsets.UTF_8));
|
||||
collector.write(new byte[] {euroSign[0], euroSign[1]});
|
||||
collector.write(new byte[] {euroSign[2]});
|
||||
|
||||
assertEquals("ab€", collector.content());
|
||||
}
|
||||
|
||||
@Test
|
||||
void capFallingInsideSurrogatePairKeepsOnlyWholeCharacters() {
|
||||
BoundedOutputCollector collector = new BoundedOutputCollector(3);
|
||||
|
||||
collector.write("ab\uD83D\uDE00".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals("ab", collector.content());
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputEndingMidCharacterShowsReplacementCharacter() {
|
||||
BoundedOutputCollector collector = new BoundedOutputCollector(10);
|
||||
byte[] euroSign = "€".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
collector.write("X".getBytes(StandardCharsets.UTF_8));
|
||||
collector.write(new byte[] {euroSign[0], euroSign[1]});
|
||||
|
||||
assertEquals("X\uFFFD", collector.content());
|
||||
}
|
||||
}
|
||||
+92
@@ -312,6 +312,98 @@ class SsmDirectCommandExecutorTest {
|
||||
assertEquals("terminated\n", result.get().standardError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stdoutExceedingAwsLimitIsTruncatedToFirst24000CharsWithoutBufferingItAll() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
Instance instance = instance("i-container", "container-1");
|
||||
when(ec2Service.findInstanceById("i-container")).thenReturn(instance);
|
||||
when(ec2Service.isInstanceContainerRunning("i-container")).thenReturn(true);
|
||||
|
||||
ExecCreateCmd execCreate = mock(ExecCreateCmd.class, withSettings().defaultAnswer(RETURNS_SELF));
|
||||
ExecCreateCmdResponse execCreateResponse = mock(ExecCreateCmdResponse.class);
|
||||
when(execCreateResponse.getId()).thenReturn("exec-1");
|
||||
when(execCreate.exec()).thenReturn(execCreateResponse);
|
||||
when(dockerClient.execCreateCmd("container-1")).thenReturn(execCreate);
|
||||
|
||||
ExecStartCmd execStart = mock(ExecStartCmd.class);
|
||||
when(execStart.exec(any())).thenAnswer(invocation -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
ResultCallback<Frame> resultCallback = invocation.getArgument(0);
|
||||
// Far more than the AWS 24,000 char stdout limit, delivered as many frames.
|
||||
byte[] chunk = "A".repeat(1000).getBytes();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
resultCallback.onNext(new Frame(StreamType.STDOUT, chunk));
|
||||
}
|
||||
resultCallback.onComplete();
|
||||
return resultCallback;
|
||||
});
|
||||
when(dockerClient.execStartCmd("exec-1")).thenReturn(execStart);
|
||||
|
||||
InspectExecCmd inspectExec = mock(InspectExecCmd.class);
|
||||
InspectExecResponse inspectExecResponse = mock(InspectExecResponse.class);
|
||||
when(inspectExecResponse.getExitCodeLong()).thenReturn(0L);
|
||||
when(inspectExec.exec()).thenReturn(inspectExecResponse);
|
||||
when(dockerClient.inspectExecCmd("exec-1")).thenReturn(inspectExec);
|
||||
|
||||
SsmDirectCommandExecutor executor = new SsmDirectCommandExecutor(dockerClient, ec2Service);
|
||||
Optional<SsmDirectCommandExecutor.ExecutionResult> result = executor.executeIfSupported(
|
||||
"i-container",
|
||||
"AWS-RunShellScript",
|
||||
Map.of("commands", List.of("yes A | head -c 100000")),
|
||||
30);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(24_000, result.get().standardOutput().length());
|
||||
assertEquals("A".repeat(24_000), result.get().standardOutput());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stderrExceedingAwsLimitIsTruncatedToFirst8000CharsWithoutBufferingItAll() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
Instance instance = instance("i-container", "container-1");
|
||||
when(ec2Service.findInstanceById("i-container")).thenReturn(instance);
|
||||
when(ec2Service.isInstanceContainerRunning("i-container")).thenReturn(true);
|
||||
|
||||
ExecCreateCmd execCreate = mock(ExecCreateCmd.class, withSettings().defaultAnswer(RETURNS_SELF));
|
||||
ExecCreateCmdResponse execCreateResponse = mock(ExecCreateCmdResponse.class);
|
||||
when(execCreateResponse.getId()).thenReturn("exec-1");
|
||||
when(execCreate.exec()).thenReturn(execCreateResponse);
|
||||
when(dockerClient.execCreateCmd("container-1")).thenReturn(execCreate);
|
||||
|
||||
ExecStartCmd execStart = mock(ExecStartCmd.class);
|
||||
when(execStart.exec(any())).thenAnswer(invocation -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
ResultCallback<Frame> resultCallback = invocation.getArgument(0);
|
||||
// Far more than the AWS 8,000 char stderr limit, delivered as many frames.
|
||||
byte[] chunk = "E".repeat(1000).getBytes();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
resultCallback.onNext(new Frame(StreamType.STDERR, chunk));
|
||||
}
|
||||
resultCallback.onComplete();
|
||||
return resultCallback;
|
||||
});
|
||||
when(dockerClient.execStartCmd("exec-1")).thenReturn(execStart);
|
||||
|
||||
InspectExecCmd inspectExec = mock(InspectExecCmd.class);
|
||||
InspectExecResponse inspectExecResponse = mock(InspectExecResponse.class);
|
||||
when(inspectExecResponse.getExitCodeLong()).thenReturn(1L);
|
||||
when(inspectExec.exec()).thenReturn(inspectExecResponse);
|
||||
when(dockerClient.inspectExecCmd("exec-1")).thenReturn(inspectExec);
|
||||
|
||||
SsmDirectCommandExecutor executor = new SsmDirectCommandExecutor(dockerClient, ec2Service);
|
||||
Optional<SsmDirectCommandExecutor.ExecutionResult> result = executor.executeIfSupported(
|
||||
"i-container",
|
||||
"AWS-RunShellScript",
|
||||
Map.of("commands", List.of("yes E 1>&2 | head -c 100000")),
|
||||
30);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(8_000, result.get().standardError().length());
|
||||
assertEquals("E".repeat(8_000), result.get().standardError());
|
||||
}
|
||||
|
||||
private static Instance instance(String instanceId, String containerId) {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId(instanceId);
|
||||
|
||||
Reference in new issue
Block a user