Search in sources :

Example 1 with Command

use of org.apache.sshd.server.Command 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 Command

use of org.apache.sshd.server.Command in project gerrit by GerritCodeReview.

the class DispatchCommand method start.

@Override
public void start(final Environment env) throws IOException {
    try {
        parseCommandLine();
        if (Strings.isNullOrEmpty(commandName)) {
            StringWriter msg = new StringWriter();
            msg.write(usage());
            throw die(msg.toString());
        }
        final CommandProvider p = commands.get(commandName);
        if (p == null) {
            String msg = (getName().isEmpty() ? "Gerrit Code Review" : getName()) + ": " + commandName + ": not found";
            throw die(msg);
        }
        final Command cmd = p.getProvider().get();
        checkRequiresCapability(cmd);
        if (cmd instanceof BaseCommand) {
            final BaseCommand bc = (BaseCommand) cmd;
            if (getName().isEmpty()) {
                bc.setName(commandName);
            } else {
                bc.setName(getName() + " " + commandName);
            }
            bc.setArguments(args.toArray(new String[args.size()]));
        } else if (!args.isEmpty()) {
            throw die(commandName + " does not take arguments");
        }
        provideStateTo(cmd);
        atomicCmd.set(cmd);
        cmd.start(env);
    } catch (UnloggedFailure e) {
        String msg = e.getMessage();
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        err.write(msg.getBytes(ENC));
        err.flush();
        onExit(e.exitCode);
    }
}
Also used : StringWriter(java.io.StringWriter) Command(org.apache.sshd.server.Command)

Example 3 with Command

use of org.apache.sshd.server.Command in project gerrit by GerritCodeReview.

the class AliasCommand method begin.

private void begin(Environment env) throws IOException, Failure {
    Map<String, CommandProvider> map = root.getMap();
    for (String name : chain(command)) {
        CommandProvider p = map.get(name);
        if (p == null) {
            throw die(getName() + ": not found");
        }
        Command cmd = p.getProvider().get();
        if (!(cmd instanceof DispatchCommand)) {
            throw die(getName() + ": not found");
        }
        map = ((DispatchCommand) cmd).getMap();
    }
    CommandProvider p = map.get(command.value());
    if (p == null) {
        throw die(getName() + ": not found");
    }
    Command cmd = p.getProvider().get();
    checkRequiresCapability(cmd);
    if (cmd instanceof BaseCommand) {
        BaseCommand bc = (BaseCommand) cmd;
        bc.setName(getName());
        bc.setArguments(getArguments());
    }
    provideStateTo(cmd);
    atomicCmd.set(cmd);
    cmd.start(env);
}
Also used : Command(org.apache.sshd.server.Command)

Example 4 with Command

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

the class SftpServerTestSupport method setUpServer.

protected void setUpServer() throws Exception {
    canTest = true;
    try {
        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 MyPasswordAuthenticator());
        PublickeyAuthenticator publickeyAuthenticator = new PublickeyAuthenticator() {

            // consider all keys as authorized for all users
            @Override
            public boolean authenticate(String username, PublicKey key, ServerSession session) {
                return true;
            }
        };
        sshd.setPublickeyAuthenticator(publickeyAuthenticator);
        sshd.start();
    } catch (Exception e) {
        // ignore if algorithm is not on the OS
        NoSuchAlgorithmException nsae = ObjectHelper.getException(NoSuchAlgorithmException.class, e);
        if (nsae != null) {
            canTest = false;
            String name = System.getProperty("os.name");
            String message = nsae.getMessage();
            log.warn("SunX509 is not avail on this platform [{}] Testing is skipped! Real cause: {}", name, message);
        } else {
            // some other error then throw it so the test can fail
            throw e;
        }
    }
}
Also used : ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ServerSession(org.apache.sshd.server.session.ServerSession) PublickeyAuthenticator(org.apache.sshd.server.PublickeyAuthenticator) Command(org.apache.sshd.server.Command) PublicKey(java.security.PublicKey) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) NamedFactory(org.apache.sshd.common.NamedFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 5 with Command

use of org.apache.sshd.server.Command in project felix by apache.

the class Ssh method start.

private void start() throws IOException {
    server = ServerBuilder.builder().build();
    server.setPort(port);
    server.setHost(ip);
    server.setShellFactory(new ShellFactoryImpl(processor));
    server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(new ShellCommandFactory(processor)).build());
    server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory.Builder().build()));
    server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    server.start();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ScpCommandFactory(org.apache.sshd.server.scp.ScpCommandFactory) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) Command(org.apache.sshd.server.Command)

Aggregations

Command (org.apache.sshd.server.Command)14 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)9 File (java.io.File)7 NamedFactory (org.apache.sshd.common.NamedFactory)6 SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)6 VirtualFileSystemFactory (org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory)4 ScpCommandFactory (org.apache.sshd.server.command.ScpCommandFactory)4 ServerSession (org.apache.sshd.server.session.ServerSession)4 PublicKey (java.security.PublicKey)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SshServer (org.apache.sshd.SshServer)2 FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)2 PasswordAuthenticator (org.apache.sshd.server.PasswordAuthenticator)2 PublickeyAuthenticator (org.apache.sshd.server.PublickeyAuthenticator)2 SshServer (org.apache.sshd.server.SshServer)2 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)2