mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(emr): give a step's command the sudo bootstrap actions have (#8415)
A step's script that ran sudo -u hadoop spark-submit ..., as a step does on EMR, failed with exit code 127, as the image steps run in has no sudo. The Release Guide's "Using HBase snapshots" runs a command-runner.jar step of sudo -u hbase, and the Management Guide's Kerberos users page a script-runner.jar script running sudo kadmin.local. Only bootstrap actions had Floci's sudo, which went before the image was kept. Each step on a cluster now finds that same sudo first on its PATH, written next to the hdfs and hadoop-streaming stand-ins the cluster's Hadoop setup already writes there, and a Hive step's shell commands find it too, written while its container is still root. It drops its options, a long one's value given apart included, and runs the command as the step's own hadoop user, so sudo -u hadoop runs as on EMR, while a command that needs root fails as it would for hadoop. The script moves to EmrServerlessSparkRunner, where the setup is, and the bootstrap actions write it from there.
This commit is contained in:
1 parent
2fa6deb0d8
commit
6971626d82
5 files changed
+121
-23
No files matched your search
@@ -11,6 +11,7 @@ import io.floci.aws.core.common.docker.ContainerExecStubs;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.floci.aws.services.emrserverless.EmrServerlessSparkRunner;
|
||||
import io.floci.aws.services.lambda.launcher.BootstrapProcess;
|
||||
import io.floci.aws.services.lambda.launcher.LambdaExecutionRoleCredentials;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
@@ -260,7 +261,7 @@ class EmrBootstrapRunnerTest {
|
||||
@Test
|
||||
void sudoDropsItsOptionsAndRunsItsCommandAsItIs(@TempDir Path directory) throws Exception {
|
||||
Path sudo = directory.resolve("sudo");
|
||||
Files.writeString(sudo, EmrBootstrapRunner.SUDO + "\n");
|
||||
Files.writeString(sudo, EmrServerlessSparkRunner.SUDO + "\n");
|
||||
|
||||
BootstrapProcess.Result ended = BootstrapProcess.run(new ProcessBuilder("bash", sudo.toString(), "-E", "-u",
|
||||
"root", "--", "GREETING=hello", "sh", "-c", "printf '%s %s|' \"$GREETING\" \"$1\"", "sh", "two words"));
|
||||
|
||||
@@ -1399,6 +1399,78 @@ class EmrServerlessSparkRunnerTest {
|
||||
assertFalse(ended.output().contains("hadoop-client-api"), ended.output());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepsCommandFindsASudoThatRunsItsCommandAsItIs(@TempDir Path folder) throws Exception {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
|
||||
runner(builder).run(commandJob(List.of("bash", "-c", "sudo -u hadoop spark-submit app.py"), Map.of(), STORAGE),
|
||||
progress);
|
||||
|
||||
ArgumentCaptor<List<String>> entrypoint = ArgumentCaptor.captor();
|
||||
verify(spec).withEntrypoint(entrypoint.capture());
|
||||
String script = entrypoint.getValue().get(2);
|
||||
// In the folder the wrapper puts first on the step's PATH.
|
||||
assertTrue(script.contains("\n export HADOOP_CONF_DIR=\"$hadoop_conf\" PATH=\"/tmp/floci-bin:$PATH\"\n"),
|
||||
script);
|
||||
// The wrapper's lines that write it and make it runnable, in the block they run in, in a folder of the test's.
|
||||
int start = script.indexOf(" cat > /tmp/floci-bin/sudo");
|
||||
assertTrue(start >= 0, script);
|
||||
int end = script.indexOf("\n", script.indexOf(" chmod +x ", start));
|
||||
Path bin = Files.createDirectory(folder.resolve("bin"));
|
||||
Files.writeString(bin.resolve("hdfs"), "");
|
||||
Files.writeString(bin.resolve("hadoop-streaming"), "");
|
||||
String lines = script.substring(start, end).replace("/tmp/floci-bin/", bin + "/");
|
||||
BootstrapProcess.Result written = BootstrapProcess.run(new ProcessBuilder("bash", "-c",
|
||||
"if true; then\n" + lines + "\nfi").directory(folder.toFile()));
|
||||
assertEquals(0, written.exit(), written.output());
|
||||
|
||||
BootstrapProcess.Result ended = BootstrapProcess.run(new ProcessBuilder(bin.resolve("sudo").toString(),
|
||||
"-u", "hadoop", "-H", "GREETING=hello", "sh", "-c", "printf '%s %s|' \"$GREETING\" \"$1\"", "sh",
|
||||
"two words"));
|
||||
|
||||
assertEquals(new BootstrapProcess.Result(0, "hello two words|"), ended);
|
||||
// A long option takes its value apart or after an =, as sudo's own do.
|
||||
BootstrapProcess.Result longOptions = BootstrapProcess.run(new ProcessBuilder(bin.resolve("sudo").toString(),
|
||||
"--user", "hadoop", "--group=hadoop", "--chdir", "/tmp", "sh", "-c", "printf '%s|' \"$0\"", "ran"));
|
||||
assertEquals(new BootstrapProcess.Result(0, "ran|"), longOptions);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHiveStepOnAClusterFindsTheSudoAStepHasButAnEmrServerlessHiveRunDoesNot(@TempDir Path folder)
|
||||
throws Exception {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
|
||||
runner(builder).run(hiveStep(List.of("-e", "!sudo id;")), progress);
|
||||
|
||||
verify(spec).withEnv("FLOCI_CLUSTER_STEP", "1");
|
||||
ArgumentCaptor<List<String>> entrypoint = ArgumentCaptor.captor();
|
||||
verify(spec).withEntrypoint(entrypoint.capture());
|
||||
String script = entrypoint.getValue().get(2);
|
||||
int start = script.indexOf("if [ -n \"${FLOCI_CLUSTER_STEP:-}\" ]; then");
|
||||
assertTrue(start >= 0, script);
|
||||
// Written as root, before hive runs as the image's user, whose shell commands then find it.
|
||||
assertTrue(start < script.indexOf("exec setpriv"), script);
|
||||
Path bin = folder.resolve("bin");
|
||||
String block = script.substring(start, script.indexOf("\nfi\n", start) + 3)
|
||||
.replace("/tmp/floci-bin", bin.toString());
|
||||
ProcessBuilder shell = new ProcessBuilder("bash", "-c",
|
||||
block + "\nsudo -u hadoop sh -c 'printf \"%s|\" \"$0\"' ran").directory(folder.toFile());
|
||||
shell.environment().put("FLOCI_CLUSTER_STEP", "1");
|
||||
assertEquals(new BootstrapProcess.Result(0, "ran|"), BootstrapProcess.run(shell));
|
||||
|
||||
ContainerBuilder serverlessBuilder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder serverless = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
when(serverlessBuilder.newContainer(anyString())).thenReturn(serverless);
|
||||
|
||||
runner(serverlessBuilder).run(hiveJob(List.of("-e", "select 1;")), progress);
|
||||
|
||||
verify(serverless, never()).withEnv(eq("FLOCI_CLUSTER_STEP"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepsHadoopCommandLogsToStandardErrorWhenItsLog4jPropertiesIsEmpty() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
|
||||
Reference in new issue
Block a user