Search in sources :

Example 1 with SftpSubsystemFactory

use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project spring-integration by spring-projects.

the class SftpServerTests method testUcPw.

@Test
public void testUcPw() 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.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
        final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
        new File(pathname).mkdirs();
        server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
        server.start();
        DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
        f.setHost("localhost");
        f.setPort(server.getPort());
        f.setUser("user");
        f.setPassword("pass");
        f.setAllowUnknownKeys(true);
        Session<LsEntry> session = f.getSession();
        doTest(server, session);
    } finally {
        server.stop(true);
    }
}
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) SshServer(org.apache.sshd.server.SshServer) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Test(org.junit.Test)

Example 2 with SftpSubsystemFactory

use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project spring-integration by spring-projects.

the class SftpServerTests method testKeyExchange.

private void testKeyExchange(String pubKey, String privKey, String passphrase) throws Exception {
    SshServer server = SshServer.setUpDefaultServer();
    final PublicKey allowedKey = decodePublicKey(pubKey);
    try {
        server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey));
        server.setPort(0);
        server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
        server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
        final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
        new File(pathname).mkdirs();
        server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
        server.start();
        DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
        f.setHost("localhost");
        f.setPort(server.getPort());
        f.setUser("user");
        f.setAllowUnknownKeys(true);
        InputStream stream = new ClassPathResource(privKey).getInputStream();
        f.setPrivateKey(new ByteArrayResource(StreamUtils.copyToByteArray(stream)));
        f.setPrivateKeyPassphrase(passphrase);
        Session<LsEntry> session = f.getSession();
        doTest(server, session);
    } finally {
        server.stop(true);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) PublicKey(java.security.PublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) ByteArrayResource(org.springframework.core.io.ByteArrayResource) SshServer(org.apache.sshd.server.SshServer) ClassPathResource(org.springframework.core.io.ClassPathResource) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) Command(org.apache.sshd.server.Command) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 3 with SftpSubsystemFactory

use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project testcases by coheigea.

the class SFTPTest method setup.

@Before
public void setup() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(10000);
    // Write port number to configuration file in target
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    // Read in camel configuration file and substitute in the correct port
    File f = new File(basedir + "/src/test/resources/camel-sftp.xml");
    FileInputStream inputStream = new FileInputStream(f);
    String content = IOUtils.toString(inputStream, "UTF-8");
    inputStream.close();
    content = content.replaceAll("portno", "" + port);
    File f2 = new File(basedir + "/target/test-classes/camel-sftp.xml");
    FileOutputStream outputStream = new FileOutputStream(f2);
    IOUtils.write(content, outputStream, "UTF-8");
    outputStream.close();
    sshServer = SshServer.setUpDefaultServer();
    sshServer.setPort(port);
    // Generate a key
    sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("target/generatedkey.pem")));
    List<NamedFactory<Command>> subsystemFactories = new ArrayList<NamedFactory<Command>>(1);
    subsystemFactories.add(new SftpSubsystemFactory());
    sshServer.setSubsystemFactories(subsystemFactories);
    sshServer.setCommandFactory(new ScpCommandFactory());
    sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
    sshServer.start();
    // Create "storage_sftp" directory (delete it first if it exists)
    File storageDirectory = new File(basedir + "/target/storage_sftp");
    if (storageDirectory.exists()) {
        storageDirectory.delete();
    }
    storageDirectory.mkdir();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) ScpCommandFactory(org.apache.sshd.server.scp.ScpCommandFactory) Command(org.apache.sshd.server.Command) FileOutputStream(java.io.FileOutputStream) NamedFactory(org.apache.sshd.common.NamedFactory) File(java.io.File) Before(org.junit.Before)

Example 4 with SftpSubsystemFactory

use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project karaf by apache.

the class Activator method createSshServer.

protected SshServer createSshServer(SessionFactory sessionFactory) {
    int sshPort = getInt("sshPort", 8181);
    String sshHost = getString("sshHost", "0.0.0.0");
    long sshIdleTimeout = getLong("sshIdleTimeout", 1800000);
    int nioWorkers = getInt("nio-workers", 2);
    String sshRealm = getString("sshRealm", "karaf");
    String sshRole = getString("sshRole", null);
    String hostKey = getString("hostKey", System.getProperty("karaf.etc") + "/host.key");
    String[] authMethods = getStringArray("authMethods", "keyboard-interactive,password,publickey");
    int keySize = getInt("keySize", 2048);
    String algorithm = getString("algorithm", "RSA");
    String[] macs = getStringArray("macs", "hmac-sha2-512,hmac-sha2-256,hmac-sha1");
    String[] ciphers = getStringArray("ciphers", "aes128-ctr,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc");
    String[] kexAlgorithms = getStringArray("kexAlgorithms", "diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1");
    String welcomeBanner = getString("welcomeBanner", null);
    String moduliUrl = getString("moduli-url", null);
    boolean sftpEnabled = getBoolean("sftpEnabled", true);
    Path serverKeyPath = Paths.get(hostKey);
    KeyPairProvider keyPairProvider = new OpenSSHKeyPairProvider(serverKeyPath.toFile(), algorithm, keySize);
    KarafJaasAuthenticator authenticator = new KarafJaasAuthenticator(sshRealm, sshRole);
    UserAuthFactoriesFactory authFactoriesFactory = new UserAuthFactoriesFactory();
    authFactoriesFactory.setAuthMethods(authMethods);
    SshServer server = SshServer.setUpDefaultServer();
    server.setPort(sshPort);
    server.setHost(sshHost);
    server.setMacFactories(SshUtils.buildMacs(macs));
    server.setCipherFactories(SshUtils.buildCiphers(ciphers));
    server.setKeyExchangeFactories(SshUtils.buildKexAlgorithms(kexAlgorithms));
    server.setShellFactory(new ShellFactoryImpl(sessionFactory));
    if (sftpEnabled) {
        server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(cmd -> new ShellCommand(sessionFactory, cmd)).build());
        server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
        server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(System.getProperty("karaf.base"))));
    } else {
        server.setCommandFactory(cmd -> new ShellCommand(sessionFactory, cmd));
    }
    server.setKeyPairProvider(keyPairProvider);
    server.setPasswordAuthenticator(authenticator);
    server.setPublickeyAuthenticator(authenticator);
    server.setUserAuthFactories(authFactoriesFactory.getFactories());
    server.setAgentFactory(KarafAgentFactory.getInstance());
    server.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
    server.getProperties().put(SshServer.IDLE_TIMEOUT, Long.toString(sshIdleTimeout));
    server.getProperties().put(SshServer.NIO_WORKERS, Integer.toString(nioWorkers));
    if (moduliUrl != null) {
        server.getProperties().put(SshServer.MODULI_URL, moduliUrl);
    }
    if (welcomeBanner != null) {
        server.getProperties().put(SshServer.WELCOME_BANNER, welcomeBanner);
    }
    return server;
}
Also used : Path(java.nio.file.Path) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) SshServer(org.apache.sshd.server.SshServer) ScpCommandFactory(org.apache.sshd.server.scp.ScpCommandFactory) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) OpenSSHKeyPairProvider(org.apache.karaf.shell.ssh.keygenerator.OpenSSHKeyPairProvider) KeyPairProvider(org.apache.sshd.common.keyprovider.KeyPairProvider) OpenSSHKeyPairProvider(org.apache.karaf.shell.ssh.keygenerator.OpenSSHKeyPairProvider)

Example 5 with SftpSubsystemFactory

use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project spring-integration-samples by spring-projects.

the class EmbeddedSftpServer method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    final PublicKey allowedKey = decodePublicKey();
    this.server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey));
    this.server.setPort(this.port);
    this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
    this.server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
    final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
    new File(pathname).mkdirs();
    server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) PublicKey(java.security.PublicKey) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) File(java.io.File)

Aggregations

SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)9 File (java.io.File)6 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)6 VirtualFileSystemFactory (org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory)5 Command (org.apache.sshd.server.Command)5 SshServer (org.apache.sshd.server.SshServer)3 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 PublicKey (java.security.PublicKey)2 NamedFactory (org.apache.sshd.common.NamedFactory)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 OpenSSHKeyPairProvider (org.apache.karaf.shell.ssh.keygenerator.OpenSSHKeyPairProvider)1 KeyPairProvider (org.apache.sshd.common.keyprovider.KeyPairProvider)1