Search in sources :

Example 1 with ServerBuilder

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);
}
Also used : ServerSession(org.apache.sshd.server.session.ServerSession) KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) PublickeyAuthenticator(org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator) PublicKey(java.security.PublicKey) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) SshServer(org.apache.sshd.server.SshServer) ImmutableMap(com.google.common.collect.ImmutableMap) ScpCommandFactory(org.apache.sshd.server.scp.ScpCommandFactory) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) KeyPairProvider(org.apache.sshd.common.keyprovider.KeyPairProvider) PasswordAuthenticator(org.apache.sshd.server.auth.password.PasswordAuthenticator) Command(org.apache.sshd.server.command.Command) ServerBuilder(org.apache.sshd.server.ServerBuilder)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 KeyPair (java.security.KeyPair)1 PublicKey (java.security.PublicKey)1 VirtualFileSystemFactory (org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory)1 KeyPairProvider (org.apache.sshd.common.keyprovider.KeyPairProvider)1 ServerBuilder (org.apache.sshd.server.ServerBuilder)1 SshServer (org.apache.sshd.server.SshServer)1 PasswordAuthenticator (org.apache.sshd.server.auth.password.PasswordAuthenticator)1 PublickeyAuthenticator (org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator)1 Command (org.apache.sshd.server.command.Command)1 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)1 ServerSession (org.apache.sshd.server.session.ServerSession)1 SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)1 PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)1