Search in sources :

Example 6 with SshAgent

use of org.apache.sshd.agent.SshAgent in project karaf by apache.

the class Main method setupAgent.

private static void setupAgent(String user, String keyFile, SshClient client, FilePasswordProvider passwordProvider) {
    SshAgent agent;
    URL builtInPrivateKey = Main.class.getClassLoader().getResource("karaf.key");
    agent = startAgent(user, builtInPrivateKey, keyFile, passwordProvider);
    client.setAgentFactory(new LocalAgentFactory(agent));
    client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local");
}
Also used : LocalAgentFactory(org.apache.sshd.agent.local.LocalAgentFactory) SshAgent(org.apache.sshd.agent.SshAgent) URL(java.net.URL)

Example 7 with SshAgent

use of org.apache.sshd.agent.SshAgent 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 8 with SshAgent

use of org.apache.sshd.agent.SshAgent 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)

Example 9 with SshAgent

use of org.apache.sshd.agent.SshAgent in project platformlayer by platformlayer.

the class MinaSshConnection method buildAgentConnection.

@Override
public SshConnection buildAgentConnection(KeyPair agentKeyPair) throws IOException, SshException {
    LocalAgentFactory agentFactory = new LocalAgentFactory();
    SshAgent agent = agentFactory.getAgent();
    try {
        agent.addIdentity(agentKeyPair, "default");
    } catch (IOException e) {
        throw new IllegalArgumentException("Error adding agent identity", e);
    }
    MinaSshConnection agentConnection = new MinaSshConnection(new MinaSshContext(agentFactory));
    agentConnection.setHost(this.getHost());
    agentConnection.setPort(this.getPort());
    agentConnection.setUser(this.getUser());
    agentConnection.setServerKeyVerifier(this.getServerKeyVerifier());
    agentConnection.setKeyPair(this.getKeyPair());
    agentConnection.ensureConnected();
    return agentConnection;
}
Also used : LocalAgentFactory(org.apache.sshd.agent.local.LocalAgentFactory) SshAgent(org.apache.sshd.agent.SshAgent) IOException(java.io.IOException)

Aggregations

SshAgent (org.apache.sshd.agent.SshAgent)9 InputStream (java.io.InputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 URL (java.net.URL)4 KeyPair (java.security.KeyPair)4 AgentImpl (org.apache.sshd.agent.local.AgentImpl)4 LocalAgentFactory (org.apache.sshd.agent.local.LocalAgentFactory)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)3 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1 AgentDelegate (org.apache.sshd.agent.common.AgentDelegate)1 AgentServerProxy (org.apache.sshd.agent.local.AgentServerProxy)1 NoCloseInputStream (org.apache.sshd.common.util.io.NoCloseInputStream)1