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.

ApacheSshTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright (C) 2018, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport.sshd;
  11. import static org.apache.sshd.core.CoreModuleProperties.MAX_CONCURRENT_SESSIONS;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertThrows;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.BufferedWriter;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.UncheckedIOException;
  21. import java.net.URISyntaxException;
  22. import java.nio.charset.StandardCharsets;
  23. import java.nio.file.Files;
  24. import java.nio.file.StandardOpenOption;
  25. import java.security.KeyPair;
  26. import java.security.KeyPairGenerator;
  27. import java.security.PublicKey;
  28. import java.util.Arrays;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.stream.Collectors;
  32. import org.apache.sshd.client.config.hosts.KnownHostEntry;
  33. import org.apache.sshd.client.config.hosts.KnownHostHashValue;
  34. import org.apache.sshd.common.config.keys.AuthorizedKeyEntry;
  35. import org.apache.sshd.common.config.keys.KeyUtils;
  36. import org.apache.sshd.common.config.keys.PublicKeyEntry;
  37. import org.apache.sshd.common.config.keys.PublicKeyEntryResolver;
  38. import org.apache.sshd.common.session.Session;
  39. import org.apache.sshd.common.util.net.SshdSocketAddress;
  40. import org.apache.sshd.server.ServerAuthenticationManager;
  41. import org.apache.sshd.server.SshServer;
  42. import org.apache.sshd.server.forward.StaticDecisionForwardingFilter;
  43. import org.eclipse.jgit.api.Git;
  44. import org.eclipse.jgit.api.errors.TransportException;
  45. import org.eclipse.jgit.junit.ssh.SshTestBase;
  46. import org.eclipse.jgit.lib.Constants;
  47. import org.eclipse.jgit.transport.SshSessionFactory;
  48. import org.eclipse.jgit.util.FS;
  49. import org.junit.Test;
  50. import org.junit.experimental.theories.Theories;
  51. import org.junit.runner.RunWith;
  52. @RunWith(Theories.class)
  53. public class ApacheSshTest extends SshTestBase {
  54. @Override
  55. protected SshSessionFactory createSessionFactory() {
  56. SshdSessionFactory result = new SshdSessionFactory(new JGitKeyCache(),
  57. null);
  58. // The home directory is mocked at this point!
  59. result.setHomeDirectory(FS.DETECTED.userHome());
  60. result.setSshDirectory(sshDir);
  61. return result;
  62. }
  63. @Override
  64. protected void installConfig(String... config) {
  65. File configFile = new File(sshDir, Constants.CONFIG);
  66. if (config != null) {
  67. try {
  68. Files.write(configFile.toPath(), Arrays.asList(config));
  69. } catch (IOException e) {
  70. throw new UncheckedIOException(e);
  71. }
  72. }
  73. }
  74. @Test
  75. public void testEd25519HostKey() throws Exception {
  76. // Using ed25519 user identities is tested in the super class in
  77. // testSshKeys().
  78. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  79. copyTestResource("id_ed25519", newHostKey);
  80. server.addHostKey(newHostKey.toPath(), true);
  81. File newHostKeyPub = new File(getTemporaryDirectory(),
  82. "newhostkey.pub");
  83. copyTestResource("id_ed25519.pub", newHostKeyPub);
  84. createKnownHostsFile(knownHosts, "localhost", testPort, newHostKeyPub);
  85. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  86. "Host git", //
  87. "HostName localhost", //
  88. "Port " + testPort, //
  89. "User " + TEST_USER, //
  90. "IdentityFile " + privateKey1.getAbsolutePath());
  91. }
  92. @Test
  93. public void testHashedKnownHosts() throws Exception {
  94. assertTrue("Failed to delete known_hosts", knownHosts.delete());
  95. // The provider will answer "yes" to all questions, so we should be able
  96. // to connect and end up with a new known_hosts file with the host key.
  97. TestCredentialsProvider provider = new TestCredentialsProvider();
  98. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  99. "HashKnownHosts yes", //
  100. "Host localhost", //
  101. "HostName localhost", //
  102. "Port " + testPort, //
  103. "User " + TEST_USER, //
  104. "IdentityFile " + privateKey1.getAbsolutePath());
  105. List<LogEntry> messages = provider.getLog();
  106. assertFalse("Expected user interaction", messages.isEmpty());
  107. assertEquals(
  108. "Expected to be asked about the key, and the file creation", 2,
  109. messages.size());
  110. assertTrue("~/.ssh/known_hosts should exist now", knownHosts.exists());
  111. // Let's clone again without provider. If it works, the server host key
  112. // was written correctly.
  113. File clonedAgain = new File(getTemporaryDirectory(), "cloned2");
  114. cloneWith("ssh://localhost/doesntmatter", clonedAgain, null, //
  115. "Host localhost", //
  116. "HostName localhost", //
  117. "Port " + testPort, //
  118. "User " + TEST_USER, //
  119. "IdentityFile " + privateKey1.getAbsolutePath());
  120. // Check that the first line contains neither "localhost" nor
  121. // "127.0.0.1", but does contain the expected hash.
  122. List<String> lines = Files.readAllLines(knownHosts.toPath()).stream()
  123. .filter(s -> s != null && s.length() >= 1 && s.charAt(0) != '#'
  124. && !s.trim().isEmpty())
  125. .collect(Collectors.toList());
  126. assertEquals("Unexpected number of known_hosts lines", 1, lines.size());
  127. String line = lines.get(0);
  128. assertFalse("Found host in line", line.contains("localhost"));
  129. assertFalse("Found IP in line", line.contains("127.0.0.1"));
  130. assertTrue("Hash not found", line.contains("|"));
  131. KnownHostEntry entry = KnownHostEntry.parseKnownHostEntry(line);
  132. assertTrue("Hash doesn't match localhost",
  133. entry.isHostMatch("localhost", testPort)
  134. || entry.isHostMatch("127.0.0.1", testPort));
  135. }
  136. @Test
  137. public void testPreamble() throws Exception {
  138. // Test that the client can deal with strange lines being sent before
  139. // the server identification string.
  140. StringBuilder b = new StringBuilder();
  141. for (int i = 0; i < 257; i++) {
  142. b.append('a');
  143. }
  144. server.setPreamble("A line with a \000 NUL",
  145. "A long line: " + b.toString());
  146. cloneWith(
  147. "ssh://" + TEST_USER + "@localhost:" + testPort
  148. + "/doesntmatter",
  149. defaultCloneDir, null,
  150. "IdentityFile " + privateKey1.getAbsolutePath());
  151. }
  152. @Test
  153. public void testLongPreamble() throws Exception {
  154. // Test that the client can deal with a long (about 60k) preamble.
  155. StringBuilder b = new StringBuilder();
  156. for (int i = 0; i < 1024; i++) {
  157. b.append('a');
  158. }
  159. String line = b.toString();
  160. String[] lines = new String[60];
  161. for (int i = 0; i < lines.length; i++) {
  162. lines[i] = line;
  163. }
  164. server.setPreamble(lines);
  165. cloneWith(
  166. "ssh://" + TEST_USER + "@localhost:" + testPort
  167. + "/doesntmatter",
  168. defaultCloneDir, null,
  169. "IdentityFile " + privateKey1.getAbsolutePath());
  170. }
  171. @Test
  172. public void testHugePreamble() throws Exception {
  173. // Test that the connection fails when the preamble is longer than 64k.
  174. StringBuilder b = new StringBuilder();
  175. for (int i = 0; i < 1024; i++) {
  176. b.append('a');
  177. }
  178. String line = b.toString();
  179. String[] lines = new String[70];
  180. for (int i = 0; i < lines.length; i++) {
  181. lines[i] = line;
  182. }
  183. server.setPreamble(lines);
  184. TransportException e = assertThrows(TransportException.class,
  185. () -> cloneWith(
  186. "ssh://" + TEST_USER + "@localhost:" + testPort
  187. + "/doesntmatter",
  188. defaultCloneDir, null,
  189. "IdentityFile " + privateKey1.getAbsolutePath()));
  190. // The assertions test that we don't run into bug 565394 / SSHD-1050
  191. assertFalse(e.getMessage().contains("timeout"));
  192. assertTrue(e.getMessage().contains("65536")
  193. || e.getMessage().contains("closed"));
  194. }
  195. /**
  196. * Test for SSHD-1028. If the server doesn't close sessions, the second
  197. * fetch will fail. Occurs on sshd 2.5.[01].
  198. *
  199. * @throws Exception
  200. * on errors
  201. * @see <a href=
  202. * "https://issues.apache.org/jira/projects/SSHD/issues/SSHD-1028">SSHD-1028</a>
  203. */
  204. @Test
  205. public void testCloneAndFetchWithSessionLimit() throws Exception {
  206. MAX_CONCURRENT_SESSIONS
  207. .set(server.getPropertyResolver(), Integer.valueOf(2));
  208. File localClone = cloneWith("ssh://localhost/doesntmatter",
  209. defaultCloneDir, null, //
  210. "Host localhost", //
  211. "HostName localhost", //
  212. "Port " + testPort, //
  213. "User " + TEST_USER, //
  214. "IdentityFile " + privateKey1.getAbsolutePath());
  215. // Fetch a couple of times
  216. try (Git git = Git.open(localClone)) {
  217. git.fetch().call();
  218. git.fetch().call();
  219. }
  220. }
  221. /**
  222. * Creates a simple proxy server. Accepts only publickey authentication from
  223. * the given user with the given key, allows all forwardings. Adds the
  224. * proxy's host key to {@link #knownHosts}.
  225. *
  226. * @param user
  227. * to accept
  228. * @param userKey
  229. * public key of that user at this server
  230. * @param report
  231. * single-element array to report back the forwarded address.
  232. * @return the started server
  233. * @throws Exception
  234. */
  235. private SshServer createProxy(String user, File userKey,
  236. SshdSocketAddress[] report) throws Exception {
  237. SshServer proxy = SshServer.setUpDefaultServer();
  238. // Give the server its own host key
  239. KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
  240. generator.initialize(2048);
  241. KeyPair proxyHostKey = generator.generateKeyPair();
  242. proxy.setKeyPairProvider(
  243. session -> Collections.singletonList(proxyHostKey));
  244. // Allow (only) publickey authentication
  245. proxy.setUserAuthFactories(Collections.singletonList(
  246. ServerAuthenticationManager.DEFAULT_USER_AUTH_PUBLIC_KEY_FACTORY));
  247. // Install the user's public key
  248. PublicKey userProxyKey = AuthorizedKeyEntry
  249. .readAuthorizedKeys(userKey.toPath()).get(0)
  250. .resolvePublicKey(null, PublicKeyEntryResolver.IGNORING);
  251. proxy.setPublickeyAuthenticator(
  252. (userName, publicKey, session) -> user.equals(userName)
  253. && KeyUtils.compareKeys(userProxyKey, publicKey));
  254. // Allow forwarding
  255. proxy.setForwardingFilter(new StaticDecisionForwardingFilter(true) {
  256. @Override
  257. protected boolean checkAcceptance(String request, Session session,
  258. SshdSocketAddress target) {
  259. report[0] = target;
  260. return super.checkAcceptance(request, session, target);
  261. }
  262. });
  263. proxy.start();
  264. // Add the proxy's host key to knownhosts
  265. try (BufferedWriter writer = Files.newBufferedWriter(
  266. knownHosts.toPath(), StandardCharsets.US_ASCII,
  267. StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
  268. writer.append('\n');
  269. KnownHostHashValue.appendHostPattern(writer, "localhost",
  270. proxy.getPort());
  271. writer.append(',');
  272. KnownHostHashValue.appendHostPattern(writer, "127.0.0.1",
  273. proxy.getPort());
  274. writer.append(' ');
  275. PublicKeyEntry.appendPublicKeyEntry(writer,
  276. proxyHostKey.getPublic());
  277. writer.append('\n');
  278. }
  279. return proxy;
  280. }
  281. @Test
  282. public void testJumpHost() throws Exception {
  283. SshdSocketAddress[] forwarded = { null };
  284. try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
  285. forwarded)) {
  286. try {
  287. // Now try to clone via the proxy
  288. cloneWith("ssh://server/doesntmatter", defaultCloneDir, null, //
  289. "Host server", //
  290. "HostName localhost", //
  291. "Port " + testPort, //
  292. "User " + TEST_USER, //
  293. "IdentityFile " + privateKey1.getAbsolutePath(), //
  294. "ProxyJump " + TEST_USER + "X@proxy:" + proxy.getPort(), //
  295. "", //
  296. "Host proxy", //
  297. "Hostname localhost", //
  298. "IdentityFile " + privateKey2.getAbsolutePath());
  299. assertNotNull(forwarded[0]);
  300. assertEquals(testPort, forwarded[0].getPort());
  301. } finally {
  302. proxy.stop();
  303. }
  304. }
  305. }
  306. @Test
  307. public void testJumpHostWrongKeyAtProxy() throws Exception {
  308. // Test that we find the proxy server's URI in the exception message
  309. SshdSocketAddress[] forwarded = { null };
  310. try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
  311. forwarded)) {
  312. try {
  313. // Now try to clone via the proxy
  314. TransportException e = assertThrows(TransportException.class,
  315. () -> cloneWith("ssh://server/doesntmatter",
  316. defaultCloneDir, null, //
  317. "Host server", //
  318. "HostName localhost", //
  319. "Port " + testPort, //
  320. "User " + TEST_USER, //
  321. "IdentityFile " + privateKey1.getAbsolutePath(),
  322. "ProxyJump " + TEST_USER + "X@proxy:"
  323. + proxy.getPort(), //
  324. "", //
  325. "Host proxy", //
  326. "Hostname localhost", //
  327. "IdentityFile "
  328. + privateKey1.getAbsolutePath()));
  329. String message = e.getMessage();
  330. assertTrue(message.contains("localhost:" + proxy.getPort()));
  331. assertTrue(message.contains("proxy:" + proxy.getPort()));
  332. } finally {
  333. proxy.stop();
  334. }
  335. }
  336. }
  337. @Test
  338. public void testJumpHostWrongKeyAtServer() throws Exception {
  339. // Test that we find the target server's URI in the exception message
  340. SshdSocketAddress[] forwarded = { null };
  341. try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
  342. forwarded)) {
  343. try {
  344. // Now try to clone via the proxy
  345. TransportException e = assertThrows(TransportException.class,
  346. () -> cloneWith("ssh://server/doesntmatter",
  347. defaultCloneDir, null, //
  348. "Host server", //
  349. "HostName localhost", //
  350. "Port " + testPort, //
  351. "User " + TEST_USER, //
  352. "IdentityFile " + privateKey2.getAbsolutePath(),
  353. "ProxyJump " + TEST_USER + "X@proxy:"
  354. + proxy.getPort(), //
  355. "", //
  356. "Host proxy", //
  357. "Hostname localhost", //
  358. "IdentityFile "
  359. + privateKey2.getAbsolutePath()));
  360. String message = e.getMessage();
  361. assertTrue(message.contains("localhost:" + testPort));
  362. assertTrue(message.contains("ssh://server"));
  363. } finally {
  364. proxy.stop();
  365. }
  366. }
  367. }
  368. @Test
  369. public void testJumpHostNonSsh() throws Exception {
  370. SshdSocketAddress[] forwarded = { null };
  371. try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
  372. forwarded)) {
  373. try {
  374. TransportException e = assertThrows(TransportException.class,
  375. () -> cloneWith("ssh://server/doesntmatter",
  376. defaultCloneDir, null, //
  377. "Host server", //
  378. "HostName localhost", //
  379. "Port " + testPort, //
  380. "User " + TEST_USER, //
  381. "IdentityFile " + privateKey1.getAbsolutePath(), //
  382. "ProxyJump http://" + TEST_USER + "X@proxy:"
  383. + proxy.getPort(), //
  384. "", //
  385. "Host proxy", //
  386. "Hostname localhost", //
  387. "IdentityFile "
  388. + privateKey2.getAbsolutePath()));
  389. // Find the expected message
  390. Throwable t = e;
  391. while (t != null) {
  392. if (t instanceof URISyntaxException) {
  393. break;
  394. }
  395. t = t.getCause();
  396. }
  397. assertNotNull(t);
  398. assertTrue(t.getMessage().contains("Non-ssh"));
  399. } finally {
  400. proxy.stop();
  401. }
  402. }
  403. }
  404. @Test
  405. public void testJumpHostWithPath() throws Exception {
  406. SshdSocketAddress[] forwarded = { null };
  407. try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
  408. forwarded)) {
  409. try {
  410. TransportException e = assertThrows(TransportException.class,
  411. () -> cloneWith("ssh://server/doesntmatter",
  412. defaultCloneDir, null, //
  413. "Host server", //
  414. "HostName localhost", //
  415. "Port " + testPort, //
  416. "User " + TEST_USER, //
  417. "IdentityFile " + privateKey1.getAbsolutePath(), //
  418. "ProxyJump ssh://" + TEST_USER + "X@proxy:"
  419. + proxy.getPort() + "/wrongPath", //
  420. "", //
  421. "Host proxy", //
  422. "Hostname localhost", //
  423. "IdentityFile "
  424. + privateKey2.getAbsolutePath()));
  425. // Find the expected message
  426. Throwable t = e;
  427. while (t != null) {
  428. if (t instanceof URISyntaxException) {
  429. break;
  430. }
  431. t = t.getCause();
  432. }
  433. assertNotNull(t);
  434. assertTrue(t.getMessage().contains("wrongPath"));
  435. } finally {
  436. proxy.stop();
  437. }
  438. }
  439. }
  440. @Test
  441. public void testJumpHostWithPathShort() throws Exception {
  442. SshdSocketAddress[] forwarded = { null };
  443. try (SshServer proxy = createProxy(TEST_USER + 'X', publicKey2,
  444. forwarded)) {
  445. try {
  446. TransportException e = assertThrows(TransportException.class,
  447. () -> cloneWith("ssh://server/doesntmatter",
  448. defaultCloneDir, null, //
  449. "Host server", //
  450. "HostName localhost", //
  451. "Port " + testPort, //
  452. "User " + TEST_USER, //
  453. "IdentityFile " + privateKey1.getAbsolutePath(), //
  454. "ProxyJump " + TEST_USER + "X@proxy:wrongPath", //
  455. "", //
  456. "Host proxy", //
  457. "Hostname localhost", //
  458. "Port " + proxy.getPort(), //
  459. "IdentityFile "
  460. + privateKey2.getAbsolutePath()));
  461. // Find the expected message
  462. Throwable t = e;
  463. while (t != null) {
  464. if (t instanceof URISyntaxException) {
  465. break;
  466. }
  467. t = t.getCause();
  468. }
  469. assertNotNull(t);
  470. assertTrue(t.getMessage().contains("wrongPath"));
  471. } finally {
  472. proxy.stop();
  473. }
  474. }
  475. }
  476. @Test
  477. public void testJumpHostChain() throws Exception {
  478. SshdSocketAddress[] forwarded1 = { null };
  479. SshdSocketAddress[] forwarded2 = { null };
  480. try (SshServer proxy1 = createProxy(TEST_USER + 'X', publicKey2,
  481. forwarded1);
  482. SshServer proxy2 = createProxy("foo", publicKey1, forwarded2)) {
  483. try {
  484. // Clone proxy1 -> proxy2 -> server
  485. cloneWith("ssh://server/doesntmatter", defaultCloneDir, null, //
  486. "Host server", //
  487. "HostName localhost", //
  488. "Port " + testPort, //
  489. "User " + TEST_USER, //
  490. "IdentityFile " + privateKey1.getAbsolutePath(), //
  491. "ProxyJump proxy2," + TEST_USER + "X@proxy:"
  492. + proxy1.getPort(), //
  493. "", //
  494. "Host proxy", //
  495. "Hostname localhost", //
  496. "IdentityFile " + privateKey2.getAbsolutePath(), //
  497. "", //
  498. "Host proxy2", //
  499. "Hostname localhost", //
  500. "User foo", //
  501. "Port " + proxy2.getPort(), //
  502. "IdentityFile " + privateKey1.getAbsolutePath());
  503. assertNotNull(forwarded1[0]);
  504. assertEquals(proxy2.getPort(), forwarded1[0].getPort());
  505. assertNotNull(forwarded2[0]);
  506. assertEquals(testPort, forwarded2[0].getPort());
  507. } finally {
  508. proxy1.stop();
  509. proxy2.stop();
  510. }
  511. }
  512. }
  513. @Test
  514. public void testJumpHostCascade() throws Exception {
  515. SshdSocketAddress[] forwarded1 = { null };
  516. SshdSocketAddress[] forwarded2 = { null };
  517. try (SshServer proxy1 = createProxy(TEST_USER + 'X', publicKey2,
  518. forwarded1);
  519. SshServer proxy2 = createProxy("foo", publicKey1, forwarded2)) {
  520. try {
  521. // Clone proxy2 -> proxy1 -> server
  522. cloneWith("ssh://server/doesntmatter", defaultCloneDir, null, //
  523. "Host server", //
  524. "HostName localhost", //
  525. "Port " + testPort, //
  526. "User " + TEST_USER, //
  527. "IdentityFile " + privateKey1.getAbsolutePath(), //
  528. "ProxyJump " + TEST_USER + "X@proxy", //
  529. "", //
  530. "Host proxy", //
  531. "Hostname localhost", //
  532. "Port " + proxy1.getPort(), //
  533. "ProxyJump ssh://proxy2:" + proxy2.getPort(), //
  534. "IdentityFile " + privateKey2.getAbsolutePath(), //
  535. "", //
  536. "Host proxy2", //
  537. "Hostname localhost", //
  538. "User foo", //
  539. "IdentityFile " + privateKey1.getAbsolutePath());
  540. assertNotNull(forwarded1[0]);
  541. assertEquals(testPort, forwarded1[0].getPort());
  542. assertNotNull(forwarded2[0]);
  543. assertEquals(proxy1.getPort(), forwarded2[0].getPort());
  544. } finally {
  545. proxy1.stop();
  546. proxy2.stop();
  547. }
  548. }
  549. }
  550. @Test
  551. public void testJumpHostRecursion() throws Exception {
  552. SshdSocketAddress[] forwarded1 = { null };
  553. SshdSocketAddress[] forwarded2 = { null };
  554. try (SshServer proxy1 = createProxy(TEST_USER + 'X', publicKey2,
  555. forwarded1);
  556. SshServer proxy2 = createProxy("foo", publicKey1, forwarded2)) {
  557. try {
  558. TransportException e = assertThrows(TransportException.class,
  559. () -> cloneWith(
  560. "ssh://server/doesntmatter", defaultCloneDir, null, //
  561. "Host server", //
  562. "HostName localhost", //
  563. "Port " + testPort, //
  564. "User " + TEST_USER, //
  565. "IdentityFile " + privateKey1.getAbsolutePath(), //
  566. "ProxyJump " + TEST_USER + "X@proxy", //
  567. "", //
  568. "Host proxy", //
  569. "Hostname localhost", //
  570. "Port " + proxy1.getPort(), //
  571. "ProxyJump ssh://proxy2:" + proxy2.getPort(), //
  572. "IdentityFile " + privateKey2.getAbsolutePath(), //
  573. "", //
  574. "Host proxy2", //
  575. "Hostname localhost", //
  576. "User foo", //
  577. "ProxyJump " + TEST_USER + "X@proxy", //
  578. "IdentityFile " + privateKey1.getAbsolutePath()));
  579. assertTrue(e.getMessage().contains("proxy"));
  580. } finally {
  581. proxy1.stop();
  582. proxy2.stop();
  583. }
  584. }
  585. }
  586. }