changeset 65:3cb4f95a4f6f

do not show credentials in the error messages
author Pavel.Sher
date Thu, 09 Jul 2009 21:21:36 +0400
parents c9f14e3ef86d
children 4add1cea853e
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTestCase.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommandTest.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/PushCommandTest.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/StatusCommandTest.java
diffstat 7 files changed, 129 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java	Thu Jul 02 20:34:56 2009 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java	Thu Jul 09 21:21:36 2009 +0400
@@ -1,10 +1,13 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
-import org.jetbrains.annotations.NotNull;
 import com.intellij.execution.configurations.GeneralCommandLine;
 import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.util.StringUtil;
 import jetbrains.buildServer.vcs.VcsException;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collections;
+import java.util.Set;
 
 /**
  * @author pavel
@@ -38,11 +41,11 @@
   }
 
   protected ExecResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
-    return CommandUtil.runCommand(cli);
+    return CommandUtil.runCommand(cli, getPrivateData());
   }
 
   protected ExecResult runCommand(@NotNull GeneralCommandLine cli, int executionTimeout) throws VcsException {
-    return CommandUtil.runCommand(cli, executionTimeout);
+    return CommandUtil.runCommand(cli, executionTimeout, getPrivateData());
   }
 
   protected void failIfNotEmptyStdErr(@NotNull GeneralCommandLine cli, @NotNull ExecResult res) throws VcsException {
@@ -50,4 +53,8 @@
       CommandUtil.commandFailed(cli.getCommandLineString(), res);
     }
   }
+
+  public Set<String> getPrivateData() {
+    return Collections.singleton(mySettings.getPassword());
+  }
 }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Thu Jul 02 20:34:56 2009 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Thu Jul 09 21:21:36 2009 +0400
@@ -23,6 +23,9 @@
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
+import java.util.Collections;
+import java.util.Set;
+
 public class CommandUtil {
   private static final int DEFAULT_COMMAND_TIMEOUT_SEC = 3600;
 
@@ -49,10 +52,14 @@
   }
 
   public static ExecResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
-    return runCommand(cli, DEFAULT_COMMAND_TIMEOUT_SEC);
+    return runCommand(cli, DEFAULT_COMMAND_TIMEOUT_SEC, Collections.<String>emptySet());
   }
 
-  public static ExecResult runCommand(@NotNull GeneralCommandLine cli, final int executionTimeout) throws VcsException {
+  public static ExecResult runCommand(@NotNull GeneralCommandLine cli, @NotNull Set<String> privateData) throws VcsException {
+    return runCommand(cli, DEFAULT_COMMAND_TIMEOUT_SEC, privateData);
+  }
+
+  public static ExecResult runCommand(@NotNull GeneralCommandLine cli, final int executionTimeout, @NotNull Set<String> privateData) throws VcsException {
     String cmdStr = cli.getCommandLineString();
     Loggers.VCS.debug("Run command: " + cmdStr);
     ExecResult res = SimpleCommandLineProcessRunner.runCommand(cli, null, new SimpleCommandLineProcessRunner.RunCommandEventsAdapter() {
@@ -61,8 +68,24 @@
         return executionTimeout;
       }
     });
+
+    cmdStr = removePrivateData(privateData, res, cmdStr);
+
     CommandUtil.checkCommandFailed(cmdStr, res);
     Loggers.VCS.debug(res.getStdout());
     return res;
   }
+
+  private static String removePrivateData(final Set<String> privateData, final ExecResult res, final String cmdStr) {
+    String newCmdStr = cmdStr;
+    for (String data: privateData) {
+      if (data == null || data.length() == 0) continue;
+      String stdout = res.getStdout().replace(data, "******");
+      res.setStdout(stdout);
+      String stderr = res.getStderr().replace(data, "******");
+      res.setStderr(stderr);
+      newCmdStr = newCmdStr.replace(data, "******");
+    }
+    return newCmdStr;
+  }
 }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java	Thu Jul 02 20:34:56 2009 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java	Thu Jul 09 21:21:36 2009 +0400
@@ -91,6 +91,14 @@
     return myHgCommandPath;
   }
 
+  public String getUsername() {
+    return myUsername;
+  }
+
+  public String getPassword() {
+    return myPassword;
+  }
+
   private final static Set<String> AUTH_PROTOS = new HashSet<String>();
   static {
     AUTH_PROTOS.add("http://");
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTestCase.java	Thu Jul 02 20:34:56 2009 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTestCase.java	Thu Jul 09 21:21:36 2009 +0400
@@ -18,31 +18,67 @@
 import jetbrains.buildServer.BaseTestCase;
 import jetbrains.buildServer.TempFiles;
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.LocalRepositoryUtil;
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.Constants;
 import jetbrains.buildServer.vcs.VcsException;
+import jetbrains.buildServer.vcs.VcsRoot;
+import jetbrains.buildServer.vcs.impl.VcsRootImpl;
 import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Map;
+import java.util.HashMap;
 
 public class BaseCommandTestCase extends BaseTestCase {
   private String myRepository;
+  private String myUsername;
+  private String myPassword;
+  private boolean myCloneRequired;
 
-  protected void setRepository(final String repository) {
+  protected void setRepository(final String repository, boolean cloneRequired) {
     myRepository = repository;
+    myCloneRequired = cloneRequired;
+  }
+
+  protected void setUsername(final String username) {
+    myUsername = username;
+  }
+
+  protected void setPassword(final String password) {
+    myPassword = password;
   }
 
   protected <T> T runCommand(CommandExecutor<T> executor) throws IOException, VcsException {
-    final Settings settings = new Settings();
-    settings.setHgCommandPath("mercurial-tests/testData/bin/hg.exe");
-    File repository = LocalRepositoryUtil.prepareRepository(new File(myRepository).getAbsolutePath());
-    settings.setRepository(repository.getAbsolutePath());
+    Map<String, String> vcsRootProps = new HashMap<String, String>();
+
+    vcsRootProps.put(Constants.REPOSITORY_PROP, myRepository);
+
+    if (myCloneRequired) {
+      File repository = LocalRepositoryUtil.prepareRepository(new File(myRepository).getAbsolutePath());
+      vcsRootProps.put(Constants.REPOSITORY_PROP, repository.getAbsolutePath());
+    }
+
+    vcsRootProps.put(Constants.HG_COMMAND_PATH_PROP, "mercurial-tests/testData/bin/hg.exe");
+    if (myUsername != null) {
+      vcsRootProps.put(Constants.USERNAME, myUsername);
+    }
+    if (myPassword != null) {
+      vcsRootProps.put(Constants.PASSWORD, myPassword);
+    }
+
     TempFiles tf = new TempFiles();
     File parentDir = tf.createTempDir();
-    settings.setWorkingDir(new File(parentDir, "rep").getAbsoluteFile());
+    final File workingDir = new File(parentDir, "rep").getAbsoluteFile();
+
+    VcsRoot vcsRoot = new VcsRootImpl(1, vcsRootProps);
+    Settings settings = new Settings(workingDir.getParentFile(), vcsRoot);
+    settings.setWorkingDir(workingDir);
     try {
-      CloneCommand cl = new CloneCommand(settings);
-      cl.setDestDir(settings.getLocalRepositoryDir().getAbsolutePath());
-      cl.execute();
+      if (myCloneRequired) {
+        CloneCommand cl = new CloneCommand(settings);
+        cl.setDestDir(settings.getLocalRepositoryDir().getAbsolutePath());
+        cl.execute();
+      }
 
       return executor.execute(settings);
     } finally {
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommandTest.java	Thu Jul 02 20:34:56 2009 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommandTest.java	Thu Jul 09 21:21:36 2009 +0400
@@ -15,16 +15,17 @@
  */
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
+import jetbrains.buildServer.vcs.VcsException;
+import org.jetbrains.annotations.NotNull;
+import org.testng.annotations.Test;
+
 import java.io.IOException;
 import java.util.List;
-import org.jetbrains.annotations.NotNull;
-import org.testng.annotations.Test;
-import jetbrains.buildServer.vcs.VcsException;
 
 @Test
 public class LogCommandTest extends BaseCommandTestCase {
   public void testOneChangeSet() throws Exception {
-    setRepository("mercurial-tests/testData/rep1");
+    setRepository("mercurial-tests/testData/rep1", true);
     final String toId = "9875b412a788";
     List<ChangeSet> changes = runLog(null, toId);
     assertEquals(1, changes.size());
@@ -36,7 +37,7 @@
   }
 
   public void testMoreThanOneChangeSet() throws Exception {
-    setRepository("mercurial-tests/testData/rep1");
+    setRepository("mercurial-tests/testData/rep1", true);
     final String fromId = "9875b412a788";
     final String toId = "7209b1f1d793";
     List<ChangeSet> changes = runLog(fromId, toId);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/PushCommandTest.java	Thu Jul 09 21:21:36 2009 +0400
@@ -0,0 +1,32 @@
+package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
+
+import jetbrains.buildServer.vcs.VcsException;
+import org.jetbrains.annotations.NotNull;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+
+/**
+ * @author Pavel.Sher
+ */
+@Test
+public class PushCommandTest extends BaseCommandTestCase {
+  public void hide_private_data() throws VcsException, IOException {
+    setRepository("http://some.host.com", false);
+    setUsername("user1");
+    final String password = "pwd1";
+    setPassword(password);
+
+    try {
+      runCommand(new CommandExecutor<Boolean>() {
+        public Boolean execute(@NotNull final Settings settings) throws VcsException {
+          PushCommand cmd = new PushCommand(settings);
+          cmd.execute();
+          return null;
+        }
+      });
+    } catch (VcsException e) {
+      assertFalse(e.getMessage().contains(password));
+    }
+  }
+}
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/StatusCommandTest.java	Thu Jul 02 20:34:56 2009 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/StatusCommandTest.java	Thu Jul 09 21:21:36 2009 +0400
@@ -26,7 +26,7 @@
 @Test
 public class StatusCommandTest extends BaseCommandTestCase {
   public void testAddedFile() throws IOException, VcsException {
-    setRepository("mercurial-tests/testData/rep1");
+    setRepository("mercurial-tests/testData/rep1", true);
     List<ModifiedFile> files = runStatus("9875b412a788", "1d446e82d356");
     assertEquals(1, files.size());
     ModifiedFile md = files.get(0);
@@ -35,7 +35,7 @@
   }
 
   public void testRemovedFile() throws IOException, VcsException {
-    setRepository("mercurial-tests/testData/rep1");
+    setRepository("mercurial-tests/testData/rep1", true);
     List<ModifiedFile> files = runStatus("7209b1f1d793", "9522278aa38d");
     assertEquals(1, files.size());
     ModifiedFile md = files.get(0);
@@ -44,7 +44,7 @@
   }
 
   public void testModifiedFile() throws IOException, VcsException {
-    setRepository("mercurial-tests/testData/rep1");
+    setRepository("mercurial-tests/testData/rep1", true);
     List<ModifiedFile> files = runStatus("9522278aa38d", "b06a290a363b");
     assertEquals(1, files.size());
     ModifiedFile md = files.get(0);