cf.await(); try (ClientSession session = cf.getSession()) { for (KeyPair pair : provider.getKeys()) { CLI.LOGGER.log(FINE, "Offering {0} private key", pair.getPrivate().getAlgorithm());
@Override public void operationComplete(final ConnectFuture future) { if (future.isConnected()) { handleSshSessionCreated(future, ctx); } else { handleSshSetupFailure(ctx, future.getException()); } } });
@Override public void connect(String hostname, String username, String password, Long port, Long timeout) throws IOException { if (port == null) port = 22l; if (timeout == null) timeout = 10000l; if (sftp != null) return; ClientSession session = client.connect(username, hostname, port.intValue()).verify(timeout.intValue()).getSession(); session.addPasswordIdentity(password); session.auth().verify(timeout.intValue()); sftp = session.createSftpClient(); }
protected ClientSession authSession(ConnectFuture future, String password) throws IOException { ClientSession session = future.getSession(); session.addPasswordIdentity(password); return authSession(session); }
connectFuture.await(configuration.getTimeout()); if (!connectFuture.isDone() || !connectFuture.isConnected()) { throw new RuntimeCamelException("Failed to connect to " + configuration.getHost() + ":" + configuration.getPort() + " within timeout " + configuration.getTimeout() + "ms"); session = connectFuture.getSession();
@Test public void testConnectTimeout() throws Exception { SSHClient client = spy(this.client); SshClient ssh = spy(SshClient.setUpDefaultClient()); ConnectFuture future = mock(ConnectFuture.class); doReturn(ssh).when(client).createSshClient(); doReturn(future).when(ssh).connect(any(), any(), anyInt()); when(future.await(anyLong())).thenReturn(false); assertThrows(TimeLimitExceededException.class, client::connect); }
protected ClientSession loginSession(ConnectFuture future, KeyPair identity) throws IOException { return authSession(future.verify(getConnectTimeout()), identity); }
@Override public void operationComplete(IoConnectFuture future) { if (future.isCanceled()) { connectFuture.cancel(); return; } Throwable t = future.getException(); if (t != null) { connectFuture.setException(t); return; } IoSession ioSession = future.getSession(); try { JGitClientSession session = createSession(ioSession, username, address, hostConfig); connectFuture.setSession(session); } catch (RuntimeException e) { connectFuture.setException(e); ioSession.close(true); } }
@Override @SuppressWarnings("synthetic-access") public void operationComplete(IoConnectFuture future) { if (future.isCanceled()) { connectFuture.cancel(); return; } Throwable t = future.getException(); if (t != null) { if (log.isDebugEnabled()) { log.debug("operationComplete({}@{}) failed ({}): {}", username, address, t.getClass().getSimpleName(), t.getMessage()); } connectFuture.setException(t); } else { IoSession ioSession = future.getSession(); try { onConnectOperationComplete(ioSession, connectFuture, username, address, identities, useDefaultIdentities); } catch (RuntimeException e) { log.warn("operationComplete({}@{}) failed ({}) to signal completion of session={}: {}", username, address, e.getClass().getSimpleName(), ioSession, e.getMessage()); if (log.isDebugEnabled()) { log.debug("operationComplete(" + username + "@" + address + ") session=" + ioSession + " completion signal failure details", e); } connectFuture.setException(e); ioSession.close(true); } } }
void connect(Duration timeout) throws IOException { if (!client.isStarted()) { client.start(); } try { String username = uri.getUser(); String host = uri.getHost(); int port = uri.getPort(); long t = timeout.toMillis(); if (t <= 0) { session = client.connect(username, host, port).verify() .getSession(); } else { session = client.connect(username, host, port) .verify(timeout.toMillis()).getSession(); } session.addSessionListener(new SessionListener() { @Override public void sessionClosed(Session s) { notifyCloseListeners(); } }); // Authentication timeout is by default 2 minutes. session.auth().verify(session.getAuthTimeout()); } catch (IOException e) { disconnect(e); throw e; } }
protected ClientSession authSession(ConnectFuture future, KeyPair identity) throws IOException { ClientSession session = future.getSession(); session.addPublicKeyIdentity(identity); return authSession(session); }
protected ClientSession loginSession(ConnectFuture future, String password) throws IOException { return authSession(future.verify(getConnectTimeout()), password); }
@Override @SuppressWarnings("synthetic-access") public void operationComplete(IoConnectFuture future) { if (future.isCanceled()) { connectFuture.cancel(); return; } Throwable t = future.getException(); if (t != null) { if (log.isDebugEnabled()) { log.debug("operationComplete({}@{}) failed ({}): {}", username, address, t.getClass().getSimpleName(), t.getMessage()); } connectFuture.setException(t); } else { IoSession ioSession = future.getSession(); try { onConnectOperationComplete(ioSession, connectFuture, username, address, identities, useDefaultIdentities); } catch (RuntimeException e) { log.warn("operationComplete({}@{}) failed ({}) to signal completion of session={}: {}", username, address, e.getClass().getSimpleName(), ioSession, e.getMessage()); if (log.isDebugEnabled()) { log.debug("operationComplete(" + username + "@" + address + ") session=" + ioSession + " completion signal failure details", e); } connectFuture.setException(e); ioSession.close(true); } } }
@Test public void testAuthenticationTimeout() throws Exception { final long testAuthTimeout = TimeUnit.SECONDS.toMillis(5L); PropertyResolverUtils.updateProperty(sshd, FactoryManager.AUTH_TIMEOUT, testAuthTimeout); sshd.start(); client.start(); try (ClientSession s = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshd.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> res = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), 2L * testAuthTimeout); assertTrue("Session should be closed: " + res, res.containsAll(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH))); } finally { client.stop(); } }
ClientSession session = client.connect("localhost", port).await().getSession(); session.authPassword("root", "");
protected ClientSession authSession(ConnectFuture future, String password) throws IOException { ClientSession session = future.getSession(); session.addPasswordIdentity(password); return authSession(session); }
protected ClientSession loginSession(ConnectFuture future, String password) throws IOException { return authSession(future.verify(getConnectTimeout()), password); }
@Override public void operationComplete(final ConnectFuture future) { if (future.isConnected()) { handleSshSessionCreated(future, ctx); } else { handleSshSetupFailure(ctx, future.getException()); } } });
@Test public void testAuthenticationTimeout() throws Exception { final long testAuthTimeout = TimeUnit.SECONDS.toMillis(5L); PropertyResolverUtils.updateProperty(sshd, FactoryManager.AUTH_TIMEOUT, testAuthTimeout); sshd.start(); client.start(); try (ClientSession s = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshd.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> res = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), 2L * testAuthTimeout); assertTrue("Session should be closed: " + res, res.containsAll(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH))); } finally { client.stop(); } }
private static ClientSession connectWithRetries(SshClient client, String username, String host, int port, int maxAttempts) throws Exception { ClientSession session = null; int retries = 0; do { ConnectFuture future = client.connect(username, host, port); future.await(); try { session = future.getSession(); } catch (Exception ex) { if (retries++ < maxAttempts) { Thread.sleep(2 * 1000); System.out.println("retrying (attempt " + retries + ") ..."); } else { throw ex; } } } while (session == null); return session; }