Search in sources :

Example 1 with SimpleGeneratorHostKeyProvider

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

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

Example 3 with SimpleGeneratorHostKeyProvider

use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project pentaho-kettle by pentaho.

the class SftpServer method createSshServer.

private SshServer createSshServer(int port, String homeDir, String hostKeyPath) {
    SshServer server = SshServer.setUpDefaultServer();
    server.setHost("localhost");
    server.setPort(port);
    server.setFileSystemFactory(new VirtualFileSystemFactory(homeDir));
    server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystem.Factory()));
    server.setCommandFactory(new ScpCommandFactory());
    server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKeyPath));
    server.setPasswordAuthenticator(this);
    return server;
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) Command(org.apache.sshd.server.Command) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) VirtualFileSystemFactory(org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) NamedFactory(org.apache.sshd.common.NamedFactory) SshServer(org.apache.sshd.SshServer)

Example 4 with SimpleGeneratorHostKeyProvider

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

the class InMemoryTestingDatabaseModule method getHostKeys.

private static synchronized KeyPairProvider getHostKeys() {
    if (keys == null) {
        keys = new SimpleGeneratorHostKeyProvider();
        keys.setAlgorithm("RSA");
        keys.loadKeys();
    }
    return keys;
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)

Example 5 with SimpleGeneratorHostKeyProvider

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

the class ConvertKey method main.

public static void main(String[] args) throws GeneralSecurityException, JSchException, IOException {
    SimpleGeneratorHostKeyProvider p;
    if (args.length != 1) {
        System.err.println("Error: requires path to the SSH host key");
        return;
    } else {
        File file = new File(args[0]);
        if (!file.exists() || !file.isFile() || !file.canRead()) {
            System.err.println("Error: ssh key should exist and be readable");
            return;
        }
    }
    p = new SimpleGeneratorHostKeyProvider();
    // Gerrit's SSH "simple" keys are always RSA.
    p.setPath(args[0]);
    p.setAlgorithm("RSA");
    // forces the key to generate.
    Iterable<KeyPair> keys = p.loadKeys();
    for (KeyPair k : keys) {
        System.out.println("Public Key (" + k.getPublic().getAlgorithm() + "):");
        // From Gerrit's SshDaemon class; use JSch to get the public
        // key/type
        final Buffer buf = new Buffer();
        buf.putRawPublicKey(k.getPublic());
        final byte[] keyBin = buf.getCompactData();
        HostKey pub = new HostKey("localhost", keyBin);
        System.out.println(pub.getType() + " " + pub.getKey());
        System.out.println("Private Key:");
        // Use Bouncy Castle to write the private key back in PEM format
        // (PKCS#1)
        // http://stackoverflow.com/questions/25129822/export-rsa-public-key-to-pem-string-using-java
        StringWriter privout = new StringWriter();
        JcaPEMWriter privWriter = new JcaPEMWriter(privout);
        privWriter.writeObject(k.getPrivate());
        privWriter.close();
        System.out.println(privout);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) Buffer(org.apache.sshd.common.util.Buffer) KeyPair(java.security.KeyPair) HostKey(com.jcraft.jsch.HostKey) StringWriter(java.io.StringWriter) JcaPEMWriter(org.bouncycastle.openssl.jcajce.JcaPEMWriter) File(java.io.File)

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