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.3KB

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