diff --git a/docs/services/eks.md b/docs/services/eks.md index cf52fd52b8..8af5ad37ac 100644 --- a/docs/services/eks.md +++ b/docs/services/eks.md @@ -115,6 +115,7 @@ back (for example Docker is unavailable), the cluster is marked `FAILED` instead | `FLOCI_SERVICES_EKS_ENDPOINT_MODE` | `host` | `describe-cluster` endpoint: `host` (`localhost:`) or `network` (container DNS) | | `FLOCI_SERVICES_EKS_IAM_AUTH_WEBHOOK` | `true` | Wire a token-auth webhook into k3s so `aws eks get-token` works | | `FLOCI_SERVICES_EKS_ECR_REGISTRY_MIRROR` | `true` | Inject a containerd `registries.yaml` so pods can pull images pushed to [Floci ECR](ecr.md) | +| `FLOCI_SERVICES_EKS_IMDS` | `false` | Enable link-local IMDS (`169.254.169.254`) proxy in cluster containers | ### Pulling images from Floci ECR @@ -172,6 +173,39 @@ services: FLOCI_SERVICES_EKS_MOCK: "true" ``` +## Instance Metadata Service (IMDS) + +When enabled (`FLOCI_SERVICES_EKS_IMDS=true`), each k3s cluster container exposes the AWS Instance Metadata Service on the link-local address `169.254.169.254:80`. Inside the container, Floci adds `169.254.169.254/32` to the loopback interface (`lo`) and runs a lightweight `socat` TCP relay forwarding metadata requests to Floci's IMDS server. + +Both IMDSv1 and IMDSv2 (`PUT /latest/api/token`) are supported. The cluster container is registered as a synthesized EC2 instance node (type `m5.large`, image `ami-eks-k3s`) associated with the cluster's IAM role: + +```bash +# IMDSv2: obtain token +TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \ + -H "x-aws-ec2-metadata-token-ttl-seconds: 21600") + +# Read node instance ID +curl -s -H "x-aws-ec2-metadata-token: $TOKEN" \ + http://169.254.169.254/latest/meta-data/instance-id +``` + +### Reachability and network namespaces + +The link-local proxy attaches to the loopback interface of the k3s container network namespace (the node network namespace). + +- **Reachable from node network namespace:** Workloads configured with `hostNetwork: true` share the node network namespace and can reach `169.254.169.254:80`. This allows local testing of host-network security policies, intrusion-detection rules, and credential exfiltration defenses. +- **Not reachable from ordinary pods:** Pods running in separate pod network namespaces cannot reach `169.254.169.254` through this loopback alias because link-local addresses are non-routable across network namespaces. Pod-CIDR DNAT routing is not implemented. + +### Configuration + +IMDS proxy initialization is disabled by default (`floci.services.eks.imds=false`). Set `FLOCI_SERVICES_EKS_IMDS=true` to enable the proxy setup inside the k3s container. + +A failure to configure the proxy (for example on custom minimal images lacking network utilities) logs a warning and allows cluster startup to continue. + +### Hop limits + +EC2 metadata options store `HttpPutResponseHopLimit`, but the userspace TCP proxy relay terminates the incoming connection and opens a new connection to Floci, regenerating the IP packet TTL. Hop limits are recorded on the instance metadata options model but are not enforced by the userspace proxy. + ## IRSA (IAM Roles for Service Accounts) Every cluster gets an OIDC identity provider, so the full IRSA flow (trust policy, service-account token, `sts:AssumeRoleWithWebIdentity`) works end to end without mocked or hardcoded tokens. diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 3f2602d139..0b10cd7369 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -2727,6 +2727,13 @@ public interface EmulatorConfig { */ @WithDefault("false") boolean disableCni(); + + /** + * When true, exposes an IMDS link-local proxy (169.254.169.254:80) inside the cluster container's + * network namespace that relays to Floci's EC2 metadata service. + */ + @WithDefault("false") + boolean imds(); } /** diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java index 9b3bfa58e5..95d0b2db9c 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java @@ -215,7 +215,7 @@ public class EmulatorLifecycle { elbClassicService.restorePersistedRuntime(); } - if (config.services().ec2().enabled() && !config.services().ec2().mock()) { + if (isMetadataServerNeeded()) { ec2MetadataServer.start().exceptionally(ex -> { LOG.warnv("EC2 IMDS server failed to start: {0}", ex.getMessage()); return null; @@ -304,7 +304,7 @@ public class EmulatorLifecycle { // still runs at the end to stop the flush schedulers and capture any shutdown-time writes. runCleanup("storage flush", storageFactory::flushAll); runCleanup("EC2 metadata server", () -> { - if (config.services().ec2().enabled() && !config.services().ec2().mock()) { + if (isMetadataServerNeeded()) { ec2MetadataServer.stop(); } }); @@ -345,4 +345,16 @@ public class EmulatorLifecycle { LOG.warnv(e, "Shutdown cleanup failed for {0}; continuing with the remaining steps", resource); } } + + private boolean isMetadataServerNeeded() { + if (config == null || config.services() == null) { + return false; + } + EmulatorConfig.Ec2ServiceConfig ec2 = config.services().ec2(); + if (ec2 != null && ec2.enabled() && !ec2.mock()) { + return true; + } + EmulatorConfig.EksServiceConfig eks = config.services().eks(); + return eks != null && eks.enabled() && !eks.mock() && eks.imds(); + } } 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 ea0a2562e0..6b33720db1 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 @@ -1788,50 +1788,14 @@ public class Ec2ContainerManager { } static String[] metadataProxyInstallCommand() { - return new String[]{"sh", "-c", String.join("\n", - "set -eu", - "if command -v ip >/dev/null 2>&1 && command -v socat >/dev/null 2>&1 && command -v curl >/dev/null 2>&1; then exit 0; fi", - "if command -v apt-get >/dev/null 2>&1; then", - " apt-get update -qq >/dev/null", - " DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends iproute2 socat curl ca-certificates >/dev/null", - "elif command -v dnf >/dev/null 2>&1; then", - // --allowerasing lets dnf swap the curl-minimal that - // public.ecr.aws/amazonlinux/amazonlinux:2023 ships by default for the full - // curl package this proxy needs. Without it, dnf aborts the whole transaction - // on a curl/curl-minimal conflict and iproute+socat never install either, even - // though neither of them conflicts with anything. - " dnf install -y --allowerasing iproute socat curl ca-certificates >/dev/null", - // Same gap as the sshd probe: Amazon Linux 2 has only yum, so on an instance - // launched from ami-amazonlinux2 this chain reached its else branch and exited 1 - // with "No supported package manager found for IMDS proxy dependencies" -- - // leaving the instance without a link-local IMDS endpoint. - "elif command -v yum >/dev/null 2>&1; then", - " yum install -y iproute socat curl ca-certificates >/dev/null", - "elif command -v apk >/dev/null 2>&1; then", - " apk add --no-cache iproute2 socat curl ca-certificates >/dev/null", - "else", - " echo 'No supported package manager found for IMDS proxy dependencies' >&2", - " exit 1", - "fi")}; + return Ec2MetadataProxy.installCommand(); } static String[] metadataProxyStartCommand(String flociHost, int imdsPort) { - return new String[]{"sh", "-c", String.join("\n", - "set -eu", - "ip addr show dev lo | grep -q '169.254.169.254/32' || ip addr add 169.254.169.254/32 dev lo", - "if [ -f /tmp/floci-imds-proxy.pid ] && kill -0 \"$(cat /tmp/floci-imds-proxy.pid)\" 2>/dev/null; then", - " exit 0", - "fi", - "nohup socat TCP-LISTEN:80,bind=169.254.169.254,fork,reuseaddr TCP:" + flociHost + ":" + imdsPort + " >/tmp/floci-imds-proxy.log 2>&1 &", - "echo $! > /tmp/floci-imds-proxy.pid", - "for i in 1 2 3 4 5 6 7 8 9 10 11 12; do", - " curl -fsS --max-time 1 http://169.254.169.254/latest/meta-data/instance-id >/dev/null && exit 0", - " sleep 1", - "done", - "cat /tmp/floci-imds-proxy.log >&2 || true", - "exit 1")}; + return Ec2MetadataProxy.startCommand(flociHost, imdsPort); } + static List localAwsEnvironment(String region, String serviceEndpoint, String imdsEndpoint) { return List.of( "AWS_EC2_METADATA_SERVICE_ENDPOINT=" + imdsEndpoint, @@ -2054,26 +2018,7 @@ public class Ec2ContainerManager { } static Optional preferredMetadataSourceIp(Map networks) { - if (networks == null || networks.isEmpty()) { - return Optional.empty(); - } - Optional configuredNetworkIp = networks.entrySet().stream() - .filter(entry -> !"bridge".equals(entry.getKey())) - .map(Map.Entry::getValue) - .map(ContainerNetwork::getIpAddress) - .filter(ip -> ip != null && !ip.isBlank()) - .findFirst(); - if (configuredNetworkIp.isPresent()) { - return configuredNetworkIp; - } - ContainerNetwork bridge = networks.get("bridge"); - if (bridge != null && bridge.getIpAddress() != null && !bridge.getIpAddress().isBlank()) { - return Optional.of(bridge.getIpAddress()); - } - return networks.values().stream() - .map(ContainerNetwork::getIpAddress) - .filter(ip -> ip != null && !ip.isBlank()) - .findFirst(); + return Ec2MetadataProxy.preferredMetadataSourceIp(networks); } private byte[] buildSingleFileTar(String filename, byte[] content, int mode) throws IOException { diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataProxy.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataProxy.java new file mode 100644 index 0000000000..8287b789f7 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataProxy.java @@ -0,0 +1,83 @@ +package io.github.hectorvent.floci.services.ec2; + +import com.github.dockerjava.api.model.ContainerNetwork; + +import java.util.Map; +import java.util.Optional; + +/** + * Shared commands and network resolution helpers for installing and starting + * the link-local IMDS proxy (169.254.169.254:80) inside containers. + */ +public final class Ec2MetadataProxy { + + private Ec2MetadataProxy() {} + + public static String[] installCommand() { + return new String[]{"sh", "-c", String.join("\n", + "set -eu", + "if command -v ip >/dev/null 2>&1 && command -v socat >/dev/null 2>&1 && command -v curl >/dev/null 2>&1; then exit 0; fi", + "if command -v apt-get >/dev/null 2>&1; then", + " apt-get update -qq >/dev/null", + " DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends iproute2 socat curl ca-certificates >/dev/null", + "elif command -v dnf >/dev/null 2>&1; then", + // --allowerasing lets dnf swap the curl-minimal that + // public.ecr.aws/amazonlinux/amazonlinux:2023 ships by default for the full + // curl package this proxy needs. Without it, dnf aborts the whole transaction + // on a curl/curl-minimal conflict and iproute+socat never install either, even + // though neither of them conflicts with anything. + " dnf install -y --allowerasing iproute socat curl ca-certificates >/dev/null", + // Same gap as the sshd probe: Amazon Linux 2 has only yum, so on an instance + // launched from ami-amazonlinux2 this chain reached its else branch and exited 1 + // with "No supported package manager found for IMDS proxy dependencies", + // leaving the instance without a link-local IMDS endpoint. + "elif command -v yum >/dev/null 2>&1; then", + " yum install -y iproute socat curl ca-certificates >/dev/null", + "elif command -v apk >/dev/null 2>&1; then", + " apk add --no-cache iproute2 socat curl ca-certificates >/dev/null", + "else", + " echo 'No supported package manager found for IMDS proxy dependencies' >&2", + " exit 1", + "fi")}; + } + + public static String[] startCommand(String flociHost, int imdsPort) { + return new String[]{"sh", "-c", String.join("\n", + "set -eu", + "ip addr show dev lo | grep -q '169.254.169.254/32' || ip addr add 169.254.169.254/32 dev lo", + "if [ -f /tmp/floci-imds-proxy.pid ] && kill -0 \"$(cat /tmp/floci-imds-proxy.pid)\" 2>/dev/null; then", + " exit 0", + "fi", + "nohup socat TCP-LISTEN:80,bind=169.254.169.254,fork,reuseaddr TCP:" + flociHost + ":" + imdsPort + " >/tmp/floci-imds-proxy.log 2>&1 &", + "echo $! > /tmp/floci-imds-proxy.pid", + "for i in 1 2 3 4 5 6 7 8 9 10 11 12; do", + " curl -fsS --max-time 1 http://169.254.169.254/latest/meta-data/instance-id >/dev/null && exit 0", + " sleep 1", + "done", + "cat /tmp/floci-imds-proxy.log >&2 || true", + "exit 1")}; + } + + public static Optional preferredMetadataSourceIp(Map networks) { + if (networks == null || networks.isEmpty()) { + return Optional.empty(); + } + Optional configuredNetworkIp = networks.entrySet().stream() + .filter(entry -> !"bridge".equals(entry.getKey())) + .map(Map.Entry::getValue) + .map(ContainerNetwork::getIpAddress) + .filter(ip -> ip != null && !ip.isBlank()) + .findFirst(); + if (configuredNetworkIp.isPresent()) { + return configuredNetworkIp; + } + ContainerNetwork bridge = networks.get("bridge"); + if (bridge != null && bridge.getIpAddress() != null && !bridge.getIpAddress().isBlank()) { + return Optional.of(bridge.getIpAddress()); + } + return networks.values().stream() + .map(ContainerNetwork::getIpAddress) + .filter(ip -> ip != null && !ip.isBlank()) + .findFirst(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java index ac65a6fb11..cd3c9ebd57 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java @@ -75,7 +75,7 @@ public class Ec2MetadataServer { } /** Reconcile every Docker attachment without retaining stale addresses after restart. */ - void reconcileContainerAddresses(Set addresses, Instance instance) { + public void reconcileContainerAddresses(Set addresses, Instance instance) { for (String address : addresses) { registerContainer(address, instance.getInstanceId(), instance); } @@ -83,15 +83,20 @@ public class Ec2MetadataServer { entry.getValue() == instance && !addresses.contains(entry.getKey())); } - void unregisterInstance(Instance instance) { - containerIpToInstance.entrySet().removeIf(entry -> entry.getValue() == instance); + public void unregisterInstance(Instance instance) { + if (instance != null) { + containerIpToInstance.entrySet().removeIf(entry -> entry.getValue() == instance); + } } Optional registeredContainer(String containerIp) { return Optional.ofNullable(containerIpToInstance.get(containerIp)); } - public CompletableFuture start() { + public synchronized CompletableFuture start() { + if (httpServer != null) { + return CompletableFuture.completedFuture(null); + } CompletableFuture future = new CompletableFuture<>(); int port = config.services().ec2().imdsPort(); @@ -137,9 +142,10 @@ public class Ec2MetadataServer { return future; } - public void stop() { + public synchronized void stop() { if (httpServer != null) { httpServer.close(); + httpServer = null; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index 0ea8535e6c..c2b5d3b7dc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -14,9 +14,17 @@ import io.github.hectorvent.floci.core.common.docker.PortAllocator; import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager; import io.github.hectorvent.floci.services.eks.model.CertificateAuthority; import io.github.hectorvent.floci.services.eks.model.Cluster; +import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.async.ResultCallback; import com.github.dockerjava.api.command.ExecCreateCmdResponse; +import com.github.dockerjava.api.command.InspectContainerResponse; +import com.github.dockerjava.api.model.ContainerNetwork; import com.github.dockerjava.api.model.Frame; +import io.github.hectorvent.floci.services.ec2.Ec2MetadataProxy; +import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer; +import io.github.hectorvent.floci.services.ec2.model.Instance; +import io.github.hectorvent.floci.services.ec2.model.InstanceState; +import io.github.hectorvent.floci.services.ec2.model.Placement; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; @@ -33,7 +41,13 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** @@ -62,6 +76,20 @@ public class EksClusterManager { private final EcrRegistryManager ecrRegistryManager; private final EmulatorConfig config; private final RegionResolver regionResolver; + private final Ec2MetadataServer metadataServer; + private final Map clusterNodeInstances = new ConcurrentHashMap<>(); + + public EksClusterManager(ContainerBuilder containerBuilder, + ContainerLifecycleManager lifecycleManager, + ContainerDetector containerDetector, + PortAllocator portAllocator, + DockerHostResolver dockerHostResolver, + EcrRegistryManager ecrRegistryManager, + EmulatorConfig config, + RegionResolver regionResolver) { + this(containerBuilder, lifecycleManager, containerDetector, portAllocator, + dockerHostResolver, ecrRegistryManager, config, regionResolver, null); + } @Inject public EksClusterManager(ContainerBuilder containerBuilder, @@ -71,7 +99,8 @@ public class EksClusterManager { DockerHostResolver dockerHostResolver, EcrRegistryManager ecrRegistryManager, EmulatorConfig config, - RegionResolver regionResolver) { + RegionResolver regionResolver, + Ec2MetadataServer metadataServer) { this.containerBuilder = containerBuilder; this.lifecycleManager = lifecycleManager; this.containerDetector = containerDetector; @@ -80,6 +109,7 @@ public class EksClusterManager { this.ecrRegistryManager = ecrRegistryManager; this.config = config; this.regionResolver = regionResolver; + this.metadataServer = metadataServer; } /** @@ -222,6 +252,7 @@ public class EksClusterManager { ContainerInfo info = lifecycleManager.startCreated(containerId, spec); applyEndpoints(cluster, containerName, hostPort, info); + configureLinkLocalMetadataEndpoint(cluster, containerId); LOG.infov("k3s container {0} started for cluster {1} on port {2} (internal: {3})", containerId, cluster.getName(), String.valueOf(hostPort), cluster.getInternalEndpoint()); @@ -272,6 +303,7 @@ public class EksClusterManager { cluster.setContainerId(info.containerId()); cluster.setHostPort(hostPort); applyEndpoints(cluster, containerName, hostPort, info); + configureLinkLocalMetadataEndpoint(cluster, info.containerId()); LOG.infov("Adopted surviving k3s container {0} for EKS cluster {1} on port {2} (internal: {3})", info.containerId(), cluster.getName(), String.valueOf(hostPort), cluster.getInternalEndpoint()); @@ -365,6 +397,7 @@ public class EksClusterManager { * cluster's workloads survive a Floci restart and are re-latched by {@link #restoreCluster}. */ public void stopCluster(Cluster cluster) { + unregisterMetadataEndpoint(cluster); if (cluster.getContainerId() == null) { return; } @@ -691,8 +724,133 @@ public class EksClusterManager { """.formatted(serverUrl); } - private String execInContainer(String containerId, String[] cmd) throws Exception { - var dockerClient = lifecycleManager.getDockerClient(); + void configureLinkLocalMetadataEndpoint(Cluster cluster, String containerId) { + if (!config.services().eks().imds()) { + return; + } + try { + String accountId = cluster.getAccountId() != null + ? cluster.getAccountId() + : regionResolver.getAccountId(); + String region = clusterRegion(cluster); + + ContainerIps containerIps = resolveContainerIps(containerId); + Instance nodeInstance = synthesizeClusterNodeInstance(cluster, containerIps.primaryIp(), region, accountId); + clusterNodeInstances.put(clusterResourceName(cluster), nodeInstance); + + if (metadataServer != null) { + metadataServer.reconcileContainerAddresses(containerIps.allIps(), nodeInstance); + } + + ContainerExecResult install = execInContainerForResult(containerId, + Ec2MetadataProxy.installCommand(), 180); + if (install.exitCode() != 0) { + LOG.warnv("Could not install IMDS proxy dependencies for EKS cluster {0}: {1}", + cluster.getName(), install.summary()); + return; + } + + String flociHost = dockerHostResolver.resolve(); + int imdsPort = config.services().ec2().imdsPort(); + + ContainerExecResult start = execInContainerForResult(containerId, + Ec2MetadataProxy.startCommand(flociHost, imdsPort), 30); + if (start.exitCode() != 0) { + LOG.warnv("Could not start link-local IMDS proxy for EKS cluster {0}: {1}", + cluster.getName(), start.summary()); + return; + } + + 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}", + cluster.getName(), e.getMessage()); + } + } + + void unregisterMetadataEndpoint(Cluster cluster) { + Instance nodeInstance = clusterNodeInstances.remove(clusterResourceName(cluster)); + if (metadataServer != null && nodeInstance != null) { + metadataServer.unregisterInstance(nodeInstance); + } + } + + private String clusterRegion(Cluster cluster) { + if (cluster != null && cluster.getArn() != null) { + String[] parts = cluster.getArn().split(":"); + if (parts.length > 3 && !parts[3].isBlank()) { + return parts[3]; + } + } + return regionResolver.getDefaultRegion(); + } + + Instance synthesizeClusterNodeInstance(Cluster cluster, String containerIp, String region, String accountId) { + Instance inst = new Instance(); + String safeClusterName = cluster.getName() != null ? cluster.getName() : "eks-cluster"; + String safeAccountId = accountId != null ? accountId : config.defaultAccountId(); + String safeRegion = region != null ? region : clusterRegion(cluster); + + String seed = safeClusterName + "-" + safeAccountId + "-" + safeRegion; + String hex = UUID.nameUUIDFromBytes(seed.getBytes(StandardCharsets.UTF_8)).toString().replace("-", ""); + String instanceId = "i-" + (hex.length() >= 17 ? hex.substring(0, 17) : (hex + "00000000000000000").substring(0, 17)); + + inst.setInstanceId(instanceId); + inst.setImageId("ami-eks-k3s"); + inst.setInstanceType("m5.large"); + inst.setPlacement(new Placement(safeRegion + "a")); + inst.setRegion(safeRegion); + inst.setState(InstanceState.running()); + + String ip = (containerIp != null && !containerIp.isBlank()) ? containerIp : "10.0.0.1"; + inst.setPrivateIpAddress(ip); + inst.setPrivateDnsName("ip-" + ip.replace('.', '-') + "." + safeRegion + ".compute.internal"); + + // AWS EKS nodes receive credentials from a node IAM role through an EC2 instance profile, + // never from the cluster control-plane role (cluster.getRoleArn()). Synthesize a distinct + // node instance profile identity so /latest/meta-data/iam/info returns a valid profile ARN. + String nodeProfileName = safeClusterName + "-node-profile"; + inst.setIamInstanceProfileArn("arn:aws:iam::" + safeAccountId + ":instance-profile/" + nodeProfileName); + return inst; + } + + record ContainerIps(String primaryIp, Set allIps) {} + + ContainerIps resolveContainerIps(String containerId) { + Set ips = new LinkedHashSet<>(); + String preferred = null; + try { + InspectContainerResponse inspect = lifecycleManager.getDockerClient().inspectContainerCmd(containerId).exec(); + if (inspect.getNetworkSettings() != null) { + Map networks = inspect.getNetworkSettings().getNetworks(); + if (networks != null) { + preferred = Ec2MetadataProxy.preferredMetadataSourceIp(networks).orElse(null); + for (ContainerNetwork network : networks.values()) { + if (network != null && network.getIpAddress() != null && !network.getIpAddress().isBlank()) { + ips.add(network.getIpAddress()); + } + } + } + String ip = inspect.getNetworkSettings().getIpAddress(); + if (ip != null && !ip.isBlank()) { + ips.add(ip); + if (preferred == null) { + preferred = ip; + } + } + } + } catch (Exception e) { + LOG.warnv("Could not inspect container {0} for IPs: {1}", containerId, e.getMessage()); + } + return new ContainerIps(preferred != null ? preferred : "10.0.0.1", ips); + } + + Instance getRegisteredClusterNodeInstance(Cluster cluster) { + return clusterNodeInstances.get(clusterResourceName(cluster)); + } + + ContainerExecResult execInContainerForResult(String containerId, String[] cmd, int timeoutSeconds) throws Exception { + DockerClient dockerClient = lifecycleManager.getDockerClient(); ExecCreateCmdResponse exec = dockerClient .execCreateCmd(containerId) .withCmd(cmd) @@ -705,15 +863,32 @@ public class EksClusterManager { .exec(new ResultCallback.Adapter() { @Override public void onNext(Frame frame) { - output.append(new String(frame.getPayload(), StandardCharsets.UTF_8)); + if (frame != null && frame.getPayload() != null) { + output.append(new String(frame.getPayload(), StandardCharsets.UTF_8)); + } } }) - .awaitCompletion(10, TimeUnit.SECONDS); + .awaitCompletion(timeoutSeconds, TimeUnit.SECONDS); if (!completed) { + return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s"); + } + Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong(); + return new ContainerExecResult(exitCode != null ? exitCode : -1, output.toString()); + } + + record ContainerExecResult(long exitCode, String output) { + String summary() { + return output == null || output.isBlank() ? "(no output)" : output.trim(); + } + } + + private String execInContainer(String containerId, String[] cmd) throws Exception { + ContainerExecResult result = execInContainerForResult(containerId, cmd, 10); + if (result.exitCode() == -1 && result.output().startsWith("Timed out")) { throw new RuntimeException("exec timed out in container " + containerId); } - return output.toString(); + return result.output(); } private String extractYamlField(String yaml, String fieldName) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3f272adce3..6f870f6dcd 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -581,6 +581,7 @@ floci: iam-auth-webhook: true # validate `aws eks get-token` SigV4 tokens through a k3s webhook ecr-registry-mirror: true # inject a registries.yaml so pods can pull images pushed to Floci ECR disable-cni: false # start k3s without its bundled flannel/kube-proxy so a real CNI (e.g. Cilium) can take over + imds: false # expose link-local IMDS (169.254.169.254) inside the cluster container mwaa: enabled: true mock: false diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java index 3b349ef7ae..ffe0b40c4e 100644 --- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java +++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java @@ -39,6 +39,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -59,6 +60,7 @@ class EmulatorLifecycleTest { @Mock private EmulatorConfig.StorageConfig storageConfig; @Mock private EmulatorConfig.ServicesConfig servicesConfig; @Mock private EmulatorConfig.Ec2ServiceConfig ec2ServiceConfig; + @Mock private EmulatorConfig.EksServiceConfig eksServiceConfig; @Mock private EmulatorConfig.ElbV2ServiceConfig elbv2ServiceConfig; @Mock private EmulatorConfig.ElbServiceConfig elbServiceConfig; @Mock private IamService iamService; @@ -107,6 +109,8 @@ class EmulatorLifecycleTest { Mockito.lenient().when(elastiCacheServiceConfig.enabled()).thenReturn(false); Mockito.lenient().when(servicesConfig.elb()).thenReturn(elbServiceConfig); Mockito.lenient().when(elbServiceConfig.enabled()).thenReturn(false); + Mockito.lenient().when(servicesConfig.eks()).thenReturn(eksServiceConfig); + Mockito.lenient().when(eksServiceConfig.enabled()).thenReturn(false); Mockito.lenient().when(config.tls()).thenReturn(tlsConfig); Mockito.lenient().when(tlsConfig.enabled()).thenReturn(false); Mockito.lenient().when(config.port()).thenReturn(4566); @@ -574,4 +578,36 @@ class EmulatorLifecycleTest { assertEquals(1, messages.stream().filter("Ready."::equals).count(), "Exactly one parity \"Ready.\" line must be emitted on the hook path"); } + + @Test + @DisplayName("Should start and stop EC2 metadata server when EKS IMDS is enabled") + void shouldStartAndStopMetadataServerWhenEksImdsEnabled() { + stubStorageConfig(); + when(eksServiceConfig.enabled()).thenReturn(true); + when(eksServiceConfig.mock()).thenReturn(false); + when(eksServiceConfig.imds()).thenReturn(true); + when(ec2MetadataServer.start()).thenReturn(CompletableFuture.completedFuture(null)); + when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false); + when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false); + + emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class)); + verify(ec2MetadataServer).start(); + + emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class)); + verify(ec2MetadataServer).stop(); + } + + @Test + @DisplayName("Should not start EC2 metadata server when EKS IMDS is disabled") + void shouldNotStartMetadataServerWhenEksImdsDisabled() { + stubStorageConfig(); + when(eksServiceConfig.enabled()).thenReturn(true); + when(eksServiceConfig.mock()).thenReturn(false); + when(eksServiceConfig.imds()).thenReturn(false); + when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false); + when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false); + + emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class)); + verify(ec2MetadataServer, never()).start(); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataProxyTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataProxyTest.java new file mode 100644 index 0000000000..56f043938a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataProxyTest.java @@ -0,0 +1,74 @@ +package io.github.hectorvent.floci.services.ec2; + +import com.github.dockerjava.api.model.ContainerNetwork; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class Ec2MetadataProxyTest { + + @Test + void installCommandContainsSupportedPackageManagers() { + String[] command = Ec2MetadataProxy.installCommand(); + assertEquals(3, command.length); + assertEquals("sh", command[0]); + assertEquals("-c", command[1]); + + String script = command[2]; + assertTrue(script.contains("command -v ip >/dev/null 2>&1 && command -v socat >/dev/null 2>&1 && command -v curl >/dev/null 2>&1; then exit 0; fi")); + assertTrue(script.contains("apt-get install -y --no-install-recommends iproute2 socat curl ca-certificates")); + assertTrue(script.contains("dnf install -y --allowerasing iproute socat curl ca-certificates")); + assertTrue(script.contains("yum install -y iproute socat curl ca-certificates")); + assertTrue(script.contains("apk add --no-cache iproute2 socat curl ca-certificates")); + } + + @Test + void startCommandAttachesAddressIdempotentlyAndTargetsHostAndPort() { + String[] command = Ec2MetadataProxy.startCommand("10.0.0.1", 9169); + assertEquals(3, command.length); + assertEquals("sh", command[0]); + assertEquals("-c", command[1]); + + String script = command[2]; + // Attaches address if missing + assertTrue(script.contains("ip addr show dev lo | grep -q '169.254.169.254/32' || ip addr add 169.254.169.254/32 dev lo")); + // Idempotent when pid file exists and process is alive + assertTrue(script.contains("if [ -f /tmp/floci-imds-proxy.pid ] && kill -0 \"$(cat /tmp/floci-imds-proxy.pid)\" 2>/dev/null; then\n exit 0\nfi")); + // Targets configured Floci host and IMDS port + assertTrue(script.contains("nohup socat TCP-LISTEN:80,bind=169.254.169.254,fork,reuseaddr TCP:10.0.0.1:9169 >/tmp/floci-imds-proxy.log 2>&1 &")); + // Verifies via link-local curl + assertTrue(script.contains("curl -fsS --max-time 1 http://169.254.169.254/latest/meta-data/instance-id >/dev/null && exit 0")); + } + + @Test + void preferredMetadataSourceIpPrefersConfiguredNetworkOverBridge() { + ContainerNetwork bridge = new ContainerNetwork(); + bridge.withIpv4Address("172.17.0.2"); + ContainerNetwork vpc = new ContainerNetwork(); + vpc.withIpv4Address("10.0.0.2"); + + Optional ip = Ec2MetadataProxy.preferredMetadataSourceIp(Map.of("bridge", bridge, "custom-net", vpc)); + assertTrue(ip.isPresent()); + assertEquals("10.0.0.2", ip.get()); + } + + @Test + void preferredMetadataSourceIpFallsBackToBridge() { + ContainerNetwork bridge = new ContainerNetwork(); + bridge.withIpv4Address("172.17.0.2"); + + Optional ip = Ec2MetadataProxy.preferredMetadataSourceIp(Map.of("bridge", bridge)); + assertTrue(ip.isPresent()); + assertEquals("172.17.0.2", ip.get()); + } + + @Test + void preferredMetadataSourceIpReturnsEmptyWhenNoNetworks() { + assertTrue(Ec2MetadataProxy.preferredMetadataSourceIp(null).isEmpty()); + assertTrue(Ec2MetadataProxy.preferredMetadataSourceIp(Map.of()).isEmpty()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java index a41f734b64..76037be657 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java @@ -16,9 +16,21 @@ import io.github.hectorvent.floci.services.eks.model.Cluster; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; 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.ExecCreateCmd; +import com.github.dockerjava.api.command.ExecCreateCmdResponse; +import com.github.dockerjava.api.command.ExecStartCmd; +import com.github.dockerjava.api.command.InspectContainerCmd; +import com.github.dockerjava.api.command.InspectContainerResponse; +import com.github.dockerjava.api.command.InspectExecCmd; +import com.github.dockerjava.api.command.InspectExecResponse; import com.github.dockerjava.api.command.InspectVolumeCmd; import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.api.model.NetworkSettings; +import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer; +import io.github.hectorvent.floci.services.ec2.model.Instance; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -26,12 +38,17 @@ import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; 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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -598,4 +615,180 @@ class EksClusterManagerTest { verify(copyCmd).exec(); } } + + @Nested + class ConfigureLinkLocalMetadataEndpoint { + + private EmulatorConfig config; + private EmulatorConfig.EksServiceConfig eks; + private EmulatorConfig.Ec2ServiceConfig ec2; + private ContainerLifecycleManager lifecycleManager; + private DockerClient dockerClient; + private Ec2MetadataServer metadataServer; + private DockerHostResolver dockerHostResolver; + private RegionResolver regionResolver; + private EksClusterManager manager; + private ExecCreateCmd execCreate; + private List capturedCmds; + + @BeforeEach + void setUp() { + config = Mockito.mock(EmulatorConfig.class); + EmulatorConfig.ServicesConfig services = Mockito.mock(EmulatorConfig.ServicesConfig.class); + eks = Mockito.mock(EmulatorConfig.EksServiceConfig.class); + ec2 = Mockito.mock(EmulatorConfig.Ec2ServiceConfig.class); + when(config.services()).thenReturn(services); + when(config.defaultAccountId()).thenReturn("000000000000"); + when(services.eks()).thenReturn(eks); + when(services.ec2()).thenReturn(ec2); + when(ec2.imdsPort()).thenReturn(9169); + when(eks.imds()).thenReturn(true); + + lifecycleManager = Mockito.mock(ContainerLifecycleManager.class); + dockerClient = Mockito.mock(DockerClient.class); + when(lifecycleManager.getDockerClient()).thenReturn(dockerClient); + + metadataServer = Mockito.mock(Ec2MetadataServer.class); + dockerHostResolver = Mockito.mock(DockerHostResolver.class); + when(dockerHostResolver.resolve()).thenReturn("floci-host"); + + regionResolver = Mockito.mock(RegionResolver.class); + when(regionResolver.getAccountId()).thenReturn("000000000000"); + when(regionResolver.getDefaultRegion()).thenReturn("us-east-1"); + + capturedCmds = new ArrayList<>(); + execCreate = Mockito.mock(ExecCreateCmd.class, Mockito.withSettings().defaultAnswer(Mockito.RETURNS_SELF)); + ExecCreateCmdResponse execResponse = Mockito.mock(ExecCreateCmdResponse.class); + when(execResponse.getId()).thenReturn("exec-123"); + when(dockerClient.execCreateCmd(anyString())).thenReturn(execCreate); + when(execCreate.withCmd(any(String[].class))).thenAnswer(inv -> { + Object[] args = inv.getArguments(); + if (args.length == 1 && args[0] instanceof String[] command) { + capturedCmds.add(command); + } else { + capturedCmds.add(Arrays.copyOf(args, args.length, String[].class)); + } + return execCreate; + }); + when(execCreate.exec()).thenReturn(execResponse); + + ExecStartCmd execStart = Mockito.mock(ExecStartCmd.class); + when(dockerClient.execStartCmd(anyString())).thenReturn(execStart); + when(execStart.exec(any())).thenAnswer(inv -> { + ResultCallback cb = inv.getArgument(0); + cb.onComplete(); + return cb; + }); + + InspectExecCmd inspectExec = Mockito.mock(InspectExecCmd.class); + InspectExecResponse inspectResponse = Mockito.mock(InspectExecResponse.class); + when(inspectResponse.getExitCodeLong()).thenReturn(0L); + when(inspectExec.exec()).thenReturn(inspectResponse); + when(dockerClient.inspectExecCmd(anyString())).thenReturn(inspectExec); + + InspectContainerCmd inspectContainer = Mockito.mock(InspectContainerCmd.class); + InspectContainerResponse containerResponse = Mockito.mock(InspectContainerResponse.class); + NetworkSettings netSettings = Mockito.mock(NetworkSettings.class); + when(inspectContainer.exec()).thenReturn(containerResponse); + when(dockerClient.inspectContainerCmd(anyString())).thenReturn(inspectContainer); + when(containerResponse.getNetworkSettings()).thenReturn(netSettings); + when(netSettings.getIpAddress()).thenReturn("172.17.0.2"); + + manager = new EksClusterManager( + Mockito.mock(ContainerBuilder.class), lifecycleManager, + Mockito.mock(ContainerDetector.class), Mockito.mock(PortAllocator.class), + dockerHostResolver, Mockito.mock(EcrRegistryManager.class), + config, regionResolver, metadataServer); + } + + @Test + void synthesizesClusterNodeInstance() { + Cluster cluster = new Cluster(); + cluster.setName("prod-cluster"); + cluster.setRoleArn("arn:aws:iam::123456789012:role/eks-node-role"); + + Instance instance = manager.synthesizeClusterNodeInstance(cluster, "172.17.0.2", "us-west-2", "123456789012"); + assertNotNull(instance); + assertTrue(instance.getInstanceId().startsWith("i-")); + assertTrue(instance.getInstanceId().length() >= 19); + assertEquals("ami-eks-k3s", instance.getImageId()); + assertEquals("m5.large", instance.getInstanceType()); + assertEquals("us-west-2a", instance.getPlacement().getAvailabilityZone()); + assertEquals("us-west-2", instance.getRegion()); + assertEquals("172.17.0.2", instance.getPrivateIpAddress()); + assertEquals("ip-172-17-0-2.us-west-2.compute.internal", instance.getPrivateDnsName()); + assertEquals("arn:aws:iam::123456789012:instance-profile/prod-cluster-node-profile", instance.getIamInstanceProfileArn()); + assertNotEquals(cluster.getRoleArn(), instance.getIamInstanceProfileArn()); + assertEquals("running", instance.getState().getName()); + } + + @Test + void sameNameClustersInDifferentRegionsGetDistinctInstanceIds() { + Cluster cluster = new Cluster(); + cluster.setName("prod-cluster"); + + Instance inst1 = manager.synthesizeClusterNodeInstance(cluster, "172.17.0.2", "us-east-1", "123456789012"); + Instance inst2 = manager.synthesizeClusterNodeInstance(cluster, "172.17.0.2", "us-west-2", "123456789012"); + + assertNotNull(inst1); + assertNotNull(inst2); + assertNotEquals(inst1.getInstanceId(), inst2.getInstanceId()); + } + + @Test + void configuresMetadataProxyWhenEnabled() { + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + + manager.configureLinkLocalMetadataEndpoint(cluster, "container-42"); + + verify(metadataServer).reconcileContainerAddresses(any(), any()); + assertEquals(2, capturedCmds.size()); + // First command: install probe + assertTrue(capturedCmds.get(0)[2].contains("command -v socat")); + // Second command: start command with 169.254.169.254 + assertTrue(capturedCmds.get(1)[2].contains("169.254.169.254")); + assertTrue(capturedCmds.get(1)[2].contains("TCP:floci-host:9169")); + + assertNotNull(manager.getRegisteredClusterNodeInstance(cluster)); + } + + @Test + void skipsWhenImdsIsDisabled() { + when(eks.imds()).thenReturn(false); + + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + + manager.configureLinkLocalMetadataEndpoint(cluster, "container-42"); + + verifyNoInteractions(metadataServer); + verifyNoInteractions(dockerClient); + assertNull(manager.getRegisteredClusterNodeInstance(cluster)); + } + + @Test + void failureToWireLogsAndDoesNotAbort() { + when(dockerClient.execCreateCmd(anyString())).thenThrow(new RuntimeException("docker exec failed")); + + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + + // Failure to wire proxy should log warning and continue without throwing + manager.configureLinkLocalMetadataEndpoint(cluster, "container-42"); + } + + @Test + void unregisterMetadataEndpointRemovesInstance() { + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + + manager.configureLinkLocalMetadataEndpoint(cluster, "container-42"); + assertNotNull(manager.getRegisteredClusterNodeInstance(cluster)); + + manager.unregisterMetadataEndpoint(cluster); + verify(metadataServer).unregisterInstance(any()); + assertNull(manager.getRegisteredClusterNodeInstance(cluster)); + } + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.java new file mode 100644 index 0000000000..e9a7361848 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.java @@ -0,0 +1,199 @@ +package io.github.hectorvent.floci.services.eks; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.async.ResultCallback; +import com.github.dockerjava.api.command.ExecCreateCmdResponse; +import com.github.dockerjava.api.model.Frame; +import io.github.hectorvent.floci.config.EmulatorConfig; +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.services.ec2.Ec2MetadataServer; +import io.github.hectorvent.floci.services.eks.model.Cluster; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +@TestProfile(EksImdsDockerIntegrationTest.Profile.class) +class EksImdsDockerIntegrationTest { + + public static final class Profile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("floci.services.eks.imds", "true"); + } + } + + private static final Logger LOG = Logger.getLogger(EksImdsDockerIntegrationTest.class); + private static final String TEST_IMAGE = "alpine:3.21"; + + @Inject + DockerClient dockerClient; + + @Inject + ContainerBuilder containerBuilder; + + @Inject + ContainerLifecycleManager lifecycleManager; + + @Inject + EmulatorConfig config; + + @Inject + Ec2MetadataServer metadataServer; + + @Inject + EksClusterManager eksClusterManager; + + private String containerId; + private Cluster cluster; + + @BeforeEach + void requireDockerAndStartMetadataServer() { + boolean dockerAvailable = isDockerAvailable(); + if (!dockerAvailable) { + LOG.warn("Docker daemon is not available; skipping EksImdsDockerIntegrationTest"); + } + Assumptions.assumeTrue(dockerAvailable, "Docker daemon must be available for EKS IMDS integration test"); + metadataServer.start().join(); + } + + @AfterEach + void tearDown() { + if (cluster != null) { + eksClusterManager.unregisterMetadataEndpoint(cluster); + } + if (containerId != null) { + try { + lifecycleManager.stopAndRemove(containerId, null); + } catch (Exception ignored) { + } + } + } + + @Test + void linkLocalImdsEndpointAnswersInsideClusterContainer() throws Exception { + String clusterName = "imds-it-" + UUID.randomUUID().toString().substring(0, 8); + cluster = new Cluster(); + cluster.setName(clusterName); + cluster.setRoleArn("arn:aws:iam::000000000000:role/eks-it-role"); + + ContainerSpec spec = containerBuilder.newContainer(TEST_IMAGE) + .withName("floci-eks-imds-test-" + clusterName) + .withPrivileged(true) + .withHostDockerInternalOnLinux() + .withCmd(List.of("sleep", "300")) + .build(); + + containerId = lifecycleManager.createAndStart(spec).containerId(); + assertNotNull(containerId, "Container ID must not be null"); + + // Ensure curl dependency is installed in the test fixture if absent + execInContainer(containerId, new String[]{"sh", "-c", + "command -v curl >/dev/null 2>&1 || apk add --no-cache curl"}); + + eksClusterManager.configureLinkLocalMetadataEndpoint(cluster, containerId); + + // IMDSv2 token test + String tokenCmd = "curl -s -f -X PUT http://169.254.169.254/latest/api/token -H 'x-aws-ec2-metadata-token-ttl-seconds: 21600'"; + String token = execInContainer(containerId, new String[]{"sh", "-c", tokenCmd}); + assertNotNull(token, "IMDSv2 token response should not be null"); + assertTrue(!token.isBlank(), "IMDSv2 token should not be blank"); + + // IMDSv2 instance-id metadata test + String instanceIdCmd = "curl -s -f -H 'x-aws-ec2-metadata-token: " + token.trim() + "' http://169.254.169.254/latest/meta-data/instance-id"; + String instanceId = execInContainer(containerId, new String[]{"sh", "-c", instanceIdCmd}); + assertNotNull(instanceId, "instance-id response should not be null"); + assertTrue(instanceId.trim().startsWith("i-"), "Instance ID should start with 'i-': " + instanceId); + + // IMDSv1 fallback test using wget (tool guaranteed by alpine:3.21 image) + String imdsv1Cmd = "wget -q -O - http://169.254.169.254/latest/meta-data/instance-id"; + String v1InstanceId = execInContainer(containerId, new String[]{"sh", "-c", imdsv1Cmd}); + assertEquals(instanceId.trim(), v1InstanceId.trim(), "IMDSv1 and IMDSv2 instance IDs should match"); + + // Pod-isolation test: ordinary pods in their own network namespace cannot reach link-local IMDS + // Tested using wget (tool guaranteed by alpine:3.21 image) + String podIsolationCmd = """ + if command -v unshare >/dev/null 2>&1; then + unshare -n wget -q -O - -T 2 http://169.254.169.254/latest/meta-data/instance-id + else + ip netns add pod-test 2>/dev/null || true + ip netns exec pod-test wget -q -O - -T 2 http://169.254.169.254/latest/meta-data/instance-id + ret=$? + ip netns del pod-test 2>/dev/null || true + exit $ret + fi + """; + ExecResult podResult = execInContainerWithExitCode(containerId, new String[]{"sh", "-c", podIsolationCmd}); + assertNotEquals(0L, podResult.exitCode(), "IMDS should not be reachable from an isolated pod network namespace"); + } + + private boolean isDockerAvailable() { + try { + dockerClient.pingCmd().exec(); + return true; + } catch (Exception e) { + return false; + } + } + + record ExecResult(long exitCode, String stdout, String stderr) {} + + private ExecResult execInContainerWithExitCode(String containerId, String[] cmd) throws Exception { + ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId) + .withCmd(cmd) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); + + StringBuilder stdout = new StringBuilder(); + StringBuilder stderr = new StringBuilder(); + boolean completed = dockerClient.execStartCmd(exec.getId()) + .exec(new ResultCallback.Adapter() { + @Override + public void onNext(Frame frame) { + if (frame != null && frame.getPayload() != null) { + String text = new String(frame.getPayload(), StandardCharsets.UTF_8); + if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDERR) { + stderr.append(text); + } else { + stdout.append(text); + } + } + } + }) + .awaitCompletion(30, TimeUnit.SECONDS); + + if (!completed) { + throw new RuntimeException("exec timed out in container " + containerId); + } + Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong(); + return new ExecResult(exitCode != null ? exitCode : -1L, stdout.toString(), stderr.toString()); + } + + private String execInContainer(String containerId, String[] cmd) throws Exception { + ExecResult result = execInContainerWithExitCode(containerId, cmd); + if (result.exitCode() != 0) { + throw new RuntimeException("exec failed with code " + result.exitCode() + ": " + result.stderr()); + } + return result.stdout(); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 41858aa365..da90ed2ece 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -303,6 +303,7 @@ floci: enabled: true mock: true ecr-registry-mirror: true + imds: false mwaa: enabled: true mock: true