From a559b1635a91d661927781932b6aa7424b906fd7 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 10 Apr 2010 01:17:04 +0000 Subject: [PATCH] Fixed http://n4.nabble.com/Password-masking-when-running-commands-on-a-slave-tp1753033p1753033.html Mask wasn't sent properly to slaves. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@29920 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/Launcher.java | 8 ++- .../java/hudson/util/ArgumentListBuilder.java | 3 + .../org/jvnet/hudson/test/PretendSlave.java | 1 + .../hudson/util/ArgumentListBuilder2Test.java | 56 +++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 test/src/test/java/hudson/util/ArgumentListBuilder2Test.java diff --git a/core/src/main/java/hudson/Launcher.java b/core/src/main/java/hudson/Launcher.java index 32f919ee5f..0fcdaffe1b 100644 --- a/core/src/main/java/hudson/Launcher.java +++ b/core/src/main/java/hudson/Launcher.java @@ -713,7 +713,7 @@ public abstract class Launcher { final InputStream in = ps.stdin==null ? null : new RemoteInputStream(ps.stdin); final String workDir = ps.pwd==null ? null : ps.pwd.getRemote(); - return new RemoteProc(getChannel().callAsync(new RemoteLaunchCallable(ps.commands, ps.envs, in, out, err, workDir, listener))); + return new RemoteProc(getChannel().callAsync(new RemoteLaunchCallable(ps.commands, ps.masks, ps.envs, in, out, err, workDir, listener))); } public Channel launchChannel(String[] cmd, OutputStream err, FilePath _workDir, Map envOverrides) throws IOException, InterruptedException { @@ -756,6 +756,7 @@ public abstract class Launcher { private static class RemoteLaunchCallable implements Callable { private final List cmd; + private final boolean[] masks; private final String[] env; private final InputStream in; private final OutputStream out; @@ -763,8 +764,9 @@ public abstract class Launcher { private final String workDir; private final TaskListener listener; - RemoteLaunchCallable(List cmd, String[] env, InputStream in, OutputStream out, OutputStream err, String workDir, TaskListener listener) { + RemoteLaunchCallable(List cmd, boolean[] masks, String[] env, InputStream in, OutputStream out, OutputStream err, String workDir, TaskListener listener) { this.cmd = new ArrayList(cmd); + this.masks = masks; this.env = env; this.in = in; this.out = out; @@ -775,7 +777,7 @@ public abstract class Launcher { public Integer call() throws IOException { Launcher.ProcStarter ps = new LocalLauncher(listener).launch(); - ps.cmds(cmd).envs(env).stdin(in).stdout(out).stderr(err); + ps.cmds(cmd).masks(masks).envs(env).stdin(in).stdout(out).stderr(err); if(workDir!=null) ps.pwd(workDir); Proc p = ps.start(); diff --git a/core/src/main/java/hudson/util/ArgumentListBuilder.java b/core/src/main/java/hudson/util/ArgumentListBuilder.java index 15d096cf94..12bd9a18e6 100644 --- a/core/src/main/java/hudson/util/ArgumentListBuilder.java +++ b/core/src/main/java/hudson/util/ArgumentListBuilder.java @@ -47,6 +47,9 @@ import org.jvnet.animal_sniffer.IgnoreJRERequirement; */ public class ArgumentListBuilder implements Serializable { private final List args = new ArrayList(); + /** + * Bit mask indicating arguments that shouldn't be echoed-back (e.g., password) + */ private BitSet mask = new BitSet(); public ArgumentListBuilder() { diff --git a/test/src/main/java/org/jvnet/hudson/test/PretendSlave.java b/test/src/main/java/org/jvnet/hudson/test/PretendSlave.java index b0e0336292..13f05d427f 100644 --- a/test/src/main/java/org/jvnet/hudson/test/PretendSlave.java +++ b/test/src/main/java/org/jvnet/hudson/test/PretendSlave.java @@ -18,6 +18,7 @@ import java.util.Collections; * Slave that pretends to fork processes. * * @author Kohsuke Kawaguchi + * @see HudsonTestCase#createPretendSlave(FakeLauncher) */ public class PretendSlave extends Slave { private transient FakeLauncher faker; diff --git a/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java b/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java new file mode 100644 index 0000000000..b8edacc77a --- /dev/null +++ b/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java @@ -0,0 +1,56 @@ +/* + * The MIT License + * + * Copyright (c) 2010, Kohsuke Kawaguchi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package hudson.util; + +import hudson.Launcher.RemoteLauncher; +import hudson.model.Slave; +import org.jvnet.hudson.test.Email; +import org.jvnet.hudson.test.HudsonTestCase; + +import java.io.StringWriter; + +/** + * + * @author Kohsuke Kawaguchi + */ +public class ArgumentListBuilder2Test extends HudsonTestCase { + /** + * Makes sure {@link RemoteLauncher} properly masks arguments. + */ + @Email("http://n4.nabble.com/Password-masking-when-running-commands-on-a-slave-tp1753033p1753033.html") + public void testSlaveMask() throws Exception { + ArgumentListBuilder args = new ArgumentListBuilder(); + args.add("java"); + args.addMasked("-version"); + + Slave s = createSlave(); + s.toComputer().connect(false).get(); + + StringWriter out = new StringWriter(); + assertEquals(0,s.createLauncher(new StreamTaskListener(out)).launch().cmds(args).join()); + System.out.println(out); + assertTrue(out.toString().contains("$ java ********")); + } +} -- GitLab