Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): install the SSH client package in guests, not just the server (#2449)
Allen Sanbornf3029ee2511d
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 362f1a942f..e7fefefaf9 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@@ -55,6 +55,8 @@ public class Ec2ContainerManager {private static final String USER_DATA_SCRIPT_PATH = "/tmp/user-data.sh";private static final Pattern MIME_BOUNDARY = Pattern.compile("(?im)^content-type:\\s*multipart/[^;]+;\\s*boundary=\"?([^\";\\n\\r]+)\"?.*$");private static final List<String> ALLOWED_SSHD_PATHS = List.of("/usr/sbin/sshd", "/usr/local/sbin/sshd", "/sbin/sshd");+ /** Exit code the sshd install probe uses for "sshd is present but scp is not". See startSshd. */+ static final int SSH_CLIENT_MISSING_EXIT_CODE = 2;static int containerBridgeIpAttempts = 30;static long containerBridgeIpPollMillis = 500;@@ -492,15 +494,36 @@ public class Ec2ContainerManager {// whose install command itself failed (e.g. no network yet, apt lock held) still exits 0// by bash convention, so every later step here would silently no-op against a daemon that// was never installed while still logging success.+ // The client package is installed alongside the server because provisioning tools+ // need scp *on the instance*: Packer's default file transfer for a shell+ // provisioner uploads the script with scp, and real AMIs carry it. Installing only+ // openssh-server leaves sftp-server present but /usr/bin/scp absent, so the upload+ // fails with "SCP failed to start. This usually means that SCP is not properly+ // installed on the remote system." The guard tests for scp too: keying it on sshd+ // alone would skip the install entirely on an image that already has the server but+ // no client. Package names differ -- openssh-clients on rpm distributions,+ // openssh-client on Debian; apk's openssh already contains both.+ // The two are not equally fatal, so the script separates them: exit 1 means no sshd+ // and there is nothing to start, while exit 2 means sshd is there but the client+ // package did not land. A guest that can serve SSH but cannot scp is still worth+ // starting - it just cannot run a Packer shell provisioner - so that case warns and+ // continues rather than leaving the instance unreachable. Checking only sshd at the+ // end would report that state as outright success, which is the silent failure this+ // probe exists to prevent.ContainerExecResult install = execInContainerForResult(containerId, new String[]{"sh", "-c",- "if ! command -v sshd >/dev/null 2>&1; then" +- " if command -v dnf >/dev/null 2>&1; then dnf install -y openssh-server >/dev/null 2>&1;" +- " elif command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server >/dev/null 2>&1;" ++ "if ! command -v sshd >/dev/null 2>&1 || ! command -v scp >/dev/null 2>&1; then" ++ " if command -v dnf >/dev/null 2>&1; then dnf install -y openssh-server openssh-clients >/dev/null 2>&1;" ++ " elif command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server openssh-client >/dev/null 2>&1;" +" elif command -v apk >/dev/null 2>&1; then apk add --no-cache openssh >/dev/null 2>&1;" +" fi;" +"fi;" +- "command -v sshd >/dev/null 2>&1"}, 120);- if (install.exitCode() != 0) {+ "command -v sshd >/dev/null 2>&1 || exit 1;" ++ "command -v scp >/dev/null 2>&1 || exit 2"}, 120);+ if (install.exitCode() == SSH_CLIENT_MISSING_EXIT_CODE) {+ LOG.warnv("sshd is available on EC2 instance {0} but the OpenSSH client package is not:"+ + " scp is missing, so provisioners that upload files over scp will fail: {1}",+ instanceId, install.summary());+ } else if (install.exitCode() != 0) {LOG.warnv("Could not install openssh-server for EC2 instance {0}: {1}",instanceId, install.summary());return;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 0777243180..179e568e20 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@@ -245,6 +245,29 @@ class Ec2ContainerManagerTest {assertTrue(command[2].contains("curl"));}+ @Test+ void sshdInstallProbeAlsoInstallsTheSshClientPackage() {+ // Packer's default file transfer runs scp *on the instance*, so a guest with only+ // openssh-server fails the shell provisioner with "SCP failed to start. This usually+ // means that SCP is not properly installed on the remote system." Real AMIs ship the+ // client; installing only the server leaves sftp-server present but /usr/bin/scp+ // absent. Package names verified against the images themselves: openssh-clients on+ // rpm distributions, openssh-client on Debian, and apk's openssh already has both.+ String script = SSHD_INSTALL_PROBE_CMD[2];++ assertTrue(script.contains("openssh-server openssh-clients"), script);+ assertTrue(script.contains("openssh-server openssh-client >"), script);+ // The guard has to consider scp as well, or an image that already has sshd but no+ // client skips the install and provisioning still fails with nothing in the logs.+ assertTrue(script.contains("! command -v scp"), script);+ // ...and so does the trailing status check, on its own exit code. Ending the script on+ // "command -v sshd" alone would report a guest that has the server but whose client+ // install failed as a success, which is the state this guard was widened to catch.+ assertTrue(script.endsWith("command -v sshd >/dev/null 2>&1 || exit 1;"+ + "command -v scp >/dev/null 2>&1 || exit "+ + Ec2ContainerManager.SSH_CLIENT_MISSING_EXIT_CODE), script);+ }+@Testvoid metadataProxyStartCommandBindsAwsLinkLocalMetadataAddress() {String[] command = Ec2ContainerManager.metadataProxyStartCommand("floci", 9169);@@ -442,13 +465,14 @@ class Ec2ContainerManagerTest {}private static final String[] SSHD_INSTALL_PROBE_CMD = {"sh", "-c",- "if ! command -v sshd >/dev/null 2>&1; then"- + " if command -v dnf >/dev/null 2>&1; then dnf install -y openssh-server >/dev/null 2>&1;"- + " elif command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server >/dev/null 2>&1;"+ "if ! command -v sshd >/dev/null 2>&1 || ! command -v scp >/dev/null 2>&1; then"+ + " if command -v dnf >/dev/null 2>&1; then dnf install -y openssh-server openssh-clients >/dev/null 2>&1;"+ + " elif command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server openssh-client >/dev/null 2>&1;"+ " elif command -v apk >/dev/null 2>&1; then apk add --no-cache openssh >/dev/null 2>&1;"+ " fi;"+ "fi;"- + "command -v sshd >/dev/null 2>&1"};+ + "command -v sshd >/dev/null 2>&1 || exit 1;"+ + "command -v scp >/dev/null 2>&1 || exit 2"};@Testvoid launchWhenSshdInstallFails_doesNotAttemptKeygenOrStart() throws Exception {@@ -460,6 +484,35 @@ class Ec2ContainerManagerTest {// logged success. This forces the install probe to report failure and asserts the later// steps are never even attempted, rather than running against a daemon that was never// installed.+ ExecCreateCmd execCreate = launchWithSshdInstallProbeExitCode(1, "i-sshdinstallfail");++ verify(execCreate, timeout(2000)).withCmd(eq(SSHD_INSTALL_PROBE_CMD));+ verify(execCreate, never()).withCmd(eq(new String[]{"ssh-keygen", "-A"}));+ verify(execCreate, never()).withCmd(eq(new String[]{"/usr/sbin/sshd"}));+ }++ @Test+ void launchWhenOnlySshClientInstallFails_stillStartsSshd() throws Exception {+ // The probe's guard now also fires when scp is missing, so a guest that already has sshd+ // but whose client-package install fails must not be reported as a plain success - that is+ // exactly the state where Packer's default scp-based file transfer breaks. It is not fatal+ // either: sshd is present and the instance is worth making reachable. The script separates+ // the two with exit 2, which warns and carries on rather than returning early.+ ExecCreateCmd execCreate = launchWithSshdInstallProbeExitCode(+ Ec2ContainerManager.SSH_CLIENT_MISSING_EXIT_CODE, "i-scpinstallfail");++ verify(execCreate, timeout(2000)).withCmd(eq(SSHD_INSTALL_PROBE_CMD));+ verify(execCreate, timeout(2000)).withCmd(eq(new String[]{"ssh-keygen", "-A"}));+ verify(execCreate, timeout(2000)).withCmd(eq(new String[]{"/usr/sbin/sshd"}));+ }++ /**+ * Launches an instance where every exec succeeds except the sshd install probe, which reports+ * {@code probeExitCode}. Returns the {@link ExecCreateCmd} the launch issued its commands+ * through so callers can assert on which steps were attempted.+ */+ private ExecCreateCmd launchWithSshdInstallProbeExitCode(int probeExitCode, String instanceId)+ throws Exception {LaunchHarness harness = launchHarness();InspectContainerCmd inspect = mock(InspectContainerCmd.class);InspectContainerResponse withIp = inspectResponse("172.18.0.21");@@ -497,7 +550,7 @@ class Ec2ContainerManagerTest {when(inspectExec.exec()).thenAnswer(invocation -> {InspectExecResponse response = mock(InspectExecResponse.class);boolean isSshdInstallProbe = Arrays.equals(currentCommand.get(), SSHD_INSTALL_PROBE_CMD);- when(response.getExitCodeLong()).thenReturn(isSshdInstallProbe ? 1L : 0L);+ when(response.getExitCodeLong()).thenReturn(isSshdInstallProbe ? (long) probeExitCode : 0L);return response;});@@ -505,14 +558,12 @@ class Ec2ContainerManagerTest {when(harness.dockerClient.copyArchiveToContainerCmd(TEST_CONTAINER_ID)).thenReturn(copy);when(copy.withTarInputStream(any(InputStream.class))).thenReturn(copy);- Instance instance = instance("i-sshdinstallfail");+ Instance instance = instance(instanceId);harness.manager.launch(instance, "ubuntu:24.04", "ssh-ed25519 AAAAtest", "us-west-2");awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(2));- verify(execCreate, timeout(2000)).withCmd(eq(SSHD_INSTALL_PROBE_CMD));- verify(execCreate, never()).withCmd(eq(new String[]{"ssh-keygen", "-A"}));- verify(execCreate, never()).withCmd(eq(new String[]{"/usr/sbin/sshd"}));+ return execCreate;}private static LaunchHarness launchHarness() {