Search in sources :

Example 1 with SshAgent

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

the class Main method startAgent.

private static SshAgent startAgent(String user, URL privateKeyUrl, String keyFile, FilePasswordProvider passwordProvider) {
    InputStream is = null;
    try {
        SshAgent agent = new AgentImpl();
        is = privateKeyUrl.openStream();
        ObjectInputStream r = new ObjectInputStream(is);
        KeyPair keyPair = (KeyPair) r.readObject();
        is.close();
        agent.addIdentity(keyPair, user);
        if (keyFile != null) {
            FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(Paths.get(keyFile));
            fileKeyPairProvider.setPasswordFinder(passwordProvider);
            for (KeyPair key : fileKeyPairProvider.loadKeys()) {
                agent.addIdentity(key, user);
            }
        }
        return agent;
    } catch (Throwable e) {
        close(is);
        System.err.println("Error starting ssh agent for: " + e.getMessage());
        return null;
    }
}
Also used : KeyPair(java.security.KeyPair) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) NoCloseInputStream(org.apache.sshd.common.util.io.NoCloseInputStream) InputStream(java.io.InputStream) SshAgent(org.apache.sshd.agent.SshAgent) AgentImpl(org.apache.sshd.agent.local.AgentImpl) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with SshAgent

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

the class DeployMojo method setupAgent.

private void setupAgent(String user, File keyFile, SshClient client) {
    URL builtInPrivateKey = ClientMojo.class.getClassLoader().getResource("karaf.key");
    SshAgent agent = startAgent(user, builtInPrivateKey, keyFile);
    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) JMXServiceURL(javax.management.remote.JMXServiceURL)

Example 3 with SshAgent

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

the class ClientMojo method setupAgent.

private void setupAgent(String user, File keyFile, SshClient client) {
    URL builtInPrivateKey = ClientMojo.class.getClassLoader().getResource("karaf.key");
    SshAgent agent = startAgent(user, builtInPrivateKey, keyFile);
    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 4 with SshAgent

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

the class KarafAgentFactory method createClient.

public SshAgent createClient(FactoryManager manager) throws IOException {
    String proxyId = (String) manager.getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME);
    if (proxyId == null) {
        throw new IllegalStateException("No " + SshAgent.SSH_AUTHSOCKET_ENV_NAME + " environment variable set");
    }
    AgentServerProxy proxy = proxies.get(proxyId);
    if (proxy != null) {
        return proxy.createClient();
    }
    SshAgent agent = locals.get(proxyId);
    if (agent != null) {
        return new AgentDelegate(agent);
    }
    throw new IllegalStateException("No ssh agent found");
}
Also used : AgentDelegate(org.apache.sshd.agent.common.AgentDelegate) SshAgent(org.apache.sshd.agent.SshAgent) AgentServerProxy(org.apache.sshd.agent.local.AgentServerProxy)

Example 5 with SshAgent

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

the class KarafAgentFactory method registerSession.

public void registerSession(org.apache.karaf.shell.api.console.Session session) {
    try {
        String user = (String) session.get("USER");
        SshAgent agent = new AgentImpl();
        URL url = getClass().getClassLoader().getResource("karaf.key");
        InputStream is = url.openStream();
        ObjectInputStream r = new ObjectInputStream(is);
        KeyPair keyPair = (KeyPair) r.readObject();
        agent.addIdentity(keyPair, "karaf");
        String agentId = "local:" + user;
        session.put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, agentId);
        locals.put(agentId, agent);
    } catch (Throwable e) {
        LOGGER.warn("Error starting ssh agent for local console", e);
    }
}
Also used : KeyPair(java.security.KeyPair) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) SshAgent(org.apache.sshd.agent.SshAgent) AgentImpl(org.apache.sshd.agent.local.AgentImpl) URL(java.net.URL) ObjectInputStream(java.io.ObjectInputStream)

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