diff --git a/client/src/main/java/hudson/plugins/swarm/Client.java b/client/src/main/java/hudson/plugins/swarm/Client.java index 3792bfdd..19bcf165 100644 --- a/client/src/main/java/hudson/plugins/swarm/Client.java +++ b/client/src/main/java/hudson/plugins/swarm/Client.java @@ -11,7 +11,6 @@ import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; @@ -216,10 +215,9 @@ static void run(SwarmClient swarmClient, Options options, String... args) throws /* * Note that any instances of InterruptedException or RuntimeException thrown - * internally by the next two lines get wrapped in RetryException. + * internally by the next line get wrapped in RetryException. */ - List jnlpArgs = swarmClient.getJnlpArgs(url); - swarmClient.connect(jnlpArgs, url); + swarmClient.connect(url); if (options.noRetryAfterConnected) { logger.warning("Connection closed, exiting..."); swarmClient.exitWithStatus(0); diff --git a/client/src/main/java/hudson/plugins/swarm/SwarmClient.java b/client/src/main/java/hudson/plugins/swarm/SwarmClient.java index e7a9ef3c..dd4743ff 100644 --- a/client/src/main/java/hudson/plugins/swarm/SwarmClient.java +++ b/client/src/main/java/hudson/plugins/swarm/SwarmClient.java @@ -69,6 +69,7 @@ public class SwarmClient { private final Options options; private final String hash; + private String secret; private String name; private HttpServer prometheusServer = null; @@ -125,8 +126,10 @@ public URL getUrl() { /** * @param url The URL * @return The JNLP arguments, as returned from {@link Launcher#parseJnlpArguments()}. + * @deprecated removed without replacement */ - List getJnlpArgs(URL url) throws IOException, RetryException { + @Deprecated + private List getJnlpArgs(URL url) throws IOException, RetryException { logger.fine("connect() invoked"); Launcher launcher = new Launcher(); @@ -157,14 +160,22 @@ List getJnlpArgs(URL url) throws IOException, RetryException { * *

Interrupt the thread to abort it and try connecting again. */ - void connect(List jnlpArgs, URL url) throws IOException, RetryException { + void connect(URL url) throws IOException, RetryException { List args = new ArrayList<>(); args.add("-url"); args.add(url.toString()); - args.add("-secret"); - args.add(jnlpArgs.get(0)); + if (secret == null) { + List jnlpArgs = getJnlpArgs(url); + if (!jnlpArgs.isEmpty()) { + secret = jnlpArgs.get(0); + } + } + if (secret != null) { + args.add("-secret"); + args.add(secret); + } args.add("-name"); args.add(name); @@ -368,6 +379,11 @@ void createSwarmAgent(URL url) throws IOException, InterruptedException, RetryEx props.load(stream); } + String secret = props.getProperty("secret"); + if (secret != null) { + this.secret = secret.trim(); + } + String name = props.getProperty("name"); if (name == null) { this.name = options.name; diff --git a/plugin/src/main/java/hudson/plugins/swarm/PluginImpl.java b/plugin/src/main/java/hudson/plugins/swarm/PluginImpl.java index 9f89b898..a436c726 100644 --- a/plugin/src/main/java/hudson/plugins/swarm/PluginImpl.java +++ b/plugin/src/main/java/hudson/plugins/swarm/PluginImpl.java @@ -22,6 +22,7 @@ import java.util.Set; import javax.servlet.http.HttpServletResponse; import jenkins.model.Jenkins; +import jenkins.slaves.JnlpAgentReceiver; import org.apache.commons.lang.ArrayUtils; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; @@ -135,6 +136,7 @@ public void doCreateSlave( Jenkins jenkins = Jenkins.get(); jenkins.checkPermission(Computer.CREATE); + jenkins.checkPermission(Computer.CONNECT); List> nodeProperties = new ArrayList<>(); @@ -210,6 +212,7 @@ public void doCreateSlave( try (OutputStream outputStream = rsp.getCompressedOutputStream(req)) { Properties props = new Properties(); props.put("name", name); + props.put("secret", JnlpAgentReceiver.SLAVE_SECRET.mac(name)); props.store(outputStream, ""); } } catch (FormException e) {