org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory Java Examples

The following examples show how to use org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory. 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: 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 #4
Source File: SftpServer.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private SshServer createSshServer( int port, String homeDir, String hostKeyPath ) {
  SshServer server = SshServer.setUpDefaultServer();
  server.setHost( "localhost" );
  server.setPort( port );
  server.setFileSystemFactory( new VirtualFileSystemFactory( homeDir ) );
  server.setSubsystemFactories( Collections.<NamedFactory<Command>>singletonList( new SftpSubsystem.Factory() ) );
  server.setCommandFactory( new ScpCommandFactory() );
  server.setKeyPairProvider( new SimpleGeneratorHostKeyProvider( hostKeyPath ) );
  server.setPasswordAuthenticator( this );
  return server;
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: EmbeddedSftpServer.java    From java-examples with MIT License 4 votes vote down vote up
public void setHomeFolder(Path path) {
    server.setFileSystemFactory(new VirtualFileSystemFactory(path));
}
 
Example #13
Source File: EmbeddedSftpServer.java    From java-examples with MIT License 4 votes vote down vote up
public void setHomeFolder(Path path) {
    server.setFileSystemFactory(new VirtualFileSystemFactory(path));
}