org.apache.sshd.common.io.IoSession Java Examples

The following examples show how to use org.apache.sshd.common.io.IoSession. 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: TestSshRequestInfoBuilder.java    From artifactory_ssh_proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testObjectEqual() throws ArtifactNotFoundException, IOException, ArtifactMetaDataParseFailureException,
                ParseException {
    IoSession ioSession = Mockito.mock(IoSession.class);
    Mockito.when(ioSession.getRemoteAddress()).thenReturn(new InetSocketAddress("10.0.0.1", 9999));
    ServerSession session = Mockito.mock(ServerSession.class);
    Mockito.when(session.getUsername()).thenReturn("screwdrv");
    Mockito.when(session.getIoSession()).thenReturn(ioSession);

    SshRequestInfo request1 =
                    new SshRequestInfo.Builder(session).setStartTimestamp(1411455384909L)
                                    .setMethod(SshRequestStatus.CREATED.getReasonPhrase())
                                    .setStatus(SshRequestStatus.CREATED.getStatusCode()).setExitValue(0)
                                    .setRepoName("maven-local-release").setPath("/com/yahoo/sshd/util/Utils.java")
                                    .setSize(1024000L).build();

    SshRequestInfo request2 =
                    new SshRequestInfo.Builder(session).setStartTimestamp(1411455384909L)
                                    .setMethod(SshRequestStatus.OK.getReasonPhrase())
                                    .setStatus(SshRequestStatus.OK.getStatusCode()).setExitValue(0)
                                    .setRepoName("maven-local-release").setPath("/com/yahoo/sshd/util/Utils.java")
                                    .setSize(1024000L).build();

    Assert.assertFalse(request1.equals(request2));
}
 
Example #2
Source File: TestSshRequestInfoBuilder.java    From artifactory_ssh_proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildSshRequestInfoObj() throws ArtifactNotFoundException, IOException,
                ArtifactMetaDataParseFailureException, ParseException {
    IoSession ioSession = Mockito.mock(IoSession.class);
    Mockito.when(ioSession.getRemoteAddress()).thenReturn(new InetSocketAddress("10.0.0.1", 9999));
    ServerSession session = Mockito.mock(ServerSession.class);
    Mockito.when(session.getUsername()).thenReturn("screwdrv");
    Mockito.when(session.getIoSession()).thenReturn(ioSession);

    SshRequestInfo request =
                    new SshRequestInfo.Builder(session).setStartTimestamp(1411455384909L)
                                    .setMethod(SshRequestStatus.CREATED.getReasonPhrase())
                                    .setStatus(SshRequestStatus.CREATED.getStatusCode()).setExitValue(0)
                                    .setRepoName("maven-local-release").setPath("/com/yahoo/sshd/util/Utils.java")
                                    .setSize(1024000L).build();

    Assert.assertEquals(request.getStartTimestamp(), 1411455384909L);
    Assert.assertEquals(request.getRemoteAddr(), "10.0.0.1");
    Assert.assertEquals(request.getRepoName(), "maven-local-release");
    Assert.assertEquals(request.getRequestPath(), "/com/yahoo/sshd/util/Utils.java");
    Assert.assertEquals(request.getStatus(), 201);
    Assert.assertEquals(request.getExitValue(), 0);
    Assert.assertEquals(request.getMethod(), "PUT");
    Assert.assertEquals(request.getUserName(), "screwdrv");
}
 
Example #3
Source File: SshShellSecurityAuthenticationProviderTest.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
void authenticate() {
    ServerSession session = Mockito.mock(ServerSession.class);
    IoSession io = Mockito.mock(IoSession.class);
    Mockito.when(session.getIoSession()).thenReturn(io);
    Mockito.when(ctx.getBeansOfType(any())).thenReturn(Collections.singletonMap("sec", sec));
    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
    Mockito.when(io.setAttribute(eq(AUTHENTICATION_ATTRIBUTE), captor.capture())).thenReturn(null);
    SshShellSecurityAuthenticationProvider provider = new SshShellSecurityAuthenticationProvider(ctx, null);
    provider.init();

    Mockito.when(sec.authenticate(any())).thenReturn(
            new UsernamePasswordAuthenticationToken("principal", "credentials",
                    Collections.singletonList(new SimpleGrantedAuthority("USER"))));
    assertTrue(provider.authenticate("user", "pass", session));
    SshAuthentication auth = (SshAuthentication) captor.getValue();
    assertEquals("principal", auth.getPrincipal());
    assertEquals("credentials", auth.getCredentials());
    assertEquals(1, auth.getAuthorities().size());
    assertNull(auth.getDetails());

    // fail auth
    Mockito.when(sec.authenticate(any())).thenThrow(new BadCredentialsException("[MOCK]"));
    assertFalse(provider.authenticate("user", "pass", session));
}
 
Example #4
Source File: TestPKAuth.java    From artifactory_ssh_proxy with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "matchedKeys")
public void testAllKeys(MultiUserPKAuthenticator mupka, PublicKey publicKey, String user, String fileName)
                throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {

    // now for all of these public keys ensure that "test", auths against them
    ServerSession session = Mockito.mock(ServerSession.class);
    IoSession ioSession = Mockito.mock(IoSession.class);
    Mockito.when(session.getIoSession()).thenReturn(ioSession);

    Assert.assertTrue(mupka.authenticate(user, publicKey, session), fileName);
    Assert.assertFalse(mupka.authenticate("areese", publicKey, session), fileName);
}
 
Example #5
Source File: AuthenticationTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    sshd = setupTestServer();
    sshd.setSessionFactory(new SessionFactory(sshd) {
        @Override
        protected ServerSessionImpl doCreateSession(IoSession ioSession) throws Exception {
            return new TestSession(getServer(), ioSession);
        }
    });
    sshd.start();
    port = sshd.getPort();
}
 
Example #6
Source File: VertxIoHandlerBridge.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(IoHandler handler, IoSession session, org.apache.sshd.common.util.Readable message) throws Exception {
  context.dispatch(v -> {
    try {
      super.messageReceived(handler, session, message);
    } catch (Exception e) {
      throw new VertxException(e);
    }
  });
}
 
Example #7
Source File: VertxIoHandlerBridge.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionClosed(IoHandler handler, IoSession session) throws Exception {
  context.dispatch(v -> {
    try {
      super.sessionClosed(handler, session);
    } catch (Exception e) {
      throw new VertxException(e);
    }
  });
}
 
Example #8
Source File: VertxIoHandlerBridge.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionCreated(IoHandler handler, IoSession session) throws Exception {
  context.dispatch(v -> {
    try {
      super.sessionCreated(handler, session);
    } catch (Exception e) {
      throw new VertxException(e);
    }
  });
}
 
Example #9
Source File: TestLocalUserPKAuthenticator.java    From artifactory_ssh_proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefault() throws Exception {
    CountDownLatch wakeupLatch = new CountDownLatch(1);
    LocalUserPKAuthenticator lpka = new LocalUserPKAuthenticator(wakeupLatch) {

        @Override
        String getUserName() {
            return "areese";
        }

        @Override
        String getUserHome() {
            return "src/test/resources/MultiUserPKAuthenticator/home/areese/";
        }
    };

    lpka.start();

    ServerSession session = Mockito.mock(ServerSession.class);
    IoSession ioSession = Mockito.mock(IoSession.class);
    Mockito.when(session.getIoSession()).thenReturn(ioSession);

    Map<PublicKey, AuthorizedKey> publicKeys = null;
    try (FileInputStream karafFis = new FileInputStream(lpka.getAuthorizedKeysPath())) {
        publicKeys =
                        KarafPublickeyAuthenticator.parseAuthorizedKeys(lpka.getAuthorizedKeysPath()
                                        .getAbsolutePath(), karafFis);
    }

    for (PublicKey publicKey : publicKeys.keySet()) {
        lpka.authenticate("areese", publicKey, session);
    }

}
 
Example #10
Source File: TestPKAuth.java    From artifactory_ssh_proxy with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "misMatchedKeys")
public void testBadKeys(MultiUserPKAuthenticator mupka, PublicKey publicKey, String user, String fileName)
                throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {

    // now for all of these public keys ensure that "test", can auth against them
    ServerSession session = Mockito.mock(ServerSession.class);
    IoSession ioSession = Mockito.mock(IoSession.class);
    Mockito.when(session.getIoSession()).thenReturn(ioSession);

    Assert.assertFalse(mupka.authenticate(user, publicKey, session), fileName);
    Assert.assertFalse(mupka.authenticate("areese", publicKey, session), fileName);
}
 
Example #11
Source File: SshShellUtilsTest.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
public static ChannelSession mockChannelSession(Long id) {
    ChannelSession session = mock(ChannelSession.class);
    ServerSession serverSession = mock(ServerSession.class);
    when(session.getSession()).thenReturn(serverSession);
    IoSession ioSession = mock(IoSession.class);
    when(serverSession.getIoSession()).thenReturn(ioSession);
    when(ioSession.getId()).thenReturn(id);
    return session;
}
 
Example #12
Source File: AuthenticationTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    sshd = setupTestServer();
    sshd.setSessionFactory(new SessionFactory(sshd) {
        @Override
        protected ServerSessionImpl doCreateSession(IoSession ioSession) throws Exception {
            return new TestSession(getServer(), ioSession);
        }
    });
    sshd.start();
    port = sshd.getPort();
}
 
Example #13
Source File: NettyIoHandlerBridge.java    From termd with Apache License 2.0 4 votes vote down vote up
public void sessionClosed(IoHandler handler, IoSession session) throws Exception {
  handler.sessionClosed(session);
}
 
Example #14
Source File: DenyingTcpipForwarder.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
    cause.printStackTrace();
    session.close(false);
}
 
Example #15
Source File: DenyingTcpipForwarder.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(IoSession session, Readable message) throws Exception {
    throw new SshException("Tcpip forwarding request denied by server");
}
 
Example #16
Source File: TestPKUpdating.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
@Test(enabled = false)
public void testUpdating() throws IOException, InterruptedException, NoSuchAlgorithmException,
                InvalidKeySpecException {
    TestContext testContext = setup();

    // at this point, all of the dummy users are in place
    // now we add some new users and check for them.

    // check they don't exist.
    checkDoesntExist(testContext, newUsers);

    // create them
    buildSshDirs(homeDir, newUsers);

    // Wait for it.
    TimeUnit.SECONDS.sleep(10);

    // check that they exist.
    checkExist(testContext, newUsers);

    // first see if we can auth them.
    User user = normalUsers[0];
    try (FileInputStream fis = new FileInputStream(user.publicKey)) {
        Map<PublicKey, AuthorizedKey> parseAuthorizedKeys =
                        KarafPublickeyAuthenticator.parseAuthorizedKeys(user.publicKey.getAbsolutePath(), fis);

        ServerSession sessionMock = Mockito.mock(ServerSession.class);
        IoSession ioSessionMock = Mockito.mock(IoSession.class);
        SocketAddress socketMock = Mockito.mock(SocketAddress.class);

        Mockito.when(sessionMock.getIoSession()).thenReturn(ioSessionMock);
        Mockito.when(ioSessionMock.getRemoteAddress()).thenReturn(socketMock);

        testContext.publickeyAuthenticator.authenticate(normalUsers[0].name, parseAuthorizedKeys.keySet()
                        .iterator().next(), sessionMock);
    }

    // now overwrite some keys and see that it works.
    // the first users are 0-5
    // the second set are 10-15

    //
}
 
Example #17
Source File: MockClientSession.java    From xenon with Apache License 2.0 4 votes vote down vote up
@Override
public IoSession getIoSession() {
    throw new RuntimeException("Not implemented");
}
 
Example #18
Source File: DenyingTcpipForwarder.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
@Override
public void sessionClosed(IoSession session) throws Exception {
    throw new SshException("Tcpip forwarding request denied by server");
}
 
Example #19
Source File: DenyingTcpipForwarder.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
@Override
public void sessionCreated(final IoSession session) throws Exception {
    throw new SshException("Tcpip forwarding request denied by server");
}
 
Example #20
Source File: AuthenticationTest.java    From termd with Apache License 2.0 4 votes vote down vote up
public TestSession(ServerFactoryManager server, IoSession ioSession) throws Exception {
    super(server, ioSession);
}
 
Example #21
Source File: ServiceLogger.java    From sftpserver with Apache License 2.0 4 votes vote down vote up
private static final String toHuman(final Session session, final String username) {
	final IoSession networkSession = session.getIoSession();
	final SocketAddress peerAddress = (networkSession == null) ? null : networkSession.getRemoteAddress();
	return (username == null ? "<unknown>" : username) + "@" + addrToHuman(peerAddress);
}
 
Example #22
Source File: NettyIoService.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public Map<Long, IoSession> getManagedSessions() {
  return null;
}
 
Example #23
Source File: NettyIoAcceptor.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public Map<Long, IoSession> getManagedSessions() {
  return ioService.sessions;
}
 
Example #24
Source File: NettyIoHandlerBridge.java    From termd with Apache License 2.0 4 votes vote down vote up
public void messageReceived(IoHandler handler, IoSession session, Readable message) throws Exception {
  handler.messageReceived(session, message);
}
 
Example #25
Source File: NettyIoHandlerBridge.java    From termd with Apache License 2.0 4 votes vote down vote up
public void sessionCreated(IoHandler handler, IoSession session) throws Exception {
  handler.sessionCreated(session);
}
 
Example #26
Source File: NettyIoHandlerBridge.java    From termd with Apache License 2.0 4 votes vote down vote up
public void sessionCreated(IoHandler handler, IoSession session) throws Exception {
  handler.sessionCreated(session);
}
 
Example #27
Source File: NettyIoService.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@Override
public Map<Long, IoSession> getManagedSessions() {
  return null;
}
 
Example #28
Source File: NettyIoAcceptor.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@Override
public Map<Long, IoSession> getManagedSessions() {
    return ioService.sessions;
}
 
Example #29
Source File: NettyIoHandlerBridge.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
public void messageReceived(IoHandler handler, IoSession session, Readable message) throws Exception {
  handler.messageReceived(session, message);
}
 
Example #30
Source File: NettyIoHandlerBridge.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
public void sessionClosed(IoHandler handler, IoSession session) throws Exception {
  handler.sessionClosed(session);
}