org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory Java Examples

The following examples show how to use org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory. 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: EmbeddedSftpServer.java    From java-examples with MIT License 7 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    final PublicKey allowedKey = decodePublicKey();
    this.server.setPublickeyAuthenticator(new PublickeyAuthenticator() {

        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return key.equals(allowedKey);
        }

    });
    this.server.setPort(this.port);
    this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Files.createTempFile("host_file", ".ser")));
    this.server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
    server.setFileSystemFactory(new VirtualFileSystemFactory(Files.createTempDirectory("SFTP_TEMP")));
    server.setCommandFactory(new ScpCommandFactory());
}
 
Example #2
Source File: EmbeddedSftpServer.java    From java-examples with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    final PublicKey allowedKey = decodePublicKey();
    this.server.setPublickeyAuthenticator(new PublickeyAuthenticator() {

        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return key.equals(allowedKey);
        }

    });
    this.server.setPort(this.port);
    this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Files.createTempFile("host_file", ".ser")));
    this.server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
    server.setFileSystemFactory(new VirtualFileSystemFactory(Files.createTempDirectory("SFTP_TEMP")));
    server.setCommandFactory(new ScpCommandFactory());
}
 
Example #3
Source File: FakeSftpServerRule.java    From fake-sftp-server-rule with MIT License 6 votes vote down vote up
private SshServer startServer(
    FileSystem fileSystem
) throws IOException {
    SshServer server = SshServer.setUpDefaultServer();
    server.setPort(port);
    server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    server.setPasswordAuthenticator(this::authenticate);
    server.setSubsystemFactories(singletonList(new SftpSubsystemFactory()));
    /* When a channel is closed SshServer calls close() on the file system.
     * In order to use the file system for multiple channels/sessions we
     * have to use a file system wrapper whose close() does nothing.
     */
    server.setFileSystemFactory(session -> new DoNotClose(fileSystem));
    server.start();
    this.server = server;
    return server;
}
 
Example #4
Source File: SftpServerRunner.java    From product-ei with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    sshd.setPort(port);
    sshd.setSubsystemFactories(
            Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    sshd.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(path)));
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
        @Override
        public boolean authenticate(final String username, final String password, final ServerSession session) {
            return StringUtils.equals(username, ftpUser) && StringUtils.equals(password, ftpPassword);
        }
    });
    try {
        LOGGER.info("Starting SFTP server on port {}", port);
        sshd.start();
    } catch (IOException e) {
        LOGGER.error("Error starting SFTP server", e);
    }
}
 
Example #5
Source File: Server.java    From sftpserver with Apache License 2.0 6 votes vote down vote up
protected void setupFactories() {
	final SftpSubsystemFactory sftpSubsys = new SftpSubsystemFactory.Builder()
			.withFileSystemAccessor(new CustomSftpFileSystemAccessor()).build();
	// Request logger
	sftpSubsys.addSftpEventListener(logger);
	// Session logger
	sshd.addSessionListener(logger);
	// org.apache.sshd.common.BaseBuilder
	sshd.setSubsystemFactories(Collections.singletonList(sftpSubsys));
	sshd.setChannelFactories(Collections.singletonList(ChannelSessionFactory.INSTANCE));
	SshConfigFileReader.configureKeyExchanges(sshd, //
			db.getKexAlgorithms(), //
			true, ServerBuilder.DH2KEX, true);
	SshConfigFileReader.configureCiphers(sshd, //
			db.getCiphers(), //
			true, true);
	SshConfigFileReader.configureMacs(sshd, //
			db.getMacs(), //
			true, true);
}
 
Example #6
Source File: SftpServerRunner.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
SftpServer(int port, String path, String ftpUser, String ftpPassword) {

            sshd.setPort(port);
            sshd.setSubsystemFactories(
                    Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory()));
            sshd.setCommandFactory(new ScpCommandFactory());
            sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
            sshd.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(path)));
            sshd.setPasswordAuthenticator((username, password, session) -> StringUtils.equals(username, ftpUser)
                                                                           && StringUtils.equals(password,
                                                                                                 ftpPassword));
        }
 
Example #7
Source File: ESBJAVA3470.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Starts a SFTP server on port 22
 * @param carbonHome
 */
private void setupSftpServer(String carbonHome) {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(FTP_PORT);
    //sshd.setKeyPairProvider(new FileKeyPairProvider(new
    // String[]{"/home/ravi/WORK/SUPPORT/JIRA/SKYTVNZDEV-26/SftpTest/dist/hostkey.ser"}));
    ClassLoader classLoader = getClass().getClassLoader();
    log.info("Using identity file: " + classLoader.getResource("sftp/id_rsa.pub").getFile());
    File file = new File(classLoader.getResource("sftp/id_rsa.pub").getFile());
    sshd.setKeyPairProvider(createTestHostKeyProvider(Paths.get(file.getAbsolutePath())));
    sshd.setUserAuthFactories(Arrays.asList(new UserAuthPublicKeyFactory()));
    sshd.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(carbonHome)));
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return "sftpuser".equals(username);
        }
    });
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setSubsystemFactories(Arrays.asList(new SftpSubsystemFactory()));
    SftpServerRunner sftpServerRunner = new SftpServerRunner(sshd);

    try {
        sftpServerRunner.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: SftpTestResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {
    try {
        final int port = AvailablePortFinder.getNextAvailable();

        sftpHome = Files.createTempDirectory("sftp-");
        sshHome = sftpHome.resolve("admin/.ssh");

        Files.createDirectories(sshHome);

        byte[] knownHostsBytes = String.format(KNOWN_HOSTS, port).getBytes(StandardCharsets.UTF_8);
        Files.write(sshHome.resolve(".known_hosts"), knownHostsBytes);

        VirtualFileSystemFactory factory = new VirtualFileSystemFactory();
        factory.setUserHomeDir("admin", sftpHome.resolve("admin").toAbsolutePath());

        sshServer = SshServer.setUpDefaultServer();
        sshServer.setPort(port);
        sshServer.setKeyPairProvider(new ClassLoadableResourceKeyPairProvider("hostkey.pem"));
        sshServer.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
        sshServer.setCommandFactory(new ScpCommandFactory());
        sshServer.setPasswordAuthenticator((username, password, session) -> true);
        sshServer.setPublickeyAuthenticator((username, key, session) -> true);
        sshServer.setFileSystemFactory(factory);
        sshServer.start();

        return CollectionHelper.mapOf("camel.sftp.test-port", Integer.toString(port));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: SFTPCryptomatorInteroperabilityTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void startSerer() throws Exception {
    server = SshServer.setUpDefaultServer();
    server.setPort(PORT_NUMBER);
    server.setPasswordAuthenticator((username, password, session) -> true);
    server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
    final java.nio.file.Path tempDir = Files.createTempDirectory(String.format("%s-", this.getClass().getName()));
    final java.nio.file.Path vault = tempDir.resolve("vault");
    Files.createDirectory(vault);
    passphrase = new AlphanumericRandomStringService().random();
    cryptoFileSystem = CryptoFileSystemProvider.newFileSystem(vault, CryptoFileSystemProperties.cryptoFileSystemProperties().withPassphrase(passphrase).build());
    server.setFileSystemFactory(new VirtualFileSystemFactory(cryptoFileSystem.getPathToVault().getParent().toAbsolutePath()));
    server.start();
}
 
Example #10
Source File: ESBJAVA3470.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Starts a SFTP server on port 22
 * @param carbonHome
 */
private void setupSftpServer(String carbonHome) {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(FTP_PORT);
    //sshd.setKeyPairProvider(new FileKeyPairProvider(new String[]{"/home/ravi/WORK/SUPPORT/JIRA/SKYTVNZDEV-26/SftpTest/dist/hostkey.ser"}));
    ClassLoader classLoader = getClass().getClassLoader();
    log.info("Using identity file: " + classLoader.getResource("sftp/id_rsa.pub").getFile());
    File file = new File(classLoader.getResource("sftp/id_rsa.pub").getFile());
    SFTPServer sftpServer = new SFTPServer();
    sshd.setKeyPairProvider(sftpServer.createTestHostKeyProvider(Paths.get(file.getAbsolutePath())));
    sshd.setKeyPairProvider(createTestHostKeyProvider(Paths.get(file.getAbsolutePath())));
    sshd.setUserAuthFactories(
            Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPublicKeyFactory()));
    sshd.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(carbonHome)));
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return "sftpuser".equals(username);
        }
    });

    sshd.setCommandFactory(new ScpCommandFactory());

    sshd.setSubsystemFactories(
            Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory()));

    try {
        sshd.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #11
Source File: SSHTestServer.java    From nifi with Apache License 2.0 5 votes vote down vote up
public void startServer() throws IOException {
    sshd = SshServer.setUpDefaultServer();
    sshd.setHost("localhost");

    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());

    //Accept all keys for authentication
    sshd.setPublickeyAuthenticator((s, publicKey, serverSession) -> true);

    //Allow username/password authentication using pre-defined credentials
    sshd.setPasswordAuthenticator((username, password, serverSession) ->  this.username.equals(username) && this.password.equals(password));

    //Setup Virtual File System (VFS)
    //Ensure VFS folder exists
    Path dir = Paths.get(getVirtualFileSystemPath());
    Files.createDirectories(dir);
    sshd.setFileSystemFactory(new VirtualFileSystemFactory(dir.toAbsolutePath()));

    //Add SFTP support
    List<NamedFactory<Command>> sftpCommandFactory = new ArrayList<>();
    sftpCommandFactory.add(new SftpSubsystemFactory());
    sshd.setSubsystemFactories(sftpCommandFactory);

    sshd.start();
}
 
Example #12
Source File: SshdPlugin.java    From Bukkit-SSHD with Apache License 2.0 5 votes vote down vote up
@Override
public void onEnable() {
    instance = this;

    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(getConfig().getInt("port", 22));
    String host = getConfig().getString("listenAddress", "all");
    sshd.setHost(host.equals("all") ? null : host);

    File hostKey = new File(getDataFolder(), "hostkey");
    File authorizedKeys = new File(getDataFolder(), "authorized_keys");

    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKey));
    sshd.setShellFactory(new ConsoleShellFactory());
    sshd.setPasswordAuthenticator(new ConfigPasswordAuthenticator());
    sshd.setPublickeyAuthenticator(new PublicKeyAuthenticator(authorizedKeys));

    if (getConfig().getBoolean("enableSFTP")) {
        sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
        sshd.setFileSystemFactory(new VirtualFileSystemFactory(
                FileSystems.getDefault().getPath(
                        getDataFolder().getAbsolutePath()
                ).getParent().getParent()
        ));
    }

    sshd.setCommandFactory(new ConsoleCommandFactory());
    try {
        sshd.start();
    } catch (IOException e) {
        getLogger().log(Level.SEVERE, "Failed to start SSH server! ", e);
    }
}
 
Example #13
Source File: SshdServerConfiguration.java    From sshd-shell-spring-boot with Apache License 2.0 4 votes vote down vote up
private void configureServerForSshAndFileTransfer(SshServer server) {
    server.setCommandFactory(sshAndScpCommandFactory());
    server.setFileSystemFactory(new SshdNativeFileSystemFactory(properties.getFilesystem().getBase().getDir()));
    server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(
            new SftpSubsystemFactory.Builder().build()));
}
 
Example #14
Source File: EmbeddedSSHServer.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
public void setupSftp() {
    this.sshServer.setCommandFactory(new ScpCommandFactory());
    this.sshServer.setSubsystemFactories(Arrays.asList(new SftpSubsystemFactory()));
}