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

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