mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
fix(ec2): create /run/sshd before starting sshd (#1855)
Modern OpenSSH refuses to start without its privilege-separation directory, and /run is a fresh tmpfs in most container images, so /run/sshd genuinely is not there. sshd exited with "Missing privilege separation directory: /run/sshd" and instances launched via `aws ec2 run-instances` came up with no SSH, requiring the directory to be created by hand inside the container. Add a regression test that drives the launch path with an SSH public key, records the container execs in order, and asserts /run/sshd is created before /usr/sbin/sshd is invoked. Signed-off-by: ProGamingDk <smashyalts@gmail.com> Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
7505110ed9
commit
3daab4a1ae
2 files changed
+52
-2
No files matched your search
@@ -512,6 +512,15 @@ public class Ec2ContainerManager {
|
||||
instanceId, keygen.summary());
|
||||
return;
|
||||
}
|
||||
// Modern OpenSSH refuses to start without its privilege-separation directory, and /run
|
||||
// is a fresh tmpfs in most container images, so /run/sshd genuinely isn't there yet.
|
||||
ContainerExecResult mkdir = execInContainerForResult(containerId,
|
||||
new String[]{"mkdir", "-p", "/run/sshd"}, 5);
|
||||
if (mkdir.exitCode() != 0) {
|
||||
LOG.warnv("Could not create /run/sshd for EC2 instance {0}: {1}",
|
||||
instanceId, mkdir.summary());
|
||||
return;
|
||||
}
|
||||
// Start sshd without -D so it daemonizes itself and survives this exec session. Since sshd
|
||||
// requires execution with an absolute path, several paths are tried until it starts
|
||||
for (String sshdPath : ALLOWED_SSHD_PATHS) {
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -62,6 +63,7 @@ class Ec2ContainerManagerTest {
|
||||
private static final String TEST_USER_DATA_OUTPUT = "test-output";
|
||||
private static final String TEST_CONTAINER_ID = "container-1";
|
||||
private static final String TEST_LOG_STREAM_NAME = "yyyy/MM/dd/user-data";
|
||||
private static final String TEST_SSH_PUBLIC_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITest test@floci";
|
||||
|
||||
@org.junit.jupiter.api.AfterEach
|
||||
void resetBridgeIpPolling() {
|
||||
@@ -392,6 +394,30 @@ class Ec2ContainerManagerTest {
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchCreatesSshdPrivilegeSeparationDirectoryBeforeStartingSshd() throws Exception {
|
||||
Ec2ContainerManager.containerBridgeIpAttempts = 1;
|
||||
Ec2ContainerManager.containerBridgeIpPollMillis = 1;
|
||||
LaunchHarness harness = launchHarness();
|
||||
InspectContainerCmd inspect = mock(InspectContainerCmd.class);
|
||||
InspectContainerResponse withIp = inspectResponse("172.18.0.12");
|
||||
when(harness.dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);
|
||||
when(inspect.exec()).thenReturn(withIp);
|
||||
harness.stubSuccessfulExecs(new CountDownLatch(0), new CountDownLatch(0));
|
||||
|
||||
harness.manager.launch(instance("i-sshd"), "ubuntu:24.04", TEST_SSH_PUBLIC_KEY, "us-west-2");
|
||||
|
||||
awaitUntil(() -> commandIndex(harness.executedCommands, "/usr/sbin/sshd") >= 0, Duration.ofSeconds(2));
|
||||
|
||||
int mkdirIndex = commandIndex(harness.executedCommands, "mkdir", "-p", "/run/sshd");
|
||||
int sshdIndex = commandIndex(harness.executedCommands, "/usr/sbin/sshd");
|
||||
assertTrue(mkdirIndex >= 0,
|
||||
"sshd startup should create the /run/sshd privilege-separation directory");
|
||||
assertTrue(mkdirIndex < sshdIndex,
|
||||
"/run/sshd must be created before sshd starts, otherwise sshd exits with "
|
||||
+ "\"Missing privilege separation directory\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchWithoutKeyPairStillStartsSshd() throws Exception {
|
||||
// Regression for #2184: sshd previously only started when a key pair was supplied, so
|
||||
@@ -527,7 +553,20 @@ class Ec2ContainerManagerTest {
|
||||
config,
|
||||
metadataServer,
|
||||
mock(Ec2PortForwardManager.class));
|
||||
return new LaunchHarness(manager, dockerClient, metadataServer, logStreamer, builder);
|
||||
return new LaunchHarness(manager, dockerClient, metadataServer, logStreamer, builder,
|
||||
new CopyOnWriteArrayList<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Index of the first exec matching {@code expected} exactly, or -1 when it was never run.
|
||||
*/
|
||||
private static int commandIndex(List<String[]> executedCommands, String... expected) {
|
||||
for (int i = 0; i < executedCommands.size(); i++) {
|
||||
if (Arrays.equals(executedCommands.get(i), expected)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static Instance instance(String instanceId) {
|
||||
@@ -562,7 +601,8 @@ class Ec2ContainerManagerTest {
|
||||
DockerClient dockerClient,
|
||||
Ec2MetadataServer metadataServer,
|
||||
ContainerLogStreamer logStreamer,
|
||||
ContainerBuilder.Builder builder) {
|
||||
ContainerBuilder.Builder builder,
|
||||
List<String[]> executedCommands) {
|
||||
void stubSuccessfulExecs(CountDownLatch userDataStarted, CountDownLatch finishUserData) throws Exception {
|
||||
AtomicReference<String[]> currentCommand = new AtomicReference<>();
|
||||
ExecCreateCmd execCreate = mock(ExecCreateCmd.class, withSettings().defaultAnswer(RETURNS_SELF));
|
||||
@@ -578,6 +618,7 @@ class Ec2ContainerManagerTest {
|
||||
} else {
|
||||
currentCommand.set(Arrays.copyOf(args, args.length, String[].class));
|
||||
}
|
||||
executedCommands.add(currentCommand.get());
|
||||
return execCreate;
|
||||
});
|
||||
when(execCreate.exec()).thenAnswer(invocation -> {
|
||||
|
||||
Reference in new issue
Block a user