mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
+4
-2
@@ -1059,8 +1059,10 @@ public class ContainerLifecycleManager {
|
||||
}
|
||||
|
||||
// DNS servers — used to inject Floci's embedded DNS so spawned containers
|
||||
// can resolve *.localhost.floci.io to Floci's Docker network IP.
|
||||
if (spec.dnsServers() != null && !spec.dnsServers().isEmpty()) {
|
||||
// can resolve *.localhost.floci.io to Floci's Docker network IP. Docker rejects them
|
||||
// together with container:<id> network mode, where the resolver comes from that container.
|
||||
if (spec.dnsServers() != null && !spec.dnsServers().isEmpty()
|
||||
&& !isContainerNetworkMode(spec.networkMode())) {
|
||||
hostConfig.withDns(spec.dnsServers().toArray(new String[0]));
|
||||
}
|
||||
|
||||
|
||||
+18
-2
@@ -10,6 +10,7 @@ import com.github.dockerjava.api.model.Info;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
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;
|
||||
@@ -42,15 +43,18 @@ public class SecurityGroupFirewallManager {
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final EmulatorConfig config;
|
||||
private final EmbeddedDnsServer embeddedDnsServer;
|
||||
private final Map<String, ProtectedEndpoint> endpoints = new HashMap<>();
|
||||
|
||||
@Inject
|
||||
public SecurityGroupFirewallManager(DockerClient dockerClient, ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager, EmulatorConfig config) {
|
||||
ContainerLifecycleManager lifecycleManager, EmulatorConfig config,
|
||||
EmbeddedDnsServer embeddedDnsServer) {
|
||||
this.dockerClient = dockerClient;
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.embeddedDnsServer = embeddedDnsServer;
|
||||
}
|
||||
|
||||
public boolean enabled() {
|
||||
@@ -107,6 +111,9 @@ public class SecurityGroupFirewallManager {
|
||||
ContainerBuilder.Builder builder = containerBuilder.newContainer(config.network().securityGroupEnforcement().helperImage())
|
||||
.withName(name)
|
||||
.withDockerNetwork(dockerNetwork)
|
||||
// Workloads join this network namespace and use its resolver and hosts file.
|
||||
.withEmbeddedDns()
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withEntrypoint(List.of("sh", "-c"))
|
||||
.withCmd(List.of("exec sleep 2147483647"))
|
||||
.withLabels(ContainerStorageHelper.resourceIdentityLabels(service, resourceId, accountId, region))
|
||||
@@ -206,10 +213,11 @@ public class SecurityGroupFirewallManager {
|
||||
}
|
||||
List<SecurityGroupNftCompiler.Endpoint> peers = endpoints.values().stream()
|
||||
.map(ProtectedEndpoint::endpoint).toList();
|
||||
List<String> vpcResolvers = vpcResolvers();
|
||||
for (ProtectedEndpoint protectedEndpoint : new ArrayList<>(endpoints.values())) {
|
||||
try {
|
||||
String rules = SecurityGroupNftCompiler.compile(protectedEndpoint.endpoint(), peers,
|
||||
protectedEndpoint.prefixLists());
|
||||
protectedEndpoint.prefixLists(), vpcResolvers);
|
||||
apply(protectedEndpoint.helperId(), rules);
|
||||
} catch (RuntimeException e) {
|
||||
endpoints.values().forEach(endpoint -> quarantine(endpoint.helperId()));
|
||||
@@ -241,6 +249,14 @@ public class SecurityGroupFirewallManager {
|
||||
reconcileAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Floci's embedded DNS stands in for the VPC's Amazon DNS server, the one resolver security
|
||||
* groups never filter on AWS. The fallback resolvers are ordinary internet destinations.
|
||||
*/
|
||||
List<String> vpcResolvers() {
|
||||
return embeddedDnsServer.getServerIp().map(List::of).orElse(List.of());
|
||||
}
|
||||
|
||||
private void quarantine(String helperId) {
|
||||
try {
|
||||
apply(helperId, "flush chain inet floci_sg ingress\nflush chain inet floci_sg egress\n");
|
||||
|
||||
+16
-2
@@ -33,6 +33,11 @@ public final class SecurityGroupNftCompiler {
|
||||
|
||||
public static String compile(Endpoint target, Collection<Endpoint> peers,
|
||||
Map<String, List<String>> prefixLists) {
|
||||
return compile(target, peers, prefixLists, List.of());
|
||||
}
|
||||
|
||||
public static String compile(Endpoint target, Collection<Endpoint> peers,
|
||||
Map<String, List<String>> prefixLists, List<String> vpcResolvers) {
|
||||
if (target == null || target.groups() == null || target.groups().isEmpty()) {
|
||||
throw new IllegalArgumentException("Protected endpoint needs at least one security group");
|
||||
}
|
||||
@@ -43,8 +48,17 @@ public final class SecurityGroupNftCompiler {
|
||||
.append("add rule inet floci_sg ingress ct state established,related accept\n")
|
||||
.append("add rule inet floci_sg egress ct state established,related accept\n");
|
||||
|
||||
// Docker's embedded DNS is loopback and covered above. DHCP and the
|
||||
// link-local metadata endpoint are the other local infrastructure exceptions.
|
||||
// Docker's embedded DNS listens on loopback but forwards from this namespace. Security
|
||||
// groups never filter the VPC resolver on AWS. Only DNS is exempt, because the resolver
|
||||
// address also serves Floci's API. DHCP and the link-local metadata endpoint are the
|
||||
// other local infrastructure exceptions.
|
||||
for (String server : vpcResolvers) {
|
||||
String family = server.contains(":") ? "ip6" : "ip";
|
||||
rules.append("add rule inet floci_sg egress ").append(family).append(" daddr ").append(server)
|
||||
.append(" udp dport 53 accept\n")
|
||||
.append("add rule inet floci_sg egress ").append(family).append(" daddr ").append(server)
|
||||
.append(" tcp dport 53 accept\n");
|
||||
}
|
||||
rules.append("add rule inet floci_sg egress udp sport 68 udp dport 67 accept\n")
|
||||
.append("add rule inet floci_sg ingress udp sport 67 udp dport 68 accept\n")
|
||||
.append("add rule inet floci_sg egress ip daddr 169.254.169.254 accept\n")
|
||||
|
||||
Reference in new issue
Block a user