Search in sources :

Example 6 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project jPOS by jpos.

the class SshService method startService.

@Override
protected void startService() throws Exception {
    String username = cfg.get("auth-username", "admin");
    String authorizedKeysFilename = cfg.get("authorized-keys-file", "cfg/authorized_keys");
    String hostKeys = cfg.get("hostkeys-file", "cfg/hostkeys.ser");
    int port = cfg.getInt("port", 2222);
    checkAuthorizedKeys(authorizedKeysFilename);
    String[] prefixes = getPrefixes();
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(port);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(hostKeys)));
    CliShellFactory csf = new CliShellFactory(getServer(), prefixes);
    sshd.setShellFactory(csf);
    sshd.setCommandFactory(csf);
    sshd.setUserAuthFactories(Collections.singletonList(new UserAuthPublicKeyFactory()));
    sshd.setPublickeyAuthenticator(new AuthorizedKeysFileBasedPKA(username, authorizedKeysFilename));
    sshd.start();
    log.info("Started SSHD @ port " + port);
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) UserAuthPublicKeyFactory(org.apache.sshd.server.auth.pubkey.UserAuthPublicKeyFactory) File(java.io.File)

Example 7 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider 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 8 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.

the class SftpServerTests method testUcPw.

@Test
public void testUcPw() throws Exception {
    SshServer server = SshServer.setUpDefaultServer();
    try {
        server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
        server.setPort(0);
        server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
        server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
        final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
        new File(pathname).mkdirs();
        server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
        server.start();
        DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
        f.setHost("localhost");
        f.setPort(server.getPort());
        f.setUser("user");
        f.setPassword("pass");
        f.setAllowUnknownKeys(true);
        Session<LsEntry> session = f.getSession();
        doTest(server, session);
    } finally {
        server.stop(true);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) Command(org.apache.sshd.server.Command) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) SshServer(org.apache.sshd.server.SshServer) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Test(org.junit.Test)

Example 9 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.

the class SftpServerTests method testKeyExchange.

private void testKeyExchange(String pubKey, String privKey, String passphrase) throws Exception {
    SshServer server = SshServer.setUpDefaultServer();
    final PublicKey allowedKey = decodePublicKey(pubKey);
    try {
        server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey));
        server.setPort(0);
        server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
        server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
        final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
        new File(pathname).mkdirs();
        server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
        server.start();
        DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
        f.setHost("localhost");
        f.setPort(server.getPort());
        f.setUser("user");
        f.setAllowUnknownKeys(true);
        InputStream stream = new ClassPathResource(privKey).getInputStream();
        f.setPrivateKey(new ByteArrayResource(StreamUtils.copyToByteArray(stream)));
        f.setPrivateKeyPassphrase(passphrase);
        Session<LsEntry> session = f.getSession();
        doTest(server, session);
    } finally {
        server.stop(true);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) PublicKey(java.security.PublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) ByteArrayResource(org.springframework.core.io.ByteArrayResource) SshServer(org.apache.sshd.server.SshServer) ClassPathResource(org.springframework.core.io.ClassPathResource) SftpSubsystemFactory(org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory) Command(org.apache.sshd.server.Command) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 10 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project structr by structr.

the class SSHService method initialize.

@Override
public boolean initialize(final StructrServices services) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    logger.info("Setting up SSH server..");
    server = SshServer.setUpDefaultServer();
    logger.info("Initializing host key generator..");
    final SimpleGeneratorHostKeyProvider hostKeyProvider = new SimpleGeneratorHostKeyProvider(Paths.get("db/structr_hostkey"));
    hostKeyProvider.setAlgorithm(KeyUtils.RSA_ALGORITHM);
    logger.info("Configuring SSH server..");
    server.setKeyPairProvider(hostKeyProvider);
    server.setPort(Settings.SshPort.getValue());
    server.setPasswordAuthenticator(this);
    server.setPublickeyAuthenticator(this);
    server.setFileSystemFactory(this);
    server.setSubsystemFactories(getSubsystems());
    server.setShellFactory(this);
    server.setCommandFactory(this);
    logger.info("Starting SSH server on port {}", server.getPort());
    try {
        server.start();
        running = true;
        logger.info("Initialization complete.");
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.warn("Initialization failed.");
    }
    return running;
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) IOException(java.io.IOException)

Aggregations

SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)21 File (java.io.File)13 Command (org.apache.sshd.server.Command)9 SftpSubsystemFactory (org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory)7 VirtualFileSystemFactory (org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory)5 ArrayList (java.util.ArrayList)4 NamedFactory (org.apache.sshd.common.NamedFactory)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 SshServer (org.apache.sshd.server.SshServer)3 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)3 Before (org.junit.Before)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 PublicKey (java.security.PublicKey)2 CommandFactory (org.apache.sshd.server.CommandFactory)2 PasswordAuthenticator (org.apache.sshd.server.PasswordAuthenticator)2 UserAuthPassword (org.apache.sshd.server.auth.UserAuthPassword)2 ScpCommandFactory (org.apache.sshd.server.command.ScpCommandFactory)2 ServerSession (org.apache.sshd.server.session.ServerSession)2