Search in sources :

Example 1 with AgentImpl

use of org.apache.sshd.agent.local.AgentImpl 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 AgentImpl

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

Example 3 with AgentImpl

use of org.apache.sshd.agent.local.AgentImpl 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 4 with AgentImpl

use of org.apache.sshd.agent.local.AgentImpl 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

InputStream (java.io.InputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 KeyPair (java.security.KeyPair)4 SshAgent (org.apache.sshd.agent.SshAgent)4 AgentImpl (org.apache.sshd.agent.local.AgentImpl)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)3 FileInputStream (java.io.FileInputStream)1 URL (java.net.URL)1 NoCloseInputStream (org.apache.sshd.common.util.io.NoCloseInputStream)1