Search in sources :

Example 6 with FileKeyPairProvider

use of org.apache.sshd.common.keyprovider.FileKeyPairProvider in project camel by apache.

the class SshComponentTestSupport method setUp.

@Override
public void setUp() throws Exception {
    port = AvailablePortFinder.getNextAvailable(22000);
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(port);
    sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
    sshd.setCommandFactory(new TestEchoCommandFactory());
    sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
    sshd.setPublickeyAuthenticator(new BogusPublickeyAuthenticator());
    sshd.start();
    super.setUp();
}
Also used : FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider)

Example 7 with FileKeyPairProvider

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

Example 8 with FileKeyPairProvider

use of org.apache.sshd.common.keyprovider.FileKeyPairProvider in project gerrit by GerritCodeReview.

the class HostKeyProvider method get.

@Override
public KeyPairProvider get() {
    Path objKey = site.ssh_key;
    Path rsaKey = site.ssh_rsa;
    Path dsaKey = site.ssh_dsa;
    Path ecdsaKey_256 = site.ssh_ecdsa_256;
    Path ecdsaKey_384 = site.ssh_ecdsa_384;
    Path ecdsaKey_521 = site.ssh_ecdsa_521;
    Path ed25519Key = site.ssh_ed25519;
    final List<File> stdKeys = new ArrayList<>(6);
    if (Files.exists(rsaKey)) {
        stdKeys.add(rsaKey.toAbsolutePath().toFile());
    }
    if (Files.exists(dsaKey)) {
        stdKeys.add(dsaKey.toAbsolutePath().toFile());
    }
    if (Files.exists(ecdsaKey_256)) {
        stdKeys.add(ecdsaKey_256.toAbsolutePath().toFile());
    }
    if (Files.exists(ecdsaKey_384)) {
        stdKeys.add(ecdsaKey_384.toAbsolutePath().toFile());
    }
    if (Files.exists(ecdsaKey_521)) {
        stdKeys.add(ecdsaKey_521.toAbsolutePath().toFile());
    }
    if (Files.exists(ed25519Key)) {
        stdKeys.add(ed25519Key.toAbsolutePath().toFile());
    }
    if (Files.exists(objKey)) {
        if (stdKeys.isEmpty()) {
            SimpleGeneratorHostKeyProvider p = new SimpleGeneratorHostKeyProvider();
            p.setPath(objKey.toAbsolutePath());
            return p;
        }
        // Both formats of host key exist, we don't know which format
        // should be authoritative. Complain and abort.
        //
        stdKeys.add(objKey.toAbsolutePath().toFile());
        throw new ProvisionException("Multiple host keys exist: " + stdKeys);
    }
    if (stdKeys.isEmpty()) {
        throw new ProvisionException("No SSH keys under " + site.etc_dir);
    }
    FileKeyPairProvider kp = new FileKeyPairProvider();
    kp.setFiles(stdKeys);
    return kp;
}
Also used : Path(java.nio.file.Path) SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) ProvisionException(com.google.inject.ProvisionException) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ArrayList(java.util.ArrayList) File(java.io.File)

Example 9 with FileKeyPairProvider

use of org.apache.sshd.common.keyprovider.FileKeyPairProvider in project karaf by apache.

the class ClientMojo method startAgent.

private SshAgent startAgent(String user, URL privateKeyUrl, File keyFile) {
    try (InputStream is = privateKeyUrl.openStream()) {
        SshAgent agent = new AgentImpl();
        ObjectInputStream r = new ObjectInputStream(is);
        KeyPair keyPair = (KeyPair) r.readObject();
        is.close();
        agent.addIdentity(keyPair, user);
        if (keyFile != null) {
            FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(keyFile.getAbsoluteFile().toPath());
            for (KeyPair key : fileKeyPairProvider.loadKeys()) {
                agent.addIdentity(key, user);
            }
        }
        return agent;
    } catch (Throwable e) {
        getLog().error("Error starting ssh agent for: " + e.getMessage(), e);
        return null;
    }
}
Also used : KeyPair(java.security.KeyPair) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SshAgent(org.apache.sshd.agent.SshAgent) AgentImpl(org.apache.sshd.agent.local.AgentImpl) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with FileKeyPairProvider

use of org.apache.sshd.common.keyprovider.FileKeyPairProvider in project karaf by apache.

the class DeployMojo method startAgent.

private SshAgent startAgent(String user, URL privateKeyUrl, File keyFile) {
    try (InputStream is = privateKeyUrl.openStream()) {
        SshAgent agent = new AgentImpl();
        ObjectInputStream r = new ObjectInputStream(is);
        KeyPair keyPair = (KeyPair) r.readObject();
        is.close();
        agent.addIdentity(keyPair, user);
        if (keyFile != null) {
            FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(keyFile.getAbsoluteFile().toPath());
            for (KeyPair key : fileKeyPairProvider.loadKeys()) {
                agent.addIdentity(key, user);
            }
        }
        return agent;
    } catch (Throwable e) {
        getLog().error("Error starting ssh agent for: " + e.getMessage(), e);
        return null;
    }
}
Also used : KeyPair(java.security.KeyPair) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SshAgent(org.apache.sshd.agent.SshAgent) AgentImpl(org.apache.sshd.agent.local.AgentImpl) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)10 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 KeyPair (java.security.KeyPair)3 SshAgent (org.apache.sshd.agent.SshAgent)3 AgentImpl (org.apache.sshd.agent.local.AgentImpl)3 NoCloseInputStream (org.apache.sshd.common.util.io.NoCloseInputStream)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 PublicKey (java.security.PublicKey)2 HashMap (java.util.HashMap)2 SshClient (org.apache.sshd.client.SshClient)2 UserInteraction (org.apache.sshd.client.auth.keyboard.UserInteraction)2 ChannelShell (org.apache.sshd.client.channel.ChannelShell)2 ClientChannel (org.apache.sshd.client.channel.ClientChannel)2 ClientSession (org.apache.sshd.client.session.ClientSession)2 NamedFactory (org.apache.sshd.common.NamedFactory)2 Command (org.apache.sshd.server.Command)2