Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): let a systemd guest's init start sshd (#6882)
Allen Sanborn0e87e6f1de31
diff --git a/docs/services/ec2.md b/docs/services/ec2.mdindex a93aa39d0d..8a6599894b 100644--- a/docs/services/ec2.md+++ b/docs/services/ec2.md@@ -131,7 +131,7 @@ The Java metadata-driven builder lives at `io.github.hectorvent.floci.tools.ami.## SSH Key Injection-If `KeyName` is specified at launch, Floci looks up the stored key pair's public key (generated by `CreateKeyPair` or supplied to `ImportKeyPair`) and copies it into `/root/.ssh/authorized_keys` inside the container at boot. It then attempts to start `sshd` if present. The SSH port (container port 22) is mapped to a host port from the configured range (default 2200–2299).+If `KeyName` is specified at launch, Floci looks up the stored key pair's public key (generated by `CreateKeyPair` or supplied to `ImportKeyPair`) and copies it into `/root/.ssh/authorized_keys` inside the container at boot. It then attempts to start `sshd` if present, except in a `systemd` guest, whose init starts `sshd` from the image's own unit as on EC2. The SSH port (container port 22) is mapped to a host port from the configured range (default 2200–2299).Key pairs created with `CreateKeyPair` return real RSA or ED25519 private key material (see [Key Pairs](#key-pairs)), and the matching public key is injected at launch, so either a created or an imported key pair works for SSH.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.javaindex df4e094780..6e7794374f 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@@ -560,7 +560,13 @@ public class Ec2ContainerManager {// key-pair association) - starting it only when a key was present meant// run-instances without --key-name produced a "connection refused" instead of AWS's// actual behavior: a running daemon with simply nothing to authenticate against.- startSshd(containerId, instanceId);+ // A systemd guest is the exception: its init starts sshd from the image's own unit,+ // as it does on EC2. A second sshd launched here raced that unit for port 22, and+ // whichever lost left either the unit failed or a daemon outside systemd that a+ // later `systemctl restart ssh` could not replace.+ if (!image.systemd()) {+ startSshd(containerId, instanceId);+ }// Execute UserDataString userData = instance.getUserData();diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.javaindex d9cf1f3a7d..e73d6af85e 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java@@ -1185,6 +1185,7 @@ class Ec2ContainerManagerTest {when(inspect.exec()).thenReturn(withIp);harness.stubSuccessfulExecs(new CountDownLatch(0), new CountDownLatch(0));Instance instance = instance("i-systemd");+ instance.setUserData("#!/bin/sh\necho ready\n");harness.manager.launch(instance,new ResolvedAmiImage("floci/ami-ubuntu:24.04-arm64", ResolvedAmiImage.SYSTEMD_RUNTIME, true,@@ -1196,6 +1197,10 @@ class Ec2ContainerManagerTest {verify(harness.builder).withCmd(List.of("/sbin/init"));verify(harness.builder).withCgroupnsMode("host");verify(harness.builder).withBind("/sys/fs/cgroup", "/sys/fs/cgroup");+ // User data runs after the sshd step, so once it has started that step is settled.+ awaitUntil(() -> commandIndex(harness.executedCommands, "/var/lib/user-data.sh") >= 0, Duration.ofSeconds(2));+ assertEquals(-1, commandIndex(harness.executedCommands, "/usr/sbin/sshd"),+ "a systemd guest's init starts sshd; Floci must not start a second one");verify(harness.lifecycleManager).create(any(ContainerSpec.class), eq("linux/arm64"));}