mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(eks): enable IMDS and link-local metadata access from pod network (#4039)
Pods running without hostNetwork: true run in isolated network namespaces managed by the CNI plugin (e.g. flannel in k3s). When pods send requests to link-local addresses such as IMDS (169.254.169.254:80), the packets reach the host node network namespace where CNI portmap installed -A PREROUTING -m addrtype --dst-type LOCAL -j CNI-HOSTPORT-DNAT. Because 169.254.169.254 is assigned to lo, the kernel evaluates its addrtype as LOCAL, causing non-host-network pods to misroute or drop link-local traffic if an ingress controller binds the same port. This introduces EksPodNetworkRouting and an opt-in configuration setting floci.services.eks.imds-pod-network (defaulting to false to preserve default pod network isolation). When enabled alongside floci.services.eks.imds, DNAT iptables rules are programmed in a dedicated FLOCI-LINK-LOCAL chain inserted at rule 1 of PREROUTING. Requests originating from the cluster pod CIDR targeting configured link-local endpoints (such as IMDS at 169.254.169.254:80, and extensible for Pod Identity at 169.254.170.23:80) are explicitly DNATed to the node's loopback listener, preempting CNI-HOSTPORT-DNAT without conflicting with CNI portmap or flannel routing. LinkLocalEndpoint strictly validates IPv4 literal format to reject non-IPs or malformed strings. Rules are idempotent and degrade gracefully if iptables configuration fails, ensuring cluster startup is never aborted.
This commit is contained in:
1 parent
cc767e8976
commit
31a19df6f5
9 files changed
+363
-19
No files matched your search
@@ -2871,6 +2871,13 @@ public interface EmulatorConfig {
|
||||
@WithDefault("false")
|
||||
boolean imds();
|
||||
|
||||
/**
|
||||
* When true, routes link-local IMDS traffic from ordinary pod network namespaces to the node's
|
||||
* link-local listener. Requires {@code imds()} to be enabled.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean imdsPodNetwork();
|
||||
|
||||
/**
|
||||
* When true, configures k3s with the cluster's per-cluster OIDC signing keypair and
|
||||
* advertises Floci's OIDC issuer URL, enabling in-cluster IAM Roles for Service Accounts (IRSA).
|
||||
|
||||
@@ -1057,6 +1057,10 @@ public class EksClusterManager {
|
||||
return;
|
||||
}
|
||||
|
||||
if (config.services().eks().imdsPodNetwork()) {
|
||||
configurePodNetworkRouting(cluster, containerId);
|
||||
}
|
||||
|
||||
LOG.infov("Configured link-local IMDS endpoint for EKS cluster {0}", cluster.getName());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not configure link-local IMDS endpoint for EKS cluster {0}: {1}",
|
||||
@@ -1064,6 +1068,26 @@ public class EksClusterManager {
|
||||
}
|
||||
}
|
||||
|
||||
void configurePodNetworkRouting(Cluster cluster, String containerId) {
|
||||
configurePodNetworkRouting(cluster, containerId, EksPodNetworkRouting.DEFAULT_ENDPOINTS);
|
||||
}
|
||||
|
||||
void configurePodNetworkRouting(Cluster cluster, String containerId, List<LinkLocalEndpoint> endpoints) {
|
||||
try {
|
||||
String[] routingCmd = EksPodNetworkRouting.buildRoutingCommand(
|
||||
EksPodNetworkRouting.DEFAULT_POD_CIDR,
|
||||
endpoints);
|
||||
ContainerExecResult routing = execInContainerForResult(containerId, routingCmd, 15);
|
||||
if (routing.exitCode() != 0) {
|
||||
LOG.warnv("Could not configure link-local pod network routing for EKS cluster {0}: {1}",
|
||||
cluster.getName(), routing.summary());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not configure link-local pod network routing for EKS cluster {0}: {1}",
|
||||
cluster.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
void unregisterMetadataEndpoint(Cluster cluster) {
|
||||
Instance nodeInstance = clusterNodeInstances.remove(clusterResourceName(cluster));
|
||||
if (metadataServer != null && nodeInstance != null) {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Generates iptables routing rules for link-local endpoints inside an EKS cluster container.
|
||||
* <p>
|
||||
* Programs DNAT rules in the node network namespace nat table so traffic originating
|
||||
* from the cluster pod CIDR directed at link-local addresses (such as IMDS 169.254.169.254:80
|
||||
* and EKS Pod Identity 169.254.170.23:80) reaches Floci's node listener rather than being
|
||||
* dropped by the Linux kernel or hijacked by CNI hostPort ingress rules.
|
||||
*/
|
||||
public final class EksPodNetworkRouting {
|
||||
|
||||
public static final String DEFAULT_POD_CIDR = "10.42.0.0/16";
|
||||
public static final String CHAIN_NAME = "FLOCI-LINK-LOCAL";
|
||||
public static final LinkLocalEndpoint IMDS_ENDPOINT = new LinkLocalEndpoint("169.254.169.254", 80);
|
||||
public static final List<LinkLocalEndpoint> DEFAULT_ENDPOINTS = List.of(IMDS_ENDPOINT);
|
||||
|
||||
private EksPodNetworkRouting() {}
|
||||
|
||||
/**
|
||||
* Builds a POSIX sh command array that idempotently programs iptables DNAT rules
|
||||
* in the node container nat table for the specified link-local endpoints.
|
||||
*
|
||||
* @param podCidr pod network CIDR (defaults to {@link #DEFAULT_POD_CIDR} if null or blank)
|
||||
* @param endpoints link-local endpoints to route from the pod network
|
||||
* @return command array suitable for container exec (["sh", "-c", "..."])
|
||||
*/
|
||||
public static String[] buildRoutingCommand(String podCidr, List<LinkLocalEndpoint> endpoints) {
|
||||
String cidr = (podCidr != null && !podCidr.isBlank()) ? podCidr : DEFAULT_POD_CIDR;
|
||||
if (endpoints == null || endpoints.isEmpty()) {
|
||||
throw new IllegalArgumentException("endpoints cannot be null or empty");
|
||||
}
|
||||
|
||||
List<String> lines = new ArrayList<>();
|
||||
lines.add("set -eu");
|
||||
lines.add("IPTABLES=$(command -v iptables 2>/dev/null || command -v /bin/aux/iptables 2>/dev/null || true)");
|
||||
lines.add("if [ -z \"$IPTABLES\" ]; then");
|
||||
lines.add(" echo 'iptables not found in container' >&2");
|
||||
lines.add(" exit 1");
|
||||
lines.add("fi");
|
||||
lines.add("$IPTABLES -t nat -N " + CHAIN_NAME + " 2>/dev/null || true");
|
||||
lines.add("$IPTABLES -t nat -C PREROUTING -j " + CHAIN_NAME + " 2>/dev/null || $IPTABLES -t nat -I PREROUTING 1 -j " + CHAIN_NAME);
|
||||
|
||||
for (LinkLocalEndpoint ep : endpoints) {
|
||||
String check = String.format(
|
||||
"$IPTABLES -t nat -C %s -s %s -d %s -p tcp --dport %d -j DNAT --to-destination %s:%d 2>/dev/null",
|
||||
CHAIN_NAME, cidr, ep.ip(), ep.port(), ep.ip(), ep.port());
|
||||
String add = String.format(
|
||||
"$IPTABLES -t nat -A %s -s %s -d %s -p tcp --dport %d -j DNAT --to-destination %s:%d",
|
||||
CHAIN_NAME, cidr, ep.ip(), ep.port(), ep.ip(), ep.port());
|
||||
lines.add(check + " || " + add);
|
||||
}
|
||||
|
||||
return new String[]{"sh", "-c", String.join("\n", lines)};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a link-local network endpoint (IP address and TCP port)
|
||||
* routed from pod network namespaces to local node listeners.
|
||||
*/
|
||||
public record LinkLocalEndpoint(String ip, int port) {
|
||||
|
||||
public LinkLocalEndpoint {
|
||||
Objects.requireNonNull(ip, "ip cannot be null");
|
||||
if (ip.isBlank()) {
|
||||
throw new IllegalArgumentException("ip cannot be blank");
|
||||
}
|
||||
if (!isValidIpv4Literal(ip.trim())) {
|
||||
throw new IllegalArgumentException("ip must be a valid IPv4 address literal: " + ip);
|
||||
}
|
||||
if (port <= 0 || port > 65535) {
|
||||
throw new IllegalArgumentException("port must be between 1 and 65535");
|
||||
}
|
||||
ip = ip.trim();
|
||||
}
|
||||
|
||||
private static boolean isValidIpv4Literal(String ip) {
|
||||
String[] parts = ip.split("\\.", -1);
|
||||
if (parts.length != 4) {
|
||||
return false;
|
||||
}
|
||||
for (String part : parts) {
|
||||
if (part.isEmpty() || part.length() > 3) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < part.length(); i++) {
|
||||
if (!Character.isDigit(part.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int val = Integer.parseInt(part);
|
||||
if (val < 0 || val > 255) {
|
||||
return false;
|
||||
}
|
||||
if (part.length() > 1 && part.startsWith("0")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user