In-memory SSH keys for the "no files" sshd tests
Avoid using a key written to a file. This makes it clearer that the test does not rely on files being present. Change-Id: I31cf4f404aab5b891c32fc4bda906b7f8fe03777 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
parent
3a499606b1
commit
5a5d85a4a3
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
|
* Copyright (C) 2018, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
|
||||||
*
|
*
|
||||||
* This program and the accompanying materials are made available under the
|
* This program and the accompanying materials are made available under the
|
||||||
* terms of the Eclipse Distribution License v. 1.0 which is available at
|
* terms of the Eclipse Distribution License v. 1.0 which is available at
|
||||||
|
@ -252,12 +252,25 @@ public void addHostKey(@NonNull Path key, boolean inFront)
|
||||||
.loadKeyPairIdentities(null,
|
.loadKeyPairIdentities(null,
|
||||||
NamedResource.ofName(key.toString()), in, null)
|
NamedResource.ofName(key.toString()), in, null)
|
||||||
.iterator().next();
|
.iterator().next();
|
||||||
if (inFront) {
|
addHostKey(pair, inFront);
|
||||||
hostKeys.add(0, pair);
|
|
||||||
} else {
|
|
||||||
hostKeys.add(pair);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an additional host key to the server.
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* {@link KeyPair} to add
|
||||||
|
* @param inFront
|
||||||
|
* whether to add the new key before other existing keys
|
||||||
|
* @since 5.8
|
||||||
|
*/
|
||||||
|
public void addHostKey(@NonNull KeyPair key, boolean inFront) {
|
||||||
|
if (inFront) {
|
||||||
|
hostKeys.add(0, key);
|
||||||
|
} else {
|
||||||
|
hostKeys.add(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -322,6 +335,18 @@ public void setTestUserPublicKey(Path key)
|
||||||
.resolvePublicKey(null, PublicKeyEntryResolver.IGNORING);
|
.resolvePublicKey(null, PublicKeyEntryResolver.IGNORING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the test user's public key on the server.
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* to set
|
||||||
|
*
|
||||||
|
* @since 5.8
|
||||||
|
*/
|
||||||
|
public void setTestUserPublicKey(@NonNull PublicKey key) {
|
||||||
|
this.testKey = key;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the lines the server sends before its server identification in the
|
* Sets the lines the server sends before its server identification in the
|
||||||
* initial protocol version exchange.
|
* initial protocol version exchange.
|
||||||
|
|
|
@ -10,29 +10,25 @@
|
||||||
package org.eclipse.jgit.transport.sshd;
|
package org.eclipse.jgit.transport.sshd;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
import java.security.PublicKey;
|
import java.security.PublicKey;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.sshd.common.NamedResource;
|
|
||||||
import org.apache.sshd.common.config.keys.KeyUtils;
|
import org.apache.sshd.common.config.keys.KeyUtils;
|
||||||
import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
|
import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
|
||||||
import org.apache.sshd.common.session.SessionContext;
|
import org.apache.sshd.common.session.SessionContext;
|
||||||
import org.apache.sshd.common.util.net.SshdSocketAddress;
|
import org.apache.sshd.common.util.net.SshdSocketAddress;
|
||||||
import org.apache.sshd.common.util.security.SecurityUtils;
|
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
import org.eclipse.jgit.transport.CredentialsProvider;
|
||||||
import org.eclipse.jgit.transport.SshSessionFactory;
|
import org.eclipse.jgit.transport.SshSessionFactory;
|
||||||
|
@ -130,27 +126,20 @@ protected void installConfig(String... config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private KeyPair load(Path path) throws Exception {
|
|
||||||
try (InputStream in = Files.newInputStream(path)) {
|
|
||||||
return SecurityUtils
|
|
||||||
.loadKeyPairIdentities(null,
|
|
||||||
NamedResource.ofName(path.toString()), in, null)
|
|
||||||
.iterator().next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCloneWithBuiltInKeys() throws Exception {
|
public void testCloneWithBuiltInKeys() throws Exception {
|
||||||
// This test should fail unless our in-memory setup is taken: no
|
// This test should fail unless our in-memory setup is taken: no
|
||||||
// known_hosts file, and a config that specifies a non-existing key.
|
// known_hosts file, a config that specifies a non-existing key,
|
||||||
File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
|
// and the test is using a newly generated KeyPairs anyway.
|
||||||
copyTestResource("id_ed25519", newHostKey);
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||||
server.addHostKey(newHostKey.toPath(), true);
|
generator.initialize(2048);
|
||||||
testServerKey = load(newHostKey.toPath()).getPublic();
|
testUserKey = generator.generateKeyPair();
|
||||||
assertTrue(newHostKey.delete());
|
KeyPair hostKey = generator.generateKeyPair();
|
||||||
testUserKey = load(privateKey1.getAbsoluteFile().toPath());
|
server.addHostKey(hostKey, true);
|
||||||
|
testServerKey = hostKey.getPublic();
|
||||||
assertNotNull(testServerKey);
|
assertNotNull(testServerKey);
|
||||||
assertNotNull(testUserKey);
|
assertNotNull(testUserKey);
|
||||||
|
server.setTestUserPublicKey(testUserKey.getPublic());
|
||||||
cloneWith(
|
cloneWith(
|
||||||
"ssh://" + TEST_USER + "@localhost:" + testPort
|
"ssh://" + TEST_USER + "@localhost:" + testPort
|
||||||
+ "/doesntmatter",
|
+ "/doesntmatter",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch> and others
|
* Copyright (C) 2019, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
|
||||||
*
|
*
|
||||||
* This program and the accompanying materials are made available under the
|
* This program and the accompanying materials are made available under the
|
||||||
* terms of the Eclipse Distribution License v. 1.0 which is available at
|
* terms of the Eclipse Distribution License v. 1.0 which is available at
|
||||||
|
@ -10,29 +10,25 @@
|
||||||
package org.eclipse.jgit.transport.sshd;
|
package org.eclipse.jgit.transport.sshd;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
import java.security.PublicKey;
|
import java.security.PublicKey;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.sshd.common.NamedResource;
|
|
||||||
import org.apache.sshd.common.config.keys.KeyUtils;
|
import org.apache.sshd.common.config.keys.KeyUtils;
|
||||||
import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
|
import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
|
||||||
import org.apache.sshd.common.session.SessionContext;
|
import org.apache.sshd.common.session.SessionContext;
|
||||||
import org.apache.sshd.common.util.net.SshdSocketAddress;
|
import org.apache.sshd.common.util.net.SshdSocketAddress;
|
||||||
import org.apache.sshd.common.util.security.SecurityUtils;
|
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
import org.eclipse.jgit.transport.CredentialsProvider;
|
||||||
import org.eclipse.jgit.transport.SshSessionFactory;
|
import org.eclipse.jgit.transport.SshSessionFactory;
|
||||||
|
@ -154,27 +150,20 @@ protected void installConfig(String... config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private KeyPair load(Path path) throws Exception {
|
|
||||||
try (InputStream in = Files.newInputStream(path)) {
|
|
||||||
return SecurityUtils
|
|
||||||
.loadKeyPairIdentities(null,
|
|
||||||
NamedResource.ofName(path.toString()), in, null)
|
|
||||||
.iterator().next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCloneWithBuiltInKeys() throws Exception {
|
public void testCloneWithBuiltInKeys() throws Exception {
|
||||||
// This test should fail unless our in-memory setup is taken: no
|
// This test should fail unless our in-memory setup is taken: no
|
||||||
// known_hosts file, and a config that specifies a non-existing key.
|
// known_hosts file, a config that specifies a non-existing key,
|
||||||
File newHostKey = new File(getTemporaryDirectory(), "newhostkey");
|
// and the test is using a newly generated KeyPairs anyway.
|
||||||
copyTestResource("id_ed25519", newHostKey);
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||||
server.addHostKey(newHostKey.toPath(), true);
|
generator.initialize(2048);
|
||||||
testServerKey = load(newHostKey.toPath()).getPublic();
|
testUserKey = generator.generateKeyPair();
|
||||||
assertTrue(newHostKey.delete());
|
KeyPair hostKey = generator.generateKeyPair();
|
||||||
testUserKey = load(privateKey1.getAbsoluteFile().toPath());
|
server.addHostKey(hostKey, true);
|
||||||
|
testServerKey = hostKey.getPublic();
|
||||||
assertNotNull(testServerKey);
|
assertNotNull(testServerKey);
|
||||||
assertNotNull(testUserKey);
|
assertNotNull(testUserKey);
|
||||||
|
server.setTestUserPublicKey(testUserKey.getPublic());
|
||||||
cloneWith(
|
cloneWith(
|
||||||
"ssh://" + TEST_USER + "@localhost:" + testPort
|
"ssh://" + TEST_USER + "@localhost:" + testPort
|
||||||
+ "/doesntmatter",
|
+ "/doesntmatter",
|
||||||
|
|
Loading…
Reference in New Issue