com.jcraft.jsch.HostKeyRepository Java Examples

The following examples show how to use com.jcraft.jsch.HostKeyRepository. 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: SSHPushWorker.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Lookup the host key type to use for a given hostname or ip address (in known_hosts).
 * @param host the hostname or ip address to lookup
 * @return the host key type or empty optional
 */
private Optional<String> hostKeyType(String host) {
    HostKeyRepository hostKeyRepo = ssh.getHostKeyRepository();
    HostKey[] hostKeys = hostKeyRepo.getHostKey();
    if (hostKeys != null) {
        if (log.isDebugEnabled()) {
            log.debug("Looking up host key in: " +
                    hostKeyRepo.getKnownHostsRepositoryID());
        }

        for (HostKey hostKey : hostKeys) {
            for (String hostString: hostKey.getHost().split(",")) {
                if (hostString.matches(host)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Host key type for " + hostString + ": " +
                                hostKey.getType());
                    }
                    return Optional.of(hostKey.getType());
                }
            }
        }
    }
    log.warn("Unknown host: " + host);
    return Optional.empty();
}
 
Example #2
Source File: SftpClient.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
@Override
public int check( String host, byte[] key ) {

    host = host.replace("[", "").replace("]", "").split(":")[0]; // get only the IP address of the server

    if (knownHostsMap.get(host) == null) {
        log.error("The presented trust store certificates could not match any of the server provided ones");
        return HostKeyRepository.NOT_INCLUDED;
    }
    Set<byte[]> keys = knownHostsMap.get(host);
    for (byte[] key1 : keys) {
        key1 = Base64.decodeBase64(key1); // we must decode the key from the client trust store first
        if (Arrays.equals(key, key1)) {
            log.info("Server certificate trusted.");
            return HostKeyRepository.OK;
        }
    }
    log.error("The presented trust store certificates could not match any of the server provided ones");
    return HostKeyRepository.NOT_INCLUDED;

}
 
Example #3
Source File: SessionHandler.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Connects this session and adds custom error handling.
 * 
 * @param tms
 * @throws JSchException
 */
public void connect(int tms) throws JSchException {
	try {
		if (!session.isConnected())
			session.connect(tms);
	} catch (JSchException e) {
		if (jSch.getHostKeyRepository() instanceof LazyKnownHosts) {
			LazyKnownHosts hostsRepo = (LazyKnownHosts) jSch.getHostKeyRepository();
			if (hostsRepo.getLastStatus() != HostKeyRepository.OK) {
				throw new HostFingerprintException(hostsRepo.getLastUnknownkedHost(), hostsRepo.getLastUnknownKey());
			}
		}
		throw e;
	}
}
 
Example #4
Source File: JschBuilderTest.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Test
public void testKnownHostsFile() throws Exception {

    final JschBuilder builder = new JschBuilder();
    builder.setKnownHostsFileName(knownHostsFile);
    builder.setPrivateKeyFileName(privateKeyFile);

    final JSch jsch = builder.build();
    final HostKeyRepository knownHosts = jsch.getHostKeyRepository();

    assertEquals(knownHostsFile,
                 knownHosts.getKnownHostsRepositoryID());
}
 
Example #5
Source File: JschBuilderTest.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Test
public void testKnownHostsFile() throws Exception {

    final JschBuilder builder = new JschBuilder();
    builder.setKnownHostsFileName(knownHostsFile);
    builder.setPrivateKeyFileName(privateKeyFile);

    final JSch jsch = builder.build();
    final HostKeyRepository knownHosts = jsch.getHostKeyRepository();

    assertEquals(knownHostsFile,
                 knownHosts.getKnownHostsRepositoryID());
}
 
Example #6
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitializeJSchFull() throws IOException, JSchException {
    SFTPEnvironment env = new SFTPEnvironment();
    initializeFully(env);

    JSch jsch = mock(JSch.class);
    env.initialize(jsch);

    verify(jsch).setIdentityRepository((IdentityRepository) env.get("identityRepository"));
    IdentityTest.assertIdentityFromFilesAdded(jsch);
    verify(jsch).setHostKeyRepository((HostKeyRepository) env.get("hostKeyRepository"));
    verify(jsch).setKnownHosts(((File) env.get("knownHosts")).getAbsolutePath());
    verifyNoMoreInteractions(jsch);
}
 
Example #7
Source File: TrustAllHostKeyRepository.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public int check(String host, byte[] key) {
    return HostKeyRepository.OK;
}
 
Example #8
Source File: SshConnectionImpl.java    From gerrit-events with MIT License 4 votes vote down vote up
@Override
public int check(String host, byte[] key) {
    return HostKeyRepository.OK;
}
 
Example #9
Source File: SftpClient.java    From ats-framework with Apache License 2.0 3 votes vote down vote up
private void addPublicKeyToHostKeyRepostitory( PublicKey key,
                                               HostKeyRepository hostKeyRepository ) throws Exception {

    if (!key.getAlgorithm().contains("RSA")) {
        throw new Exception("Only RSA keys are supported!.");
    }

    byte[] opensshKeyContent = convertToOpenSSHKeyFormat((RSAPublicKey) key);

    HostKey hostkey = new HostKey(hostname, HostKey.SSHRSA, opensshKeyContent);
    hostKeyRepository.add(hostkey, null);

}
 
Example #10
Source File: SFTPEnvironment.java    From sftp-fs with Apache License 2.0 2 votes vote down vote up
/**
 * Stores the host key repository to use.
 *
 * @param repository The host key repository to use.
 * @return This object.
 */
public SFTPEnvironment withHostKeyRepository(HostKeyRepository repository) {
    put(HOST_KEY_REPOSITORY, repository);
    return this;
}