use of org.apache.sshd.server.ServerBuilder in project nomulus by google.
the class TestSftpServer method createSftpServer.
public static FtpServer createSftpServer(final String authorizedUser, @Nullable final String authorizedPassword, @Nullable final PublicKey authorizedPublicKey, int port, final File home) {
ServerBuilder serverBuilder = ServerBuilder.builder();
serverBuilder.randomFactory(secureRandomFactory);
if (authorizedPublicKey != null) {
// This authenticator checks that the user is presenting the right key. If authenticate
// returns true, then the server will make sure that the user can prove they have that key.
// Not that you would know this from the Apache javadocs.
serverBuilder.publickeyAuthenticator(new PublickeyAuthenticator() {
@Override
public boolean authenticate(String username, PublicKey publicKey, ServerSession session) {
return Arrays.equals(publicKey.getEncoded(), authorizedPublicKey.getEncoded());
}
});
}
serverBuilder.fileSystemFactory(new VirtualFileSystemFactory(home.toPath()));
SshServer server = serverBuilder.build();
server.setCommandFactory(new ScpCommandFactory());
server.setPort(port);
NamedFactory<Command> sftpSubsystemFactory = new SftpSubsystemFactory.Builder().build();
server.setSubsystemFactories(ImmutableList.of(sftpSubsystemFactory));
if (authorizedPassword != null) {
server.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession session) {
return username.equals(authorizedUser) && password.equals(authorizedPassword);
}
});
}
KeyPairProvider keyPairProvider = new KeyPairProvider() {
final ImmutableMap<String, KeyPair> keyPairByTypeMap = ImmutableMap.of(KEY_TYPE, HOST_KEY_PAIR);
@Override
public Iterable<KeyPair> loadKeys() {
return keyPairByTypeMap.values();
}
@Override
public Iterable<String> getKeyTypes() {
return keyPairByTypeMap.keySet();
}
@Override
public KeyPair loadKey(final String type) {
return keyPairByTypeMap.get(type);
}
};
server.setKeyPairProvider(keyPairProvider);
return new TestSftpServer(server);
}
Aggregations