Search in sources :

Example 1 with PasswordAuthenticator

use of org.apache.sshd.server.PasswordAuthenticator in project hadoop by apache.

the class TestSFTPFileSystem method startSshdServer.

private static void startSshdServer() throws IOException {
    sshd = SshServer.setUpDefaultServer();
    // ask OS to assign a port
    sshd.setPort(0);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
    userAuthFactories.add(new UserAuthPassword.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            if (username.equals("user") && password.equals("password")) {
                return true;
            }
            return false;
        }
    });
    sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
    sshd.start();
    port = sshd.getPort();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ServerSession(org.apache.sshd.server.session.ServerSession) ArrayList(java.util.ArrayList) NamedFactory(org.apache.sshd.common.NamedFactory) PasswordAuthenticator(org.apache.sshd.server.PasswordAuthenticator) Command(org.apache.sshd.server.Command) NamedFactory(org.apache.sshd.common.NamedFactory) UserAuth(org.apache.sshd.server.UserAuth) UserAuthPassword(org.apache.sshd.server.auth.UserAuthPassword)

Example 2 with PasswordAuthenticator

use of org.apache.sshd.server.PasswordAuthenticator in project scheduling by ow2-proactive.

the class TestSSHInfrastructureV2 method startSSHServer.

@BeforeClass
public static void startSSHServer() throws Exception {
    // Disable bouncy castle to avoid versions conflict
    System.setProperty("org.apache.sshd.registerBouncyCastle", "false");
    sshd = SshServer.setUpDefaultServer();
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<>(1);
    userAuthFactories.add(new UserAuthPassword.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            return username != null && username.equals(password);
        }
    });
    sshd.setCommandFactory(new ScpCommandFactory(new CommandFactory() {

        @Override
        public Command createCommand(String command) {
            String[] splitCommand;
            if (OsUtils.isUNIX()) {
                splitCommand = SSHInfrastructureHelper.splitCommand(command);
            } else if (OsUtils.isWin32()) {
                splitCommand = SSHInfrastructureHelper.splitCommandWithoutRemovingQuotes(command);
            } else {
                throw new IllegalStateException("Operating system is not recognized");
            }
            StringBuilder rebuiltCommand = new StringBuilder();
            for (String commandPiece : splitCommand) {
                rebuiltCommand.append(commandPiece).append(" ");
            }
            rebuiltCommand.trimToSize();
            EnumSet<ProcessShellFactory.TtyOptions> ttyOptions;
            if (OsUtils.isUNIX()) {
                ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr);
            } else {
                ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr);
            }
            if (OsUtils.isUNIX()) {
                return new ProcessShellFactory(new String[] { "/bin/sh", "-c", rebuiltCommand.toString() }, ttyOptions).create();
            } else {
                return new ProcessShellFactory(new String[] { "cmd.exe", "/C", rebuiltCommand.toString() }, ttyOptions).create();
            }
        }
    }));
    sshd.start();
    port = sshd.getPort();
    javaExePath = System.getProperty("java.home") + File.separator + "bin" + File.separator + (OsUtils.isWin32() ? "java.exe" : "java");
    javaExePath = "\"" + javaExePath + "\"";
    infraParams = new Object[] { // hosts
    ("localhost " + NB_NODES + "\n").getBytes(), // timeout
    60000, // attempts
    0, // wait between failures
    10, // ssh server port
    port, // ssh username
    "toto", // ssh password
    "toto", // optional ssh private key
    new byte[0], // optional ssh options file
    new byte[0], // java path on the remote machines
    javaExePath, // Scheduling path on remote machines
    PAResourceManagerProperties.RM_HOME.getValueAsString(), OperatingSystem.getOperatingSystem(), // extra java options
    "" };
    policyParameters = new Object[] { AccessType.ALL.toString(), AccessType.ALL.toString(), "20000" };
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ServerSession(org.apache.sshd.server.session.ServerSession) ArrayList(java.util.ArrayList) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) CommandFactory(org.apache.sshd.server.CommandFactory) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) PasswordAuthenticator(org.apache.sshd.server.PasswordAuthenticator) NamedFactory(org.apache.sshd.common.NamedFactory) ProcessShellFactory(org.apache.sshd.server.shell.ProcessShellFactory) UserAuthPassword(org.apache.sshd.server.auth.UserAuthPassword) BeforeClass(org.junit.BeforeClass)

Example 3 with PasswordAuthenticator

use of org.apache.sshd.server.PasswordAuthenticator in project camel by apache.

the class ScpServerTestSupport method startSshd.

protected boolean startSshd() {
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(getPort());
    sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
    sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            // dummy authentication: allow any user whose password is the same as the username
            return username != null && username.equals(password);
        }
    });
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {

        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return true;
        }
    });
    try {
        sshd.start();
        return true;
    } catch (IOException e) {
        LOG.info("Failed to start ssh server.", e);
    }
    return false;
}
Also used : ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ServerSession(org.apache.sshd.server.session.ServerSession) PasswordAuthenticator(org.apache.sshd.server.PasswordAuthenticator) PublickeyAuthenticator(org.apache.sshd.server.PublickeyAuthenticator) Command(org.apache.sshd.server.Command) PublicKey(java.security.PublicKey) LoggerFactory(org.slf4j.LoggerFactory) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) NamedFactory(org.apache.sshd.common.NamedFactory) IOException(java.io.IOException)

Aggregations

NamedFactory (org.apache.sshd.common.NamedFactory)3 PasswordAuthenticator (org.apache.sshd.server.PasswordAuthenticator)3 ServerSession (org.apache.sshd.server.session.ServerSession)3 ArrayList (java.util.ArrayList)2 Command (org.apache.sshd.server.Command)2 UserAuthPassword (org.apache.sshd.server.auth.UserAuthPassword)2 ScpCommandFactory (org.apache.sshd.server.command.ScpCommandFactory)2 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)2 IOException (java.io.IOException)1 PublicKey (java.security.PublicKey)1 FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)1 CommandFactory (org.apache.sshd.server.CommandFactory)1 PublickeyAuthenticator (org.apache.sshd.server.PublickeyAuthenticator)1 UserAuth (org.apache.sshd.server.UserAuth)1 ProcessShellFactory (org.apache.sshd.server.shell.ProcessShellFactory)1 BeforeClass (org.junit.BeforeClass)1 LoggerFactory (org.slf4j.LoggerFactory)1