diff --git a/src/com/tw/go/plugin/cmd/Console.java b/src/com/tw/go/plugin/cmd/Console.java index 31a57e0..878a0f2 100644 --- a/src/com/tw/go/plugin/cmd/Console.java +++ b/src/com/tw/go/plugin/cmd/Console.java @@ -13,8 +13,12 @@ public static CommandLine createCommand(String... args) { gitCmd.addArguments(args); return gitCmd; } - + public static ConsoleResult runOrBomb(CommandLine commandLine, File workingDir, ProcessOutputStreamConsumer stdOut, ProcessOutputStreamConsumer stdErr) { + return runOrBomb(commandLine, workingDir, stdOut, stdErr, commandLine.toString()); + } + + public static ConsoleResult runOrBomb(CommandLine commandLine, File workingDir, ProcessOutputStreamConsumer stdOut, ProcessOutputStreamConsumer stdErr, String prettyMessage) { Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(stdOut, stdErr)); if (workingDir != null) { @@ -25,16 +29,16 @@ public static ConsoleResult runOrBomb(CommandLine commandLine, File workingDir, int exitCode = executor.execute(commandLine); if (exitCode != 0) { - throw new RuntimeException(getMessage("Error", commandLine, workingDir)); + throw new RuntimeException(getMessage("Error", prettyMessage, workingDir)); } return new ConsoleResult(exitCode, stdOut.output(), stdErr.output()); } catch (Exception e) { - throw new RuntimeException(getMessage("Exception", commandLine, workingDir), e); + throw new RuntimeException(getMessage("Exception", prettyMessage, workingDir), e); } } - private static String getMessage(String type, CommandLine commandLine, File workingDir) { - return String.format("%s Occurred: %s - %s", type, commandLine.toString(), workingDir); + private static String getMessage(String type, String prettyMessage, File workingDir) { + return String.format("%s Occurred: %s - %s", type, prettyMessage, workingDir); } } diff --git a/src/com/tw/go/plugin/git/GitCmdHelper.java b/src/com/tw/go/plugin/git/GitCmdHelper.java index fa7fb0c..7d2f4b0 100644 --- a/src/com/tw/go/plugin/git/GitCmdHelper.java +++ b/src/com/tw/go/plugin/git/GitCmdHelper.java @@ -40,7 +40,8 @@ public String version() { @Override public void checkConnection() { CommandLine gitCmd = Console.createCommand("ls-remote", gitConfig.getEffectiveUrl()); - runAndGetOutput(gitCmd); + CommandLine gitCmdMasked = Console.createCommand("ls-remote", gitConfig.getEffectiveMaskedUrl()); + runAndGetOutput(gitCmd, workingDir, gitCmdMasked.toString()); } @Override @@ -49,10 +50,14 @@ public void cloneRepository() { if (gitConfig.isShallowClone()) { args.add("--depth=1"); } + List maskedArgs = new ArrayList(args); args.add(gitConfig.getEffectiveUrl()); + maskedArgs.add(gitConfig.getEffectiveMaskedUrl()); args.add(workingDir.getAbsolutePath()); + maskedArgs.add(workingDir.getAbsolutePath()); CommandLine gitClone = Console.createCommand(ListUtil.toArray(args)); - runAndGetOutput(gitClone, null, stdOut, stdErr); + CommandLine gitCloneMasked = Console.createCommand(ListUtil.toArray(maskedArgs)); + runAndGetOutput(gitClone, null, stdOut, stdErr, gitCloneMasked.toString()); } @Override @@ -388,12 +393,20 @@ private ConsoleResult runOrBomb(CommandLine gitCmd) { private ConsoleResult runAndGetOutput(CommandLine gitCmd) { return runAndGetOutput(gitCmd, workingDir); } + + private ConsoleResult runAndGetOutput(CommandLine gitCmd, File workingDir, String prettyMessage) { + return runAndGetOutput(gitCmd, workingDir, new ProcessOutputStreamConsumer(new InMemoryConsumer()), new ProcessOutputStreamConsumer(new InMemoryConsumer()), prettyMessage); + } private ConsoleResult runAndGetOutput(CommandLine gitCmd, File workingDir) { return runAndGetOutput(gitCmd, workingDir, new ProcessOutputStreamConsumer(new InMemoryConsumer()), new ProcessOutputStreamConsumer(new InMemoryConsumer())); } private ConsoleResult runAndGetOutput(CommandLine gitCmd, File workingDir, ProcessOutputStreamConsumer stdOut, ProcessOutputStreamConsumer stdErr) { - return Console.runOrBomb(gitCmd, workingDir, stdOut, stdErr); + return runAndGetOutput(gitCmd, workingDir, stdOut, stdErr, gitCmd.toString()); + } + + private ConsoleResult runAndGetOutput(CommandLine gitCmd, File workingDir, ProcessOutputStreamConsumer stdOut, ProcessOutputStreamConsumer stdErr, String prettyMessage) { + return Console.runOrBomb(gitCmd, workingDir, stdOut, stdErr, prettyMessage); } } diff --git a/src/com/tw/go/plugin/model/GitConfig.java b/src/com/tw/go/plugin/model/GitConfig.java index b1c6baa..d8b3667 100644 --- a/src/com/tw/go/plugin/model/GitConfig.java +++ b/src/com/tw/go/plugin/model/GitConfig.java @@ -3,6 +3,8 @@ import com.tw.go.plugin.util.StringUtil; public class GitConfig { + private static final String MASKED_PASSWORD = "*****"; + private String url; private String username; private String password; @@ -42,10 +44,25 @@ public String getEffectiveUrl() { } return getUrl(); } + + public String getEffectiveMaskedUrl() { + if (isRemoteUrl() && hasCredentials()) { + return getUrlWithMaskedCredentials(); + } + return getUrl(); + } + + private String getUrlWithCredentials(String user, String pass) { + String[] parts = url.split("://"); + return String.format("%s://%s:%s@%s", parts[0], user, pass, parts[1]); + } public String getUrlWithCredentials() { - String[] parts = url.split("://"); - return String.format("%s://%s:%s@%s", parts[0], username, password, parts[1]); + return getUrlWithCredentials(username, password); + } + + public String getUrlWithMaskedCredentials() { + return getUrlWithCredentials(username, MASKED_PASSWORD); } public String getUrl() { diff --git a/src/com/tw/go/plugin/util/StringUtil.java b/src/com/tw/go/plugin/util/StringUtil.java index d83d443..8c75605 100644 --- a/src/com/tw/go/plugin/util/StringUtil.java +++ b/src/com/tw/go/plugin/util/StringUtil.java @@ -4,4 +4,12 @@ public class StringUtil { public static boolean isEmpty(String str) { return str == null || str.trim().isEmpty(); } + + public static String repeat(String str, int count) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < count; i++) { + result.append(str); + } + return result.toString(); + } } diff --git a/test/com/tw/go/plugin/model/GitConfigTest.java b/test/com/tw/go/plugin/model/GitConfigTest.java index 171f621..ba644ce 100644 --- a/test/com/tw/go/plugin/model/GitConfigTest.java +++ b/test/com/tw/go/plugin/model/GitConfigTest.java @@ -14,6 +14,15 @@ public void shouldGetEffectiveUrl() throws Exception { assertThat(new GitConfig("http://github.com/gocd/gocd", "username", "password", null).getEffectiveUrl(), is("http://username:password@github.com/gocd/gocd")); assertThat(new GitConfig("https://github.com/gocd/gocd", "username", "password", null).getEffectiveUrl(), is("https://username:password@github.com/gocd/gocd")); } + + @Test + public void shouldGetEffectiveMaskedUrl() throws Exception { + assertThat(new GitConfig("/tmp/git-repo", null, null, null).getEffectiveMaskedUrl(), is("/tmp/git-repo")); + assertThat(new GitConfig("/tmp/git-repo", "username", "password", null).getEffectiveMaskedUrl(), is("/tmp/git-repo")); + assertThat(new GitConfig("http://github.com/gocd/gocd", null, null, null).getEffectiveMaskedUrl(), is("http://github.com/gocd/gocd")); + assertThat(new GitConfig("http://github.com/gocd/gocd", "username", "password", null).getEffectiveMaskedUrl(), is("http://username:*****@github.com/gocd/gocd")); + assertThat(new GitConfig("https://github.com/gocd/gocd", "username", "password", null).getEffectiveMaskedUrl(), is("https://username:*****@github.com/gocd/gocd")); + } @Test public void shouldGetEffectiveBranch() throws Exception {