Search in sources :

Example 1 with Command

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

the class DispatchCommand method start.

@Override
public void start(ChannelSession channel, Environment env) throws IOException {
    try (DynamicOptions pluginOptions = new DynamicOptions(injector, dynamicBeans)) {
        parseCommandLine(pluginOptions);
        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);
        String actualCommandName = commandName;
        if (cmd instanceof BaseCommand) {
            final BaseCommand bc = (BaseCommand) cmd;
            if (!getName().isEmpty()) {
                actualCommandName = getName() + " " + commandName;
            }
            bc.setName(actualCommandName);
            bc.setArguments(args.toArray(new String[args.size()]));
        } else if (!args.isEmpty()) {
            throw die(commandName + " does not take arguments");
        }
        for (SshExecuteCommandInterceptor commandInterceptor : commandInterceptors) {
            if (!commandInterceptor.accept(actualCommandName, args)) {
                throw new UnloggedFailure(126, String.format("blocked by %s, contact gerrit administrators for more details", commandInterceptor.name()));
            }
        }
        provideStateTo(cmd);
        atomicCmd.set(cmd);
        cmd.start(channel, 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 : DynamicOptions(com.google.gerrit.server.DynamicOptions) StringWriter(java.io.StringWriter) Command(org.apache.sshd.server.command.Command)

Example 2 with Command

use of org.apache.sshd.server.command.Command in project testcases by coheigea.

the class SSHTest method setup.

@Before
public void setup() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(10000);
    // Write port number to configuration file in target
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    // Read in camel configuration file and substitute in the correct port
    File f = new File(basedir + "/src/test/resources/camel-ssh.xml");
    FileInputStream inputStream = new FileInputStream(f);
    String content = IOUtils.toString(inputStream, "UTF-8");
    inputStream.close();
    content = content.replaceAll("portno", "" + port);
    File f2 = new File(basedir + "/target/test-classes/camel-ssh.xml");
    FileOutputStream outputStream = new FileOutputStream(f2);
    IOUtils.write(content, outputStream, "UTF-8");
    outputStream.close();
    sshServer = SshServer.setUpDefaultServer();
    sshServer.setPort(port);
    // Generate a key
    sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("target/generatedkey.pem")));
    // Simple CommandFactory to run commands in a process
    sshServer.setCommandFactory(new CommandFactory() {

        public Command createCommand(String command) {
            return new ProcessShellFactory(command.split(";")).create();
        }
    });
    // sshServer.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" }));
    sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
    sshServer.setPublickeyAuthenticator(new CamelPublicKeyAuthenticator());
    sshServer.start();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) Command(org.apache.sshd.server.command.Command) FileOutputStream(java.io.FileOutputStream) ProcessShellFactory(org.apache.sshd.server.shell.ProcessShellFactory) CommandFactory(org.apache.sshd.server.command.CommandFactory) File(java.io.File) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 3 with Command

use of org.apache.sshd.server.command.Command in project testcases by coheigea.

the class SFTPTest method setup.

@Before
public void setup() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(10000);
    // Write port number to configuration file in target
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    // Read in camel configuration file and substitute in the correct port
    File f = new File(basedir + "/src/test/resources/camel-sftp.xml");
    FileInputStream inputStream = new FileInputStream(f);
    String content = IOUtils.toString(inputStream, "UTF-8");
    inputStream.close();
    content = content.replaceAll("portno", "" + port);
    File f2 = new File(basedir + "/target/test-classes/camel-sftp.xml");
    FileOutputStream outputStream = new FileOutputStream(f2);
    IOUtils.write(content, outputStream, "UTF-8");
    outputStream.close();
    sshServer = SshServer.setUpDefaultServer();
    sshServer.setPort(port);
    // Generate a key
    sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("target/generatedkey.pem")));
    List<NamedFactory<Command>> subsystemFactories = new ArrayList<NamedFactory<Command>>(1);
    subsystemFactories.add(new SftpSubsystemFactory());
    sshServer.setSubsystemFactories(subsystemFactories);
    sshServer.setCommandFactory(new ScpCommandFactory());
    sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
    sshServer.start();
    // Create "storage_sftp" directory (delete it first if it exists)
    File storageDirectory = new File(basedir + "/target/storage_sftp");
    if (storageDirectory.exists()) {
        storageDirectory.delete();
    }
    storageDirectory.mkdir();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) ScpCommandFactory(org.apache.sshd.server.scp.ScpCommandFactory) Command(org.apache.sshd.server.command.Command) FileOutputStream(java.io.FileOutputStream) NamedFactory(org.apache.sshd.common.NamedFactory) File(java.io.File) Before(org.junit.Before)

Example 4 with Command

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

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

the class AliasCommand method begin.

private void begin(ChannelSession channel, 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(channel, env);
}
Also used : Command(org.apache.sshd.server.command.Command)

Aggregations

Command (org.apache.sshd.server.command.Command)5 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)2 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)2 SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)2 Before (org.junit.Before)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 DynamicOptions (com.google.gerrit.server.DynamicOptions)1 StringWriter (java.io.StringWriter)1 KeyPair (java.security.KeyPair)1 PublicKey (java.security.PublicKey)1 ArrayList (java.util.ArrayList)1 NamedFactory (org.apache.sshd.common.NamedFactory)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