use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.
the class SftpSessionFactoryTests method createServerAndClient.
private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException {
server.setPublickeyAuthenticator((username, key, session) -> true);
server.setPort(0);
server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
server.start();
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost");
f.setPort(server.getPort());
f.setUser("user");
Resource privateKey = new ClassPathResource("id_rsa");
f.setPrivateKey(privateKey);
return f;
}
use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.
the class SftpSessionFactoryTests method testConnectFailSocketOpen.
/*
* Verify the socket is closed if the channel.connect() fails.
* INT-3305
*/
@Test
public void testConnectFailSocketOpen() throws Exception {
SshServer server = SshServer.setUpDefaultServer();
try {
server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
server.setPort(0);
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
server.start();
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost");
f.setPort(server.getPort());
f.setUser("user");
f.setPassword("pass");
int n = 0;
while (true) {
try {
f.getSession();
fail("Expected Exception");
} catch (Exception e) {
if (e instanceof IllegalStateException && "failed to create SFTP Session".equals(e.getMessage())) {
if (e.getCause() instanceof IllegalStateException) {
if (e.getCause().getCause() instanceof JSchException) {
if (e.getCause().getCause().getCause() instanceof ConnectException) {
assertTrue("Server failed to start in 10 seconds", n++ < 100);
Thread.sleep(100);
continue;
}
}
}
}
assertThat(e, instanceOf(IllegalStateException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause().getMessage(), equalTo("failed to connect"));
break;
}
}
n = 0;
while (n++ < 100 && server.getActiveSessions().size() > 0) {
Thread.sleep(100);
}
assertEquals(0, server.getActiveSessions().size());
} finally {
server.stop(true);
}
}
use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project spring-integration by spring-projects.
the class SftpTestSupport method createServer.
@BeforeClass
public static void createServer() throws Exception {
server = SshServer.setUpDefaultServer();
server.setPasswordAuthenticator((username, password, session) -> true);
server.setPort(0);
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
server.setFileSystemFactory(new VirtualFileSystemFactory(remoteTemporaryFolder.getRoot().toPath()));
server.start();
port = server.getPort();
}
use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project gerrit by GerritCodeReview.
the class HostKeyProvider method get.
@Override
public KeyPairProvider get() {
Path objKey = site.ssh_key;
Path rsaKey = site.ssh_rsa;
Path ecdsaKey_256 = site.ssh_ecdsa_256;
Path ecdsaKey_384 = site.ssh_ecdsa_384;
Path ecdsaKey_521 = site.ssh_ecdsa_521;
Path ed25519Key = site.ssh_ed25519;
final List<Path> stdKeys = new ArrayList<>(6);
if (Files.exists(rsaKey)) {
stdKeys.add(rsaKey);
}
if (Files.exists(ecdsaKey_256)) {
stdKeys.add(ecdsaKey_256);
}
if (Files.exists(ecdsaKey_384)) {
stdKeys.add(ecdsaKey_384);
}
if (Files.exists(ecdsaKey_521)) {
stdKeys.add(ecdsaKey_521);
}
if (Files.exists(ed25519Key)) {
stdKeys.add(ed25519Key);
}
if (Files.exists(objKey)) {
if (stdKeys.isEmpty()) {
SimpleGeneratorHostKeyProvider p = new SimpleGeneratorHostKeyProvider();
p.setAlgorithm(KeyUtils.RSA_ALGORITHM);
p.setPath(objKey.toAbsolutePath());
logger.atWarning().log("Defaulting to RSA algorithm for SSH key exchange." + "This is a weak security setting, consider changing it (see 'sshd.kex' documentation section).");
return p;
}
// Both formats of host key exist, we don't know which format
// should be authoritative. Complain and abort.
//
stdKeys.add(objKey);
throw new ProvisionException("Multiple host keys exist: " + stdKeys);
}
if (stdKeys.isEmpty()) {
throw new ProvisionException("No SSH keys under " + site.etc_dir);
}
FileKeyPairProvider kp = new FileKeyPairProvider();
kp.setPaths(stdKeys);
return kp;
}
use of org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider in project tomee by apache.
the class SSHServer method start.
@Override
public void start() throws ServiceException {
sshServer = SshServer.setUpDefaultServer();
sshServer.setPort(port);
sshServer.setHost(bind);
final String basePath = SystemInstance.get().getBase().getDirectory().getAbsolutePath();
if (SecurityUtils.isBouncyCastleRegistered()) {
sshServer.setKeyPairProvider(new BouncyCastleGeneratorHostKeyProvider(new File(basePath, KEY_NAME + ".pem").toPath()));
} else {
sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(basePath, KEY_NAME + ".ser").toPath()));
}
final OpenEJBShellFactory sf = new OpenEJBShellFactory(bind, port);
sshServer.setShellFactory(sf);
final JaasPasswordAuthenticator authenticator = new OpenEJBJaasPasswordAuthenticator();
authenticator.setDomain(domain);
sshServer.setPasswordAuthenticator(authenticator);
try {
sshServer.start();
} catch (IOException e) {
// no-op
}
}
Aggregations