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 17KB

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