mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): retry the security-group ruleset upload with a fresh tar stream (#8929)
This commit is contained in:
1 parent
8d005d70d0
commit
dd0f2bfc3a
2 files changed
+85
-17
No files matched your search
@@ -1,10 +1,14 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
|
||||
import com.github.dockerjava.api.command.ExecStartCmd;
|
||||
import com.github.dockerjava.api.command.InspectImageResponse;
|
||||
import com.github.dockerjava.api.command.ListContainersCmd;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.dns.EmbeddedDnsServer;
|
||||
@@ -13,13 +17,20 @@ import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager.ContainerInfo;
|
||||
import io.floci.aws.core.common.docker.ContainerSpec;
|
||||
import io.floci.aws.core.common.docker.DockerHostResolver;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -29,8 +40,11 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -216,6 +230,74 @@ class SecurityGroupFirewallManagerTest {
|
||||
verify(lifecycleManager, never()).removeIfExists("disagreeing");
|
||||
}
|
||||
|
||||
// Catches: the helper's ruleset upload calling copyArchive directly, so a connection dropped
|
||||
// mid-upload failed the namespace instead of being retried with a fresh tar stream.
|
||||
@Test
|
||||
void aRulesetUploadThatLosesItsConnectionIsRetriedWithTheWholeArchive() throws Exception {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.network().securityGroupEnforcement().enabled()).thenReturn(true);
|
||||
when(config.network().securityGroupEnforcement().helperImage()).thenReturn("example/network-helper:test");
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
when(config.docker().imageRegistryBase()).thenReturn(Optional.empty());
|
||||
DockerClient dockerClient = mock(DockerClient.class, RETURNS_DEEP_STUBS);
|
||||
Info linux = mock(Info.class);
|
||||
when(linux.getOsType()).thenReturn("linux");
|
||||
when(linux.getArchitecture()).thenReturn("amd64");
|
||||
when(dockerClient.infoCmd().exec()).thenReturn(linux);
|
||||
InspectImageResponse image = mock(InspectImageResponse.class);
|
||||
when(image.getOs()).thenReturn("linux");
|
||||
when(image.getArch()).thenReturn("amd64");
|
||||
when(dockerClient.inspectImageCmd(anyString()).exec()).thenReturn(image);
|
||||
ContainerNetwork network = mock(ContainerNetwork.class);
|
||||
when(network.getIpAddress()).thenReturn("172.18.0.5");
|
||||
when(dockerClient.inspectContainerCmd("helper-1").exec().getNetworkSettings().getNetworks())
|
||||
.thenReturn(Map.of("floci-net", network));
|
||||
|
||||
List<byte[]> uploads = new CopyOnWriteArrayList<>();
|
||||
CopyArchiveToContainerCmd copy = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.copyArchiveToContainerCmd("helper-1")).thenReturn(copy);
|
||||
doAnswer(invocation -> {
|
||||
uploads.add(invocation.<InputStream>getArgument(0).readAllBytes());
|
||||
return copy;
|
||||
}).when(copy).withTarInputStream(any());
|
||||
doThrow(new RuntimeException(new IOException("Broken pipe"))).doNothing().when(copy).exec();
|
||||
|
||||
ExecStartCmd execStart = mock(ExecStartCmd.class);
|
||||
when(dockerClient.execStartCmd(any())).thenReturn(execStart);
|
||||
when(execStart.exec(any())).thenAnswer(invocation -> {
|
||||
ResultCallback.Adapter<Frame> callback = invocation.getArgument(0);
|
||||
callback.onComplete();
|
||||
return callback;
|
||||
});
|
||||
when(dockerClient.inspectExecCmd(any()).exec().getExitCodeLong()).thenReturn(0L);
|
||||
|
||||
DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class);
|
||||
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("helper-1", Map.of(), Map.of()));
|
||||
SecurityGroupFirewallManager manager = new SecurityGroupFirewallManager(dockerClient,
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer), lifecycleManager, config,
|
||||
embeddedDnsServer);
|
||||
|
||||
SecurityGroupFirewallManager.Namespace namespace = manager.createNamespace("ecs", "abc123",
|
||||
"000000000000", "us-east-1", Optional.of("floci-net"), Map.of(), Map.of(), "127.0.0.1", List.of());
|
||||
|
||||
assertEquals("172.18.0.5", namespace.transportAddress());
|
||||
verify(copy, times(2)).exec();
|
||||
verify(copy, times(2)).withRemotePath("/tmp");
|
||||
assertEquals(2, uploads.size());
|
||||
try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(uploads.get(1)))) {
|
||||
TarArchiveEntry entry = tar.getNextEntry();
|
||||
assertEquals("floci-sg.nft", entry.getName());
|
||||
assertEquals(SecurityGroupNftCompiler.initialRuleset(),
|
||||
new String(tar.readAllBytes(), StandardCharsets.UTF_8),
|
||||
"the retry should upload the whole ruleset, not a drained stream");
|
||||
}
|
||||
verify(lifecycleManager, never()).removeIfExists("helper-1");
|
||||
}
|
||||
|
||||
private static Container exitedEcsHelper(String id, Map<String, String> ownerLabels) {
|
||||
Map<String, String> labels = new HashMap<>(ownerLabels);
|
||||
labels.put("io.floci.service", "ecs");
|
||||
|
||||
Reference in new issue
Block a user