com.jcraft.jsch.Identity Java Examples

The following examples show how to use com.jcraft.jsch.Identity. 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: SystemInfo.java    From app-runner with MIT License 6 votes vote down vote up
private static List<String> getPublicKeys() throws Exception {
    return new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
        }
        List<String> getPublicKeys() throws Exception {
            JSch jSch = createDefaultJSch(FS.DETECTED);
            List<String> keys = new ArrayList<>();
            for (Object o : jSch.getIdentityRepository().getIdentities()) {
                Identity i = (Identity) o;
                KeyPair keyPair = KeyPair.load(jSch, i.getName(), null);
                StringBuilder sb = new StringBuilder();
                try (StringBuilderWriter sbw = new StringBuilderWriter(sb);
                     OutputStream os = new WriterOutputStream(sbw, "UTF-8")) {
                    keyPair.writePublicKey(os, keyPair.getPublicKeyComment());
                } finally {
                    keyPair.dispose();
                }
                keys.add(sb.toString().trim());
            }
            return keys;
        }
    }.getPublicKeys();
}
 
Example #2
Source File: AgentProxyAwareJschConfigSessionFactory.java    From gitflow-incremental-builder with MIT License 6 votes vote down vote up
@Override
protected Session createSession(Host hc, String user, String host, int port, FS fs)
        throws JSchException {
    JSch jSch = getJSch(hc, fs);

    // assumption: identities from agent are always unencrypted
    final Collection<Identity> allUnencryptedIdentities = getIdentitiesFromAgentProxy();

    @SuppressWarnings("unchecked")
    Collection<Identity> identities = ((Collection<Identity>) jSch.getIdentityRepository().getIdentities());
    identities.stream()
            .filter(id -> !id.isEncrypted())
            .forEach(allUnencryptedIdentities::add);
    
    Session session = jSch.getSession(user, host, port);
    session.setIdentityRepository(new ReadOnlyIdentityRepository(allUnencryptedIdentities));
    return session;
}
 
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: JschKey.java    From httpsig-java with The Unlicense 5 votes vote down vote up
public static Keychain getIdentities(JSch jSch) {
    ArrayList<JschKey> identities = new ArrayList<JschKey>();
    Vector _identities = jSch.getIdentityRepository().getIdentities();
    if (_identities != null) {
        for (Object obj : _identities) {
            identities.add(new JschKey((Identity) obj));
        }
    }

    return new DefaultKeychain(Collections.unmodifiableList(identities));
}
 
Example #5
Source File: GitWithAuth.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@Override
public Vector<Identity> getIdentities() {
    final Vector<Identity> identities = new Vector<>();
    identities.add(identity);
    return identities;
}
 
Example #6
Source File: AgentProxyAwareJschConfigSessionFactory.java    From gitflow-incremental-builder with MIT License 4 votes vote down vote up
private ReadOnlyIdentityRepository(Collection<Identity> allUnencryptedIdentities) {
    this.allUnencryptedIdentities = allUnencryptedIdentities;
}
 
Example #7
Source File: AgentProxyAwareJschConfigSessionFactory.java    From gitflow-incremental-builder with MIT License 4 votes vote down vote up
@Override
public Vector<Identity> getIdentities() {
    return new Vector<>(allUnencryptedIdentities);
}
 
Example #8
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 );
    }
}
 
Example #9
Source File: JschKey.java    From httpsig-java with The Unlicense 4 votes vote down vote up
public JschKey(Identity identity) {
    this.fingerprint = Magic.getFingerprint(identity.getPublicKeyBlob());
    this.identity = identity;
    this.algorithm = Algorithm.forName(identity.getAlgName());
}