private void addCredentials(ArrayList<String> argList) { String userName = material.getUserName(); if (!StringUtils.isBlank(userName)) { argList.add("--username"); argList.add(userName); if (!StringUtils.isBlank(material.getPassword())) { argList.add("--password"); argList.add(material.getPassword()); } } }
@Override public MaterialConfig config() { return new SvnMaterialConfig(url, userName, getPassword(), checkExternals, goCipher, autoUpdate, filter, invertFilter, folder, name); }
@Override public Map<String, Object> getAttributes(boolean addSecureFields) { Map<String, Object> materialMap = new HashMap<>(); materialMap.put("type", "svn"); Map<String, Object> configurationMap = new HashMap<>(); if (addSecureFields) { configurationMap.put("url", url.forCommandline()); } else { configurationMap.put("url", url.forDisplay()); } configurationMap.put("username", userName); if (addSecureFields) { configurationMap.put("password", getPassword()); } configurationMap.put("check-externals", checkExternals); materialMap.put("svn-configuration", configurationMap); return materialMap; }
private SvnMaterial buildMockSvnMaterial(String url, String username, String password) { final SvnMaterial svnMaterial = mock(SvnMaterial.class); when(svnMaterial.getUrl()).thenReturn(url); when(svnMaterial.getUserName()).thenReturn(username); when(svnMaterial.getPassword()).thenReturn(password); return svnMaterial; } }
private Subversion svn() { if (svnLazyLoaded == null || !svnLazyLoaded.getUrl().equals(url)) { svnLazyLoaded = new SvnCommand(getFingerprint(), url.forCommandline(), userName, getPassword(), checkExternals); } return svnLazyLoaded; }
@Test public void shouldNotDecryptSvnPasswordIfPasswordIsNotNull() throws Exception { GoCipher mockGoCipher = mock(GoCipher.class); when(mockGoCipher.encrypt("password")).thenReturn("encrypted"); when(mockGoCipher.decrypt("encrypted")).thenReturn("password"); SvnMaterial material = new SvnMaterial("/foo", "username", "password", false, mockGoCipher); material.ensureEncrypted(); when(mockGoCipher.encrypt("new_password")).thenReturn("new_encrypted"); material.setPassword("new_password"); when(mockGoCipher.decrypt("new_encrypted")).thenReturn("new_password"); assertThat(material.getPassword(), is("new_password")); }
@Test public void shouldEncryptSvnPasswordAndMarkPasswordAsNull() throws Exception { GoCipher mockGoCipher = mock(GoCipher.class); when(mockGoCipher.encrypt("password")).thenReturn("encrypted"); when(mockGoCipher.maybeReEncryptForPostConstructWithoutExceptions("encrypted")).thenReturn("encrypted"); SvnMaterial material = new SvnMaterial("/foo", "username", "password", false, mockGoCipher); material.ensureEncrypted(); assertThat(material.getPassword(), is(nullValue())); assertThat(material.getEncryptedPassword(), is("encrypted")); }
@Test public void shouldDecryptSvnPassword() throws Exception { GoCipher mockGoCipher = mock(GoCipher.class); when(mockGoCipher.decrypt("encrypted")).thenReturn("password"); when(mockGoCipher.maybeReEncryptForPostConstructWithoutExceptions("encrypted")).thenReturn("encrypted"); SvnMaterial material = new SvnMaterial("/foo", "username", null, false, mockGoCipher); ReflectionUtil.setField(material, "encryptedPassword", "encrypted"); material.ensureEncrypted(); assertThat(material.getPassword(), is("password")); }
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 public void shouldErrorOutIfDecryptionFails() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String fakeCipherText = "fake cipher text"; when(mockGoCipher.decrypt(fakeCipherText)).thenThrow(new CryptoException("exception")); SvnMaterial material = new SvnMaterial("/foo", "username", null, false, mockGoCipher); ReflectionUtil.setField(material, "encryptedPassword", fakeCipherText); try { material.getPassword(); fail("Should have thrown up"); } catch (Exception e) { assertThat(e.getMessage(), is("Could not decrypt the password to get the real password")); } }
when(svnMaterial.getUrl()).thenReturn("http://localhost/svn/project1"); when(svnMaterial.getUserName()).thenReturn("user"); when(svnMaterial.getPassword()).thenReturn("password"); final ConsoleResult consoleResult = mock(ConsoleResult.class); when(consoleResult.outputAsString()).thenReturn(svnInfoOutput);
verify(svnMaterial1).getPassword(); verify(svnMaterial2).getUserName(); verify(svnMaterial2).getPassword();
public BuildCommand updateTo(String baseDir, RevisionContext revisionContext) { Revision revision = revisionContext.getLatestRevision(); String workingDir = material.workingdir(new File(baseDir)).getPath(); UrlArgument url = material.getUrlArgument(); return compose( echoWithPrefix(format("Start updating %s at revision %s from %s", material.updatingTarget(), revision.getRevision(), url.forDisplay())), secret(url.forCommandline(), url.forDisplay()), secret(material.getPassword(), "*********************"), cleanupAndUpdate(workingDir, revision).setTest(shouldDoCleanupAndUpdate(workingDir)), freshCheckout(workingDir, revision).setTest(isNotRepository(workingDir)), freshCheckout(workingDir, revision).setTest(test("-nd", workingDir)), freshCheckout(workingDir, revision).setTest(repoUrlChanged(workingDir)), echoWithPrefix(format("Done.\n")) ); }