/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.coheigea.camel.scp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.camel.spring.Main; import org.apache.commons.io.IOUtils; import org.apache.mina.util.AvailablePortFinder; import org.apache.sshd.SshServer; import org.apache.sshd.server.command.ScpCommandFactory; import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; import org.junit.After; import org.junit.Before; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.UserInfo; /** * This test uses the Apache Camel File component to take XML files from * src/test/resources/scp_data. These files are then copied using SCP to a directory on * the SSH server (target/storage) using the camel-jsch component. */ public class SCPTest extends org.junit.Assert { private static final String SCP_ROOT_DIR = "target/test-classes"; private static final String KNOWN_HOSTS = "known_hosts"; private SshServer sshServer; @Before public void setup() throws Exception { int port = AvailablePortFinder.getNextAvailable(10000); // Write port number to configuration file in target String basedir = System.getProperty("basedir"); if (basedir == null) { basedir = new File(".").getCanonicalPath(); } // Read in camel configuration file and substitute in the correct port File f = new File(basedir + "/src/test/resources/camel-scp.xml"); FileInputStream inputStream = new FileInputStream(f); String content = IOUtils.toString(inputStream, "UTF-8"); inputStream.close(); content = content.replaceAll("portno", "" + port); File f2 = new File(basedir + "/target/test-classes/camel-scp.xml"); FileOutputStream outputStream = new FileOutputStream(f2); IOUtils.write(content, outputStream, "UTF-8"); outputStream.close(); sshServer = SshServer.setUpDefaultServer(); sshServer.setPort(port); // Generate a key sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("target/generatedkey.pem")); sshServer.setCommandFactory(new ScpCommandFactory()); sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator()); sshServer.start(); setupKnownHosts(port); // Create "storage" directory (delete it first if it exists) File storageDirectory = new File(basedir + "/target/storage"); if (storageDirectory.exists()) { storageDirectory.delete(); } storageDirectory.mkdir(); } // Taken from Apache Camel test source private void setupKnownHosts(int port) { String knownHostsFile = SCP_ROOT_DIR + "/" + KNOWN_HOSTS; // For security reasons (avoiding man in the middle attacks), // camel-jsch will only connect to known hosts. For unit testing // we use a known key, but since the port is dynamic, the // known_hosts file will be generated by the following code and // should contain a line like below (if // "HashKnownHosts"=="yes" the hostname:port part will be // hashed and look a bit more complicated). // // [localhost]:21000 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDd \ // fIWeSV4o68dRrKSzFd/Bk51E65UTmmSrmW0O1ohtzi6HzsDPjXgCtlTt3F \ // qTcfFfI92IlTr4JWqC9UK1QT1ZTeng0MkPQmv68hDANHbt5CpETZHjW5q4 \ // OOgWhVvj5IyOC2NZHtKlJBkdsMAa15ouOOJLzBvAvbqOR/yUROsEiQ== JSch jsch = new JSch(); try { jsch.setKnownHosts(knownHostsFile); Session s = jsch.getSession("alice", "localhost", port); s.setConfig("StrictHostKeyChecking", "ask"); // TODO: by the current jsch (0.1.51) setting "HashKnownHosts" to "no" is a workaround // to make the tests run green, see also http://sourceforge.net/p/jsch/bugs/63/ s.setConfig("HashKnownHosts", "no"); s.setUserInfo(new UserInfo() { @Override public String getPassphrase() { return null; } @Override public String getPassword() { return "security"; } @Override public boolean promptPassword(String message) { return true; } @Override public boolean promptPassphrase(String message) { return false; } @Override public boolean promptYesNo(String message) { // accept host authenticity return true; } @Override public void showMessage(String message) { } }); // in the process of connecting, "[localhost]:<port>" is added to the knownHostsFile s.connect(); s.disconnect(); } catch (JSchException e) { e.printStackTrace(); } } @After public void cleanup() throws Exception { if (sshServer != null) { sshServer.stop(true); } } @org.junit.Test public void testSCP() throws Exception { // Start up the Camel route Main main = new Main(); main.setApplicationContextUri("camel-scp.xml"); main.start(); // Sleep to allow time to copy the files etc. Thread.sleep(10 * 1000); main.stop(); } }