fix(ec2): let a systemd guest's init start sshd (#6882)

Floci started /usr/sbin/sshd itself in every guest, including systemd ones
whose image starts sshd from its own unit at boot. The two raced for port
22: if Floci's won, ssh.service failed, and the stray daemon outside systemd
kept the port, so a later `systemctl restart ssh` (to apply an
sshd_config change, as user data commonly does) failed too. On EC2 the AMI's
init alone starts sshd; systemd guests now work the same way.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-28 21:11:09 -04:00
1 parent 177f8e9360
commit 0e87e6f1de
3 files changed
+13 -2

No files matched your search

+1 -1
View File
@@ -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.
@@ -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 UserData
String userData = instance.getUserData();
@@ -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"));
}