com.jcraft.jsch.HostKey Java Examples

The following examples show how to use com.jcraft.jsch.HostKey. 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: GitConfig.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Map<String, String> toMap() {
  Map<String, String> map = new HashMap<>();
  if (isGitEnabled()) {
    map.put(KEY_PATH, getPath());
    map.put(KEY_REMOTE, getRemote());
    map.put(KEY_PRIVATE_KEY, getPrivateKeyBase64());
    map.put(KEY_HOSTS_COUNT, String.valueOf(hostKeys.length));
    for (int i = 0; i < hostKeys.length; i++) {
      HostKey hostKey = hostKeys[i];
      map.put(
          String.format("%s%d%s", KEY_HOSTS_PREFIX, i, KEY_HOSTS_HOST_SUFFIX), hostKey.getHost());
      map.put(
          String.format("%s%d%s", KEY_HOSTS_PREFIX, i, KEY_HOSTS_TYPE_SUFFIX), hostKey.getType());
      map.put(
          String.format("%s%d%s", KEY_HOSTS_PREFIX, i, KEY_HOSTS_KEY_SUFFIX), hostKey.getKey());
    }
  }
  return map;
}
 
Example #2
Source File: PropertyBasedSshSessionFactoryTest.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void hostKeyIsUsed() throws Exception {
	JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties();
	sshKey.setUri("[email protected]:someorg/somerepo.git");
	sshKey.setHostKey(HOST_KEY);
	sshKey.setPrivateKey(PRIVATE_KEY);
	setupSessionFactory(sshKey);

	this.factory.createSession(this.hc, null,
			SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null);
	ArgumentCaptor<HostKey> captor = ArgumentCaptor.forClass(HostKey.class);
	verify(this.hostKeyRepository).add(captor.capture(), isNull());
	HostKey hostKey = captor.getValue();
	assertThat(hostKey.getHost()).isEqualTo("gitlab.example.local");
	assertThat(hostKey.getKey()).isEqualTo(HOST_KEY);
}
 
Example #3
Source File: PropertyBasedSshSessionFactory.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Override
protected Session createSession(Host hc, String user, String host, int port, FS fs)
		throws JSchException {
	if (this.sshKeysByHostname.containsKey(host)) {
		JGitEnvironmentProperties sshUriProperties = this.sshKeysByHostname.get(host);
		this.jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null,
				null);
		if (sshUriProperties.getKnownHostsFile() != null) {
			this.jSch.setKnownHosts(sshUriProperties.getKnownHostsFile());
		}
		if (sshUriProperties.getHostKey() != null) {
			HostKey hostkey = new HostKey(host,
					Base64.decode(sshUriProperties.getHostKey()));
			this.jSch.getHostKeyRepository().add(hostkey, null);
		}
		return this.jSch.getSession(user, host, port);
	}
	throw new JSchException("no keys configured for hostname " + host);
}
 
Example #4
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 #5
Source File: SftpClient.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
@Override
public HostKey[] getHostKey() {

    List<HostKey[]> hostKeysList = new ArrayList<>();
    Iterator<String> it = knownHostsMap.keySet().iterator();
    int size = 0;
    while (it.hasNext()) {
        HostKey[] hostKeysEntry = getHostKey(it.next(), "public key");
        hostKeysList.add(hostKeysEntry);
        size += hostKeysEntry.length;
    }

    HostKey[] hostKeys = new HostKey[size];
    int i = 0;
    for (HostKey[] keys : hostKeysList) {
        for (HostKey key : keys) {
            hostKeys[i++] = key;
        }
    }

    return hostKeys;
}
 
Example #6
Source File: SftpClient.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
@Override
public HostKey[] getHostKey( String host, String type ) {

    host = host.replace("[", "").replace("]", "").split(":")[0];

    Set<byte[]> keys = knownHostsMap.get(host);

    HostKey[] hostKeys = new HostKey[keys.size()];
    Iterator<byte[]> it = keys.iterator();
    int i = 0;
    while (it.hasNext()) {
        try {
            hostKeys[i++] = new HostKey(host, HostKey.SSHRSA, it.next());
        } catch (JSchException e) {
            throw new RuntimeException("Unable to get hostkey for host '" + host + "'");
        }
    }
    return hostKeys;
}
 
Example #7
Source File: HostFingerprintException.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public HostFingerprintException(String host, byte[] key) {
	super("The authenticity of host " + host + " can't be established");
	try {
		this.hostkey = new HostKey(host, key);
	} catch (JSchException e) {
		// no hostkey
	}
}
 
Example #8
Source File: SshAgentSessionFactoryTest.java    From multi-module-maven-release-plugin with MIT License 5 votes vote down vote up
@Test
public void createDefaultJSch_WithKnownHosts() throws Exception {
	final SshAgentSessionFactory factory = new SshAgentSessionFactory(log, KNOWN_HOSTS, null, null);
	factory.setKnownHosts(getFile(KNOWN_HOSTS));
	final JSch jsch = factory.createDefaultJSch(fs);
	final HostKey[] keys = jsch.getHostKeyRepository().getHostKey("github.com", "ssh-rsa");
	assertEquals(1, keys.length);
}
 
Example #9
Source File: HostKeyTypeTest.java    From ssh-proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetType() throws Exception {
	assertEquals(HostKey.SSHRSA, HostKeyType.SSH_RSA.getType());
	assertEquals(HostKey.SSHDSS, HostKeyType.SSH_DSS.getType());

	for (HostKeyType hostKeyType : HostKeyType.values()) {
		assertTrue(hostKeyType.getType() > 0);
	}
}
 
Example #10
Source File: SftpClient.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void add( HostKey hostkey, UserInfo ui ) {

    Set<byte[]> keys = knownHostsMap.get(hostkey.getHost());
    if (keys == null) {
        keys = new HashSet<>();
    }
    keys.add(hostkey.getKey().getBytes());
    knownHostsMap.put(hostkey.getHost(), keys);

}
 
Example #11
Source File: ConfigTest.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Test
public void resolvedGitConfig() {
  MetaStoreConfig config = loadConfig("test_config.yaml");
  RegistryConfig registryConfig = config.getRegistryConfig("git");
  GitConfig gitConfig = registryConfig.getGitConfig();

  HostKey hostKey = gitConfig.getHostKeys().get(0);
  assertEquals("cHVibGljCg==", hostKey.getKey());
  assertEquals("[host.example.com]", hostKey.getHost());
  assertEquals("ecdsa-sha2-nistp256", hostKey.getType());
}
 
Example #12
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public void add(HostKey hostkey, UserInfo ui) {
    // does nothing
}
 
Example #13
Source File: SshConnectionImpl.java    From gerrit-events with MIT License 4 votes vote down vote up
@Override
public HostKey[] getHostKey(String host, String type) {
    return EMPTY;
}
 
Example #14
Source File: SshConnectionImpl.java    From gerrit-events with MIT License 4 votes vote down vote up
@Override
public HostKey[] getHostKey() {
    return EMPTY;
}
 
Example #15
Source File: SshConnectionImpl.java    From gerrit-events with MIT License 4 votes vote down vote up
@Override
public void add(HostKey hostkey, UserInfo ui) {
}
 
Example #16
Source File: GitConfig.java    From metastore with Apache License 2.0 4 votes vote down vote up
public List<HostKey> getHostKeys() {
  return Arrays.stream(hostKeys).collect(Collectors.toList());
}
 
Example #17
Source File: GitWithAuth.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@Override
public void add(HostKey hostkey, UserInfo ui) {}
 
Example #18
Source File: GitWithAuth.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey() {
    throw new UnsupportedOperationException();
}
 
Example #19
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey(String host, String type) {
    return null;
}
 
Example #20
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey() {
    return null;
}
 
Example #21
Source File: TrustAllHostKeyRepository.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey(String host, String type) {
    return NO_HOST_KEYS;
}
 
Example #22
Source File: TrustAllHostKeyRepository.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey() {
    return NO_HOST_KEYS;
}
 
Example #23
Source File: TrustAllHostKeyRepository.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
@Override
public void add(HostKey hostkey, UserInfo ui) {
    // skip
}
 
Example #24
Source File: GitConfig.java    From metastore with Apache License 2.0 4 votes vote down vote up
public static GitConfig fromConfigFile(GitProviderConfig config, GitGlobalConfig global) {
  GitConfig gitConfig = new GitConfig();
  if (config == null) {
    gitConfig.hasGit = false;
    return gitConfig;
  }
  gitConfig.hasGit = true;
  gitConfig.path = config.path;
  if (gitConfig.path == null) {
    throw new RuntimeException("git path needs to be set when git is enabled.");
  }
  gitConfig.remote = config.remote;
  if (gitConfig.remote == null) {
    throw new RuntimeException("git remote needs to be set when git is enabled.");
  }
  gitConfig.privateKeyBase64 = config.privateKey;
  if (gitConfig.privateKeyBase64 == null) {
    gitConfig.privateKeyBase64 = global != null ? global.privateKey : null;
  }
  if (gitConfig.privateKeyBase64 == null) {
    throw new RuntimeException("git private key needs to be set when git is enabled.");
  }

  if (global != null && global.hosts != null) {
    gitConfig.hostKeys = new HostKey[global.hosts.length];
    for (int i = 0; i < global.hosts.length; i++) {
      int keyType = 0;
      String keyTypeName = global.hosts[i].type;
      if (keyTypeName != null) {
        if (!keyTypeMap.containsKey(keyTypeName)) {
          throw new IllegalStateException("Unrecognized host key type " + keyTypeName);
        }
        keyType = keyTypeMap.get(keyTypeName);
      }
      try {
        gitConfig.hostKeys[i] =
            new HostKey(
                global.hosts[i].host,
                keyType,
                Base64.getDecoder().decode(global.hosts[i].key),
                null);
      } catch (JSchException e) {
        throw new IllegalArgumentException("Unable to create host key", e);
      }
    }
  }
  return gitConfig;
}
 
Example #25
Source File: LazyKnownHosts.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey(String host, String type) {
	return repo.getHostKey();
}
 
Example #26
Source File: LazyKnownHosts.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey() {
	return repo.getHostKey();
}
 
Example #27
Source File: LazyKnownHosts.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void add(HostKey hostkey, UserInfo ui) {
	repo.add(hostkey, ui);
}
 
Example #28
Source File: GitWithAuth.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@Override
public HostKey[] getHostKey(String host, String type) {
    // TODO(trustin): Store the hostkeys in the meta repository.
    return EMPTY_HOST_KEYS;
}
 
Example #29
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 #30
Source File: HostFingerprintException.java    From orion.server with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Contains a {@link HostKey} information about a host that is not added to known hosts.
 * 
 * @return
 */
public HostKey getHostkey() {
	return hostkey;
}