Search in sources :

Example 1 with PublickeyAuthenticator

use of org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator in project ebad by informatique-cdc.

the class ShellServiceTest method setupSSHServer.

private void setupSSHServer() throws IOException {
    sshd = SshServer.setUpDefaultServer();
    final PublicKey allowedKey;
    sshd.setPasswordAuthenticator((username, password, session) -> StringUtils.equals(username, USERNAME) && StringUtils.equals(password, PASSWORD));
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {

        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return false;
        // return key.equals(allowedKey);
        }
    });
    sshd.setPort(2048);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Files.createTempFile("host_file", ".ser")));
    sshd.setSubsystemFactories(Collections.<SubsystemFactory>singletonList(new SftpSubsystemFactory()));
    sshd.setFileSystemFactory(new VirtualFileSystemFactory(Files.createTempDirectory("SFTP_TEMP")));
    sshd.setCommandFactory(new ProcessShellCommandFactory());
    sshd.start();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) SftpSubsystemFactory(org.apache.sshd.sftp.server.SftpSubsystemFactory) ServerSession(org.apache.sshd.server.session.ServerSession) PublickeyAuthenticator(org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator) PublicKey(java.security.PublicKey) ProcessShellCommandFactory(org.apache.sshd.server.shell.ProcessShellCommandFactory) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 2 with PublickeyAuthenticator

use of org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator 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)

Example 3 with PublickeyAuthenticator

use of org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator in project equinox.bundles by eclipse-equinox.

the class SshServ method createSimpleAuthorizedKeysAuthenticator.

private PublickeyAuthenticator createSimpleAuthorizedKeysAuthenticator() {
    // use authorized keys file if property is set
    final String authorizedKeysFile = System.getProperty(SSH_AUTHORIZED_KEYS_FILE_PROP);
    if (null != authorizedKeysFile) {
        AuthorizedKeysFileAuthenticator authenticator = new AuthorizedKeysFileAuthenticator();
        authenticator.setAuthorizedKeysFile(authorizedKeysFile);
        return authenticator;
    }
    final String customPublicKeysAuthentication = System.getProperty(SSH_CUSTOM_PUBLIC_KEY_AUTHENTICATION);
    // fall back to dynamic provider based on available OSGi services only if explicitly specified
    if ("true".equals(customPublicKeysAuthentication)) {
        return (username, key, session) -> {
            // find available services
            try {
                for (ServiceReference<PublickeyAuthenticator> reference : context.getServiceReferences(PublickeyAuthenticator.class, null)) {
                    PublickeyAuthenticator authenticator = null;
                    try {
                        authenticator = context.getService(reference);
                        // first positive match wins; continue looking otherwise
                        if (authenticator.authenticate(username, key, session))
                            return true;
                    } finally {
                        if (null != authenticator)
                            context.ungetService(reference);
                    }
                }
            } catch (InvalidSyntaxException e) {
            // no filter is used
            }
            return false;
        };
    }
    return null;
}
Also used : CommandProcessor(org.apache.felix.service.command.CommandProcessor) SshServer(org.apache.sshd.server.SshServer) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) BundleContext(org.osgi.framework.BundleContext) List(java.util.List) AuthorizedKeysFileAuthenticator(org.eclipse.equinox.console.internal.ssh.AuthorizedKeysFileAuthenticator) Paths(java.nio.file.Paths) PublickeyAuthenticator(org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator) JaasPasswordAuthenticator(org.apache.sshd.server.jaas.JaasPasswordAuthenticator) PasswordAuthenticator(org.apache.sshd.server.auth.password.PasswordAuthenticator) ServiceReference(org.osgi.framework.ServiceReference) PublickeyAuthenticator(org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator) AuthorizedKeysFileAuthenticator(org.eclipse.equinox.console.internal.ssh.AuthorizedKeysFileAuthenticator) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

PublickeyAuthenticator (org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator)3 PublicKey (java.security.PublicKey)2 VirtualFileSystemFactory (org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory)2 SshServer (org.apache.sshd.server.SshServer)2 PasswordAuthenticator (org.apache.sshd.server.auth.password.PasswordAuthenticator)2 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)2 ServerSession (org.apache.sshd.server.session.ServerSession)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 Paths (java.nio.file.Paths)1 KeyPair (java.security.KeyPair)1 List (java.util.List)1 CommandProcessor (org.apache.felix.service.command.CommandProcessor)1 KeyPairProvider (org.apache.sshd.common.keyprovider.KeyPairProvider)1 ServerBuilder (org.apache.sshd.server.ServerBuilder)1 Command (org.apache.sshd.server.command.Command)1 JaasPasswordAuthenticator (org.apache.sshd.server.jaas.JaasPasswordAuthenticator)1 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)1 ProcessShellCommandFactory (org.apache.sshd.server.shell.ProcessShellCommandFactory)1 SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)1