diff --git a/docs/services/emr.md b/docs/services/emr.md index eaaf700cb9..f86fce8530 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -186,7 +186,12 @@ holding the driver and its executors, or a command or a script. The cluster itse 3, Java, `curl` and the AWS CLI v2, which reaches Floci through `AWS_ENDPOINT_URL`. A `script-runner.jar` step's script is fetched from S3 with the AWS CLI and the step's credentials, then run with the step's other arguments; a path not in S3 is run as it is, a bare name from `/home/hadoop` - when a file there has it, one a former step left there included. `hadoop fs` and `hdfs dfs` reach the + when a file there has it, one a former step left there included. A step can run `sudo`, as the Release Guide's + "Using HBase snapshots" step does with `command-runner.jar` and `sudo -u hbase`, and the Management Guide's + Kerberos users step with a `script-runner.jar` script running `sudo kadmin.local`. The image has no `sudo`, so a + step finds Floci's first on its `PATH`, a Hive step's shell commands (Hive's `!`) too, the bootstrap actions' one: it + drops its options, with their values, and runs its command as it is, as the step's `hadoop` user. So + `sudo -u hadoop spark-submit ...` runs as on EMR, and a command that needs root fails, as it would for `hadoop`. `hadoop fs` and `hdfs dfs` reach the cluster's HDFS (see **Storage**) and Floci's S3. A `spark-submit` they start runs on the cluster's YARN, or in local mode where it runs none, on Floci's S3 with the cluster's `spark-defaults`, `core-site`, `spark-hive-site` and `spark-log4j2`, which Floci puts in a Spark diff --git a/src/main/java/io/floci/aws/services/emr/EmrBootstrapRunner.java b/src/main/java/io/floci/aws/services/emr/EmrBootstrapRunner.java index e3c608b31d..ccc883c880 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrBootstrapRunner.java +++ b/src/main/java/io/floci/aws/services/emr/EmrBootstrapRunner.java @@ -60,27 +60,13 @@ public class EmrBootstrapRunner implements BootstrapRunner, ContainerTeardown { private static final int STOP_TIMEOUT_SECONDS = 0; // No source gives a bootstrap action a time limit: it runs until it ends, or its cluster does. private static final int NO_TIMEOUT = Integer.MAX_VALUE; - // The sudo an action finds first on its PATH: its options are dropped, as the action already runs as root, - // and its command, with any NAME=value before it, runs as it is. - static final String SUDO = String.join("\n", - "#!/bin/bash", - "while [ $# -gt 0 ]; do", - " case \"$1\" in", - " -u|-g|-C|-D|-p|-r|-t|-T|-U) shift 2 ;;", - " --) shift; break ;;", - " -*) shift ;;", - " *) break ;;", - " esac", - "done", - "exec env \"$@\""); - // What each action runs as root: $1 is its script, and the rest that script's arguments. The JVMs it starts - // trust Floci's CA once the container has its trust store. + // What each action runs as root, with the sudo a step has too (see EmrServerlessSparkRunner.SUDO), whose options + // the action, already root, has no need of: $1 is its script, and the rest that script's arguments. The JVMs it + // starts trust Floci's CA once the container has its trust store. private static final String ACTION_COMMAND = String.join("\n", ContainerCaBundle.JAVA_TRUST_SHELL_LINE, "mkdir -p /tmp/floci-bin", - "cat > /tmp/floci-bin/sudo <<'SUDO'", - SUDO, - "SUDO", + EmrServerlessSparkRunner.SUDO_FILE, "chmod 755 /tmp/floci-bin/sudo", "export PATH=\"/tmp/floci-bin:$PATH\" HOME=/home/hadoop", "cd /home/hadoop", diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java index e601e60386..9af16a66b1 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunner.java @@ -157,6 +157,26 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo "/usr/lib/spark/jars/hadoop-client-api-*.jar", "/usr/lib/spark/jars/hadoop-client-runtime-*.jar", "/usr/lib/spark/jars/emr-serverless-spark-goodies.jar"); + /** + * The {@code sudo} a cluster's bootstrap actions and steps find first on their PATH, as EMR's nodes have one and + * the image none: its options are dropped, each with its value where it takes one, given apart or after an + * {@code =}, and its command, with any NAME=value before it, runs as it is, as whoever runs it, root in a bootstrap + * action and the {@code hadoop} user in a step. + */ + public static final String SUDO = String.join("\n", + "#!/bin/bash", + "while [ $# -gt 0 ]; do", + " case \"$1\" in", + " -u|-g|-C|-D|-p|-r|-t|-T|-U|--user|--group|--close-from|--chdir|--prompt|--role|--type" + + "|--command-timeout|--other-user) shift 2 ;;", + " --) shift; break ;;", + " -*) shift ;;", + " *) break ;;", + " esac", + "done", + "exec env \"$@\""); + /** Writes {@link #SUDO} to /tmp/floci-bin/sudo, a folder that is there already. */ + public static final String SUDO_FILE = String.join("\n", "cat > /tmp/floci-bin/sudo <<'SUDO'", SUDO, "SUDO"); // A Spark configuration of the container's own: a copy of the folder a spark-env export of SPARK_CONF_DIR names, // else of the image's, links followed, whose log4j2.properties ends with the job's lines, passed in // SPARK_LOG4J2_VARIABLE, a later line winning. What is added to a file starts on a line of its own, as the @@ -180,7 +200,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo // cluster sets keeps the LZO codec the image's core-site.xml names. The hadoop script runs only once its HDFS, // MapReduce and YARN homes name folders that are there, and the image has /usr/lib/hadoop alone, without // MapReduce, so the MapReduce home is Floci's (see MAPRED_HOME). The image's hdfs command cannot run, so hdfs dfs - // runs as hadoop fs, and its hadoop-streaming names a jar it does not have, so the one it has runs. + // runs as hadoop fs, and its hadoop-streaming names a jar it does not have, so the one it has runs. A step's + // command can run sudo, as on EMR, which the image does not have, so it finds Floci's first (see SUDO). private static final List HADOOP_CONF_SETUP = List.of( "if [ -n \"${" + HADOOP_SITE_VARIABLE + ":-}\" ]; then", " hadoop_conf=/tmp/floci-hadoop-conf source_conf=\"${HADOOP_CONF_DIR:-/etc/hadoop/conf}\"", @@ -218,7 +239,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo + " 'echo \"hdfs $1: only hdfs dfs runs here, as hadoop fs\" >&2' 'exit 1' > /tmp/floci-bin/hdfs", " printf '%s\\n' '#!/bin/bash' 'exec hadoop jar /usr/lib/hadoop/hadoop-streaming.jar \"$@\"'" + " > /tmp/floci-bin/hadoop-streaming", - " chmod +x /tmp/floci-bin/hdfs /tmp/floci-bin/hadoop-streaming", + " " + SUDO_FILE, + " chmod +x /tmp/floci-bin/hdfs /tmp/floci-bin/hadoop-streaming /tmp/floci-bin/sudo", " for jar in " + String.join(" ", MAPRED_JARS) + "; do", " if [ -f \"$jar\" ]; then ln -sf \"$jar\" " + MAPRED_HOME + "/;", " else echo \"" + MISSING_JAR + "\" >&2; fi", @@ -307,6 +329,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo // output and error. private static final List HIVE_ENVIRONMENT = List.of("LOGS_DIR=/tmp/floci-hive-logs"); private static final String HIVE_SCRATCH_VARIABLE = "FLOCI_HIVE_SCRATCH"; + // Set for a Hive step on a cluster, whose shell commands (Hive's "!") find the sudo a step has (see SUDO). + private static final String CLUSTER_STEP_VARIABLE = "FLOCI_CLUSTER_STEP"; /** * Where an EMR cluster's image with Hive keeps its Hive image's Hadoop configuration, which puts Tez on the class * path, apart from the one its other steps' Hadoop reads; Hive runs with it where an image has it. @@ -336,7 +360,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo // the root of the container for one; so the container starts as root, makes that path its user's, then runs hive // as that user, in that user's home, as a Spark image's working directory is: Tez in local mode puts each task's // files in the working directory, and the Hive image names none, which leaves the root. In an EMR cluster's image - // with Hive, Hive reads its own Hadoop configuration (HIVE_HADOOP_CONF). + // with Hive, Hive reads its own Hadoop configuration (HIVE_HADOOP_CONF), and a step on a cluster finds SUDO first on + // its PATH, as a Spark step does. private static final String HIVE_WRAPPER = String.join("\n", "host=$(hostname)", "export CLIENT_AM_BIND_ADDRESS=0.0.0.0 TASK_AM_BIND_ADDRESS=0.0.0.0 SHUFFLE_BIND_ADDRESS=0.0.0.0", @@ -346,6 +371,12 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo + HIVE_SCRATCH_VARIABLE + "\" || exit", "fi", "if [ -d " + HIVE_HADOOP_CONF + " ]; then export HADOOP_CONF_DIR=" + HIVE_HADOOP_CONF + "; fi", + "if [ -n \"${" + CLUSTER_STEP_VARIABLE + ":-}\" ]; then", + " mkdir -p /tmp/floci-bin || exit", + " " + SUDO_FILE, + " chmod 755 /tmp/floci-bin/sudo", + " export PATH=\"/tmp/floci-bin:$PATH\"", + "fi", HIVE_ONE_SDK, "export HOME=~" + IMAGE_USER, "cd \"$HOME\" || exit", @@ -447,6 +478,9 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo .withEntrypoint(entrypoint) .withCmd(hive) .withEnv(HIVE_ENVIRONMENT); + if (job.storage() != null) { + container.withEnv(CLUSTER_STEP_VARIABLE, "1"); + } String scratch = hiveScratchMirror(hive); if (scratch != null) { container.withEnv(HIVE_SCRATCH_VARIABLE, scratch); diff --git a/src/test/java/io/floci/aws/services/emr/EmrBootstrapRunnerTest.java b/src/test/java/io/floci/aws/services/emr/EmrBootstrapRunnerTest.java index d10c1fee38..5b1388db4a 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrBootstrapRunnerTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrBootstrapRunnerTest.java @@ -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")); diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java index 33a77bb2b9..a4ed62768b 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSparkRunnerTest.java @@ -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> 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> 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);