mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): instances under security group enforcement no longer terminate at launch (#4510)
* fix(ec2): include the security group helper Dockerfile in the native image With security group enforcement on and no helper image on the daemon, the native image failed every EC2 launch with "Floci helper Dockerfile is missing". The Dockerfile is read with getResourceAsStream, which the native image only serves for listed resources. * fix(docker): omit DNS servers for containers that share another container's network Docker rejects DNS servers together with container:<id> network mode, so an EC2 instance or ECS task under security group enforcement failed to launch whenever Floci ran in a container, where it injects its embedded DNS. The container already uses the resolver of the container whose network it shares, the same reason extra hosts are left out. * fix(ec2): give the security group helper Floci's DNS and host entries A workload under enforcement joins its helper's network namespace and uses the helper's resolver and hosts file, where DNS servers and extra hosts on the workload itself are not allowed. The helper had neither, so a workload could not resolve Floci's endpoint hostname when Floci ran in a container. * chore(config): check that helper Dockerfiles are in the native image LocallyBuiltHelperImage reads its Dockerfile through a method parameter, which the constant check cannot follow. * fix(ec2): let workloads reach their DNS servers whatever their security groups allow Docker's resolver listens on loopback but forwards from the workload's network namespace, so a security group without UDP 53 egress left the workload unable to resolve any name. On AWS, security groups never filter traffic to the VPC resolver. The firewall now always allows port 53 to the helper's DNS servers. * chore(ec2): keep each security group helper's DNS servers with the helper Service fields stay final, and a helper no longer takes the DNS servers of the last helper created. * fix(ec2): read a security group helper's DNS servers from its container Keeping them in a map leaked an entry for every helper whose task failed before registering, and a helper restored after a restart had none. The rules now take them from the helper's HostConfig, which reconcileAll already inspects. * fix(ec2): exempt only Floci's DNS from security groups, not the fallback resolvers On AWS the exemption covers only the Amazon DNS server in the VPC, which Floci's embedded DNS stands in for. A public resolver is ordinary internet traffic that a security group filters, so a group that denies egress now blocks 8.8.8.8 and 8.8.4.4 too.
This commit is contained in:
1 parent
50dc610d35
commit
4faa2b1354
8 files changed
+186
-8
No files matched your search
@@ -44,6 +44,9 @@ class NativeImageResourceIncludesTest {
|
||||
|
||||
private static final String JAVA_SUFFIX = ".java";
|
||||
|
||||
/** A Dockerfile path literal. LocallyBuiltHelperImage reads it through a method parameter. */
|
||||
private static final Pattern DOCKERFILE_LITERAL = Pattern.compile("\"(/?[\\w.\\-]+(?:/[\\w.\\-]+)*\\.Dockerfile)\"");
|
||||
|
||||
/** A {@code static final String NAME = "some/path.ext";} declaration holding a resource path. */
|
||||
private static final Pattern RESOURCE_CONSTANT = Pattern.compile(
|
||||
"static\\s+final\\s+String\\s+(\\w+)\\s*=\\s*\"(/?[\\w.\\-]+(?:/[\\w.\\-]+)+\\.[A-Za-z0-9]+)\"");
|
||||
@@ -91,6 +94,41 @@ class NativeImageResourceIncludesTest {
|
||||
.reduce((a, b) -> a + "\n " + b).orElse(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyHelperDockerfileIsIncludedInTheNativeImage() throws IOException {
|
||||
List<PathMatcher> includes = parseIncludes();
|
||||
Map<String, String> offenders = new LinkedHashMap<>();
|
||||
|
||||
try (Stream<Path> sources = Files.walk(MAIN_SOURCE_ROOT)) {
|
||||
sources.filter(p -> p.toString().endsWith(JAVA_SUFFIX)).forEach(source -> {
|
||||
Matcher literals = DOCKERFILE_LITERAL.matcher(readSource(source));
|
||||
while (literals.find()) {
|
||||
String resource = literals.group(1).startsWith("/")
|
||||
? literals.group(1).substring(1) : literals.group(1);
|
||||
if (Files.exists(MAIN_RESOURCE_ROOT.resolve(resource))
|
||||
&& includes.stream().noneMatch(m -> m.matches(Path.of(resource)))) {
|
||||
offenders.put(resource, source.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
assertTrue(offenders.isEmpty(),
|
||||
"These Dockerfiles are built from the classpath at runtime, but no glob in " + APPLICATION_YML
|
||||
+ " under quarkus.native.resources.includes matches them:\n "
|
||||
+ offenders.entrySet().stream()
|
||||
.map(e -> e.getKey() + " (named in " + e.getValue() + ")")
|
||||
.reduce((a, b) -> a + "\n " + b).orElse(""));
|
||||
}
|
||||
|
||||
private static String readSource(Path javaFile) {
|
||||
try {
|
||||
return Files.readString(javaFile);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Unable to read source file " + javaFile, e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Resource paths declared as a constant in {@code javaFile} and passed to getResourceAsStream there. */
|
||||
private List<String> resourcesReadThroughAConstant(Path javaFile) {
|
||||
String source;
|
||||
|
||||
+15
@@ -181,6 +181,21 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
|| hostConfig.getValue().getExtraHosts().length == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createOmitsDnsForContainerNetworkMode() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
ContainerSpec spec = new ContainerSpec(
|
||||
"busybox:stable", null, List.of(), null, null, null, Map.of(), List.of(),
|
||||
"container:router-id", List.of(), List.of(), List.of(),
|
||||
Map.of(), null, false, null, List.of("172.18.0.2"), null, null, List.of());
|
||||
|
||||
manager().create(spec);
|
||||
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
assertTrue(hostConfig.getValue().getDns() == null || hostConfig.getValue().getDns().length == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createIncludesNamespaceLabelWhenConfigured() {
|
||||
when(dockerConfig.resourceNamespace()).thenReturn(Optional.of("run-one"));
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class SecurityGroupFirewallManagerTest {
|
||||
|
||||
@Test
|
||||
void helperCarriesTheResolverAndHostsItsWorkloadsUse() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.network().securityGroupEnforcement().enabled()).thenReturn(true);
|
||||
when(config.network().securityGroupEnforcement().helperImage()).thenReturn("floci/network-helper:local");
|
||||
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(dockerClient.infoCmd().exec()).thenReturn(linux);
|
||||
when(dockerClient.inspectImageCmd(anyString()).exec()).thenReturn(null);
|
||||
DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class);
|
||||
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.2"));
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.createAndStart(any())).thenThrow(new IllegalStateException("stop after the spec"));
|
||||
SecurityGroupFirewallManager manager = new SecurityGroupFirewallManager(dockerClient,
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer), lifecycleManager, config,
|
||||
embeddedDnsServer);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> manager.createNamespace("ec2", "i-1", "000000000000",
|
||||
"us-east-1", Optional.empty(), Map.of()));
|
||||
|
||||
ArgumentCaptor<ContainerSpec> helper = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
verify(lifecycleManager).createAndStart(helper.capture());
|
||||
assertTrue(helper.getValue().dnsServers().contains("172.18.0.2"), "the helper uses Floci's DNS");
|
||||
assertTrue(helper.getValue().extraHosts().contains("host.docker.internal:host-gateway"),
|
||||
"the helper resolves host.docker.internal");
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyFlocisDnsIsExemptFromTheSecurityGroups() {
|
||||
EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class);
|
||||
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.2"));
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.dns().containerFallbackServers()).thenReturn(List.of("8.8.8.8", "8.8.4.4"));
|
||||
SecurityGroupFirewallManager manager = new SecurityGroupFirewallManager(mock(DockerClient.class),
|
||||
mock(ContainerBuilder.class), mock(ContainerLifecycleManager.class), config, embeddedDnsServer);
|
||||
|
||||
assertEquals(List.of("172.18.0.2"), manager.vpcResolvers());
|
||||
|
||||
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
|
||||
assertEquals(List.of(), manager.vpcResolvers());
|
||||
}
|
||||
}
|
||||
+15
@@ -72,6 +72,21 @@ class SecurityGroupNftCompilerTest {
|
||||
assertTrue(nft.contains("ip saddr 10.1.0.0/16 meta l4proto icmpv6 accept"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcResolversStayReachableWhenTheGroupsAllowNoEgress() {
|
||||
SecurityGroup group = new SecurityGroup();
|
||||
group.setVpcId("vpc-1");
|
||||
group.setOwnerId("000000000000");
|
||||
|
||||
String nft = SecurityGroupNftCompiler.compile(endpoint("eni-a", "10.0.0.2", "172.17.0.2", group),
|
||||
List.of(), Map.of(), List.of("172.18.0.2", "fd00::53"));
|
||||
|
||||
assertTrue(nft.contains("egress ip daddr 172.18.0.2 udp dport 53 accept"));
|
||||
assertTrue(nft.contains("egress ip daddr 172.18.0.2 tcp dport 53 accept"));
|
||||
assertTrue(nft.contains("egress ip6 daddr fd00::53 udp dport 53 accept"));
|
||||
assertFalse(nft.contains("daddr 172.18.0.2 accept"), "only DNS is allowed to the resolver");
|
||||
}
|
||||
|
||||
private static SecurityGroupNftCompiler.Endpoint endpoint(String eni, String logical,
|
||||
String transport, SecurityGroup group) {
|
||||
return new SecurityGroupNftCompiler.Endpoint("000000000000", "us-east-1", "vpc-1",
|
||||
|
||||
Reference in new issue
Block a user