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.

SshdSessionFactory.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.transport.sshd;
  44. import java.io.Closeable;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.nio.file.Files;
  48. import java.nio.file.Path;
  49. import java.time.Duration;
  50. import java.util.ArrayList;
  51. import java.util.Arrays;
  52. import java.util.Collections;
  53. import java.util.HashSet;
  54. import java.util.List;
  55. import java.util.Map;
  56. import java.util.Set;
  57. import java.util.concurrent.ConcurrentHashMap;
  58. import java.util.concurrent.atomic.AtomicBoolean;
  59. import java.util.stream.Collectors;
  60. import org.apache.sshd.client.ClientBuilder;
  61. import org.apache.sshd.client.SshClient;
  62. import org.apache.sshd.client.auth.UserAuth;
  63. import org.apache.sshd.client.auth.keyboard.UserAuthKeyboardInteractiveFactory;
  64. import org.apache.sshd.client.auth.password.UserAuthPasswordFactory;
  65. import org.apache.sshd.client.config.hosts.HostConfigEntryResolver;
  66. import org.apache.sshd.client.keyverifier.ServerKeyVerifier;
  67. import org.apache.sshd.common.NamedFactory;
  68. import org.apache.sshd.common.compression.BuiltinCompressions;
  69. import org.apache.sshd.common.config.keys.FilePasswordProvider;
  70. import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
  71. import org.apache.sshd.common.keyprovider.KeyPairProvider;
  72. import org.eclipse.jgit.annotations.NonNull;
  73. import org.eclipse.jgit.errors.TransportException;
  74. import org.eclipse.jgit.internal.transport.sshd.CachingKeyPairProvider;
  75. import org.eclipse.jgit.internal.transport.sshd.JGitPublicKeyAuthFactory;
  76. import org.eclipse.jgit.internal.transport.sshd.JGitSshClient;
  77. import org.eclipse.jgit.internal.transport.sshd.JGitUserInteraction;
  78. import org.eclipse.jgit.internal.transport.sshd.OpenSshServerKeyVerifier;
  79. import org.eclipse.jgit.internal.transport.sshd.SshdText;
  80. import org.eclipse.jgit.transport.CredentialsProvider;
  81. import org.eclipse.jgit.transport.SshConstants;
  82. import org.eclipse.jgit.transport.SshSessionFactory;
  83. import org.eclipse.jgit.transport.URIish;
  84. import org.eclipse.jgit.util.FS;
  85. /**
  86. * A {@link SshSessionFactory} that uses Apache MINA sshd.
  87. *
  88. * @since 5.2
  89. */
  90. public class SshdSessionFactory extends SshSessionFactory implements Closeable {
  91. private final AtomicBoolean closing = new AtomicBoolean();
  92. private final Set<SshdSession> sessions = new HashSet<>();
  93. private final Map<Tuple, HostConfigEntryResolver> defaultHostConfigEntryResolver = new ConcurrentHashMap<>();
  94. private final Map<Tuple, ServerKeyVerifier> defaultServerKeyVerifier = new ConcurrentHashMap<>();
  95. private final Map<Tuple, FileKeyPairProvider> defaultKeys = new ConcurrentHashMap<>();
  96. private final KeyCache keyCache;
  97. private File sshDirectory;
  98. private File homeDirectory;
  99. /**
  100. * Creates a new {@link SshdSessionFactory} without {@link KeyCache}.
  101. */
  102. public SshdSessionFactory() {
  103. this(null);
  104. }
  105. /**
  106. * Creates a new {@link SshdSessionFactory} using the given
  107. * {@link KeyCache}. The {@code keyCache} is used for all sessions created
  108. * through this session factory; cached keys are destroyed when the session
  109. * factory is {@link #close() closed}.
  110. * <p>
  111. * Caching ssh keys in memory for an extended period of time is generally
  112. * considered bad practice, but there may be circumstances where using a
  113. * {@link KeyCache} is still the right choice, for instance to avoid that a
  114. * user gets prompted several times for the same password for the same key.
  115. * In general, however, it is preferable <em>not</em> to use a key cache but
  116. * to use a {@link #createFilePasswordProvider(CredentialsProvider)
  117. * FilePasswordProvider} that has access to some secure storage and can save
  118. * and retrieve passwords from there without user interaction. Another
  119. * approach is to use an ssh agent.
  120. * </p>
  121. * <p>
  122. * Note that the underlying ssh library (Apache MINA sshd) may or may not
  123. * keep ssh keys in memory for unspecified periods of time irrespective of
  124. * the use of a {@link KeyCache}.
  125. * </p>
  126. *
  127. * @param keyCache
  128. * {@link KeyCache} to use for caching ssh keys, or {@code null}
  129. * to not use a key cache
  130. */
  131. public SshdSessionFactory(KeyCache keyCache) {
  132. super();
  133. this.keyCache = keyCache;
  134. }
  135. /** A simple general map key. */
  136. private static final class Tuple {
  137. private Object[] objects;
  138. public Tuple(Object... objects) {
  139. this.objects = objects;
  140. }
  141. @Override
  142. public boolean equals(Object obj) {
  143. if (obj == this) {
  144. return true;
  145. }
  146. if (obj != null && obj.getClass() == Tuple.class) {
  147. Tuple other = (Tuple) obj;
  148. return Arrays.equals(objects, other.objects);
  149. }
  150. return false;
  151. }
  152. @Override
  153. public int hashCode() {
  154. return Arrays.hashCode(objects);
  155. }
  156. }
  157. // We can't really use a single client. Clients need to be stopped
  158. // properly, and we don't really know when to do that. Instead we use
  159. // a dedicated SshClient instance per session. We need a bit of caching to
  160. // avoid re-loading the ssh config and keys repeatedly.
  161. @Override
  162. public SshdSession getSession(URIish uri,
  163. CredentialsProvider credentialsProvider, FS fs, int tms)
  164. throws TransportException {
  165. SshdSession session = null;
  166. try {
  167. session = new SshdSession(uri, () -> {
  168. File home = getHomeDirectory();
  169. if (home == null) {
  170. // Always use the detected filesystem for the user home!
  171. // It makes no sense to have different "user home"
  172. // directories depending on what file system a repository
  173. // is.
  174. home = FS.DETECTED.userHome();
  175. }
  176. File sshDir = getSshDirectory();
  177. if (sshDir == null) {
  178. sshDir = new File(home, SshConstants.SSH_DIR);
  179. }
  180. HostConfigEntryResolver configFile = getHostConfigEntryResolver(
  181. home, sshDir);
  182. KeyPairProvider defaultKeysProvider = getDefaultKeysProvider(
  183. sshDir);
  184. SshClient client = ClientBuilder.builder()
  185. .factory(JGitSshClient::new)
  186. .filePasswordProvider(
  187. createFilePasswordProvider(credentialsProvider))
  188. .hostConfigEntryResolver(configFile)
  189. .serverKeyVerifier(getServerKeyVerifier(home, sshDir))
  190. .compressionFactories(
  191. new ArrayList<>(BuiltinCompressions.VALUES))
  192. .build();
  193. client.setUserInteraction(
  194. new JGitUserInteraction(credentialsProvider));
  195. client.setUserAuthFactories(getUserAuthFactories());
  196. client.setKeyPairProvider(defaultKeysProvider);
  197. // JGit-specific things:
  198. JGitSshClient jgitClient = (JGitSshClient) client;
  199. jgitClient.setKeyCache(getKeyCache());
  200. jgitClient.setCredentialsProvider(credentialsProvider);
  201. // Other things?
  202. return client;
  203. });
  204. session.addCloseListener(s -> unregister(s));
  205. register(session);
  206. session.connect(Duration.ofMillis(tms));
  207. return session;
  208. } catch (Exception e) {
  209. unregister(session);
  210. throw new TransportException(uri, e.getMessage(), e);
  211. }
  212. }
  213. @Override
  214. public void close() {
  215. closing.set(true);
  216. boolean cleanKeys = false;
  217. synchronized (this) {
  218. cleanKeys = sessions.isEmpty();
  219. }
  220. if (cleanKeys) {
  221. KeyCache cache = getKeyCache();
  222. if (cache != null) {
  223. cache.close();
  224. }
  225. }
  226. }
  227. private void register(SshdSession newSession) throws IOException {
  228. if (newSession == null) {
  229. return;
  230. }
  231. if (closing.get()) {
  232. throw new IOException(SshdText.get().sshClosingDown);
  233. }
  234. synchronized (this) {
  235. sessions.add(newSession);
  236. }
  237. }
  238. private void unregister(SshdSession oldSession) {
  239. boolean cleanKeys = false;
  240. synchronized (this) {
  241. sessions.remove(oldSession);
  242. cleanKeys = closing.get() && sessions.isEmpty();
  243. }
  244. if (cleanKeys) {
  245. KeyCache cache = getKeyCache();
  246. if (cache != null) {
  247. cache.close();
  248. }
  249. }
  250. }
  251. /**
  252. * Set a global directory to use as the user's home directory
  253. *
  254. * @param homeDir
  255. * to use
  256. */
  257. public void setHomeDirectory(@NonNull File homeDir) {
  258. if (homeDir.isAbsolute()) {
  259. homeDirectory = homeDir;
  260. } else {
  261. homeDirectory = homeDir.getAbsoluteFile();
  262. }
  263. }
  264. /**
  265. * Retrieves the global user home directory
  266. *
  267. * @return the directory, or {@code null} if not set
  268. */
  269. public File getHomeDirectory() {
  270. return homeDirectory;
  271. }
  272. /**
  273. * Set a global directory to use as the .ssh directory
  274. *
  275. * @param sshDir
  276. * to use
  277. */
  278. public void setSshDirectory(@NonNull File sshDir) {
  279. if (sshDir.isAbsolute()) {
  280. sshDirectory = sshDir;
  281. } else {
  282. sshDirectory = sshDir.getAbsoluteFile();
  283. }
  284. }
  285. /**
  286. * Retrieves the global .ssh directory
  287. *
  288. * @return the directory, or {@code null} if not set
  289. */
  290. public File getSshDirectory() {
  291. return sshDirectory;
  292. }
  293. /**
  294. * Obtain a {@link HostConfigEntryResolver} to read the ssh config file and
  295. * to determine host entries for connections.
  296. *
  297. * @param homeDir
  298. * home directory to use for ~ replacement
  299. * @param sshDir
  300. * to use for looking for the config file
  301. * @return the resolver
  302. */
  303. @NonNull
  304. protected HostConfigEntryResolver getHostConfigEntryResolver(
  305. @NonNull File homeDir, @NonNull File sshDir) {
  306. return defaultHostConfigEntryResolver.computeIfAbsent(
  307. new Tuple(homeDir, sshDir),
  308. t -> new JGitSshConfig(homeDir,
  309. new File(sshDir, SshConstants.CONFIG),
  310. getLocalUserName()));
  311. }
  312. /**
  313. * Obtain a {@link ServerKeyVerifier} to read known_hosts files and to
  314. * verify server host keys. The default implementation returns a
  315. * {@link ServerKeyVerifier} that recognizes the two openssh standard files
  316. * {@code ~/.ssh/known_hosts} and {@code ~/.ssh/known_hosts2} as well as any
  317. * files configured via the {@code UserKnownHostsFile} option in the ssh
  318. * config file.
  319. *
  320. * @param homeDir
  321. * home directory to use for ~ replacement
  322. * @param sshDir
  323. * representing ~/.ssh/
  324. * @return the resolver
  325. */
  326. @NonNull
  327. protected ServerKeyVerifier getServerKeyVerifier(@NonNull File homeDir,
  328. @NonNull File sshDir) {
  329. return defaultServerKeyVerifier.computeIfAbsent(
  330. new Tuple(homeDir, sshDir),
  331. t -> new OpenSshServerKeyVerifier(true,
  332. Arrays.asList(
  333. new File(sshDir, SshConstants.KNOWN_HOSTS),
  334. new File(sshDir,
  335. SshConstants.KNOWN_HOSTS + '2'))));
  336. }
  337. /**
  338. * Determines a {@link KeyPairProvider} to use to load the default keys.
  339. *
  340. * @param sshDir
  341. * to look in for keys
  342. * @return the {@link KeyPairProvider}
  343. */
  344. @NonNull
  345. protected KeyPairProvider getDefaultKeysProvider(@NonNull File sshDir) {
  346. return defaultKeys.computeIfAbsent(new Tuple(sshDir),
  347. t -> new CachingKeyPairProvider(getDefaultIdentities(sshDir),
  348. getKeyCache()));
  349. }
  350. /**
  351. * Gets a list of default identities, i.e., private key files that shall
  352. * always be tried for public key authentication. Typically those are
  353. * ~/.ssh/id_dsa, ~/.ssh/id_rsa, and so on. The default implementation
  354. * returns the files defined in {@link SshConstants#DEFAULT_IDENTITIES}.
  355. *
  356. * @param sshDir
  357. * the directory that represents ~/.ssh/
  358. * @return a possibly empty list of paths containing default identities
  359. * (private keys)
  360. */
  361. @NonNull
  362. protected List<Path> getDefaultIdentities(@NonNull File sshDir) {
  363. return Arrays
  364. .asList(SshConstants.DEFAULT_IDENTITIES).stream()
  365. .map(s -> new File(sshDir, s).toPath()).filter(Files::exists)
  366. .collect(Collectors.toList());
  367. }
  368. /**
  369. * Obtains the {@link KeyCache} to use to cache loaded keys.
  370. *
  371. * @return the {@link KeyCache}, or {@code null} if none.
  372. */
  373. protected final KeyCache getKeyCache() {
  374. return keyCache;
  375. }
  376. /**
  377. * Creates a {@link FilePasswordProvider} for a new session.
  378. *
  379. * @param provider
  380. * the {@link CredentialsProvider} to delegate for for user
  381. * interactions
  382. * @return a new {@link FilePasswordProvider}
  383. */
  384. @NonNull
  385. protected FilePasswordProvider createFilePasswordProvider(
  386. CredentialsProvider provider) {
  387. return new IdentityPasswordProvider(provider);
  388. }
  389. /**
  390. * Gets the user authentication mechanisms (or rather, factories for them).
  391. * By default this returns public-key, keyboard-interactive, and password,
  392. * in that order. (I.e., we don't do gssapi-with-mic or hostbased (yet)).
  393. *
  394. * @return the non-empty list of factories.
  395. */
  396. @NonNull
  397. protected List<NamedFactory<UserAuth>> getUserAuthFactories() {
  398. return Collections.unmodifiableList(
  399. Arrays.asList(JGitPublicKeyAuthFactory.INSTANCE,
  400. UserAuthKeyboardInteractiveFactory.INSTANCE,
  401. UserAuthPasswordFactory.INSTANCE));
  402. }
  403. }