Search in sources :

Example 1 with ServerSession

use of org.apache.sshd.server.session.ServerSession in project camel by apache.

the class SftpServerTestSupport method setUpServer.

protected void setUpServer() throws Exception {
    canTest = true;
    try {
        sshd = SshServer.setUpDefaultServer();
        sshd.setPort(getPort());
        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
        sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
        sshd.setCommandFactory(new ScpCommandFactory());
        sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
        PublickeyAuthenticator publickeyAuthenticator = new PublickeyAuthenticator() {

            // consider all keys as authorized for all users
            @Override
            public boolean authenticate(String username, PublicKey key, ServerSession session) {
                return true;
            }
        };
        sshd.setPublickeyAuthenticator(publickeyAuthenticator);
        sshd.start();
    } catch (Exception e) {
        // ignore if algorithm is not on the OS
        NoSuchAlgorithmException nsae = ObjectHelper.getException(NoSuchAlgorithmException.class, e);
        if (nsae != null) {
            canTest = false;
            String name = System.getProperty("os.name");
            String message = nsae.getMessage();
            log.warn("SunX509 is not avail on this platform [{}] Testing is skipped! Real cause: {}", name, message);
        } else {
            // some other error then throw it so the test can fail
            throw e;
        }
    }
}
Also used : ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ServerSession(org.apache.sshd.server.session.ServerSession) PublickeyAuthenticator(org.apache.sshd.server.PublickeyAuthenticator) Command(org.apache.sshd.server.Command) PublicKey(java.security.PublicKey) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) NamedFactory(org.apache.sshd.common.NamedFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with ServerSession

use of org.apache.sshd.server.session.ServerSession in project hadoop by apache.

the class TestSFTPFileSystem method startSshdServer.

private static void startSshdServer() throws IOException {
    sshd = SshServer.setUpDefaultServer();
    // ask OS to assign a port
    sshd.setPort(0);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
    userAuthFactories.add(new UserAuthPassword.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            if (username.equals("user") && password.equals("password")) {
                return true;
            }
            return false;
        }
    });
    sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
    sshd.start();
    port = sshd.getPort();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ServerSession(org.apache.sshd.server.session.ServerSession) ArrayList(java.util.ArrayList) NamedFactory(org.apache.sshd.common.NamedFactory) PasswordAuthenticator(org.apache.sshd.server.PasswordAuthenticator) Command(org.apache.sshd.server.Command) NamedFactory(org.apache.sshd.common.NamedFactory) UserAuth(org.apache.sshd.server.UserAuth) UserAuthPassword(org.apache.sshd.server.auth.UserAuthPassword)

Example 3 with ServerSession

use of org.apache.sshd.server.session.ServerSession in project gitblit by gitblit.

the class SshKerberosAuthenticationTest method testUserManager.

@Test
public void testUserManager() {
    IRuntimeManager rm = Mockito.mock(IRuntimeManager.class);
    //Build an UserManager that can build a UserModel
    IUserManager im = Mockito.mock(IUserManager.class);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            String user = (String) args[0];
            return new UserModel(user);
        }
    }).when(im).getUserModel(Mockito.anyString());
    AuthenticationManager am = new AuthenticationManager(rm, im);
    GSSAuthenticator gssAuthenticator = new SshKrbAuthenticator(new MemorySettings(), am);
    ServerSession session = Mockito.mock(ServerSession.class);
    //Build an SshDaemonClient that can set and get the UserModel
    final UserModelWrapper umw = new UserModelWrapper();
    SshDaemonClient client = Mockito.mock(SshDaemonClient.class);
    Mockito.when(client.getUser()).thenReturn(umw.um);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            UserModel um = (UserModel) args[0];
            umw.um = um;
            return null;
        }
    }).when(client).setUser(Mockito.any(UserModel.class));
    Mockito.when(session.getAttribute(SshDaemonClient.KEY)).thenReturn(client);
    Assert.assertTrue(gssAuthenticator.validateIdentity(session, "jhappy"));
}
Also used : GSSAuthenticator(org.apache.sshd.server.auth.gss.GSSAuthenticator) ServerSession(org.apache.sshd.server.session.ServerSession) IUserManager(com.gitblit.manager.IUserManager) SshDaemonClient(com.gitblit.transport.ssh.SshDaemonClient) SshKrbAuthenticator(com.gitblit.transport.ssh.SshKrbAuthenticator) IRuntimeManager(com.gitblit.manager.IRuntimeManager) UserModel(com.gitblit.models.UserModel) AuthenticationManager(com.gitblit.manager.AuthenticationManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MemorySettings(com.gitblit.tests.mock.MemorySettings) Test(org.junit.Test)

Example 4 with ServerSession

use of org.apache.sshd.server.session.ServerSession in project camel by apache.

the class ScpServerTestSupport method startSshd.

protected boolean startSshd() {
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(getPort());
    sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
    sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            // dummy authentication: allow any user whose password is the same as the username
            return username != null && username.equals(password);
        }
    });
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {

        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return true;
        }
    });
    try {
        sshd.start();
        return true;
    } catch (IOException e) {
        LOG.info("Failed to start ssh server.", e);
    }
    return false;
}
Also used : ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ServerSession(org.apache.sshd.server.session.ServerSession) PasswordAuthenticator(org.apache.sshd.server.PasswordAuthenticator) PublickeyAuthenticator(org.apache.sshd.server.PublickeyAuthenticator) Command(org.apache.sshd.server.Command) PublicKey(java.security.PublicKey) LoggerFactory(org.slf4j.LoggerFactory) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) NamedFactory(org.apache.sshd.common.NamedFactory) IOException(java.io.IOException)

Example 5 with ServerSession

use of org.apache.sshd.server.session.ServerSession in project karaf by apache.

the class KarafAgentFactory method createServer.

public SshAgentServer createServer(ConnectionService service) throws IOException {
    Session session = service.getSession();
    if (!(session instanceof ServerSession)) {
        throw new IllegalStateException("The session used to create an agent server proxy must be a server session");
    }
    final AgentServerProxy proxy = new AgentServerProxy(service);
    proxies.put(proxy.getId(), proxy);
    return new SshAgentServer() {

        public String getId() {
            return proxy.getId();
        }

        @Override
        public boolean isOpen() {
            return proxy.isOpen();
        }

        public void close() throws IOException {
            proxies.remove(proxy.getId());
            proxy.close();
        }
    };
}
Also used : ServerSession(org.apache.sshd.server.session.ServerSession) SshAgentServer(org.apache.sshd.agent.SshAgentServer) AgentServerProxy(org.apache.sshd.agent.local.AgentServerProxy) ServerSession(org.apache.sshd.server.session.ServerSession) Session(org.apache.sshd.common.session.Session)

Aggregations

ServerSession (org.apache.sshd.server.session.ServerSession)5 NamedFactory (org.apache.sshd.common.NamedFactory)3 Command (org.apache.sshd.server.Command)3 IOException (java.io.IOException)2 PublicKey (java.security.PublicKey)2 FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)2 PasswordAuthenticator (org.apache.sshd.server.PasswordAuthenticator)2 PublickeyAuthenticator (org.apache.sshd.server.PublickeyAuthenticator)2 ScpCommandFactory (org.apache.sshd.server.command.ScpCommandFactory)2 AuthenticationManager (com.gitblit.manager.AuthenticationManager)1 IRuntimeManager (com.gitblit.manager.IRuntimeManager)1 IUserManager (com.gitblit.manager.IUserManager)1 UserModel (com.gitblit.models.UserModel)1 MemorySettings (com.gitblit.tests.mock.MemorySettings)1 SshDaemonClient (com.gitblit.transport.ssh.SshDaemonClient)1 SshKrbAuthenticator (com.gitblit.transport.ssh.SshKrbAuthenticator)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 SshAgentServer (org.apache.sshd.agent.SshAgentServer)1 AgentServerProxy (org.apache.sshd.agent.local.AgentServerProxy)1