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.

ApacheSshTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.transport.sshd;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertThrows;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.io.UncheckedIOException;
  18. import java.nio.file.Files;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.stream.Collectors;
  22. import org.apache.sshd.client.config.hosts.KnownHostEntry;
  23. import org.apache.sshd.common.PropertyResolverUtils;
  24. import org.apache.sshd.server.ServerFactoryManager;
  25. import org.eclipse.jgit.api.Git;
  26. import org.eclipse.jgit.api.errors.TransportException;
  27. import org.eclipse.jgit.junit.ssh.SshTestBase;
  28. import org.eclipse.jgit.lib.Constants;
  29. import org.eclipse.jgit.transport.SshSessionFactory;
  30. import org.eclipse.jgit.util.FS;
  31. import org.junit.Test;
  32. import org.junit.experimental.theories.Theories;
  33. import org.junit.runner.RunWith;
  34. @RunWith(Theories.class)
  35. public class ApacheSshTest extends SshTestBase {
  36. @Override
  37. protected SshSessionFactory createSessionFactory() {
  38. SshdSessionFactory result = new SshdSessionFactory(new JGitKeyCache(),
  39. null);
  40. // The home directory is mocked at this point!
  41. result.setHomeDirectory(FS.DETECTED.userHome());
  42. result.setSshDirectory(sshDir);
  43. return result;
  44. }
  45. @Override
  46. protected void installConfig(String... config) {
  47. File configFile = new File(sshDir, Constants.CONFIG);
  48. if (config != null) {
  49. try {
  50. Files.write(configFile.toPath(), Arrays.asList(config));
  51. } catch (IOException e) {
  52. throw new UncheckedIOException(e);
  53. }
  54. }
  55. }
  56. @Test
  57. public void testEd25519HostKey() throws Exception {
  58. // Using ed25519 user identities is tested in the super class in
  59. // testSshKeys().
  60. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  61. copyTestResource("id_ed25519", newHostKey);
  62. server.addHostKey(newHostKey.toPath(), true);
  63. File newHostKeyPub = new File(getTemporaryDirectory(),
  64. "newhostkey.pub");
  65. copyTestResource("id_ed25519.pub", newHostKeyPub);
  66. createKnownHostsFile(knownHosts, "localhost", testPort, newHostKeyPub);
  67. cloneWith("ssh://git/doesntmatter", defaultCloneDir, null, //
  68. "Host git", //
  69. "HostName localhost", //
  70. "Port " + testPort, //
  71. "User " + TEST_USER, //
  72. "IdentityFile " + privateKey1.getAbsolutePath());
  73. }
  74. @Test
  75. public void testHashedKnownHosts() throws Exception {
  76. assertTrue("Failed to delete known_hosts", knownHosts.delete());
  77. // The provider will answer "yes" to all questions, so we should be able
  78. // to connect and end up with a new known_hosts file with the host key.
  79. TestCredentialsProvider provider = new TestCredentialsProvider();
  80. cloneWith("ssh://localhost/doesntmatter", defaultCloneDir, provider, //
  81. "HashKnownHosts yes", //
  82. "Host localhost", //
  83. "HostName localhost", //
  84. "Port " + testPort, //
  85. "User " + TEST_USER, //
  86. "IdentityFile " + privateKey1.getAbsolutePath());
  87. List<LogEntry> messages = provider.getLog();
  88. assertFalse("Expected user interaction", messages.isEmpty());
  89. assertEquals(
  90. "Expected to be asked about the key, and the file creation", 2,
  91. messages.size());
  92. assertTrue("~/.ssh/known_hosts should exist now", knownHosts.exists());
  93. // Let's clone again without provider. If it works, the server host key
  94. // was written correctly.
  95. File clonedAgain = new File(getTemporaryDirectory(), "cloned2");
  96. cloneWith("ssh://localhost/doesntmatter", clonedAgain, null, //
  97. "Host localhost", //
  98. "HostName localhost", //
  99. "Port " + testPort, //
  100. "User " + TEST_USER, //
  101. "IdentityFile " + privateKey1.getAbsolutePath());
  102. // Check that the first line contains neither "localhost" nor
  103. // "127.0.0.1", but does contain the expected hash.
  104. List<String> lines = Files.readAllLines(knownHosts.toPath()).stream()
  105. .filter(s -> s != null && s.length() >= 1 && s.charAt(0) != '#'
  106. && !s.trim().isEmpty())
  107. .collect(Collectors.toList());
  108. assertEquals("Unexpected number of known_hosts lines", 1, lines.size());
  109. String line = lines.get(0);
  110. assertFalse("Found host in line", line.contains("localhost"));
  111. assertFalse("Found IP in line", line.contains("127.0.0.1"));
  112. assertTrue("Hash not found", line.contains("|"));
  113. KnownHostEntry entry = KnownHostEntry.parseKnownHostEntry(line);
  114. assertTrue("Hash doesn't match localhost",
  115. entry.isHostMatch("localhost", testPort)
  116. || entry.isHostMatch("127.0.0.1", testPort));
  117. }
  118. @Test
  119. public void testPreamble() throws Exception {
  120. // Test that the client can deal with strange lines being sent before
  121. // the server identification string.
  122. StringBuilder b = new StringBuilder();
  123. for (int i = 0; i < 257; i++) {
  124. b.append('a');
  125. }
  126. server.setPreamble("A line with a \000 NUL",
  127. "A long line: " + b.toString());
  128. cloneWith(
  129. "ssh://" + TEST_USER + "@localhost:" + testPort
  130. + "/doesntmatter",
  131. defaultCloneDir, null,
  132. "IdentityFile " + privateKey1.getAbsolutePath());
  133. }
  134. @Test
  135. public void testLongPreamble() throws Exception {
  136. // Test that the client can deal with a long (about 60k) preamble.
  137. StringBuilder b = new StringBuilder();
  138. for (int i = 0; i < 1024; i++) {
  139. b.append('a');
  140. }
  141. String line = b.toString();
  142. String[] lines = new String[60];
  143. for (int i = 0; i < lines.length; i++) {
  144. lines[i] = line;
  145. }
  146. server.setPreamble(lines);
  147. cloneWith(
  148. "ssh://" + TEST_USER + "@localhost:" + testPort
  149. + "/doesntmatter",
  150. defaultCloneDir, null,
  151. "IdentityFile " + privateKey1.getAbsolutePath());
  152. }
  153. @Test
  154. public void testHugePreamble() throws Exception {
  155. // Test that the connection fails when the preamble is longer than 64k.
  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[70];
  162. for (int i = 0; i < lines.length; i++) {
  163. lines[i] = line;
  164. }
  165. server.setPreamble(lines);
  166. TransportException e = assertThrows(TransportException.class,
  167. () -> cloneWith(
  168. "ssh://" + TEST_USER + "@localhost:" + testPort
  169. + "/doesntmatter",
  170. defaultCloneDir, null,
  171. "IdentityFile " + privateKey1.getAbsolutePath()));
  172. // The assertions test that we don't run into bug 565394 / SSHD-1050
  173. assertFalse(e.getMessage().contains("timeout"));
  174. assertTrue(e.getMessage().contains("65536")
  175. || e.getMessage().contains("closed"));
  176. }
  177. /**
  178. * Test for SSHD-1028. If the server doesn't close sessions, the second
  179. * fetch will fail. Occurs on sshd 2.5.[01].
  180. *
  181. * @throws Exception
  182. * on errors
  183. * @see <a href=
  184. * "https://issues.apache.org/jira/projects/SSHD/issues/SSHD-1028">SSHD-1028</a>
  185. */
  186. @Test
  187. public void testCloneAndFetchWithSessionLimit() throws Exception {
  188. PropertyResolverUtils.updateProperty(server.getPropertyResolver(),
  189. ServerFactoryManager.MAX_CONCURRENT_SESSIONS, 2);
  190. File localClone = cloneWith("ssh://localhost/doesntmatter",
  191. defaultCloneDir, null, //
  192. "Host localhost", //
  193. "HostName localhost", //
  194. "Port " + testPort, //
  195. "User " + TEST_USER, //
  196. "IdentityFile " + privateKey1.getAbsolutePath());
  197. // Fetch a couple of times
  198. try (Git git = Git.open(localClone)) {
  199. git.fetch().call();
  200. git.fetch().call();
  201. }
  202. }
  203. }