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.

NoFilesSshTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2019 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.assertNotNull;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.UncheckedIOException;
  17. import java.net.InetSocketAddress;
  18. import java.nio.file.Files;
  19. import java.nio.file.Path;
  20. import java.security.GeneralSecurityException;
  21. import java.security.KeyPair;
  22. import java.security.PublicKey;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import org.apache.sshd.common.NamedResource;
  28. import org.apache.sshd.common.config.keys.KeyUtils;
  29. import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
  30. import org.apache.sshd.common.session.SessionContext;
  31. import org.apache.sshd.common.util.net.SshdSocketAddress;
  32. import org.apache.sshd.common.util.security.SecurityUtils;
  33. import org.eclipse.jgit.lib.Constants;
  34. import org.eclipse.jgit.transport.CredentialsProvider;
  35. import org.eclipse.jgit.transport.SshSessionFactory;
  36. import org.eclipse.jgit.transport.ssh.SshTestHarness;
  37. import org.eclipse.jgit.util.FS;
  38. import org.junit.After;
  39. import org.junit.Test;
  40. /**
  41. * Test for using the SshdSessionFactory without files in ~/.ssh but with an
  42. * in-memory setup.
  43. */
  44. public class NoFilesSshTest extends SshTestHarness {
  45. private PublicKey testServerKey;
  46. private KeyPair testUserKey;
  47. @Override
  48. protected SshSessionFactory createSessionFactory() {
  49. SshdSessionFactory result = new SshdSessionFactory(new JGitKeyCache(),
  50. null) {
  51. @Override
  52. protected File getSshConfig(File dir) {
  53. return null;
  54. }
  55. @Override
  56. protected ServerKeyDatabase getServerKeyDatabase(File homeDir,
  57. File dir) {
  58. return new ServerKeyDatabase() {
  59. @Override
  60. public List<PublicKey> lookup(String connectAddress,
  61. InetSocketAddress remoteAddress,
  62. Configuration config) {
  63. return Collections.singletonList(testServerKey);
  64. }
  65. @Override
  66. public boolean accept(String connectAddress,
  67. InetSocketAddress remoteAddress,
  68. PublicKey serverKey, Configuration config,
  69. CredentialsProvider provider) {
  70. return KeyUtils.compareKeys(serverKey, testServerKey);
  71. }
  72. };
  73. }
  74. @Override
  75. protected Iterable<KeyPair> getDefaultKeys(File dir) {
  76. // This would work for this simple test case:
  77. // return Collections.singletonList(testUserKey);
  78. // But let's see if we can check the host and username that's used.
  79. // For that, we need access to the sshd SessionContext:
  80. return new KeyAuthenticator();
  81. }
  82. @Override
  83. protected String getDefaultPreferredAuthentications() {
  84. return "publickey";
  85. }
  86. };
  87. // The home directory is mocked at this point!
  88. result.setHomeDirectory(FS.DETECTED.userHome());
  89. result.setSshDirectory(sshDir);
  90. return result;
  91. }
  92. private class KeyAuthenticator implements KeyIdentityProvider, Iterable<KeyPair> {
  93. @Override
  94. public Iterator<KeyPair> iterator() {
  95. // Should not be called. The use of the Iterable interface in
  96. // SshdSessionFactory.getDefaultKeys() made sense in sshd 2.0.0,
  97. // but sshd 2.2.0 added the SessionContext, which although good
  98. // (without it we couldn't check here) breaks the Iterable analogy.
  99. // But we're stuck now with that interface for getDefaultKeys, and
  100. // so this override throwing an exception is unfortunately needed.
  101. throw new UnsupportedOperationException();
  102. }
  103. @Override
  104. public Iterable<KeyPair> loadKeys(SessionContext session)
  105. throws IOException, GeneralSecurityException {
  106. if (!TEST_USER.equals(session.getUsername())) {
  107. return Collections.emptyList();
  108. }
  109. SshdSocketAddress remoteAddress = SshdSocketAddress
  110. .toSshdSocketAddress(session.getRemoteAddress());
  111. switch (remoteAddress.getHostName()) {
  112. case "localhost":
  113. case "127.0.0.1":
  114. return Collections.singletonList(testUserKey);
  115. default:
  116. return Collections.emptyList();
  117. }
  118. }
  119. }
  120. @After
  121. public void cleanUp() {
  122. testServerKey = null;
  123. testUserKey = null;
  124. }
  125. @Override
  126. protected void installConfig(String... config) {
  127. File configFile = new File(sshDir, Constants.CONFIG);
  128. if (config != null) {
  129. try {
  130. Files.write(configFile.toPath(), Arrays.asList(config));
  131. } catch (IOException e) {
  132. throw new UncheckedIOException(e);
  133. }
  134. }
  135. }
  136. private KeyPair load(Path path) throws Exception {
  137. try (InputStream in = Files.newInputStream(path)) {
  138. return SecurityUtils
  139. .loadKeyPairIdentities(null,
  140. NamedResource.ofName(path.toString()), in, null)
  141. .iterator().next();
  142. }
  143. }
  144. @Test
  145. public void testCloneWithBuiltInKeys() throws Exception {
  146. // This test should fail unless our in-memory setup is taken: no
  147. // known_hosts file, and a config that specifies a non-existing key.
  148. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  149. copyTestResource("id_ed25519", newHostKey);
  150. server.addHostKey(newHostKey.toPath(), true);
  151. testServerKey = load(newHostKey.toPath()).getPublic();
  152. assertTrue(newHostKey.delete());
  153. testUserKey = load(privateKey1.getAbsoluteFile().toPath());
  154. assertNotNull(testServerKey);
  155. assertNotNull(testUserKey);
  156. cloneWith(
  157. "ssh://" + TEST_USER + "@localhost:" + testPort
  158. + "/doesntmatter",
  159. new File(getTemporaryDirectory(), "cloned"), null, //
  160. "Host localhost", //
  161. "IdentityFile "
  162. + new File(sshDir, "does_not_exist").getAbsolutePath());
  163. }
  164. }