com.jcraft.jsch.agentproxy.RemoteIdentityRepository Java Examples

The following examples show how to use com.jcraft.jsch.agentproxy.RemoteIdentityRepository. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSchExecutor.java    From vividus with Apache License 2.0 11 votes vote down vote up
private JSch createJSchInstance(ServerConfiguration server) throws AgentProxyException, JSchException
{
    JSch jSch = new JSch();
    if (server.isAgentForwarding())
    {
        Connector connector = ConnectorFactory.getDefault().createConnector();
        jSch.setIdentityRepository(new RemoteIdentityRepository(connector));
    }
    else if (server.getPrivateKey() != null && server.getPublicKey() != null)
    {
        String passphrase = server.getPassphrase();
        jSch.addIdentity("default", getBytes(server.getPrivateKey()), getBytes(server.getPublicKey()),
                passphrase != null ? getBytes(passphrase) : null);
    }
    return jSch;
}
 
Example #2
Source File: JSchExecutorTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void shouldExecuteSuccessfullyWithAgentForwarding() throws Exception
{
    ServerConfiguration server = getDefaultServerConfiguration();
    server.setAgentForwarding(true);
    JSch jSch = mock(JSch.class);
    whenNew(JSch.class).withNoArguments().thenReturn(jSch);
    mockStatic(ConnectorFactory.class);
    ConnectorFactory connectorFactory = mock(ConnectorFactory.class);
    when(ConnectorFactory.getDefault()).thenReturn(connectorFactory);
    Connector connector = mock(Connector.class);
    when(connectorFactory.createConnector()).thenReturn(connector);
    RemoteIdentityRepository remoteIdentityRepository = mock(RemoteIdentityRepository.class);
    whenNew(RemoteIdentityRepository.class).withArguments(connector).thenReturn(remoteIdentityRepository);
    doNothing().when(jSch).setIdentityRepository(remoteIdentityRepository);
    Session session = mock(Session.class);
    when(jSch.getSession(server.getUsername(), server.getHost(), server.getPort())).thenReturn(session);
    ChannelExec channelExec = mockChannelOpening(session);
    SshOutput actual = new TestJSchExecutor().execute(server, COMMANDS);
    assertEquals(SSH_OUTPUT, actual);
    InOrder ordered = inOrder(jSch, session, channelExec);
    ordered.verify(jSch).setIdentityRepository(remoteIdentityRepository);
    verifyFullConnection(ordered, server, session, channelExec);
}
 
Example #3
Source File: AgentProxyAwareJschConfigSessionFactory.java    From gitflow-incremental-builder with MIT License 5 votes vote down vote up
private Collection<Identity> getIdentitiesFromAgentProxy() {

        Connector con = null;
        try {
            con = ConnectorFactory.getDefault().createConnector();
        } catch(AgentProxyException e) {
            logger.warn("AgentProxy setup failed, cannot read identities from agent", e);
        }
        return con != null ? new RemoteIdentityRepository(con).getIdentities() : new ArrayList<>();
    }
 
Example #4
Source File: SSHSessionProvider.java    From teamcity-deployer-plugin with Apache License 2.0 5 votes vote down vote up
private Session initSessionSshAgent(String username, String socketPath, JSch jsch) throws JSchException {
  final Session session = jsch.getSession(username, myHost, myPort);
  session.setConfig("PreferredAuthentications", "publickey");

  try {
    ConnectorFactory cf = ConnectorFactory.getDefault();
    cf.setUSocketPath(socketPath);
    Connector con = cf.createConnector();
    IdentityRepository irepo = new RemoteIdentityRepository(con);
    jsch.setIdentityRepository(irepo);
    return session;
  } catch (AgentProxyException e) {
    throw new JSchException("Failed to connect to ssh agent.", e);
  }
}
 
Example #5
Source File: SshCache.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to connect to a local SSH agent (using either UNIX sockets or PuTTY's Pageant)
 *
 * @param jsch
 *            Connection to be attached to an available local agent
 * @return true if connected to agent, false otherwise
 */
private boolean attemptAgentUse(JSch jsch) {
    try {
        Connector con = ConnectorFactory.getDefault().createConnector();
        jsch.setIdentityRepository(new RemoteIdentityRepository(con));
        return true;
    } catch (Exception e) {
        Message.verbose(":: SSH :: Failure connecting to agent :: " + e.toString());
        return false;
    }
}
 
Example #6
Source File: DefaultSessionFactory.java    From jsch-extension with MIT License 4 votes vote down vote up
private void setDefaultIdentities() throws JSchException {
    boolean identitiesSet = false;
    try {
        Connector connector = ConnectorFactory.getDefault()
                .createConnector();
        if ( connector != null ) {
            logger.info( "An AgentProxy Connector was found, check for identities" );
            RemoteIdentityRepository repository = new RemoteIdentityRepository( connector );
            Vector<Identity> identities = repository.getIdentities();
            if ( identities.size() > 0 ) {
                logger.info( "Using AgentProxy identities: {}", identities );
                setIdentityRepository( repository );
                identitiesSet = true;
            }
        }
    }
    catch ( AgentProxyException e ) {
        logger.debug( "Failed to load any keys from AgentProxy:", e );
    }
    if ( !identitiesSet ) {
        String privateKeyFilesString = System.getProperty( PROPERTY_JSCH_PRIVATE_KEY_FILES );
        if ( privateKeyFilesString != null && !privateKeyFilesString.isEmpty() ) {
            logger.info( "Using local identities from {}: {}",
                    PROPERTY_JSCH_PRIVATE_KEY_FILES, privateKeyFilesString );
            setIdentitiesFromPrivateKeys( Arrays.asList( privateKeyFilesString.split( "," ) ) );
            identitiesSet = true;
        }
    }
    if ( !identitiesSet ) {
        List<String> privateKeyFiles = new ArrayList<String>();
        for ( File file : new File[] {
                new File( dotSshDir(), "id_rsa" ),
                new File( dotSshDir(), "id_dsa" ),
                new File( dotSshDir(), "id_ecdsa" ) } ) {
            if ( file.exists() ) {
                privateKeyFiles.add( file.getAbsolutePath() );
            }
        }
        logger.info( "Using local identities: {}", privateKeyFiles );
        setIdentitiesFromPrivateKeys( privateKeyFiles );
    }
}