diff --git a/docs/services/eks.md b/docs/services/eks.md index 43a39fee6a..6bb55af6ba 100644 --- a/docs/services/eks.md +++ b/docs/services/eks.md @@ -393,7 +393,7 @@ Cluster metadata is stored in-process. No Docker containers are started. The clu Floci starts a **k3s** (`rancher/k3s`) container for each cluster. The k3s API server is exposed on a host port from the configured range (`6500-6599`). Once `/readyz` responds, the cluster transitions to `ACTIVE` and the CA certificate is extracted from the kubeconfig. -By default `describe-cluster` returns a **host-reachable** endpoint (`https://localhost:`); the k3s server certificate includes a `localhost` SAN, so it verifies against the CA in `cluster.certificateAuthority.data`. Set `endpoint-mode: network` to return the container DNS name (`https://floci-eks-:6443`) instead: reachable from other containers on the Docker network (the pre-#1118 behaviour). In `network` mode the endpoint falls back to the host-reachable form when Floci runs natively, since there is no container DNS name a host client could use. +By default `describe-cluster` returns a **host-reachable** endpoint (`https://localhost:`); the k3s server certificate includes a `localhost` SAN, so it verifies against the CA in `cluster.certificateAuthority.data`. Set `endpoint-mode: network` to return the container DNS name (`https://floci-eks-:6443`) instead: reachable from other containers on the Docker network (the pre-#1118 behaviour). Set `endpoint-host` to advertise a hostname other than `localhost` in `host` mode, for example when `kubectl` runs on another machine; Floci adds it to the k3s certificate too. It takes a DNS name (letters, digits, hyphens and dots), a dotted IPv4 address, or an IPv6 address with or without brackets, without scheme, port or path; anything else stops Floci at startup with an error naming the setting, whenever EKS runs real clusters (even in `network` mode). The certificate is fixed when the k3s container is created, so when `endpoint-host` changes, Floci recreates a surviving container on its next restart (keeping its data volume and port). If that replacement fails, the old container is kept and keeps advertising the host its certificate carries. `network` mode ignores it when Floci runs in a container, and metadata-only clusters (mock mode, or no Docker daemon) do not use it. In `network` mode the endpoint falls back to the host-reachable form when Floci runs natively, since there is no container DNS name a host client could use. #### Connecting with `kubectl` (native AWS workflow) @@ -586,6 +586,7 @@ When an instance joins a cluster, Floci registers a Kubernetes `Node` matching A | `FLOCI_SERVICES_EKS_DOCKER_NETWORK` | *(unset)* | Docker network for k3s containers (falls back to the global `FLOCI_SERVICES_DOCKER_NETWORK`, then Floci's own network) | | `FLOCI_SERVICES_EKS_KEEP_RUNNING_ON_SHUTDOWN` | `false` | Leave k3s containers running after Floci stops | | `FLOCI_SERVICES_EKS_ENDPOINT_MODE` | `host` | `describe-cluster` endpoint: `host` (`localhost:`) or `network` (container DNS) | +| `FLOCI_SERVICES_EKS_ENDPOINT_HOST` | _(unset)_ | Hostname or IP address `host` mode advertises instead of `localhost`, for clients on other machines. Added to the k3s certificate as a SAN when the cluster's container is created | | `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_DISABLE_CNI` | `false` | Start k3s without bundled flannel, network policy, and kube-proxy so an external CNI can take over | 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 3f9cd7a212..9405bd460a 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -3541,6 +3541,13 @@ public interface EmulatorConfig { @WithDefault("host") String endpointMode(); + /** + * Hostname advertised for {@code endpoint-mode=host} endpoints instead of {@code localhost}, for + * clients on other machines. It is also added to the k3s server certificate as a SAN, so the + * endpoint still verifies against the cluster CA. + */ + Optional endpointHost(); + /** * When true, wires a token-authentication webhook into k3s so that the bearer token * produced by {@code aws eks get-token} is validated by Floci and mapped to cluster-admin. 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 064b5ddd34..271a520d28 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 @@ -69,6 +69,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.Inet4Address; +import java.net.Inet6Address; import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -94,6 +96,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.BiFunction; import java.util.function.Consumer; +import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -110,6 +113,9 @@ public class EksClusterManager static final String DEFAULT_NODE_INSTANCE_TYPE = "m5.large"; private static final String NODE_CAPACITY_LABEL = "io.floci.eks.node-capacity"; private static final String DEFAULT_STORAGE_CLASS_LABEL = "io.floci.eks.default-storage-class"; + private static final String ENDPOINT_HOST_LABEL = "io.floci.eks.endpoint-host"; + private static final Pattern HOSTNAME = Pattern.compile( + "[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*"); private static final String WEBHOOK_CONFIG_DIR = "/etc"; private static final String WEBHOOK_CONFIG_FILE = "token-webhook.yaml"; @@ -595,6 +601,10 @@ public class EksClusterManager defaultStorageClass, serviceCidr, clusterCidr); + String endpointHost = endpointHost(); + if (!"localhost".equals(endpointHost)) { + serverArgs.add("--tls-san=" + endpointHost); + } EksNodeCapacity.Limits nodeLimits = resolveNodeCapacity(cluster); if (nodeLimits != null) { @@ -644,6 +654,9 @@ public class EksClusterManager "eks", cluster.getName(), labelAccountId, clusterRegion(cluster))); labels.put(NODE_CAPACITY_LABEL, capacityLabel(cluster, nodeLimits)); labels.put(DEFAULT_STORAGE_CLASS_LABEL, String.valueOf(defaultStorageClass)); + if (!"localhost".equals(endpointHost)) { + labels.put(ENDPOINT_HOST_LABEL, endpointHost); + } ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image) .withName(containerName) .withEnv("K3S_KUBECONFIG_MODE", "644") @@ -760,7 +773,7 @@ public class EksClusterManager throw e; } - applyEndpoints(cluster, containerName, hostPort, info); + applyEndpoints(cluster, containerName, hostPort, info, endpointHost); registerClusterNodeInstance(cluster, containerId); configureLinkLocalMetadataEndpoint(cluster, containerId); configurePodIdentityRelay(cluster, containerId); @@ -783,6 +796,7 @@ public class EksClusterManager * server and re-extracts the certificate authority before marking it ACTIVE again. */ public void restoreCluster(Cluster cluster) { + String endpointHost = endpointHost(); if (cluster.getDockerName() == null) { cluster.setDockerName(resolveRestoredDockerName(cluster)); } @@ -814,11 +828,12 @@ public class EksClusterManager cluster.setDefaultStorageClass(true); LOG.infov("Adopting pre-upgrade EKS cluster {0} with local-storage enabled", cluster.getName()); } - if (!adoptSurvivingCluster(cluster, existing.get().getId())) { + if (!adoptSurvivingCluster(cluster, existing.get().getId(), certificateHost(existingLabels))) { startCluster(cluster); return; } - if (capacityChanged || onAnotherNetwork(existing.get())) { + if (capacityChanged || onAnotherNetwork(existing.get()) + || !endpointHost.equals(certificateHost(existingLabels))) { replaceClusterContainer(cluster, null); } } @@ -842,7 +857,8 @@ public class EksClusterManager return moved; } - private boolean adoptSurvivingCluster(Cluster cluster, String containerId) { + /** An adopted container keeps advertising the host its certificate was created for. */ + private boolean adoptSurvivingCluster(Cluster cluster, String containerId, String certificateHost) { if (config.services().eks().irsaSigningKey() && oidcService != null) { reinjectSigningKeys(containerId, cluster); } @@ -869,7 +885,7 @@ public class EksClusterManager portAllocator.markReserved(hostPort); cluster.setContainerId(info.containerId()); cluster.setHostPort(hostPort); - applyEndpoints(cluster, cluster.getDockerName(), hostPort, info); + applyEndpoints(cluster, cluster.getDockerName(), hostPort, info, certificateHost); registerClusterNodeInstance(cluster, info.containerId()); configureLinkLocalMetadataEndpoint(cluster, info.containerId()); configurePodIdentityRelay(cluster, info.containerId()); @@ -888,6 +904,8 @@ public class EksClusterManager int oldPort = cluster.getHostPort(); String containerName = cluster.getDockerName(); String backupName = capacityBackupName(cluster); + String oldCertificateHost = certificateHost(lifecycleManager.findByName(containerName) + .map(Container::getLabels).orElse(null)); DockerClient docker = lifecycleManager.getDockerClient(); try { lifecycleManager.removeIfExistsStrict(backupName); @@ -917,7 +935,7 @@ public class EksClusterManager try { lifecycleManager.removeIfExistsStrict(containerName); docker.renameContainerCmd(oldId).withName(containerName).exec(); - if (!adoptSurvivingCluster(cluster, oldId)) { + if (!adoptSurvivingCluster(cluster, oldId, oldCertificateHost)) { throw new IllegalStateException("Could not adopt previous EKS container " + oldId); } } catch (RuntimeException rollback) { @@ -952,15 +970,17 @@ public class EksClusterManager /** * Sets the cluster's public and internal endpoints for a started or adopted container. * Public endpoint: see floci.services.eks.endpoint-mode. `host` (default) is the host-reachable - * published port (k3s cert carries `--tls-san=localhost`, so it verifies against the CA that - * describe-cluster returns); `network` is the container DNS name (pre-#1118 behaviour). + * published port on floci.services.eks.endpoint-host, or localhost (the k3s cert carries a SAN for + * either, so it verifies against the CA that describe-cluster returns); `network` is the container + * DNS name (pre-#1118 behaviour). * The internal endpoint uses the resolved container IP so the readiness poller works from inside * the Docker network (where localhost: would not reach the k3s container). */ - private void applyEndpoints(Cluster cluster, String containerName, int hostPort, ContainerInfo info) { + private void applyEndpoints(Cluster cluster, String containerName, int hostPort, ContainerInfo info, + String endpointHost) { cluster.setEndpoint(resolvePublicEndpoint( containerDetector.isRunningInContainer(), config.services().eks().endpointMode(), - containerName, hostPort)); + containerName, endpointHost, hostPort)); if (containerDetector.isRunningInContainer()) { ContainerLifecycleManager.EndpointInfo ep = info.getEndpoint(K3S_API_SERVER_PORT); @@ -1554,15 +1574,79 @@ public class EksClusterManager /** * Resolves the public {@code describe-cluster} endpoint. Returns the container DNS name only when - * Floci runs in a container and {@code endpoint-mode=network}; otherwise the host-reachable - * published port (the default, and the only usable value in native mode). + * Floci runs in a container and {@code endpoint-mode=network}; otherwise the published port on + * {@code endpointHost} (the default, and the only usable value in native mode). */ static String resolvePublicEndpoint(boolean inContainer, String endpointMode, - String containerName, int hostPort) { + String containerName, String endpointHost, int hostPort) { if (inContainer && ENDPOINT_MODE_NETWORK.equalsIgnoreCase(endpointMode)) { return "https://" + containerName + ":" + K3S_API_SERVER_PORT; } - return "https://localhost:" + hostPort; + String host = endpointHost.indexOf(':') >= 0 ? "[" + endpointHost + "]" : endpointHost; + return "https://" + host + ":" + hostPort; + } + + /** + * The host a container's certificate is made for and host-mode endpoints advertise. Network mode + * advertises the container name instead, so there the setting is ignored and this is localhost. + * {@link EksStartupValidator} has already refused an invalid value at boot. + */ + private String endpointHost() { + if (containerDetector.isRunningInContainer() + && ENDPOINT_MODE_NETWORK.equalsIgnoreCase(config.services().eks().endpointMode())) { + return "localhost"; + } + return endpointHostFrom(config.services().eks().endpointHost()); + } + + /** + * The configured endpoint host as a bare hostname or IP address, the form a {@code --tls-san} takes. + * A bracketed IPv6 literal is unbracketed; a scheme, port or path is refused, since the endpoint + * adds its own. + */ + static String endpointHostFrom(Optional configured) { + String host = configured.map(String::strip).filter(value -> !value.isEmpty()).orElse("localhost") + .toLowerCase(Locale.ROOT); + if (host.startsWith("[") && host.endsWith("]")) { + host = host.substring(1, host.length() - 1); + } + boolean numeric = host.chars().allMatch(c -> c == '.' || Character.isDigit(c)); + if (numeric ? isIpv4Literal(host) : host.length() <= 253 && HOSTNAME.matcher(host).matches() + || isIpv6Literal(host)) { + return host; + } + throw new IllegalArgumentException("floci.services.eks.endpoint-host must be a hostname or IP address" + + " without scheme, port or path, got: " + configured.orElse("")); + } + + /** The endpoint host a container's certificate carries, from the label {@link #startCluster} sets. */ + private static String certificateHost(Map labels) { + return labels == null ? "localhost" : labels.getOrDefault(ENDPOINT_HOST_LABEL, "localhost"); + } + + private static boolean isIpv4Literal(String host) { + // ofLiteral also takes BSD shorthand such as 1.2.3, which no URL or certificate means. + if (host.split("\\.", -1).length != 4) { + return false; + } + try { + Inet4Address.ofLiteral(host); + return true; + } catch (IllegalArgumentException e) { + return false; + } + } + + private static boolean isIpv6Literal(String host) { + if (host.indexOf(':') < 0 || host.indexOf('%') >= 0 || host.indexOf('[') >= 0 || host.indexOf(']') >= 0) { + return false; + } + try { + Inet6Address.ofLiteral(host); + return true; + } catch (IllegalArgumentException e) { + return false; + } } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksStartupValidator.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksStartupValidator.java new file mode 100644 index 0000000000..36b8ad1605 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksStartupValidator.java @@ -0,0 +1,31 @@ +package io.github.hectorvent.floci.services.eks; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.quarkus.runtime.StartupEvent; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; +import jakarta.interceptor.Interceptor; + +/** + * Refuses to start Floci with an invalid {@code floci.services.eks.endpoint-host}. Checked once here, before + * persisted clusters are restored, so a typo stops the boot instead of failing every cluster on restore. + * Metadata-only EKS (disabled or mock) never uses the setting, so it is not checked there. + */ +@ApplicationScoped +public class EksStartupValidator { + + private final EmulatorConfig config; + + @Inject + public EksStartupValidator(EmulatorConfig config) { + this.config = config; + } + + void onStart(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent ignored) { + if (config.services().eks().enabled() && !config.services().eks().mock()) { + EksClusterManager.endpointHostFrom(config.services().eks().endpointHost()); + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 70c57b7802..fa389a7869 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -800,6 +800,7 @@ floci: data-path: ./data/eks keep-running-on-shutdown: false endpoint-mode: host # host = https://localhost: (host-reachable); network = container DNS name + # endpoint-host: aws.example.test # host mode advertises this instead of localhost; added to the k3s cert 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 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 d24e94dd67..90da0c86ab 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 @@ -157,27 +157,71 @@ class EksClusterManagerTest { @Test void hostModeAlwaysReturnsHostReachableEndpoint() { assertEquals("https://localhost:6500", - EksClusterManager.resolvePublicEndpoint(true, "host", "floci-eks-demo", 6500)); + EksClusterManager.resolvePublicEndpoint(true, "host", "floci-eks-demo", "localhost", 6500)); assertEquals("https://localhost:6500", - EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", 6500)); + EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "localhost", 6500)); + } + + @Test + void hostModeAdvertisesAnIpv4EndpointHostAsIs() { + assertEquals("https://192.168.8.24:6500", + EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "192.168.8.24", 6500)); + } + + @Test + void hostModeBracketsAnIpv6EndpointHost() { + assertEquals("https://[fd00::24]:6500", + EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "fd00::24", 6500)); + } + + @Test + void endpointHostAcceptsHostnamesAndIpAddresses() { + assertEquals("localhost", EksClusterManager.endpointHostFrom(Optional.empty())); + assertEquals("localhost", EksClusterManager.endpointHostFrom(Optional.of(" "))); + assertEquals("aws.example.test", EksClusterManager.endpointHostFrom(Optional.of(" aws.example.test "))); + assertEquals("192.168.8.24", EksClusterManager.endpointHostFrom(Optional.of("192.168.8.24"))); + assertEquals("fd00::24", EksClusterManager.endpointHostFrom(Optional.of("fd00::24"))); + assertEquals("fd00::24", EksClusterManager.endpointHostFrom(Optional.of("[fd00::24]"))); + assertEquals("localhost", EksClusterManager.endpointHostFrom(Optional.of("LOCALHOST"))); + assertEquals("a".repeat(63) + ".test", EksClusterManager.endpointHostFrom(Optional.of("a".repeat(63) + ".test"))); + assertEquals("aws-1.example.test", EksClusterManager.endpointHostFrom(Optional.of("AWS-1.Example.Test"))); + } + + @Test + void endpointHostRejectsAUrlOrAPort() { + for (String value : List.of("https://aws.example.test", "aws.example.test:443", "aws.example.test/eks", + "aws example.test", "[fd00::24]:6443", "a.example.test,b.example.test", "[fd00::24", "fd00::24%eth0", + "user@aws.example.test", "aws.example.test?x", "aws.example.test#x", "-aws.example.test", "[[::1]]", + "999.1.1.1", "1.2.3", "my_host", "a".repeat(64) + ".example.test", + ("a".repeat(60) + ".").repeat(5) + "test")) { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EksClusterManager.endpointHostFrom(Optional.of(value)), value); + assertTrue(e.getMessage().contains("floci.services.eks.endpoint-host"), e.getMessage()); + } + } + + @Test + void hostModeAdvertisesTheConfiguredEndpointHost() { + assertEquals("https://aws.example.test:6500", + EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "aws.example.test", 6500)); } @Test void networkModeReturnsContainerDnsOnlyInContainer() { assertEquals("https://floci-eks-demo:6443", - EksClusterManager.resolvePublicEndpoint(true, "network", "floci-eks-demo", 6500)); + EksClusterManager.resolvePublicEndpoint(true, "network", "floci-eks-demo", "localhost", 6500)); // Native mode has no usable container DNS name — falls back to the host endpoint. assertEquals("https://localhost:6500", - EksClusterManager.resolvePublicEndpoint(false, "network", "floci-eks-demo", 6500)); + EksClusterManager.resolvePublicEndpoint(false, "network", "floci-eks-demo", "localhost", 6500)); } @Test void endpointModeIsCaseInsensitiveAndDefaultsToHost() { assertEquals("https://floci-eks-demo:6443", - EksClusterManager.resolvePublicEndpoint(true, "NETWORK", "floci-eks-demo", 6500)); + EksClusterManager.resolvePublicEndpoint(true, "NETWORK", "floci-eks-demo", "localhost", 6500)); // Unknown / unset modes behave as host. assertEquals("https://localhost:6500", - EksClusterManager.resolvePublicEndpoint(true, "bogus", "floci-eks-demo", 6500)); + EksClusterManager.resolvePublicEndpoint(true, "bogus", "floci-eks-demo", "localhost", 6500)); } @Test @@ -702,6 +746,108 @@ class EksClusterManagerTest { .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); } + @Test + void recreatesASurvivorWhoseCertificateLacksTheEndpointHost() { + when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test")); + when(lifecycleManager.findByName("floci-eks-demo")) + .thenReturn(Optional.of(survivingContainer("cid-old"))); + when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); + stubFreshStart("cid-new", 6440); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-new", cluster.getContainerId()); + assertEquals("https://aws.example.test:6440", cluster.getEndpoint()); + verify(dockerClient).stopContainerCmd("cid-old"); + } + + @Test + void adoptsASurvivorCreatedForTheConfiguredEndpointHost() { + when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test")); + when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson( + "{\"Id\":\"cid-1\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large:unbounded\"," + + "\"io.floci.eks.endpoint-host\":\"aws.example.test\"}}"))); + when(lifecycleManager.adopt(eq("cid-1"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512))); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-1", cluster.getContainerId()); + assertEquals("https://aws.example.test:6512", cluster.getEndpoint()); + verify(lifecycleManager, never()).create(any()); + } + + @Test + void recreatesASurvivorCreatedForAnotherEndpointHostWhenTheHostIsUnset() { + when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson( + "{\"Id\":\"cid-old\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large:unbounded\"," + + "\"io.floci.eks.endpoint-host\":\"aws.example.test\"}}"))); + when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); + stubFreshStart("cid-new", 6440); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-new", cluster.getContainerId()); + assertEquals("https://localhost:6440", cluster.getEndpoint()); + } + + @Test + void aFailedReplacementKeepsAdvertisingTheHostTheOldCertificateCarries() { + when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test")); + when(lifecycleManager.findByName("floci-eks-demo")) + .thenReturn(Optional.of(survivingContainer("cid-old"))); + when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); + when(lifecycleManager.create(any())).thenReturn("cid-new"); + when(lifecycleManager.startCreated(any(), any())) + .thenThrow(new RuntimeException("replacement failed")); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-old", cluster.getContainerId()); + assertEquals("https://localhost:6440", cluster.getEndpoint()); + } + + @Test + void replacesASurvivorMadeForTheEndpointHostWhenItsCapacityChanged() { + when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test")); + when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson( + "{\"Id\":\"cid-old\",\"Labels\":{\"io.floci.eks.node-capacity\":\"t3.small:unbounded\"," + + "\"io.floci.eks.endpoint-host\":\"aws.example.test\"}}"))); + when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); + stubFreshStart("cid-new", 6440); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-new", cluster.getContainerId()); + assertEquals("https://aws.example.test:6440", cluster.getEndpoint()); + } + + @Test + void networkModeIgnoresTheEndpointHost() { + when(containerDetector.isRunningInContainer()).thenReturn(true); + when(config.services().eks().endpointMode()).thenReturn("network"); + when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test")); + when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson( + "{\"Id\":\"cid-1\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large:unbounded\"}}"))); + when(lifecycleManager.adopt(eq("cid-1"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512))); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-1", cluster.getContainerId()); + verify(lifecycleManager, never()).create(any()); + } + @Test void recreatesAnOldSurvivorWithoutCapacityLimits() { when(lifecycleManager.findByName("floci-eks-demo")) @@ -2231,6 +2377,87 @@ class EksClusterManagerTest { verify(copyCmd, atLeastOnce()).exec(); } + @Test + void startClusterAddsTheConfiguredEndpointHostToTheCertificate(@TempDir Path tempDir) { + when(eks.irsaSigningKey()).thenReturn(false); + when(eks.dataPath()).thenReturn(tempDir.toString()); + when(eks.endpointHost()).thenReturn(Optional.of("aws.example.test")); + + Cluster cluster = new Cluster(); + cluster.setName("my-cluster"); + + manager.startCluster(cluster); + + @SuppressWarnings("unchecked") + ArgumentCaptor> cmdCaptor = ArgumentCaptor.forClass(List.class); + verify(builder).withCmd(cmdCaptor.capture()); + List cmd = cmdCaptor.getValue(); + + assertTrue(cmd.contains("--tls-san=localhost")); + assertTrue(cmd.contains("--tls-san=aws.example.test")); + + @SuppressWarnings("unchecked") + ArgumentCaptor> labels = ArgumentCaptor.forClass(Map.class); + verify(builder).withLabels(labels.capture()); + assertEquals("aws.example.test", labels.getValue().get("io.floci.eks.endpoint-host")); + } + + @Test + void startClusterAddsABareIpv6EndpointHostToTheCertificate(@TempDir Path tempDir) { + when(eks.irsaSigningKey()).thenReturn(false); + when(eks.dataPath()).thenReturn(tempDir.toString()); + when(eks.endpointHost()).thenReturn(Optional.of("[fd00::24]")); + + Cluster cluster = new Cluster(); + cluster.setName("my-cluster"); + + manager.startCluster(cluster); + + @SuppressWarnings("unchecked") + ArgumentCaptor> cmdCaptor = ArgumentCaptor.forClass(List.class); + verify(builder).withCmd(cmdCaptor.capture()); + + assertTrue(cmdCaptor.getValue().contains("--tls-san=fd00::24"), cmdCaptor.getValue()::toString); + } + + @Test + void startClusterAddsLocalhostOnceWhenItIsTheConfiguredEndpointHost(@TempDir Path tempDir) { + when(eks.irsaSigningKey()).thenReturn(false); + when(eks.dataPath()).thenReturn(tempDir.toString()); + when(eks.endpointHost()).thenReturn(Optional.of("LOCALHOST")); + + Cluster cluster = new Cluster(); + cluster.setName("my-cluster"); + + manager.startCluster(cluster); + + @SuppressWarnings("unchecked") + ArgumentCaptor> cmdCaptor = ArgumentCaptor.forClass(List.class); + verify(builder).withCmd(cmdCaptor.capture()); + + assertEquals(List.of("--tls-san=localhost"), + cmdCaptor.getValue().stream().filter(a -> a.startsWith("--tls-san=")).toList()); + } + + @Test + void startClusterAddsNoExtraCertificateNameWithoutAnEndpointHost(@TempDir Path tempDir) { + when(eks.irsaSigningKey()).thenReturn(false); + when(eks.dataPath()).thenReturn(tempDir.toString()); + + Cluster cluster = new Cluster(); + cluster.setName("my-cluster"); + + manager.startCluster(cluster); + + @SuppressWarnings("unchecked") + ArgumentCaptor> cmdCaptor = ArgumentCaptor.forClass(List.class); + verify(builder).withCmd(cmdCaptor.capture()); + List cmd = cmdCaptor.getValue(); + + assertEquals(List.of("--tls-san=localhost"), + cmd.stream().filter(a -> a.startsWith("--tls-san=")).toList()); + } + @Test void startClusterOmitsIrsaArgsAndKeyInjectionWhenDisabled(@TempDir Path tempDir) { when(eks.irsaSigningKey()).thenReturn(false); diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksStartupValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksStartupValidatorTest.java new file mode 100644 index 0000000000..041e8e7b59 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksStartupValidatorTest.java @@ -0,0 +1,52 @@ +package io.github.hectorvent.floci.services.eks; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.quarkus.runtime.StartupEvent; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class EksStartupValidatorTest { + + @ParameterizedTest + @ValueSource(strings = {"https://aws.example.test", "aws.example.test:6443", "a.example.test,b.example.test"}) + void invalidEndpointHostIsRejectedByStartupObserver(String value) { + EksStartupValidator validator = validator(Optional.of(value)); + assertThrows(IllegalArgumentException.class, () -> validator.onStart(new StartupEvent())); + } + + @ParameterizedTest + @ValueSource(strings = {"", "aws.example.test", "192.168.8.24", "[fd00::24]"}) + void validOrUnsetEndpointHostIsAcceptedByStartupObserver(String value) { + EksStartupValidator validator = validator(value.isEmpty() ? Optional.empty() : Optional.of(value)); + assertDoesNotThrow(() -> validator.onStart(new StartupEvent())); + } + + @Test + void invalidEndpointHostIsIgnoredWhenEksIsMetadataOnly() { + EksStartupValidator disabled = validator(Optional.of("https://aws.example.test"), false, false); + EksStartupValidator mock = validator(Optional.of("https://aws.example.test"), true, true); + assertDoesNotThrow(() -> disabled.onStart(new StartupEvent())); + assertDoesNotThrow(() -> mock.onStart(new StartupEvent())); + } + + private static EksStartupValidator validator(Optional endpointHost) { + return validator(endpointHost, true, false); + } + + private static EksStartupValidator validator(Optional endpointHost, boolean enabled, boolean mock) { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().eks().endpointHost()).thenReturn(endpointHost); + when(config.services().eks().enabled()).thenReturn(enabled); + when(config.services().eks().mock()).thenReturn(mock); + return new EksStartupValidator(config); + } +}