Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SshTestBase.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * Copyright (C) 2018, 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.junit.ssh;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertArrayEquals;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertFalse;
  15. import static org.junit.Assert.assertNotNull;
  16. import static org.junit.Assert.assertTrue;
  17. import static org.junit.Assume.assumeTrue;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.Files;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import org.eclipse.jgit.api.errors.TransportException;
  24. import org.eclipse.jgit.transport.CredentialItem;
  25. import org.junit.Test;
  26. import org.junit.experimental.theories.DataPoints;
  27. import org.junit.experimental.theories.Theory;
  28. /**
  29. * The ssh tests. Concrete subclasses can re-use these tests by implementing the
  30. * abstract operations from {@link SshTestHarness}. This gives a way to test
  31. * different ssh clients against a unified test suite.
  32. */
  33. public abstract class SshTestBase extends SshTestHarness {
  34. @DataPoints
  35. public static String[] KEY_RESOURCES = { //
  36. "id_dsa", //
  37. "id_rsa_1024", //
  38. "id_rsa_2048", //
  39. "id_rsa_3072", //
  40. "id_rsa_4096", //
  41. "id_ecdsa_256", //
  42. "id_ecdsa_384", //
  43. "id_ecdsa_521", //
  44. "id_ed25519", //
  45. // And now encrypted. Passphrase is "testpass".
  46. "id_dsa_testpass", //
  47. "id_rsa_1024_testpass", //
  48. "id_rsa_2048_testpass", //
  49. "id_rsa_3072_testpass", //
  50. "id_rsa_4096_testpass", //
  51. "id_ecdsa_256_testpass", //
  52. "id_ecdsa_384_testpass", //
  53. "id_ecdsa_521_testpass", //
  54. "id_ed25519_testpass", //
  55. "id_ed25519_expensive_testpass" };
  56. protected File defaultCloneDir;
  57. @Override
  58. public void setUp() throws Exception {
  59. super.setUp();
  60. defaultCloneDir = new File(getTemporaryDirectory(), "cloned");
  61. }
  62. @Test(expected = TransportException.class)
  63. public void testSshWithoutConfig() throws Exception {
  64. cloneWith("ssh://" + TEST_USER + "@localhost:" + testPort
  65. + "/doesntmatter", defaultCloneDir, null);
  66. }
  67. @Test
  68. public void testSshWithGlobalIdentity() throws Exception {
  69. cloneWith(
  70. "ssh://" + TEST_USER + "@localhost:" + testPort
  71. + "/doesntmatter",
  72. defaultCloneDir, null,
  73. "IdentityFile " + privateKey1.getAbsolutePath());
  74. }
  75. @Test
  76. public void testSshWithDefaultIdentity() throws Exception {
  77. File idRsa = new File(privateKey1.getParentFile(), "id_rsa");
  78. Files.copy(privateKey1.toPath(), idRsa.toPath());
  79. // We expect the session factory to pick up these keys...
  80. cloneWith("ssh://" + TEST_USER + "@localhost:" + testPort
  81. + "/doesntmatter", defaultCloneDir, null);
  82. }
  83. @Test
  84. public void testSshWithConfig() throws Exception {
  85. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  86. "Host localhost", //
  87. "HostName localhost", //
  88. "Port " + testPort, //
  89. "User " + TEST_USER, //
  90. "IdentityFile " + privateKey1.getAbsolutePath());
  91. }
  92. @Test
  93. public void testSshWithConfigEncryptedUnusedKey() throws Exception {
  94. // Copy the encrypted test key from the bundle.
  95. File encryptedKey = new File(sshDir, "id_dsa");
  96. copyTestResource("id_dsa_testpass", encryptedKey);
  97. TestCredentialsProvider provider = new TestCredentialsProvider(
  98. "testpass");
  99. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  100. "Host localhost", //
  101. "HostName localhost", //
  102. "Port " + testPort, //
  103. "User " + TEST_USER, //
  104. "IdentityFile " + privateKey1.getAbsolutePath());
  105. assertEquals("CredentialsProvider should not have been called", 0,
  106. provider.getLog().size());
  107. }
  108. @Test
  109. public void testSshWithConfigEncryptedUnusedKeyInConfigLast()
  110. throws Exception {
  111. // Copy the encrypted test key from the bundle.
  112. File encryptedKey = new File(sshDir, "id_dsa_test_key");
  113. copyTestResource("id_dsa_testpass", encryptedKey);
  114. TestCredentialsProvider provider = new TestCredentialsProvider(
  115. "testpass");
  116. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  117. "Host localhost", //
  118. "HostName localhost", //
  119. "Port " + testPort, //
  120. "User " + TEST_USER, //
  121. "IdentityFile " + privateKey1.getAbsolutePath(),
  122. "IdentityFile " + encryptedKey.getAbsolutePath());
  123. // This test passes with JSch per chance because JSch completely ignores
  124. // the second IdentityFile
  125. assertEquals("CredentialsProvider should not have been called", 0,
  126. provider.getLog().size());
  127. }
  128. private boolean isJsch() {
  129. return getSessionFactory().getType().equals("jsch");
  130. }
  131. @Test
  132. public void testSshWithConfigEncryptedUnusedKeyInConfigFirst()
  133. throws Exception {
  134. // Test cannot pass with JSch; it handles only one IdentityFile.
  135. // assumeTrue(!(getSessionFactory() instanceof
  136. // JschConfigSessionFactory)); gives in bazel a failure with "Never
  137. // found parameters that satisfied method assumptions."
  138. // In maven it's fine!?
  139. if (isJsch()) {
  140. return;
  141. }
  142. // Copy the encrypted test key from the bundle.
  143. File encryptedKey = new File(sshDir, "id_dsa_test_key");
  144. copyTestResource("id_dsa_testpass", encryptedKey);
  145. TestCredentialsProvider provider = new TestCredentialsProvider(
  146. "testpass");
  147. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  148. "Host localhost", //
  149. "HostName localhost", //
  150. "Port " + testPort, //
  151. "User " + TEST_USER, //
  152. "IdentityFile " + encryptedKey.getAbsolutePath(),
  153. "IdentityFile " + privateKey1.getAbsolutePath());
  154. assertEquals("CredentialsProvider should have been called once", 1,
  155. provider.getLog().size());
  156. }
  157. @Test
  158. public void testSshEncryptedUsedKeyCached() throws Exception {
  159. // Make sure we are asked for the password only once if we do several
  160. // operations with an encrypted key.
  161. File encryptedKey = new File(sshDir, "id_dsa_test_key");
  162. copyTestResource("id_dsa_testpass", encryptedKey);
  163. File encryptedPublicKey = new File(sshDir, "id_dsa_test_key.pub");
  164. copyTestResource("id_dsa_testpass.pub", encryptedPublicKey);
  165. server.setTestUserPublicKey(encryptedPublicKey.toPath());
  166. TestCredentialsProvider provider = new TestCredentialsProvider(
  167. "testpass");
  168. pushTo(provider,
  169. cloneWith("ssh://localhost/doesntmatter", //
  170. defaultCloneDir, provider, //
  171. "Host localhost", //
  172. "HostName localhost", //
  173. "Port " + testPort, //
  174. "User " + TEST_USER, //
  175. "IdentityFile " + encryptedKey.getAbsolutePath()));
  176. assertEquals("CredentialsProvider should have been called once", 1,
  177. provider.getLog().size());
  178. }
  179. @Test(expected = TransportException.class)
  180. public void testSshEncryptedUsedKeyWrongPassword() throws Exception {
  181. File encryptedKey = new File(sshDir, "id_dsa_test_key");
  182. copyTestResource("id_dsa_testpass", encryptedKey);
  183. File encryptedPublicKey = new File(sshDir, "id_dsa_test_key.pub");
  184. copyTestResource("id_dsa_testpass.pub", encryptedPublicKey);
  185. server.setTestUserPublicKey(encryptedPublicKey.toPath());
  186. TestCredentialsProvider provider = new TestCredentialsProvider(
  187. "wrongpass");
  188. cloneWith("ssh://localhost/doesntmatter", //
  189. defaultCloneDir, provider, //
  190. "Host localhost", //
  191. "HostName localhost", //
  192. "Port " + testPort, //
  193. "User " + TEST_USER, //
  194. "NumberOfPasswordPrompts 1", //
  195. "IdentityFile " + encryptedKey.getAbsolutePath());
  196. }
  197. @Test
  198. public void testSshEncryptedUsedKeySeveralPassword() throws Exception {
  199. File encryptedKey = new File(sshDir, "id_dsa_test_key");
  200. copyTestResource("id_dsa_testpass", encryptedKey);
  201. File encryptedPublicKey = new File(sshDir, "id_dsa_test_key.pub");
  202. copyTestResource("id_dsa_testpass.pub", encryptedPublicKey);
  203. server.setTestUserPublicKey(encryptedPublicKey.toPath());
  204. TestCredentialsProvider provider = new TestCredentialsProvider(
  205. "wrongpass", "wrongpass2", "testpass");
  206. cloneWith("ssh://localhost/doesntmatter", //
  207. defaultCloneDir, provider, //
  208. "Host localhost", //
  209. "HostName localhost", //
  210. "Port " + testPort, //
  211. "User " + TEST_USER, //
  212. "IdentityFile " + encryptedKey.getAbsolutePath());
  213. assertEquals("CredentialsProvider should have been called 3 times", 3,
  214. provider.getLog().size());
  215. }
  216. @Test(expected = TransportException.class)
  217. public void testSshWithoutKnownHosts() throws Exception {
  218. assertTrue("Could not delete known_hosts", knownHosts.delete());
  219. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  220. "Host localhost", //
  221. "HostName localhost", //
  222. "Port " + testPort, //
  223. "User " + TEST_USER, //
  224. "IdentityFile " + privateKey1.getAbsolutePath());
  225. }
  226. @Test
  227. public void testSshWithoutKnownHostsWithProviderAsk()
  228. throws Exception {
  229. File copiedHosts = new File(knownHosts.getParentFile(),
  230. "copiedKnownHosts");
  231. assertTrue("Failed to rename known_hosts",
  232. knownHosts.renameTo(copiedHosts));
  233. // The provider will answer "yes" to all questions, so we should be able
  234. // to connect and end up with a new known_hosts file with the host key.
  235. TestCredentialsProvider provider = new TestCredentialsProvider();
  236. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  237. "Host localhost", //
  238. "HostName localhost", //
  239. "Port " + testPort, //
  240. "User " + TEST_USER, //
  241. "IdentityFile " + privateKey1.getAbsolutePath());
  242. List<LogEntry> messages = provider.getLog();
  243. assertFalse("Expected user interaction", messages.isEmpty());
  244. if (isJsch()) {
  245. // JSch doesn't create a non-existing file.
  246. assertEquals("Expected to be asked about the key", 1,
  247. messages.size());
  248. return;
  249. }
  250. assertEquals(
  251. "Expected to be asked about the key, and the file creation",
  252. 2, messages.size());
  253. assertTrue("~/.ssh/known_hosts should exist now", knownHosts.exists());
  254. // Instead of checking the file contents, let's just clone again
  255. // without provider. If it works, the server host key was written
  256. // correctly.
  257. File clonedAgain = new File(getTemporaryDirectory(), "cloned2");
  258. cloneWith("ssh://localhost/doesntmatter", clonedAgain, null, //
  259. "Host localhost", //
  260. "HostName localhost", //
  261. "Port " + testPort, //
  262. "User " + TEST_USER, //
  263. "IdentityFile " + privateKey1.getAbsolutePath());
  264. }
  265. @Test
  266. public void testSshWithoutKnownHostsWithProviderAcceptNew()
  267. throws Exception {
  268. File copiedHosts = new File(knownHosts.getParentFile(),
  269. "copiedKnownHosts");
  270. assertTrue("Failed to rename known_hosts",
  271. knownHosts.renameTo(copiedHosts));
  272. TestCredentialsProvider provider = new TestCredentialsProvider();
  273. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  274. "Host localhost", //
  275. "HostName localhost", //
  276. "Port " + testPort, //
  277. "User " + TEST_USER, //
  278. "StrictHostKeyChecking accept-new", //
  279. "IdentityFile " + privateKey1.getAbsolutePath());
  280. if (isJsch()) {
  281. // JSch doesn't create new files.
  282. assertTrue("CredentialsProvider not called",
  283. provider.getLog().isEmpty());
  284. return;
  285. }
  286. assertEquals("Expected to be asked about the file creation", 1,
  287. provider.getLog().size());
  288. assertTrue("~/.ssh/known_hosts should exist now", knownHosts.exists());
  289. // Instead of checking the file contents, let's just clone again
  290. // without provider. If it works, the server host key was written
  291. // correctly.
  292. File clonedAgain = new File(getTemporaryDirectory(), "cloned2");
  293. cloneWith("ssh://localhost/doesntmatter", clonedAgain, null, //
  294. "Host localhost", //
  295. "HostName localhost", //
  296. "Port " + testPort, //
  297. "User " + TEST_USER, //
  298. "IdentityFile " + privateKey1.getAbsolutePath());
  299. }
  300. @Test(expected = TransportException.class)
  301. public void testSshWithoutKnownHostsDeny() throws Exception {
  302. File copiedHosts = new File(knownHosts.getParentFile(),
  303. "copiedKnownHosts");
  304. assertTrue("Failed to rename known_hosts",
  305. knownHosts.renameTo(copiedHosts));
  306. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  307. "Host localhost", //
  308. "HostName localhost", //
  309. "Port " + testPort, //
  310. "User " + TEST_USER, //
  311. "StrictHostKeyChecking yes", //
  312. "IdentityFile " + privateKey1.getAbsolutePath());
  313. }
  314. @Test(expected = TransportException.class)
  315. public void testSshModifiedHostKeyDeny()
  316. throws Exception {
  317. File copiedHosts = new File(knownHosts.getParentFile(),
  318. "copiedKnownHosts");
  319. assertTrue("Failed to rename known_hosts",
  320. knownHosts.renameTo(copiedHosts));
  321. // Now produce a new known_hosts file containing some other key.
  322. createKnownHostsFile(knownHosts, "localhost", testPort, publicKey1);
  323. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  324. "Host localhost", //
  325. "HostName localhost", //
  326. "Port " + testPort, //
  327. "User " + TEST_USER, //
  328. "StrictHostKeyChecking yes", //
  329. "IdentityFile " + privateKey1.getAbsolutePath());
  330. }
  331. @Test(expected = TransportException.class)
  332. public void testSshModifiedHostKeyWithProviderDeny() throws Exception {
  333. File copiedHosts = new File(knownHosts.getParentFile(),
  334. "copiedKnownHosts");
  335. assertTrue("Failed to rename known_hosts",
  336. knownHosts.renameTo(copiedHosts));
  337. // Now produce a new known_hosts file containing some other key.
  338. createKnownHostsFile(knownHosts, "localhost", testPort, publicKey1);
  339. TestCredentialsProvider provider = new TestCredentialsProvider();
  340. try {
  341. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  342. "Host localhost", //
  343. "HostName localhost", //
  344. "Port " + testPort, //
  345. "User " + TEST_USER, //
  346. "StrictHostKeyChecking yes", //
  347. "IdentityFile " + privateKey1.getAbsolutePath());
  348. } catch (Exception e) {
  349. assertEquals("Expected to be told about the modified key", 1,
  350. provider.getLog().size());
  351. assertTrue("Only messages expected", provider.getLog().stream()
  352. .flatMap(l -> l.getItems().stream()).allMatch(
  353. c -> c instanceof CredentialItem.InformationalMessage));
  354. throw e;
  355. }
  356. }
  357. private void checkKnownHostsModifiedHostKey(File backup, File newFile,
  358. String wrongKey) throws IOException {
  359. List<String> oldLines = Files.readAllLines(backup.toPath(), UTF_8);
  360. // Find the original entry. We should have that again in known_hosts.
  361. String oldKeyPart = null;
  362. for (String oldLine : oldLines) {
  363. if (oldLine.contains("[localhost]:")) {
  364. String[] parts = oldLine.split("\\s+");
  365. if (parts.length > 2) {
  366. oldKeyPart = parts[parts.length - 2] + ' '
  367. + parts[parts.length - 1];
  368. break;
  369. }
  370. }
  371. }
  372. assertNotNull("Old key not found", oldKeyPart);
  373. List<String> newLines = Files.readAllLines(newFile.toPath(), UTF_8);
  374. assertFalse("Old host key still found in known_hosts file" + newFile,
  375. hasHostKey("localhost", testPort, wrongKey, newLines));
  376. assertTrue("New host key not found in known_hosts file" + newFile,
  377. hasHostKey("localhost", testPort, oldKeyPart, newLines));
  378. }
  379. @Test
  380. public void testSshModifiedHostKeyAllow() throws Exception {
  381. assertTrue("Failed to delete known_hosts", knownHosts.delete());
  382. createKnownHostsFile(knownHosts, "localhost", testPort, publicKey1);
  383. File backup = new File(getTemporaryDirectory(), "backupKnownHosts");
  384. Files.copy(knownHosts.toPath(), backup.toPath());
  385. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  386. "Host localhost", //
  387. "HostName localhost", //
  388. "Port " + testPort, //
  389. "User " + TEST_USER, //
  390. "StrictHostKeyChecking no", //
  391. "IdentityFile " + privateKey1.getAbsolutePath());
  392. // File should not have been updated!
  393. String[] oldLines = Files
  394. .readAllLines(backup.toPath(), UTF_8)
  395. .toArray(new String[0]);
  396. String[] newLines = Files
  397. .readAllLines(knownHosts.toPath(), UTF_8)
  398. .toArray(new String[0]);
  399. assertArrayEquals("Known hosts file should not be modified", oldLines,
  400. newLines);
  401. }
  402. @Test
  403. public void testSshModifiedHostKeyAsk() throws Exception {
  404. File copiedHosts = new File(knownHosts.getParentFile(),
  405. "copiedKnownHosts");
  406. assertTrue("Failed to rename known_hosts",
  407. knownHosts.renameTo(copiedHosts));
  408. String wrongKeyPart = createKnownHostsFile(knownHosts, "localhost",
  409. testPort, publicKey1);
  410. TestCredentialsProvider provider = new TestCredentialsProvider();
  411. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  412. "Host localhost", //
  413. "HostName localhost", //
  414. "Port " + testPort, //
  415. "User " + TEST_USER, //
  416. "IdentityFile " + privateKey1.getAbsolutePath());
  417. checkKnownHostsModifiedHostKey(copiedHosts, knownHosts, wrongKeyPart);
  418. assertEquals("Expected to be asked about the modified key", 1,
  419. provider.getLog().size());
  420. }
  421. @Test
  422. public void testSshCloneWithConfigAndPush() throws Exception {
  423. pushTo(cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  424. "Host localhost", //
  425. "HostName localhost", //
  426. "Port " + testPort, //
  427. "User " + TEST_USER, //
  428. "IdentityFile " + privateKey1.getAbsolutePath()));
  429. }
  430. @Test
  431. public void testSftpWithConfig() throws Exception {
  432. cloneWith("sftp://localhost/.git", defaultCloneDir, null, //
  433. "Host localhost", //
  434. "HostName localhost", //
  435. "Port " + testPort, //
  436. "User " + TEST_USER, //
  437. "IdentityFile " + privateKey1.getAbsolutePath());
  438. }
  439. @Test
  440. public void testSftpCloneWithConfigAndPush() throws Exception {
  441. pushTo(cloneWith("sftp://localhost/.git", defaultCloneDir, null, //
  442. "Host localhost", //
  443. "HostName localhost", //
  444. "Port " + testPort, //
  445. "User " + TEST_USER, //
  446. "IdentityFile " + privateKey1.getAbsolutePath()));
  447. }
  448. @Test(expected = TransportException.class)
  449. public void testSshWithConfigWrongKey() throws Exception {
  450. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, null, //
  451. "Host localhost", //
  452. "HostName localhost", //
  453. "Port " + testPort, //
  454. "User " + TEST_USER, //
  455. "IdentityFile " + privateKey2.getAbsolutePath());
  456. }
  457. @Test
  458. public void testSshWithWrongUserNameInConfig() throws Exception {
  459. // Bug 526778
  460. cloneWith(
  461. "ssh://" + TEST_USER + "@localhost:" + testPort
  462. + "/doesntmatter",
  463. defaultCloneDir, null, //
  464. "Host localhost", //
  465. "HostName localhost", //
  466. "User sombody_else", //
  467. "IdentityFile " + privateKey1.getAbsolutePath());
  468. }
  469. @Test
  470. public void testSshWithWrongPortInConfig() throws Exception {
  471. // Bug 526778
  472. cloneWith(
  473. "ssh://" + TEST_USER + "@localhost:" + testPort
  474. + "/doesntmatter",
  475. defaultCloneDir, null, //
  476. "Host localhost", //
  477. "HostName localhost", //
  478. "Port 22", //
  479. "User " + TEST_USER, //
  480. "IdentityFile " + privateKey1.getAbsolutePath());
  481. }
  482. @Test
  483. public void testSshWithAliasInConfig() throws Exception {
  484. // Bug 531118
  485. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  486. "Host git", //
  487. "HostName localhost", //
  488. "Port " + testPort, //
  489. "User " + TEST_USER, //
  490. "IdentityFile " + privateKey1.getAbsolutePath(), "", //
  491. "Host localhost", //
  492. "HostName localhost", //
  493. "Port 22", //
  494. "User someone_else", //
  495. "IdentityFile " + privateKey2.getAbsolutePath());
  496. }
  497. @Test
  498. public void testSshWithUnknownCiphersInConfig() throws Exception {
  499. // Bug 535672
  500. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  501. "Host git", //
  502. "HostName localhost", //
  503. "Port " + testPort, //
  504. "User " + TEST_USER, //
  505. "IdentityFile " + privateKey1.getAbsolutePath(), //
  506. "Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr");
  507. }
  508. @Test
  509. public void testSshWithUnknownHostKeyAlgorithmsInConfig()
  510. throws Exception {
  511. // Bug 535672
  512. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  513. "Host git", //
  514. "HostName localhost", //
  515. "Port " + testPort, //
  516. "User " + TEST_USER, //
  517. "IdentityFile " + privateKey1.getAbsolutePath(), //
  518. "HostKeyAlgorithms foobar,ssh-rsa,ssh-dss");
  519. }
  520. @Test
  521. public void testSshWithUnknownKexAlgorithmsInConfig()
  522. throws Exception {
  523. // Bug 535672
  524. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  525. "Host git", //
  526. "HostName localhost", //
  527. "Port " + testPort, //
  528. "User " + TEST_USER, //
  529. "IdentityFile " + privateKey1.getAbsolutePath(), //
  530. "KexAlgorithms foobar,diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521");
  531. }
  532. @Test
  533. public void testSshWithMinimalHostKeyAlgorithmsInConfig()
  534. throws Exception {
  535. // Bug 537790
  536. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  537. "Host git", //
  538. "HostName localhost", //
  539. "Port " + testPort, //
  540. "User " + TEST_USER, //
  541. "IdentityFile " + privateKey1.getAbsolutePath(), //
  542. "HostKeyAlgorithms ssh-rsa,ssh-dss");
  543. }
  544. @Test
  545. public void testSshWithUnknownAuthInConfig() throws Exception {
  546. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  547. "Host git", //
  548. "HostName localhost", //
  549. "Port " + testPort, //
  550. "User " + TEST_USER, //
  551. "IdentityFile " + privateKey1.getAbsolutePath(), //
  552. "PreferredAuthentications gssapi-with-mic,hostbased,publickey,keyboard-interactive,password");
  553. }
  554. @Test(expected = TransportException.class)
  555. public void testSshWithNoMatchingAuthInConfig() throws Exception {
  556. // Server doesn't do password, and anyway we set no password.
  557. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  558. "Host git", //
  559. "HostName localhost", //
  560. "Port " + testPort, //
  561. "User " + TEST_USER, //
  562. "IdentityFile " + privateKey1.getAbsolutePath(), //
  563. "PreferredAuthentications password");
  564. }
  565. @Test
  566. public void testRsaHostKeySecond() throws Exception {
  567. // See https://git.eclipse.org/r/#/c/130402/ : server has EcDSA
  568. // (preferred), RSA, we have RSA in known_hosts: client and server
  569. // should agree on RSA.
  570. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  571. copyTestResource("id_ecdsa_256", newHostKey);
  572. server.addHostKey(newHostKey.toPath(), true);
  573. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  574. "Host git", //
  575. "HostName localhost", //
  576. "Port " + testPort, //
  577. "User " + TEST_USER, //
  578. "IdentityFile " + privateKey1.getAbsolutePath());
  579. }
  580. @Test
  581. public void testEcDsaHostKey() throws Exception {
  582. // See https://git.eclipse.org/r/#/c/130402/ : server has RSA
  583. // (preferred), EcDSA, we have EcDSA in known_hosts: client and server
  584. // should agree on EcDSA.
  585. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  586. copyTestResource("id_ecdsa_256", newHostKey);
  587. server.addHostKey(newHostKey.toPath(), false);
  588. File newHostKeyPub = new File(getTemporaryDirectory(),
  589. "newhostkey.pub");
  590. copyTestResource("id_ecdsa_256.pub", newHostKeyPub);
  591. createKnownHostsFile(knownHosts, "localhost", testPort, newHostKeyPub);
  592. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  593. "Host git", //
  594. "HostName localhost", //
  595. "Port " + testPort, //
  596. "User " + TEST_USER, //
  597. "IdentityFile " + privateKey1.getAbsolutePath());
  598. }
  599. @Test
  600. public void testPasswordAuth() throws Exception {
  601. server.enablePasswordAuthentication();
  602. TestCredentialsProvider provider = new TestCredentialsProvider(
  603. TEST_USER.toUpperCase(Locale.ROOT));
  604. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  605. "Host git", //
  606. "HostName localhost", //
  607. "Port " + testPort, //
  608. "User " + TEST_USER, //
  609. "PreferredAuthentications password");
  610. }
  611. @Test
  612. public void testPasswordAuthSeveralTimes() throws Exception {
  613. server.enablePasswordAuthentication();
  614. TestCredentialsProvider provider = new TestCredentialsProvider(
  615. "wrongpass", "wrongpass", TEST_USER.toUpperCase(Locale.ROOT));
  616. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  617. "Host git", //
  618. "HostName localhost", //
  619. "Port " + testPort, //
  620. "User " + TEST_USER, //
  621. "PreferredAuthentications password");
  622. }
  623. @Test(expected = TransportException.class)
  624. public void testPasswordAuthWrongPassword() throws Exception {
  625. server.enablePasswordAuthentication();
  626. TestCredentialsProvider provider = new TestCredentialsProvider(
  627. "wrongpass");
  628. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  629. "Host git", //
  630. "HostName localhost", //
  631. "Port " + testPort, //
  632. "User " + TEST_USER, //
  633. "PreferredAuthentications password");
  634. }
  635. @Test(expected = TransportException.class)
  636. public void testPasswordAuthNoPassword() throws Exception {
  637. server.enablePasswordAuthentication();
  638. TestCredentialsProvider provider = new TestCredentialsProvider();
  639. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  640. "Host git", //
  641. "HostName localhost", //
  642. "Port " + testPort, //
  643. "User " + TEST_USER, //
  644. "PreferredAuthentications password");
  645. }
  646. @Test(expected = TransportException.class)
  647. public void testPasswordAuthCorrectPasswordTooLate() throws Exception {
  648. server.enablePasswordAuthentication();
  649. TestCredentialsProvider provider = new TestCredentialsProvider(
  650. "wrongpass", "wrongpass", "wrongpass",
  651. TEST_USER.toUpperCase(Locale.ROOT));
  652. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  653. "Host git", //
  654. "HostName localhost", //
  655. "Port " + testPort, //
  656. "User " + TEST_USER, //
  657. "PreferredAuthentications password");
  658. }
  659. @Test
  660. public void testKeyboardInteractiveAuth() throws Exception {
  661. server.enableKeyboardInteractiveAuthentication();
  662. TestCredentialsProvider provider = new TestCredentialsProvider(
  663. TEST_USER.toUpperCase(Locale.ROOT));
  664. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  665. "Host git", //
  666. "HostName localhost", //
  667. "Port " + testPort, //
  668. "User " + TEST_USER, //
  669. "PreferredAuthentications keyboard-interactive");
  670. }
  671. @Test
  672. public void testKeyboardInteractiveAuthSeveralTimes() throws Exception {
  673. server.enableKeyboardInteractiveAuthentication();
  674. TestCredentialsProvider provider = new TestCredentialsProvider(
  675. "wrongpass", "wrongpass", TEST_USER.toUpperCase(Locale.ROOT));
  676. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  677. "Host git", //
  678. "HostName localhost", //
  679. "Port " + testPort, //
  680. "User " + TEST_USER, //
  681. "PreferredAuthentications keyboard-interactive");
  682. }
  683. @Test(expected = TransportException.class)
  684. public void testKeyboardInteractiveAuthWrongPassword() throws Exception {
  685. server.enableKeyboardInteractiveAuthentication();
  686. TestCredentialsProvider provider = new TestCredentialsProvider(
  687. "wrongpass");
  688. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  689. "Host git", //
  690. "HostName localhost", //
  691. "Port " + testPort, //
  692. "User " + TEST_USER, //
  693. "PreferredAuthentications keyboard-interactive");
  694. }
  695. @Test(expected = TransportException.class)
  696. public void testKeyboardInteractiveAuthNoPassword() throws Exception {
  697. server.enableKeyboardInteractiveAuthentication();
  698. TestCredentialsProvider provider = new TestCredentialsProvider();
  699. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  700. "Host git", //
  701. "HostName localhost", //
  702. "Port " + testPort, //
  703. "User " + TEST_USER, //
  704. "PreferredAuthentications keyboard-interactive");
  705. }
  706. @Test(expected = TransportException.class)
  707. public void testKeyboardInteractiveAuthCorrectPasswordTooLate()
  708. throws Exception {
  709. server.enableKeyboardInteractiveAuthentication();
  710. TestCredentialsProvider provider = new TestCredentialsProvider(
  711. "wrongpass", "wrongpass", "wrongpass",
  712. TEST_USER.toUpperCase(Locale.ROOT));
  713. cloneWith("ssh://git/doesntmatter", defaultCloneDir, provider, //
  714. "Host git", //
  715. "HostName localhost", //
  716. "Port " + testPort, //
  717. "User " + TEST_USER, //
  718. "PreferredAuthentications keyboard-interactive");
  719. }
  720. @Theory
  721. public void testSshKeys(String keyName) throws Exception {
  722. // JSch fails on ECDSA 384/521 keys. Compare
  723. // https://sourceforge.net/p/jsch/patches/10/
  724. assumeTrue(!(isJsch() && (keyName.contains("ed25519")
  725. || keyName.startsWith("id_ecdsa_384")
  726. || keyName.startsWith("id_ecdsa_521"))));
  727. File cloned = new File(getTemporaryDirectory(), "cloned");
  728. String keyFileName = keyName + "_key";
  729. File privateKey = new File(sshDir, keyFileName);
  730. copyTestResource(keyName, privateKey);
  731. File publicKey = new File(sshDir, keyFileName + ".pub");
  732. copyTestResource(keyName + ".pub", publicKey);
  733. server.setTestUserPublicKey(publicKey.toPath());
  734. TestCredentialsProvider provider = new TestCredentialsProvider(
  735. "testpass");
  736. pushTo(provider,
  737. cloneWith("ssh://localhost/doesntmatter", //
  738. cloned, provider, //
  739. "Host localhost", //
  740. "HostName localhost", //
  741. "Port " + testPort, //
  742. "User " + TEST_USER, //
  743. "IdentityFile " + privateKey.getAbsolutePath()));
  744. int expectedCalls = keyName.endsWith("testpass") ? 1 : 0;
  745. assertEquals("Unexpected calls to CredentialsProvider", expectedCalls,
  746. provider.getLog().size());
  747. // Should now also work without credentials provider, even if the key
  748. // was encrypted.
  749. cloned = new File(getTemporaryDirectory(), "cloned2");
  750. pushTo(null,
  751. cloneWith("ssh://localhost/doesntmatter", //
  752. cloned, null, //
  753. "Host localhost", //
  754. "HostName localhost", //
  755. "Port " + testPort, //
  756. "User " + TEST_USER, //
  757. "IdentityFile " + privateKey.getAbsolutePath()));
  758. }
  759. }