You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JGitSshConfig.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.transport.sshd;
  44. import static org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.flag;
  45. import static org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.positive;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.net.SocketAddress;
  49. import java.util.Map;
  50. import java.util.TreeMap;
  51. import org.apache.sshd.client.config.hosts.HostConfigEntry;
  52. import org.apache.sshd.client.config.hosts.HostConfigEntryResolver;
  53. import org.apache.sshd.common.AttributeRepository;
  54. import org.apache.sshd.common.util.net.SshdSocketAddress;
  55. import org.eclipse.jgit.annotations.NonNull;
  56. import org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile;
  57. import org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.HostEntry;
  58. import org.eclipse.jgit.transport.SshConstants;
  59. /**
  60. * A {@link HostConfigEntryResolver} adapted specifically for JGit.
  61. * <p>
  62. * We use our own config file parser and entry resolution since the default
  63. * {@link org.apache.sshd.client.config.hosts.ConfigFileHostEntryResolver
  64. * ConfigFileHostEntryResolver} has a number of problems:
  65. * </p>
  66. * <ul>
  67. * <li>It does case-insensitive pattern matching. Matching in OpenSsh is
  68. * case-sensitive! Compare also bug 531118.</li>
  69. * <li>It only merges values from the global items (before the first "Host"
  70. * line) into the host entries. Otherwise it selects the most specific match.
  71. * OpenSsh processes <em>all</em> entries in the order they appear in the file
  72. * and whenever one matches, it updates values as appropriate.</li>
  73. * <li>We have to ensure that ~ replacement uses the same HOME directory as
  74. * JGit. Compare bug bug 526175.</li>
  75. * </ul>
  76. * Therefore, this re-uses the parsing and caching from
  77. * {@link OpenSshConfigFile}.
  78. *
  79. */
  80. public class JGitSshConfig implements HostConfigEntryResolver {
  81. private final OpenSshConfigFile configFile;
  82. private final String localUserName;
  83. /**
  84. * Creates a new {@link OpenSshConfigFile} that will read the config from
  85. * file {@code config} use the given file {@code home} as "home" directory.
  86. *
  87. * @param home
  88. * user's home directory for the purpose of ~ replacement
  89. * @param config
  90. * file to load; may be {@code null} if no ssh config file
  91. * handling is desired
  92. * @param localUserName
  93. * user name of the current user on the local host OS
  94. */
  95. public JGitSshConfig(@NonNull File home, File config,
  96. @NonNull String localUserName) {
  97. this.localUserName = localUserName;
  98. configFile = config == null ? null : new OpenSshConfigFile(home, config, localUserName);
  99. }
  100. @Override
  101. public HostConfigEntry resolveEffectiveHost(String host, int port,
  102. SocketAddress localAddress, String username,
  103. AttributeRepository attributes) throws IOException {
  104. HostEntry entry = configFile == null ? new HostEntry() : configFile.lookup(host, port, username);
  105. JGitHostConfigEntry config = new JGitHostConfigEntry();
  106. // Apache MINA conflates all keys, even multi-valued ones, in one map
  107. // and puts multiple values separated by commas in one string. See
  108. // the javadoc on HostConfigEntry.
  109. Map<String, String> allOptions = new TreeMap<>(
  110. String.CASE_INSENSITIVE_ORDER);
  111. allOptions.putAll(entry.getOptions());
  112. // And what if a value contains a comma??
  113. entry.getMultiValuedOptions().entrySet().stream()
  114. .forEach(e -> allOptions.put(e.getKey(),
  115. String.join(",", e.getValue()))); //$NON-NLS-1$
  116. config.setProperties(allOptions);
  117. // The following is an extension from JGitHostConfigEntry
  118. config.setMultiValuedOptions(entry.getMultiValuedOptions());
  119. // Also make sure the underlying properties are set
  120. String hostName = entry.getValue(SshConstants.HOST_NAME);
  121. if (hostName == null || hostName.isEmpty()) {
  122. hostName = host;
  123. }
  124. config.setHostName(hostName);
  125. config.setProperty(SshConstants.HOST_NAME, hostName);
  126. config.setHost(SshdSocketAddress.isIPv6Address(hostName) ? "" : hostName); //$NON-NLS-1$
  127. String user = username != null && !username.isEmpty() ? username
  128. : entry.getValue(SshConstants.USER);
  129. if (user == null || user.isEmpty()) {
  130. user = localUserName;
  131. }
  132. config.setUsername(user);
  133. config.setProperty(SshConstants.USER, user);
  134. int p = port >= 0 ? port : positive(entry.getValue(SshConstants.PORT));
  135. config.setPort(p >= 0 ? p : SshConstants.SSH_DEFAULT_PORT);
  136. config.setProperty(SshConstants.PORT,
  137. Integer.toString(config.getPort()));
  138. config.setIdentities(entry.getValues(SshConstants.IDENTITY_FILE));
  139. config.setIdentitiesOnly(
  140. flag(entry.getValue(SshConstants.IDENTITIES_ONLY)));
  141. return config;
  142. }
  143. }