public CommandLine argPassword(String password) { arguments.add(new PasswordArgument(password)); return this; }
private void addCredentials(CommandLine line, StringArgument svnUserName, PasswordArgument svnPassword) { if (!StringUtils.isBlank(svnUserName.forCommandline())) { line.withArgs("--username", svnUserName.forCommandline()); if (!StringUtils.isBlank(svnPassword.forCommandline())) { line.withArg("--password"); line.withArg(svnPassword); } line.withNonArgSecret(svnPassword); } }
public String getPassword() { return password == null ? null : password.forDisplay(); }
@Test public void shouldReturnSampNumberOfStarsForAnyPassword() throws Exception { assertThat(new PasswordArgument("foo").toString(), is("******")); assertThat(new PasswordArgument("very very long password").toString(), is("******")); }
public List<SecretString> secrets() { List<SecretString> passwordArguments = new ArrayList<>(); for (EnvironmentVariable secureEnvironmentVariable : getSecureEnvironmentVariables()) { passwordArguments.add(new PasswordArgument(secureEnvironmentVariable.value())); } return passwordArguments; }
public SvnCommand(String materialFingerprint, String url, String userName, String password, boolean checkExternals) { super(materialFingerprint); this.repositoryUrl = url == null ? null : new UrlArgument(url); this.checkExternals = checkExternals; this.userName = new StringArgument(userName); this.password = new PasswordArgument(password); this.svnLogXmlParser = new SvnLogXmlParser(); }
@Test public void shouldReplaceSecretInfoShouldNotFailForNull() { ArrayList<CommandArgument> commands = new ArrayList<>(); commands.add(new PasswordArgument("foo")); ArrayList<SecretString> secretStrings = new ArrayList<>(); secretStrings.add(new PasswordArgument("foo")); ConsoleResult result = new ConsoleResult(10, new ArrayList<>(), new ArrayList<>(), commands, secretStrings); assertThat(result.replaceSecretInfo(null), is(nullValue())); }
CommandLine p4(String command, String... params) { Map<String, String> env = new HashMap<>(); env.put("P4PORT", p4Port); env.put("P4CLIENT", p4ClientName); if (hasUser()) { env.put("P4USER", p4user); } if (usingPassword()) { env.put("P4PASSWD", p4passwd); } CommandLine line = createCommandLine("p4") .withArgs(command).withArgs(params) .withEnv(env) .withEncoding("UTF-8"); if (hasPassword()) { line = line.withNonArgSecret(new PasswordArgument(p4passwd)); } return line; }
@Before public void setUp() throws Exception { request = mock(GoApiRequest.class); descriptor = mock(GoPluginDescriptor.class); goPublisher = mock(DefaultGoPublisher.class); EnvironmentVariableContext environmentVariableContext = mock(EnvironmentVariableContext.class); when(environmentVariableContext.secrets()).thenReturn(Collections.singletonList(new PasswordArgument("secret.value"))); artifactRequestProcessorForPublish = ArtifactRequestProcessor.forPublishArtifact(goPublisher, environmentVariableContext); artifactRequestProcessorForFetch = ArtifactRequestProcessor.forFetchArtifact(goPublisher, environmentVariableContext); when(request.apiVersion()).thenReturn(getRequestPluginVersion()); when(descriptor.id()).thenReturn("cd.go.artifact.docker"); when(request.api()).thenReturn(CONSOLE_LOG.requestName()); }
@Test @SuppressWarnings({"ThrowableInstanceNeverThrown"}) public void shouldSmudgeExceptionMessagesForNestedExceptions() { List<CommandArgument> args = Arrays.asList(new StringArgument("foo"), new PasswordArgument("bar")); List<SecretString> secrets = Arrays.asList((SecretString) new PasswordArgument("quux")); ConsoleResult result = new ConsoleResult(0, Arrays.asList(" foo ", " bar ", " baz ", " abc "), Arrays.asList(" quux ", " bang "), args, secrets); Exception innerException = new Exception("baz quux baz"); Exception topException = new RuntimeException("foo bar abc", innerException); Exception exception = result.smudgedException(topException); assertThat(exception.getMessage(), is("foo ****** abc")); assertThat(exception, sameInstance(topException)); assertThat(exception.getCause().getMessage(), is("baz ****** baz")); assertThat(exception.getCause(), sameInstance(innerException)); }
@Test public void shouldDescribeResult() { List<CommandArgument> args = Arrays.asList(new StringArgument("foo"), new PasswordArgument("bar")); List<SecretString> secrets = Arrays.asList((SecretString) new PasswordArgument("quux")); ConsoleResult result = new ConsoleResult(42, Arrays.asList(" foo ", " bar ", " baz ", " abc "), Arrays.asList(" quux ", " bang "), args, secrets); assertThat(result.describe(), containsString("--- EXIT CODE (42) ---")); assertThat(result.describe(), containsString("--- STANDARD OUT ---")); assertThat(result.describe(), containsString("--- STANDARD ERR ---")); } }
public HashMap<String, String> createUrlToRemoteUUIDMap(Set<SvnMaterial> svnMaterials) { HashMap<String, String> urlToUUIDMap = new HashMap<>(); for (SvnMaterial svnMaterial : svnMaterials) { CommandLine command = svnExecutable().withArgs("info", "--xml"); addCredentials(command, new StringArgument(svnMaterial.getUserName()), new PasswordArgument(svnMaterial.getPassword())); final String queryUrl = svnMaterial.getUrl(); command.withArg(queryUrl); ConsoleResult consoleResult = null; try { consoleResult = executeCommand(command); urlToUUIDMap.putAll(svnLogXmlParser.parseInfoToGetUUID(consoleResult.outputAsString(), queryUrl, getBuilder())); } catch (RuntimeException e) { LOG.warn("Failed to map UUID to URL. SVN post-commit will not work for materials with URL {}", queryUrl, e); } } return urlToUUIDMap; }
@Test @RunIf(value = EnhancedOSChecker.class, arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS}) public void shouldNotLogPasswordsOnExceptionThrown() throws IOException { File dir = temporaryFolder.newFolder(); File file = new File(dir, "test.sh"); FileOutputStream out = new FileOutputStream(file); out.write("echo $1 && exit 10".getBytes()); out.close(); CommandLine line = CommandLine.createCommandLine("/bin/sh").withArg(file.getAbsolutePath()).withArg(new PasswordArgument("secret")).withEncoding("utf-8"); try { line.runOrBomb(null); } catch (CommandLineException e) { assertThat(e.getMessage(), not(containsString("secret"))); } }
@Test public void shouldShowPasswordsInDescribeAsStars() throws IOException { HashMap<String, String> map = new HashMap<>(); map.put("password1", "secret"); map.put("password2", "secret"); CommandLine line = CommandLine.createCommandLine("echo") .withArg("My Password is:") .withEnv(map) .withArg(new PasswordArgument("secret")) .withArg(new PasswordArgument("new-pwd")) .withEncoding("utf-8"); line.addInput(new String[]{"my pwd is: new-pwd "}); assertThat(line.describe(), not(containsString("secret"))); assertThat(line.describe(), not(containsString("new-pwd"))); }
@Test public void shouldReportPasswordsOnTheLogAsStars() { CommandLine line = CommandLine.createCommandLine("notexist").withArg(new PasswordArgument("secret")).withEncoding("utf-8"); assertThat(line.toString(), not(containsString("secret"))); }
@Test public void shouldShowPasswordsInToStringForDisplayAsStars() throws IOException { CommandLine line = CommandLine.createCommandLine("echo") .withArg("My Password is:") .withArg(new PasswordArgument("secret")) .withEncoding("utf-8"); assertThat(line.toStringForDisplay(), not(containsString("secret"))); }
@Test public void shouldReplaceSecretsInErrors() throws CheckedCommandLineException { CommandLine command = CommandLine.createCommandLine("notexist").withEncoding("utf-8").withArg("My password is ").withArg( new PasswordArgument("secret")); InMemoryConsumer output = new InMemoryConsumer(); try { command.runScript(new ExecScript("FOO"), output, new EnvironmentVariableContext(), null); fail("Exception expected"); } catch (Exception e) { assertThat(e.getMessage(), not(containsString("secret"))); } }
@Test @RunIf(value = EnhancedOSChecker.class, arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS}) public void shouldNotLogPasswordsFromStream() { try (LogFixture logFixture = logFixtureFor(CommandLine.class, Level.DEBUG)) { CommandLine line = CommandLine.createCommandLine("/bin/echo").withArg("=>").withArg(new PasswordArgument("secret")).withEncoding("utf-8"); line.runOrBomb(null); assertThat(logFixture.getLog(), not(containsString("secret"))); assertThat(logFixture.getLog(), containsString("=> ******")); } }
@Test public void shouldLogPasswordsOnTheLogAsStars() { try (LogFixture logFixture = logFixtureFor(ProcessManager.class, Level.DEBUG)) { CommandLine line = CommandLine.createCommandLine("notexist").withArg(new PasswordArgument("secret")).withEncoding("utf-8"); try { line.runOrBomb(null); } catch (Exception e) { //ignored } assertThat(logFixture.getLog(), containsString("notexist ******")); } }
@Test @RunIf(value = OSChecker.class, arguments = OSChecker.LINUX) public void shouldReplaceSecretsOnTheOutputUnderLinux() throws CheckedCommandLineException { CommandLine command = CommandLine.createCommandLine("echo").withArg("My password is ").withArg( new PasswordArgument("secret")).withEncoding("utf-8"); InMemoryConsumer output = new InMemoryConsumer(); command.runScript(new ExecScript("FOO"), output, new EnvironmentVariableContext(), null); assertThat(output.toString(), not(containsString("secret"))); }