From 41d09e0fa0b61e61bc3e2393b1fc2f1d85893ae2 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 9 Nov 2023 07:09:00 -0800 Subject: [PATCH] Stop recommending `-jnlpUrl` (#8639) --- .../main/java/hudson/slaves/JNLPLauncher.java | 56 +++++++++++++++++++ .../hudson/slaves/JNLPLauncher/main.jelly | 33 ++++++----- src/checkstyle/checkstyle-configuration.xml | 2 +- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/hudson/slaves/JNLPLauncher.java b/core/src/main/java/hudson/slaves/JNLPLauncher.java index e8bf009eb9b3..917097d8267b 100644 --- a/core/src/main/java/hudson/slaves/JNLPLauncher.java +++ b/core/src/main/java/hudson/slaves/JNLPLauncher.java @@ -24,6 +24,8 @@ package hudson.slaves; +import com.google.common.escape.Escaper; +import com.google.common.escape.Escapers; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -38,6 +40,7 @@ import jenkins.slaves.RemotingWorkDirSettings; import jenkins.util.SystemProperties; import jenkins.websocket.WebSockets; +import org.apache.commons.lang.StringUtils; import org.jenkinsci.Symbol; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -195,6 +198,59 @@ public void launch(SlaveComputer computer, TaskListener listener) { @Restricted(NoExternalUse.class) public static /*almost final*/ Descriptor DESCRIPTOR; + @NonNull + @Restricted(NoExternalUse.class) + public String getRemotingOptionsUnix(@NonNull Computer computer) { + return getRemotingOptions(escapeUnix(computer.getName())); + } + + @NonNull + @Restricted(NoExternalUse.class) + public String getRemotingOptionsWindows(@NonNull Computer computer) { + return getRemotingOptions(escapeWindows(computer.getName())); + } + + private String getRemotingOptions(String computerName) { + StringBuilder sb = new StringBuilder(); + sb.append("-name "); + sb.append(computerName); + sb.append(' '); + if (isWebSocket()) { + sb.append("-webSocket "); + } + if (tunnel != null) { + sb.append(" -tunnel "); + sb.append(tunnel); + sb.append(' '); + } + return sb.toString(); + } + + /** + * {@link Jenkins#checkGoodName(String)} saves us from most troublesome characters, but we still have to deal with + * spaces and therefore with double quotes and backticks. + */ + private static String escapeUnix(String input) { + if (StringUtils.isAlphanumeric(input)) { + return input; + } + Escaper escaper = + Escapers.builder().addEscape('"', "\\\"").addEscape('`', "\\`").build(); + return "\"" + escaper.escape(input) + "\""; + } + + /** + * {@link Jenkins#checkGoodName(String)} saves us from most troublesome characters, but we still have to deal with + * spaces and therefore with double quotes. + */ + private static String escapeWindows(String input) { + if (StringUtils.isAlphanumeric(input)) { + return input; + } + Escaper escaper = Escapers.builder().addEscape('"', "\\\"").build(); + return "\"" + escaper.escape(input) + "\""; + } + /** * Gets work directory options as a String. * diff --git a/core/src/main/resources/hudson/slaves/JNLPLauncher/main.jelly b/core/src/main/resources/hudson/slaves/JNLPLauncher/main.jelly index 96066cc8e216..621d9ffdf068 100644 --- a/core/src/main/resources/hudson/slaves/JNLPLauncher/main.jelly +++ b/core/src/main/resources/hudson/slaves/JNLPLauncher/main.jelly @@ -35,71 +35,74 @@ THE SOFTWARE. - + +

${%slaveAgent.cli.run} (Unix) - +

 ${copy_agent_jar_unix}
-${copy_java_cmd}
+${copy_java_cmd_unix}
 

${%slaveAgent.cli.run} (Windows) - +

 ${copy_agent_jar_windows}
-${copy_java_cmd}
+${copy_java_cmd_windows}
 
- + +

${%slaveAgent.cli.run} (Unix) - +

 ${copy_agent_jar_unix}
-${copy_java_cmd_secret}
+${copy_java_cmd_secret_unix}
 

${%slaveAgent.cli.run} (Windows) - +

 ${copy_agent_jar_windows}
-${copy_java_cmd_secret}
+${copy_java_cmd_secret_windows}
 
- + +

${%slaveAgent.cli.run.secret} (Unix) - +

 ${copy_secret_to_file}
 ${copy_agent_jar_unix}
-${copy_java_cmd_secret2}
+${copy_java_cmd_secret2_unix}
 

${%slaveAgent.cli.run.secret} (Windows) - +

 ${copy_secret_to_file}
 ${copy_agent_jar_windows}
-${copy_java_cmd_secret2}
+${copy_java_cmd_secret2_windows}
 
diff --git a/src/checkstyle/checkstyle-configuration.xml b/src/checkstyle/checkstyle-configuration.xml index d2454e254844..92261fb123bf 100644 --- a/src/checkstyle/checkstyle-configuration.xml +++ b/src/checkstyle/checkstyle-configuration.xml @@ -58,7 +58,7 @@ - +