@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(); }
private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException { server.setPublickeyAuthenticator((username, key, session) -> true); server.setPort(0); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); 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; }
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); } }
/** * @param key Key in DER format */ public MockSshServer allowDerPublicKey(byte[] key) throws GeneralSecurityException { final KeySpec spec = new X509EncodedKeySpec(key); keys.add(KeyFactory.getInstance("RSA").generatePublic(spec)); sshServer.setPublickeyAuthenticator(new KeySetPublickeyAuthenticator(this.keys)); return this; }
public void setPublicKeyAuthenticator(PublickeyAuthenticator publicKeyAuthenticator) { sshdServer.setPublickeyAuthenticator(publicKeyAuthenticator); }
@Override public SshServer build(boolean isFillWithDefaultValues) { SshServer server = super.build(isFillWithDefaultValues); server.setPublickeyAuthenticator(pubkeyAuthenticator); server.setKeyboardInteractiveAuthenticator(interactiveAuthenticator); return server; }
@Override public SshServer build(boolean isFillWithDefaultValues) { SshServer server = super.build(isFillWithDefaultValues); server.setPublickeyAuthenticator(pubkeyAuthenticator); server.setKeyboardInteractiveAuthenticator(interactiveAuthenticator); return server; }
protected void setupAuth() { sshd.setPasswordAuthenticator(this); sshd.setPublickeyAuthenticator(this); sshd.setGSSAuthenticator(null); }
public MockSshServer allowOpensshPublicKey(String key) throws IOException, GeneralSecurityException { final SshRsaCrypto rsa = new SshRsaCrypto(); keys.add(rsa.readPublicKey(rsa.slurpPublicKey(key))); sshServer.setPublickeyAuthenticator(new KeySetPublickeyAuthenticator(this.keys)); return this; }
private void configureAuthenticationPolicies(SshServer server, Shell props) { server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get(props.getHostKeyFile()))); server.setPublickeyAuthenticator(Objects.isNull(props.getPublicKeyFile()) ? RejectAllPublickeyAuthenticator.INSTANCE : new SshdAuthorizedKeysAuthenticator(Paths.get(props.getPublicKeyFile()))); server.setPasswordAuthenticator(passwordAuthenticator(props)); }
protected SshServer initSshServer(int port) { final SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(port); sshd.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession session) { return USERNAME.equals(username) && PASSWORD.equals(password); } }); sshd.setPublickeyAuthenticator(new KeySetPublickeyAuthenticator(this.keys)); return sshd; }
private static SshServer createServer(int port, String repository) { SshServer s = SshServer.setUpDefaultServer(); s.setPort(port); s.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); s.setPublickeyAuthenticator((username, key, session) -> true); s.setCommandFactory(new GitPackCommandFactory(repository)); return s; } }
public EmbeddedSSHServer(Path homeDir, int port) { this.sshServer = SshServer.setUpDefaultServer(); this.sshServer.setPort(port); this.sshServer.setCommandFactory(new EchoCommandFactory()); this.sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); this.sshServer.setPasswordAuthenticator((username, password, serverSession) -> username.equals("admin") && password.equals("admin")); this.sshServer.setPublickeyAuthenticator((s, publicKey, serverSession) -> true); this.homeDir = homeDir; if (EnvironmentUtils.isWindows()) { sshServer.setShellFactory(new ProcessShellFactory(new String[] { "cmd.exe " })); } else { sshServer.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-s" })); } }
public void setUser(String user, String password, PublicKey key) { sshd.setPasswordAuthenticator(new MyPasswordAuthenticator(user, password)); sshd.setPublickeyAuthenticator(new MyPublickeyAuthenticator(user, key)); }
@Before public void setUp() throws Exception { sshd = setupTestServer(); PropertyResolverUtils.updateProperty(sshd, ServerFactoryManager.AUTH_METHODS, UserAuthPublicKeyFactory.NAME); sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() { @SuppressWarnings("synthetic-access") @Override public boolean authenticate(String username, PublicKey key, ServerSession session) { return delegate.authenticate(username, key, session); } }); sshd.start(); port = sshd.getPort(); }
@Before public void setUp() throws Exception { sshd = setupTestServer(); PropertyResolverUtils.updateProperty(sshd, ServerFactoryManager.AUTH_METHODS, UserAuthPublicKeyFactory.NAME); sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() { @SuppressWarnings("synthetic-access") @Override public boolean authenticate(String username, PublicKey key, ServerSession session) { return delegate.authenticate(username, key, session); } }); sshd.start(); port = sshd.getPort(); }
public static SshServer setupTestServer(Class<?> anchor) { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setKeyPairProvider(createTestHostKeyProvider(anchor)); sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setShellFactory(EchoShellFactory.INSTANCE); sshd.setCommandFactory(UnknownCommandFactory.INSTANCE); return sshd; }
public static SshServer setupTestServer(Class<?> anchor) { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setKeyPairProvider(createTestHostKeyProvider(anchor)); sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setShellFactory(EchoShellFactory.INSTANCE); sshd.setCommandFactory(UnknownCommandFactory.INSTANCE); return sshd; }
public static SshServer setupTestServer(Class<?> anchor) { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setKeyPairProvider(createTestHostKeyProvider(anchor)); sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setShellFactory(EchoShellFactory.INSTANCE); sshd.setCommandFactory(UnknownCommandFactory.INSTANCE); return sshd; }