Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-49197][CORE] Redact Spark Command output in launcher module #47704

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Helper methods for command builders.
Expand All @@ -30,6 +33,11 @@ class CommandBuilderUtils {
static final String DEFAULT_MEM = "1g";
static final String DEFAULT_PROPERTIES_FILE = "spark-defaults.conf";
static final String ENV_SPARK_HOME = "SPARK_HOME";
// This should be consistent with org.apache.spark.internal.config.SECRET_REDACTION_PATTERN
// We maintain this copy to avoid depending on `core` module.
static final String SECRET_REDACTION_PATTERN = "(?i)secret|password|token|access[.]?key";
static final Pattern redactPattern = Pattern.compile(SECRET_REDACTION_PATTERN);
static final Pattern keyValuePattern = Pattern.compile("-D(.+?)=(.+)");

/** Returns whether the given string is null or empty. */
static boolean isEmpty(String s) {
Expand Down Expand Up @@ -326,4 +334,23 @@ static String findJarsDir(String sparkHome, String scalaVersion, boolean failIfN
return libdir.getAbsolutePath();
}

/**
* Redact a command-line argument's value part which matches `-Dkey=value` pattern.
* Note that this should be consistent with `org.apache.spark.util.Utils.redactCommandLineArgs`.
*/
static List<String> redactCommandLineArgs(List<String> args) {
return args.stream().map(CommandBuilderUtils::redact).collect(Collectors.toList());
}

/**
* Redact a command-line argument's value part which matches `-Dkey=value` pattern.
*/
static String redact(String arg) {
Matcher m = keyValuePattern.matcher(arg);
if (m.find() && redactPattern.matcher(m.group(1)).find()) {
return String.format("-D%s=%s", m.group(1), "*********(redacted)");
} else {
return arg;
}
}
}
2 changes: 1 addition & 1 deletion launcher/src/main/java/org/apache/spark/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static List<String> buildCommand(
boolean printLaunchCommand) throws IOException, IllegalArgumentException {
List<String> cmd = builder.buildCommand(env);
if (printLaunchCommand) {
System.err.println("Spark Command: " + join(" ", cmd));
System.err.println("Spark Command: " + join(" ", redactCommandLineArgs(cmd)));
System.err.println("========================================");
}
return cmd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public void testInvalidOptionStrings() {
testInvalidOpt("'abcde");
}

@Test
public void testRedactCommandLineArgs() {
assertEquals(redact("secret"), "secret");
assertEquals(redact("-Dk=v"), "-Dk=v");
assertEquals(redact("-Dk=secret"), "-Dk=secret");
assertEquals(redact("-DsecretKey=my-secret"), "-DsecretKey=*********(redacted)");
assertEquals(redactCommandLineArgs(Arrays.asList("-DsecretKey=my-secret")),
Arrays.asList("-DsecretKey=*********(redacted)"));
}

@Test
public void testWindowsBatchQuoting() {
assertEquals("abc", quoteForBatchScript("abc"));
Expand Down