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.

NoFilesSshBuilderTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 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 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, creating the factory via the builder API.
  43. */
  44. public class NoFilesSshBuilderTest extends SshTestHarness {
  45. private PublicKey testServerKey;
  46. private KeyPair testUserKey;
  47. @Override
  48. protected SshSessionFactory createSessionFactory() {
  49. return new SshdSessionFactoryBuilder() //
  50. .setConfigStoreFactory((h, f, u) -> null)
  51. .setDefaultKeysProvider(f -> new KeyAuthenticator())
  52. .setServerKeyDatabase((h, s) -> new ServerKeyDatabase() {
  53. @Override
  54. public List<PublicKey> lookup(String connectAddress,
  55. InetSocketAddress remoteAddress,
  56. Configuration config) {
  57. return Collections.singletonList(testServerKey);
  58. }
  59. @Override
  60. public boolean accept(String connectAddress,
  61. InetSocketAddress remoteAddress,
  62. PublicKey serverKey, Configuration config,
  63. CredentialsProvider provider) {
  64. return KeyUtils.compareKeys(serverKey, testServerKey);
  65. }
  66. }) //
  67. .setPreferredAuthentications("publickey")
  68. .setHomeDirectory(FS.DETECTED.userHome())
  69. .setSshDirectory(sshDir) //
  70. .build(new JGitKeyCache());
  71. }
  72. private class KeyAuthenticator
  73. implements KeyIdentityProvider, Iterable<KeyPair> {
  74. @Override
  75. public Iterator<KeyPair> iterator() {
  76. // Should not be called. The use of the Iterable interface in
  77. // SshdSessionFactory.getDefaultKeys() made sense in sshd 2.0.0,
  78. // but sshd 2.2.0 added the SessionContext, which although good
  79. // (without it we couldn't check here) breaks the Iterable analogy.
  80. // But we're stuck now with that interface for getDefaultKeys, and
  81. // so this override throwing an exception is unfortunately needed.
  82. throw new UnsupportedOperationException();
  83. }
  84. @Override
  85. public Iterable<KeyPair> loadKeys(SessionContext session)
  86. throws IOException, GeneralSecurityException {
  87. if (!TEST_USER.equals(session.getUsername())) {
  88. return Collections.emptyList();
  89. }
  90. SshdSocketAddress remoteAddress = SshdSocketAddress
  91. .toSshdSocketAddress(session.getRemoteAddress());
  92. switch (remoteAddress.getHostName()) {
  93. case "localhost":
  94. case "127.0.0.1":
  95. return Collections.singletonList(testUserKey);
  96. default:
  97. return Collections.emptyList();
  98. }
  99. }
  100. }
  101. @After
  102. public void cleanUp() {
  103. testServerKey = null;
  104. testUserKey = null;
  105. }
  106. @Override
  107. protected void installConfig(String... config) {
  108. File configFile = new File(sshDir, Constants.CONFIG);
  109. if (config != null) {
  110. try {
  111. Files.write(configFile.toPath(), Arrays.asList(config));
  112. } catch (IOException e) {
  113. throw new UncheckedIOException(e);
  114. }
  115. }
  116. }
  117. private KeyPair load(Path path) throws Exception {
  118. try (InputStream in = Files.newInputStream(path)) {
  119. return SecurityUtils
  120. .loadKeyPairIdentities(null,
  121. NamedResource.ofName(path.toString()), in, null)
  122. .iterator().next();
  123. }
  124. }
  125. @Test
  126. public void testCloneWithBuiltInKeys() throws Exception {
  127. // This test should fail unless our in-memory setup is taken: no
  128. // known_hosts file, and a config that specifies a non-existing key.
  129. File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
  130. copyTestResource("id_ed25519", newHostKey);
  131. server.addHostKey(newHostKey.toPath(), true);
  132. testServerKey = load(newHostKey.toPath()).getPublic();
  133. assertTrue(newHostKey.delete());
  134. testUserKey = load(privateKey1.getAbsoluteFile().toPath());
  135. assertNotNull(testServerKey);
  136. assertNotNull(testUserKey);
  137. cloneWith(
  138. "ssh://" + TEST_USER + "@localhost:" + testPort
  139. + "/doesntmatter",
  140. new File(getTemporaryDirectory(), "cloned"), null, //
  141. "Host localhost", //
  142. "IdentityFile "
  143. + new File(sshDir, "does_not_exist").getAbsolutePath());
  144. }
  145. }