diff --git a/docs/services/ec2.md b/docs/services/ec2.md index f0448e1ce3..4a39c43cfe 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -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. 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.java index 4cbc3f6083..2f5c2800d3 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 @@ -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 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(); 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.java index 3ec52e905f..7d93295852 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 @@ -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;