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

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