@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))); }
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(); }
@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").toPath())); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setFileSystemFactory(new VirtualFileSystemFactory(remoteTemporaryFolder.getRoot().toPath())); server.start(); port = server.getPort(); }
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").toPath())); 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))); 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(FileCopyUtils.copyToByteArray(stream))); f.setPrivateKeyPassphrase(passphrase); Session<LsEntry> session = f.getSession(); doTest(server, session); } finally { server.stop(true); } }
@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").toPath())); 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))); 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); } }
@Override public void start() throws IOException { baseDirectory = Files.createTempDirectory("sftproot"); sshServer.setFileSystemFactory(new VirtualFileSystemFactory(baseDirectory.toAbsolutePath().toString())); super.start(); }
protected void setupScp() { sshd.setCommandFactory(new ScpCommandFactory()); sshd.setFileSystemFactory(new SecureFileSystemFactory(db)); sshd.setTcpipForwardingFilter(null); sshd.setAgentFactory(null); }
@Override protected void before() throws Throwable { sshd = SshServer.setUpDefaultServer(); sshd.setPort(findRandomOpenPortOnAllLocalInterfaces()); sshd.setKeyPairProvider( new ClassLoadableResourceKeyPairProvider(getClass().getClassLoader(), "server_key")); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); sshd.setUserAuthFactories(Collections.singletonList(new UserAuthPublicKeyFactory())); sshd.setHost("localhost"); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setHostBasedAuthenticator(new StaticHostBasedAuthenticator(true)); VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory(); fileSystemFactory.setDefaultHomeDir(currentFolder.get().toPath()); sshd.setFileSystemFactory(fileSystemFactory); sshd.start(); }
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())); }
@Override public void afterPropertiesSet() throws Exception { this.sftpFolder.create(); this.localFolder.create(); this.server.setPasswordAuthenticator((username, password, session) -> true); this.server.setPort(this.port); this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser"))); this.server.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); this.server.setFileSystemFactory(new VirtualFileSystemFactory(sftpRootFolder.toPath())); this.server.start(); }
public static SshServer create() { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(SpashConfig.getInstance().spashListenPort()); AbstractGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider(new File(SpashConfig.getInstance().spashKeyFileName())); keyProvider.setAlgorithm(SpashConfig.getInstance().spashKeyAlgorithm()); keyProvider.setKeySize(SpashConfig.getInstance().spashKeyLength()); sshd.setKeyPairProvider(keyProvider); List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>(); userAuthFactories.add(new UserAuthPasswordFactory()); sshd.setUserAuthFactories(userAuthFactories); sshd.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession serverSession) throws PasswordChangeRequiredException { return username!=null && username.length()>0 && username.equals(password); } }); sshd.setShellFactory(new SpashShellFactory()); List<NamedFactory<Command>> namedFactoryList = new ArrayList<>(); namedFactoryList.add(new SftpSubsystemFactory()); sshd.setSubsystemFactories(namedFactoryList); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setFileSystemFactory(new FileSystemFactory() { @Override public FileSystem createFileSystem(Session session) throws IOException { return SpashFileSystem.get().getFileSystem(); } }); return sshd; }
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.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));