Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ec2): retain firewall identity during container teardown (#6492)

Andre Kurait73fc6fd0c1de
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java
index 79b230acc4..430f43ac48 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java
@@ -1337,12 +1337,15 @@ public class Ec2ContainerManager {
String containerId;
String containerIp;
String imdsSourceIp;
+ String firewallEniId;
int sshHostPort;
synchronized (instance) {
containerId = instance.getDockerContainerId();
containerIp = instance.getContainerBridgeIp();
imdsSourceIp = instance.getImdsSourceIp();
sshHostPort = instance.getSshHostPort();
+ firewallEniId = instance.getNetworkInterfaces() == null || instance.getNetworkInterfaces().isEmpty()
+ ? null : instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId();
instance.setState(InstanceState.shuttingDown());
}
executor.submit(() -> {
@@ -1371,9 +1374,8 @@ public class Ec2ContainerManager {
Thread.currentThread().interrupt();
}
}
- if (firewallManager != null && firewallManager.enabled()
- && instance.getNetworkInterfaces() != null && !instance.getNetworkInterfaces().isEmpty()) {
- firewallManager.unregister(instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId());
+ if (firewallManager != null && firewallManager.enabled() && firewallEniId != null) {
+ firewallManager.unregister(firewallEniId);
}
if (sshHostPort > 0) {
portAllocator.release(sshHostPort);
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java
index 14e69e1ee5..09c1222bfb 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java
@@ -12,6 +12,7 @@ import com.github.dockerjava.api.command.InspectExecCmd;
import com.github.dockerjava.api.command.InspectExecResponse;
import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.command.PingCmd;
+import com.github.dockerjava.api.command.RemoveContainerCmd;
import com.github.dockerjava.api.command.StartContainerCmd;
import com.github.dockerjava.api.command.StopContainerCmd;
import com.github.dockerjava.api.exception.DockerException;
@@ -100,6 +101,41 @@ import static org.mockito.Mockito.withSettings;
class Ec2ContainerManagerTest {
+ @Test
+ void terminationRetainsFirewallIdentityAfterInterfacesAreReleased() throws Exception {
+ DockerClient docker = mock(DockerClient.class);
+ SecurityGroupFirewallManager firewall = mock(SecurityGroupFirewallManager.class);
+ when(firewall.enabled()).thenReturn(true);
+ Ec2ContainerManager manager = new Ec2ContainerManager(
+ mock(ContainerBuilder.class), mock(ContainerLifecycleManager.class),
+ mock(ContainerLogStreamer.class), mock(ContainerDetector.class),
+ mock(DockerHostResolver.class), docker, mock(PortAllocator.class), mock(EmulatorConfig.class),
+ mock(Ec2MetadataServer.class), mock(Ec2PortForwardManager.class), mock(RegionResolver.class),
+ mock(ContainerNetworkReachability.class), mock(VpcNetworkManager.class),
+ mock(ContainerReachableEndpoint.class), firewall);
+ Instance instance = instance("i-firewall-teardown");
+ instance.setDockerContainerId(TEST_CONTAINER_ID);
+ InstanceNetworkInterface eni = new InstanceNetworkInterface();
+ eni.setNetworkInterfaceId("eni-firewall-teardown");
+ instance.setNetworkInterfaces(new ArrayList<>(List.of(eni)));
+ RemoveContainerCmd remove = mock(RemoveContainerCmd.class, RETURNS_SELF);
+ when(docker.removeContainerCmd(TEST_CONTAINER_ID)).thenReturn(remove);
+ doAnswer(invocation -> {
+ // The service releases ENIs while container teardown is still in progress.
+ instance.getNetworkInterfaces().clear();
+ return null;
+ }).when(remove).exec();
+ CountDownLatch completed = new CountDownLatch(1);
+ try {
+ manager.terminate(instance, completed::countDown);
+ assertTrue(completed.await(5, TimeUnit.SECONDS), "Container teardown must finish");
+ assertEquals("terminated", instance.getState().getName());
+ verify(firewall).unregister("eni-firewall-teardown");
+ } finally {
+ manager.stop();
+ }
+ }
+
private static final String TEST_USER_DATA_OUTPUT = "test-output";
private static final String TEST_CONTAINER_ID = "container-1";
private static final String TEST_LOG_STREAM_NAME = "yyyy/MM/dd/user-data";