Search in sources :

Example 16 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.

the class SftpSessionFactoryTests method createServerAndClient.

private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException {
    server.setPublickeyAuthenticator((username, key, session) -> true);
    server.setPort(0);
    server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
    server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
    server.start();
    DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
    f.setHost("localhost");
    f.setPort(server.getPort());
    f.setUser("user");
    Resource privateKey = new ClassPathResource("id_rsa");
    f.setPrivateKey(privateKey);
    return f;
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) Command(org.apache.sshd.server.Command) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 17 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.

the class SftpSessionFactoryTests method testConnectFailSocketOpen.

/*
	 * Verify the socket is closed if the channel.connect() fails.
	 * INT-3305
	 */
@Test
public void testConnectFailSocketOpen() throws Exception {
    SshServer server = SshServer.setUpDefaultServer();
    try {
        server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
        server.setPort(0);
        server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
        server.start();
        DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
        f.setHost("localhost");
        f.setPort(server.getPort());
        f.setUser("user");
        f.setPassword("pass");
        int n = 0;
        while (true) {
            try {
                f.getSession();
                fail("Expected Exception");
            } catch (Exception e) {
                if (e instanceof IllegalStateException && "failed to create SFTP Session".equals(e.getMessage())) {
                    if (e.getCause() instanceof IllegalStateException) {
                        if (e.getCause().getCause() instanceof JSchException) {
                            if (e.getCause().getCause().getCause() instanceof ConnectException) {
                                assertTrue("Server failed to start in 10 seconds", n++ < 100);
                                Thread.sleep(100);
                                continue;
                            }
                        }
                    }
                }
                assertThat(e, instanceOf(IllegalStateException.class));
                assertThat(e.getCause(), instanceOf(IllegalStateException.class));
                assertThat(e.getCause().getMessage(), equalTo("failed to connect"));
                break;
            }
        }
        n = 0;
        while (n++ < 100 && server.getActiveSessions().size() > 0) {
            Thread.sleep(100);
        }
        assertEquals(0, server.getActiveSessions().size());
    } finally {
        server.stop(true);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) JSchException(com.jcraft.jsch.JSchException) SshServer(org.apache.sshd.server.SshServer) File(java.io.File) ConnectException(java.net.ConnectException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 18 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.

the class SftpTestSupport method createServer.

@BeforeClass
public static void createServer() throws Exception {
    server = SshServer.setUpDefaultServer();
    server.setPasswordAuthenticator((username, password, session) -> true);
    server.setPort(0);
    server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
    server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
    server.setFileSystemFactory(new VirtualFileSystemFactory(remoteTemporaryFolder.getRoot().toPath()));
    server.start();
    port = server.getPort();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) Command(org.apache.sshd.server.Command) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 19 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project gerrit by GerritCodeReview.

the class HostKeyProvider method get.

@Override
public KeyPairProvider get() {
    Path objKey = site.ssh_key;
    Path rsaKey = site.ssh_rsa;
    Path ecdsaKey_256 = site.ssh_ecdsa_256;
    Path ecdsaKey_384 = site.ssh_ecdsa_384;
    Path ecdsaKey_521 = site.ssh_ecdsa_521;
    Path ed25519Key = site.ssh_ed25519;
    final List<Path> stdKeys = new ArrayList<>(6);
    if (Files.exists(rsaKey)) {
        stdKeys.add(rsaKey);
    }
    if (Files.exists(ecdsaKey_256)) {
        stdKeys.add(ecdsaKey_256);
    }
    if (Files.exists(ecdsaKey_384)) {
        stdKeys.add(ecdsaKey_384);
    }
    if (Files.exists(ecdsaKey_521)) {
        stdKeys.add(ecdsaKey_521);
    }
    if (Files.exists(ed25519Key)) {
        stdKeys.add(ed25519Key);
    }
    if (Files.exists(objKey)) {
        if (stdKeys.isEmpty()) {
            SimpleGeneratorHostKeyProvider p = new SimpleGeneratorHostKeyProvider();
            p.setAlgorithm(KeyUtils.RSA_ALGORITHM);
            p.setPath(objKey.toAbsolutePath());
            logger.atWarning().log("Defaulting to RSA algorithm for SSH key exchange." + "This is a weak security setting, consider changing it (see 'sshd.kex' documentation section).");
            return p;
        }
        // Both formats of host key exist, we don't know which format
        // should be authoritative. Complain and abort.
        // 
        stdKeys.add(objKey);
        throw new ProvisionException("Multiple host keys exist: " + stdKeys);
    }
    if (stdKeys.isEmpty()) {
        throw new ProvisionException("No SSH keys under " + site.etc_dir);
    }
    FileKeyPairProvider kp = new FileKeyPairProvider();
    kp.setPaths(stdKeys);
    return kp;
}
Also used : Path(java.nio.file.Path) SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ProvisionException(com.google.inject.ProvisionException) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ArrayList(java.util.ArrayList)

Example 20 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project tomee by apache.

the class SSHServer method start.

@Override
public void start() throws ServiceException {
    sshServer = SshServer.setUpDefaultServer();
    sshServer.setPort(port);
    sshServer.setHost(bind);
    final String basePath = SystemInstance.get().getBase().getDirectory().getAbsolutePath();
    if (SecurityUtils.isBouncyCastleRegistered()) {
        sshServer.setKeyPairProvider(new BouncyCastleGeneratorHostKeyProvider(new File(basePath, KEY_NAME + ".pem").toPath()));
    } else {
        sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(basePath, KEY_NAME + ".ser").toPath()));
    }
    final OpenEJBShellFactory sf = new OpenEJBShellFactory(bind, port);
    sshServer.setShellFactory(sf);
    final JaasPasswordAuthenticator authenticator = new OpenEJBJaasPasswordAuthenticator();
    authenticator.setDomain(domain);
    sshServer.setPasswordAuthenticator(authenticator);
    try {
        sshServer.start();
    } catch (IOException e) {
    // no-op
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) JaasPasswordAuthenticator(org.apache.sshd.server.jaas.JaasPasswordAuthenticator) IOException(java.io.IOException) File(java.io.File) BouncyCastleGeneratorHostKeyProvider(org.apache.sshd.common.util.security.bouncycastle.BouncyCastleGeneratorHostKeyProvider)

Aggregations

SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)21 File (java.io.File)13 Command (org.apache.sshd.server.Command)9 SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)7 VirtualFileSystemFactory (org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory)5 ArrayList (java.util.ArrayList)4 NamedFactory (org.apache.sshd.common.NamedFactory)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 SshServer (org.apache.sshd.server.SshServer)3 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)3 Before (org.junit.Before)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 PublicKey (java.security.PublicKey)2 CommandFactory (org.apache.sshd.server.CommandFactory)2 PasswordAuthenticator (org.apache.sshd.server.PasswordAuthenticator)2 UserAuthPassword (org.apache.sshd.server.auth.UserAuthPassword)2 ScpCommandFactory (org.apache.sshd.server.command.ScpCommandFactory)2 ServerSession (org.apache.sshd.server.session.ServerSession)2