mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): stage user data outside guest temporary mounts (#3736)
* fix(ec2): stage user data outside guest temporary mounts * docs(ec2): correct user-data script staging path
This commit is contained in:
1 parent
ef12a83418
commit
03603aeacd
3 files changed
+29
-6
No files matched your search
@@ -98,7 +98,7 @@ Notes and limitations:
|
||||
|
||||
## UserData
|
||||
|
||||
`UserData` must be base64-encoded in the request (matching the AWS wire format). Floci decodes it, copies the script into `/tmp/user-data.sh` inside the container, and executes the script directly after SSH key injection so the script shebang selects the interpreter. Output is captured and logged.
|
||||
`UserData` must be base64-encoded in the request (matching the AWS wire format). Floci decodes it, copies the script into `/var/lib/user-data.sh` inside the container, and executes the script directly after SSH key injection so the script shebang selects the interpreter. Output is captured and logged.
|
||||
|
||||
EC2 containers receive `AWS_EC2_METADATA_SERVICE_ENDPOINT` for IMDS and `AWS_ENDPOINT_URL` for AWS service API calls back to Floci.
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ import java.util.zip.GZIPInputStream;
|
||||
public class Ec2ContainerManager {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(Ec2ContainerManager.class);
|
||||
private static final String USER_DATA_SCRIPT_PATH = "/tmp/user-data.sh";
|
||||
// Guest-created /tmp mounts hide files copied through Docker's archive API.
|
||||
private static final String USER_DATA_SCRIPT_PATH = "/var/lib/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. */
|
||||
@@ -1471,7 +1472,7 @@ public class Ec2ContainerManager {
|
||||
byte[] script = scriptContent.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] tar = buildSingleFileTar("user-data.sh", script, 0755);
|
||||
dockerClient.copyArchiveToContainerCmd(containerId)
|
||||
.withRemotePath("/tmp")
|
||||
.withRemotePath("/var/lib")
|
||||
.withTarInputStream(new ByteArrayInputStream(tar))
|
||||
.exec();
|
||||
|
||||
|
||||
@@ -436,7 +436,7 @@ class Ec2ContainerManagerTest {
|
||||
|
||||
@Test
|
||||
void userDataExecutionCommandRunsScriptDirectlySoShebangIsHonored() {
|
||||
assertArrayEquals(new String[]{"/tmp/user-data.sh"}, Ec2ContainerManager.userDataExecutionCommand());
|
||||
assertArrayEquals(new String[]{"/var/lib/user-data.sh"}, Ec2ContainerManager.userDataExecutionCommand());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1198,6 +1198,28 @@ class Ec2ContainerManagerTest {
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void userDataIsCopiedAndExecutedOutsideGuestTemporaryMounts() throws Exception {
|
||||
LaunchHarness harness = launchHarness();
|
||||
InspectContainerCmd inspect = mock(InspectContainerCmd.class);
|
||||
when(harness.dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);
|
||||
InspectContainerResponse withIp = inspectResponse("172.18.0.10");
|
||||
when(inspect.exec()).thenReturn(withIp);
|
||||
CountDownLatch userDataStarted = new CountDownLatch(1);
|
||||
harness.stubSuccessfulExecs(userDataStarted, new CountDownLatch(0));
|
||||
CopyArchiveToContainerCmd copy = harness.dockerClient.copyArchiveToContainerCmd(TEST_CONTAINER_ID);
|
||||
Instance instance = instance("i-userdata-persistent-path");
|
||||
instance.setUserData("#!/bin/sh\necho ready\n");
|
||||
|
||||
harness.manager.launch(instance, "amazonlinux:2023", null, "us-west-2");
|
||||
|
||||
assertTrue(userDataStarted.await(2, TimeUnit.SECONDS), "user data should start");
|
||||
verify(copy).withRemotePath("/var/lib");
|
||||
verify(copy, never()).withRemotePath("/tmp");
|
||||
assertTrue(harness.executedCommands.stream()
|
||||
.anyMatch(command -> Arrays.equals(command, new String[]{"/var/lib/user-data.sh"})));
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchAppliesBackpressureWhenDockerLaunchesAreSaturated() throws Exception {
|
||||
ThreadPoolExecutor launchExecutor = new ThreadPoolExecutor(
|
||||
@@ -1653,7 +1675,7 @@ class Ec2ContainerManagerTest {
|
||||
});
|
||||
when(execCreate.exec()).thenAnswer(invocation -> {
|
||||
String[] command = currentCommand.get();
|
||||
return command != null && command.length == 1 && "/tmp/user-data.sh".equals(command[0])
|
||||
return command != null && command.length == 1 && "/var/lib/user-data.sh".equals(command[0])
|
||||
? userDataExec : metadataExec;
|
||||
});
|
||||
|
||||
@@ -1816,7 +1838,7 @@ class Ec2ContainerManagerTest {
|
||||
});
|
||||
when(execCreate.exec()).thenAnswer(invocation -> {
|
||||
String[] command = currentCommand.get();
|
||||
if (command != null && command.length == 1 && "/tmp/user-data.sh".equals(command[0])) {
|
||||
if (command != null && command.length == 1 && "/var/lib/user-data.sh".equals(command[0])) {
|
||||
return userDataExec;
|
||||
}
|
||||
return metadataExec;
|
||||
|
||||
Reference in new issue
Block a user