/** * Authenticate {@code username} using the {@code "password"} authentication method and as a fallback basic * challenge-response authentication. * * @param username user to authenticate * @param password the password to use for authentication * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPassword(String username, String password) throws UserAuthException, TransportException { authPassword(username, password.toCharArray()); }
/** * Authenticate {@code username} using the {@code "password"} authentication method and as a fallback basic * challenge-response authentication.. The {@code password} array is blanked out after use. * * @param username user to authenticate * @param password the password to use for authentication * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPassword(final String username, final char[] password) throws UserAuthException, TransportException { try { authPassword(username, new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return password.clone(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }); } finally { PasswordUtils.blankOut(password); } }
private SSHClient getFixtureClient() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", "jeroen"); return sshClient; }
@Test public void shouldHandlePasswordChangeWithWrongPasswordOnFirstAttempt() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return "changeme".toCharArray(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }, new StaticPasswordUpdateProvider("bad", "jeroen")); assertThat("Should have been authenticated", sshClient.isAuthenticated()); }
@Test public void shouldNotHandlePasswordChangeIfNoPasswordUpdateProviderSet() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); expectedException.expect(UserAuthException.class); sshClient.authPassword("jeroen", "changeme"); assertThat("Should not have authenticated", !sshClient.isAuthenticated()); }
@Test public void shouldExecuteBackgroundCommand() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", "jeroen"); File file = new File(temp.getRoot(), "testdir"); assertThat("File should not exist", !file.exists()); // TODO figure out why this does not really execute in the background. Session.Command exec = sshClient.startSession().exec("mkdir " + file.getPath() + " &"); exec.join(); assertThat("File should exist", file.exists()); assertThat("File should be directory", file.isDirectory()); SFTPClient sftpClient = sshClient.newSFTPClient(); if (sftpClient.statExistence("&") != null) { sftpClient.rmdir("&"); // TODO fail here when this is fixed } } }
@Test public void shouldHandlePasswordChange() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return "changeme".toCharArray(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }, new StaticPasswordUpdateProvider("jeroen")); assertThat("Should be authenticated", sshClient.isAuthenticated()); }
@Test public void shouldNotGoOutOfBoundsInReadAheadInputStream() throws IOException { SSHClient ssh = fixture.setupConnectedDefaultClient(); ssh.authPassword("test", "test"); SFTPEngine sftp = new SFTPEngine(ssh).init();
@Before public void init() throws IOException { sourceFile = tempFolder.newFile(DEFAULT_FILE_NAME); FileUtil.writeToFile(sourceFile, "This is my file"); targetDir = tempFolder.newFolder(); targetFile = new File(targetDir, DEFAULT_FILE_NAME); sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("test", "test"); }
@Test public void shouldHandlePasswordChangeWithWrongPassword() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); expectedException.expect(UserAuthException.class); sshClient.authPassword("jeroen", new PasswordFinder() { @Override public char[] reqPassword(Resource<?> resource) { return "changeme".toCharArray(); } @Override public boolean shouldRetry(Resource<?> resource) { return false; } }, new StaticPasswordUpdateProvider("bad")); assertThat("Should not have authenticated", !sshClient.isAuthenticated()); }
@Test public void shouldCorrectlyHandleSessionChannelEof() throws IOException, InterruptedException { fixture.setupConnectedDefaultClient().authPassword("jeroen", "jeroen"); Session session = fixture.getClient().startSession(); session.allocateDefaultPTY(); session.close(); Thread.sleep(1000); assertThat("Should still be connected", fixture.getClient().isConnected()); }
@Test @Category({SlowTests.class, KnownFailingTests.class}) public void shouldCorrectlyTerminateThreadOnDisconnect() throws IOException, InterruptedException { DefaultConfig defaultConfig = new DefaultConfig(); defaultConfig.setKeepAliveProvider(KeepAliveProvider.KEEP_ALIVE); for (int i = 0; i < 10; i++) { SSHClient sshClient = fixture.setupClient(defaultConfig); fixture.connectClient(sshClient); sshClient.getConnection().getKeepAlive().setKeepAliveInterval(1); try { sshClient.authPassword("bad", "credentials"); fail("Should not auth."); } catch (UserAuthException e) { // OK } fixture.stopClient(); Thread.sleep(2000); } ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); for (long l : threadMXBean.getAllThreadIds()) { ThreadInfo threadInfo = threadMXBean.getThreadInfo(l); if (threadInfo.getThreadName().equals("keep-alive") && threadInfo.getThreadState() != Thread.State.TERMINATED) { fail("Found alive keep-alive thread in state " + threadInfo.getThreadState()); } } } }
@Test public void authenticated() throws UserAuthException, TransportException { fixture.getClient().authPassword("dummy", "dummy"); assertTrue(fixture.getClient().isAuthenticated()); }
@Test public void shouldNotThrowTimeoutOnDisconnect() throws IOException { fixture.getClient().authPassword("u", "u"); Session session = fixture.getClient().startSession(); session.allocateDefaultPTY(); Session.Shell shell = session.startShell(); session.close(); fixture.getClient().disconnect(); } }
private void build() throws IOException { if (init) { return; } ssh = new SSHClient(); ssh.addHostKeyVerifier(new HostKeyVerifier() { @Override public boolean verify(String arg0, int arg1, PublicKey arg2) { return true; } }); ssh.connect(hostname, port); if (privateKey != null) { privateKeyFile = File.createTempFile("zstack", "tmp"); FileUtils.writeStringToFile(privateKeyFile, privateKey); ssh.authPublickey(username, privateKeyFile.getAbsolutePath()); } else { ssh.authPassword(username, password); } init = true; }
/** * Authenticate {@code username} using the {@code "password"} authentication method and as a fallback basic * challenge-response authentication. * * @param username user to authenticate * @param password the password to use for authentication * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPassword(String username, String password) throws UserAuthException, TransportException { authPassword(username, password.toCharArray()); }
/** * Authenticate {@code username} using the {@code "password"} authentication method and as a fallback basic * challenge-response authentication. * * @param username user to authenticate * @param password the password to use for authentication * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPassword(String username, String password) throws UserAuthException, TransportException { authPassword(username, password.toCharArray()); }
public void connect() throws IOException { ssh.connect(host); if (connectMethod.equals(ConnectMethod.PASSWORD)) { ssh.authPassword(user, password); } else { ssh.authPublickey(user, keyFile); } }
@Test(expected = RaspiQueryException.class) public void connect_auth_failure() throws RaspiQueryException, UserAuthException, TransportException { Mockito.doThrow(UserAuthException.class).when(sshClient) .authPassword(Mockito.anyString(), Mockito.anyString()); raspiQuery.connect("wrong_pw"); }
final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); // or, to skip host verification: ssh.addHostKeyVerifier(new PromiscuousVerifier()) ssh.connect("localhost"); try { ssh.authPassword("user", "password"); // or ssh.authPublickey(System.getProperty("user.name")) final SFTPClient sftp = ssh.newSFTPClient(); try { sftp.get("test_file", "/tmp/test.tmp"); } finally { sftp.close(); } } finally { ssh.disconnect(); }