Java Code Examples for org.apache.sshd.client.session.ClientSession#ClientSessionEvent
The following examples show how to use
org.apache.sshd.client.session.ClientSession#ClientSessionEvent .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthenticationTest.java From termd with Apache License 2.0 | 6 votes |
@Test public void testAuthPasswordOnly() throws Exception { try (SshClient client = setupTestClient()) { sshd.setPasswordAuthenticator(RejectAllPasswordAuthenticator.INSTANCE); client.start(); try (ClientSession s = client.connect(null, TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> result = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH), TimeUnit.SECONDS.toMillis(11L)); assertFalse("Timeout while waiting for session", result.contains(ClientSession.ClientSessionEvent.TIMEOUT)); String password = getCurrentTestName(); try { assertAuthenticationResult(getCurrentTestName(), authPassword(s, getCurrentTestName(), password), false); } finally { s.removePasswordIdentity(password); } } finally { client.stop(); } } }
Example 2
Source File: AuthenticationTest.java From termd with Apache License 2.0 | 6 votes |
@Test public void testAuthPasswordOnly() throws Exception { try (SshClient client = setupTestClient()) { sshd.setPasswordAuthenticator(RejectAllPasswordAuthenticator.INSTANCE); client.start(); try (ClientSession s = client.connect(null, TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> result = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH), TimeUnit.SECONDS.toMillis(11L)); assertFalse("Timeout while waiting for session", result.contains(ClientSession.ClientSessionEvent.TIMEOUT)); String password = getCurrentTestName(); try { assertAuthenticationResult(getCurrentTestName(), authPassword(s, getCurrentTestName(), password), false); } finally { s.removePasswordIdentity(password); } } finally { client.stop(); } } }
Example 3
Source File: FuseUtils.java From keycloak with Apache License 2.0 | 6 votes |
private static ClientSession openSshChannel(String username, String password) throws IOException { SshClient client = SshClient.setUpDefaultClient(); client.start(); ConnectFuture future = client.connect(username, "localhost", 8101); future.await(); ClientSession session = future.getSession(); Set<ClientSession.ClientSessionEvent> ret = EnumSet.of(ClientSession.ClientSessionEvent.WAIT_AUTH); while (ret.contains(ClientSession.ClientSessionEvent.WAIT_AUTH)) { session.addPasswordIdentity(password); session.auth().verify(); ret = session.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.WAIT_AUTH, ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.AUTHED), 0); } if (ret.contains(ClientSession.ClientSessionEvent.CLOSED)) { throw new RuntimeException("Could not open SSH channel"); } return session; }
Example 4
Source File: ServerTest.java From termd with Apache License 2.0 | 5 votes |
@Test public void testFailAuthenticationWithWaitFor() throws Exception { final int maxAllowedAuths = 10; PropertyResolverUtils.updateProperty(sshd, ServerAuthenticationManager.MAX_AUTH_REQUESTS, maxAllowedAuths); sshd.start(); client.setServiceFactories(Arrays.asList( new ClientUserAuthServiceOld.Factory(), ClientConnectionServiceFactory.INSTANCE )); client.start(); try (ClientSession s = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshd.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) { int nbTrials = 0; Collection<ClientSession.ClientSessionEvent> res = Collections.emptySet(); Collection<ClientSession.ClientSessionEvent> mask = EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH); while (!res.contains(ClientSession.ClientSessionEvent.CLOSED)) { nbTrials++; s.getService(ClientUserAuthServiceOld.class) .auth(new org.apache.sshd.deprecated.UserAuthPassword(s, "ssh-connection", "buggy")); res = s.waitFor(mask, TimeUnit.SECONDS.toMillis(5L)); assertFalse("Timeout signalled", res.contains(ClientSession.ClientSessionEvent.TIMEOUT)); } assertTrue("Number trials (" + nbTrials + ") below min.=" + maxAllowedAuths, nbTrials > maxAllowedAuths); } finally { client.stop(); } }
Example 5
Source File: ServerTest.java From termd with Apache License 2.0 | 5 votes |
@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(); } }
Example 6
Source File: AuthenticationTest.java From termd with Apache License 2.0 | 5 votes |
@Test public void testChangeUser() throws Exception { try (SshClient client = setupTestClient()) { client.start(); try (ClientSession s = client.connect(null, TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> mask = EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH); Collection<ClientSession.ClientSessionEvent> result = s.waitFor(mask, TimeUnit.SECONDS.toMillis(11L)); assertFalse("Timeout while waiting on session events", result.contains(ClientSession.ClientSessionEvent.TIMEOUT)); String password = "the-password"; for (String username : new String[]{"user1", "user2"}) { try { assertAuthenticationResult(username, authPassword(s, username, password), false); } finally { s.removePasswordIdentity(password); } } // Note that WAIT_AUTH flag should be false, but since the internal // authentication future is not updated, it's still returned result = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), TimeUnit.SECONDS.toMillis(3L)); assertTrue("Mismatched client session close mask: " + result, result.containsAll(mask)); } finally { client.stop(); } } }
Example 7
Source File: AuthenticationTest.java From termd with Apache License 2.0 | 5 votes |
@Test public void testAuthKeyPassword() throws Exception { try (SshClient client = setupTestClient()) { sshd.setPublickeyAuthenticator(RejectAllPublickeyAuthenticator.INSTANCE); sshd.setKeyboardInteractiveAuthenticator(KeyboardInteractiveAuthenticator.NONE); client.start(); try (ClientSession s = client.connect(null, TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> result = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH), TimeUnit.SECONDS.toMillis(11L)); assertFalse("Timeout while waiting for session", result.contains(ClientSession.ClientSessionEvent.TIMEOUT)); KeyPair pair = createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA); try { assertAuthenticationResult(UserAuthMethodFactory.PUBLIC_KEY, authPublicKey(s, getCurrentTestName(), pair), false); } finally { s.removePublicKeyIdentity(pair); } String password = getCurrentTestName(); try { assertAuthenticationResult(UserAuthMethodFactory.PASSWORD, authPassword(s, getCurrentTestName(), password), true); } finally { s.removePasswordIdentity(password); } } finally { client.stop(); } } }
Example 8
Source File: ServerTest.java From termd with Apache License 2.0 | 5 votes |
@Test public void testFailAuthenticationWithWaitFor() throws Exception { final int maxAllowedAuths = 10; PropertyResolverUtils.updateProperty(sshd, ServerAuthenticationManager.MAX_AUTH_REQUESTS, maxAllowedAuths); sshd.start(); client.setServiceFactories(Arrays.asList( new ClientUserAuthServiceOld.Factory(), ClientConnectionServiceFactory.INSTANCE )); client.start(); try (ClientSession s = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshd.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) { int nbTrials = 0; Collection<ClientSession.ClientSessionEvent> res = Collections.emptySet(); Collection<ClientSession.ClientSessionEvent> mask = EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH); while (!res.contains(ClientSession.ClientSessionEvent.CLOSED)) { nbTrials++; s.getService(ClientUserAuthServiceOld.class) .auth(new org.apache.sshd.deprecated.UserAuthPassword(s, "ssh-connection", "buggy")); res = s.waitFor(mask, TimeUnit.SECONDS.toMillis(5L)); assertFalse("Timeout signalled", res.contains(ClientSession.ClientSessionEvent.TIMEOUT)); } assertTrue("Number trials (" + nbTrials + ") below min.=" + maxAllowedAuths, nbTrials > maxAllowedAuths); } finally { client.stop(); } }
Example 9
Source File: ServerTest.java From termd with Apache License 2.0 | 5 votes |
@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(); } }
Example 10
Source File: AuthenticationTest.java From termd with Apache License 2.0 | 5 votes |
@Test public void testChangeUser() throws Exception { try (SshClient client = setupTestClient()) { client.start(); try (ClientSession s = client.connect(null, TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> mask = EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH); Collection<ClientSession.ClientSessionEvent> result = s.waitFor(mask, TimeUnit.SECONDS.toMillis(11L)); assertFalse("Timeout while waiting on session events", result.contains(ClientSession.ClientSessionEvent.TIMEOUT)); String password = "the-password"; for (String username : new String[]{"user1", "user2"}) { try { assertAuthenticationResult(username, authPassword(s, username, password), false); } finally { s.removePasswordIdentity(password); } } // Note that WAIT_AUTH flag should be false, but since the internal // authentication future is not updated, it's still returned result = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), TimeUnit.SECONDS.toMillis(3L)); assertTrue("Mismatched client session close mask: " + result, result.containsAll(mask)); } finally { client.stop(); } } }
Example 11
Source File: AuthenticationTest.java From termd with Apache License 2.0 | 5 votes |
@Test public void testAuthKeyPassword() throws Exception { try (SshClient client = setupTestClient()) { sshd.setPublickeyAuthenticator(RejectAllPublickeyAuthenticator.INSTANCE); sshd.setKeyboardInteractiveAuthenticator(KeyboardInteractiveAuthenticator.NONE); client.start(); try (ClientSession s = client.connect(null, TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) { Collection<ClientSession.ClientSessionEvent> result = s.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.WAIT_AUTH), TimeUnit.SECONDS.toMillis(11L)); assertFalse("Timeout while waiting for session", result.contains(ClientSession.ClientSessionEvent.TIMEOUT)); KeyPair pair = createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA); try { assertAuthenticationResult(UserAuthMethodFactory.PUBLIC_KEY, authPublicKey(s, getCurrentTestName(), pair), false); } finally { s.removePublicKeyIdentity(pair); } String password = getCurrentTestName(); try { assertAuthenticationResult(UserAuthMethodFactory.PASSWORD, authPassword(s, getCurrentTestName(), password), true); } finally { s.removePasswordIdentity(password); } } finally { client.stop(); } } }
Example 12
Source File: NetconfSessionMinaImpl.java From onos with Apache License 2.0 | 5 votes |
@Deprecated private void startSession() throws IOException { final ConnectFuture connectFuture; connectFuture = client.connect(deviceInfo.name(), deviceInfo.ip().toString(), deviceInfo.port()) .verify(connectTimeout, TimeUnit.SECONDS); session = connectFuture.getSession(); //Using the device ssh key if possible if (deviceInfo.getKey() != null) { try (PEMParser pemParser = new PEMParser(new CharArrayReader(deviceInfo.getKey()))) { JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME); try { KeyPair kp = converter.getKeyPair((PEMKeyPair) pemParser.readObject()); session.addPublicKeyIdentity(kp); } catch (IOException e) { throw new NetconfException("Failed to authenticate session with device " + deviceInfo + "check key to be a valid key", e); } } } else { session.addPasswordIdentity(deviceInfo.password()); } session.auth().verify(connectTimeout, TimeUnit.SECONDS); Set<ClientSession.ClientSessionEvent> event = session.waitFor( ImmutableSet.of(ClientSession.ClientSessionEvent.WAIT_AUTH, ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.AUTHED), 0); if (!event.contains(ClientSession.ClientSessionEvent.AUTHED)) { log.debug("Session closed {} {}", event, session.isClosed()); throw new NetconfException("Failed to authenticate session with device " + deviceInfo + "check the user/pwd or key"); } openChannel(); }