/** * Updates the values that are <U>not</U> already configured with those * from the global entry * * @param globalEntry The global entry - ignored if {@code null} or * same reference as this entry * @return {@code true} if anything updated */ public boolean processGlobalValues(HostConfigEntry globalEntry) { if ((globalEntry == null) || (this == globalEntry)) { return false; } boolean modified = false; /* * NOTE !!! DO NOT TRY TO CHANGE THE ORDER OF THE OR-ing AS IT * WOULD CAUSE INVALID CODE EXECUTION */ modified = updateGlobalPort(globalEntry.getPort()) || modified; modified = updateGlobalHostName(globalEntry.getHostName()) || modified; modified = updateGlobalUserName(globalEntry.getUsername()) || modified; modified = updateGlobalIdentities(globalEntry.getIdentities()) || modified; modified = updateGlobalIdentityOnly(globalEntry.isIdentitiesOnly()) || modified; Map<String, String> updated = updateGlobalProperties(globalEntry.getProperties()); modified = (GenericUtils.size(updated) > 0) || modified; return modified; }
/** * @param name Property name - never {@code null}/empty * @return Property value or {@code null} if no such property * @see #getProperty(String, String) */ public String getProperty(String name) { return getProperty(name, null); }
/** * @param path A {@link Path} to a file that contains an identity key * - never {@code null} */ public void addIdentity(Path path) { addIdentity(Objects.requireNonNull(path, "No path").toAbsolutePath().normalize().toString()); }
/** * Resolves the effective username * * @param originalUser The original requested username * @return If the configured host entry username is not {@code null}/empty * then it is used, otherwise the original one. * @see #resolveUsername(String) */ public String resolveUsername(String originalUser) { return resolveUsername(originalUser, getUsername()); }
/** * @param portValue The global port value - ignored if not positive * or already have a configured port * @return {@code true} if updated the port value */ public boolean updateGlobalPort(int portValue) { if ((portValue <= 0) || (getPort() > 0)) { return false; } setPort(portValue); return true; }
/** * Resolves the effective port to use * * @param originalPort The original requested port * @return If the host entry port is positive, then it is used, otherwise * the original requested port * @see #resolvePort(int, int) */ public int resolvePort(int originalPort) { return resolvePort(originalPort, getPort()); }
HostConfigEntry normal = new HostConfigEntry(); normal.setHost(host); normal.setHostName(entry.resolveHostName(host)); normal.setPort(entry.resolvePort(port)); normal.setUsername(entry.resolveUsername(username)); Map<String, String> props = entry.getProperties(); if (GenericUtils.size(props) > 0) { normal.setProperties(new TreeMap<>(props)); Collection<String> ids = entry.getIdentities(); if (GenericUtils.isEmpty(ids)) { return normal; normal.setIdentities(Collections.emptyList()); // start fresh for (String id : ids) { String path = resolveIdentityFilePath(id, host, port, username); normal.addIdentity(path);
public <A extends Appendable> A append(A sb) throws IOException { sb.append(HOST_CONFIG_PROP).append(' ').append(ValidateUtils.checkNotNullAndNotEmpty(getHost(), "No host pattern")).append(IoUtils.EOL); appendNonEmptyProperty(sb, HOST_NAME_CONFIG_PROP, getHostName()); appendNonEmptyPort(sb, PORT_CONFIG_PROP, getPort()); appendNonEmptyProperty(sb, USER_CONFIG_PROP, getUsername()); appendNonEmptyValues(sb, IDENTITY_FILE_CONFIG_PROP, getIdentities()); if (exclusiveIdentites != null) { appendNonEmptyProperty(sb, EXCLUSIVE_IDENTITIES_CONFIG_PROP, ConfigFileReaderSupport.yesNoValueOf(exclusiveIdentites)); } appendNonEmptyProperties(sb, getProperties()); return sb; }
String key = ValidateUtils.checkNotNullAndNotEmpty(name, "No property name"); String joinedValue = GenericUtils.join(valsList, ','); appendPropertyValue(key, joinedValue); String curValue = getHostName(); ValidateUtils.checkTrue(GenericUtils.isEmpty(curValue) || ignoreAlreadyInitialized, "Already initialized %s: %s", key, curValue); setHostName(joinedValue); } else if (PORT_CONFIG_PROP.equalsIgnoreCase(key)) { ValidateUtils.checkTrue(GenericUtils.size(valsList) == 1, "Multiple target ports N/A: %s", joinedValue); int curValue = getPort(); ValidateUtils.checkTrue((curValue <= 0) || ignoreAlreadyInitialized, "Already initialized %s: %d", key, curValue); setPort(newValue); } else if (USER_CONFIG_PROP.equalsIgnoreCase(key)) { ValidateUtils.checkTrue(GenericUtils.size(valsList) == 1, "Multiple target users N/A: %s", joinedValue); String curValue = getUsername(); ValidateUtils.checkTrue(GenericUtils.isEmpty(curValue) || ignoreAlreadyInitialized, "Already initialized %s: %s", key, curValue); setUsername(joinedValue); } else if (IDENTITY_FILE_CONFIG_PROP.equalsIgnoreCase(key)) { ValidateUtils.checkTrue(GenericUtils.size(valsList) > 0, "No identity files specified"); for (String id : valsList) { addIdentity(id); setIdentitiesOnly( ConfigFileReaderSupport.parseBooleanValue( ValidateUtils.checkNotNullAndNotEmpty(joinedValue, "No identities option value")));
@Override public ConnectFuture connect(HostConfigEntry hostConfig, SocketAddress localAddress) throws IOException { Objects.requireNonNull(hostConfig, "No host configuration"); String host = ValidateUtils.checkNotNullAndNotEmpty(hostConfig.getHostName(), "No target host"); int port = hostConfig.getPort(); ValidateUtils.checkTrue(port > 0, "Invalid port: %d", port); Collection<KeyPair> keys = loadClientIdentities(hostConfig.getIdentities(), IoUtils.EMPTY_LINK_OPTIONS); return doConnect(hostConfig.getUsername(), new InetSocketAddress(host, port), localAddress, keys, !hostConfig.isIdentitiesOnly()); }
@Override public String toString() { return getHost() + ": " + getUsername() + "@" + getHostName() + ":" + getPort(); }
hostConfig.getHostName(), "No target host"); //$NON-NLS-1$ int port = hostConfig.getPort(); ValidateUtils.checkTrue(port > 0, "Invalid port: %d", port); //$NON-NLS-1$ String userName = hostConfig.getUsername(); InetSocketAddress address = new InetSocketAddress(host, port); ConnectFuture connectFuture = new DefaultConnectFuture( hostConfig.getProperty(SshConstants.PREFERRED_AUTHENTICATIONS, getAttribute(PREFERRED_AUTHENTICATIONS)), PREFERRED_AUTHS);
List<String> valsList = parseConfigValue(value); curEntry.processGlobalValues(globalEntry); entries = updateEntriesList(entries, curEntry); curEntry = new HostConfigEntry(); curEntry.setHost(valsList); } else if (curEntry == null) { curEntry = new HostConfigEntry(); curEntry.setHost(Collections.singletonList(ALL_HOSTS_PATTERN)); globalEntry = curEntry; curEntry.processProperty(key, valsList, false); } catch (RuntimeException e) { throw new StreamCorruptedException("Failed (" + e.getClass().getSimpleName() + ")" curEntry.processGlobalValues(globalEntry); entries = updateEntriesList(entries, curEntry); if (entries == null) { return Collections.emptyList();
/** * Appends a value using a <U>comma</U> to an existing one. If no previous * value then same as calling {@link #setProperty(String, String)}. * * @param name Property name - never {@code null}/empty * @param value The value to be appended - ignored if {@code null}/empty * @return The value <U>before</U> appending - {@code null} if no previous value */ public String appendPropertyValue(String name, String value) { String key = ValidateUtils.checkNotNullAndNotEmpty(name, "No property name"); String curVal = getProperty(key); if (GenericUtils.isEmpty(value)) { return curVal; } if (GenericUtils.isEmpty(curVal)) { return setProperty(key, value); } return setProperty(key, curVal + ',' + value); }
List<Path> identities = hostConfig.getIdentities().stream() .map(s -> { try { keyCache); ourConfiguredKeysProvider.setPasswordFinder(getFilePasswordProvider()); if (hostConfig.isIdentitiesOnly()) { session.setKeyPairProvider(ourConfiguredKeysProvider); } else {
/** * @param name The global host name - ignored if {@code null}/empty or * already have a configured target host * @return {@code true} if updated the target host */ public boolean updateGlobalHostName(String name) { if (GenericUtils.isEmpty(name) || (GenericUtils.length(getHostName()) > 0)) { return false; } setHostName(name); return true; }
/** * @param user The global user name - ignored if {@code null}/empty or * already have a configured user * @return {@code true} if updated the username */ public boolean updateGlobalUserName(String user) { if (GenericUtils.isEmpty(user) || (GenericUtils.length(getUsername()) > 0)) { return false; } setUsername(user); return true; }
/** * @param ids Global identities - ignored if {@code null}/empty or already * have configured identities * @return {@code true} if updated identities */ public boolean updateGlobalIdentities(Collection<String> ids) { if (GenericUtils.isEmpty(ids) || (GenericUtils.size(getIdentities()) > 0)) { return false; } for (String id : ids) { addIdentity(id); } return true; }
public String resolveHostName(String originalHost) { return resolveHostName(originalHost, getHostName()); }
private String getHostName(ClientSession session) { SocketAddress remote = session.getConnectAddress(); if (remote instanceof InetSocketAddress) { InetAddress address = GssApiMechanisms .resolve((InetSocketAddress) remote); if (address != null) { return address.getCanonicalHostName(); } } if (session instanceof JGitClientSession) { String hostName = ((JGitClientSession) session).getHostConfigEntry() .getHostName(); try { hostName = InetAddress.getByName(hostName) .getCanonicalHostName(); } catch (UnknownHostException e) { // Ignore here; try with the non-canonical name } return hostName; } throw new IllegalStateException( "Wrong session class :" + session.getClass().getName()); //$NON-NLS-1$ }