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

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