use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project spring-integration by spring-projects.
the class SftpServerTests method testUcPw.
@Test
public void testUcPw() 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.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
new File(pathname).mkdirs();
server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
server.start();
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost");
f.setPort(server.getPort());
f.setUser("user");
f.setPassword("pass");
f.setAllowUnknownKeys(true);
Session<LsEntry> session = f.getSession();
doTest(server, session);
} finally {
server.stop(true);
}
}
use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project spring-integration by spring-projects.
the class SftpServerTests method testKeyExchange.
private void testKeyExchange(String pubKey, String privKey, String passphrase) throws Exception {
SshServer server = SshServer.setUpDefaultServer();
final PublicKey allowedKey = decodePublicKey(pubKey);
try {
server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey));
server.setPort(0);
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory()));
final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
new File(pathname).mkdirs();
server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
server.start();
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost");
f.setPort(server.getPort());
f.setUser("user");
f.setAllowUnknownKeys(true);
InputStream stream = new ClassPathResource(privKey).getInputStream();
f.setPrivateKey(new ByteArrayResource(StreamUtils.copyToByteArray(stream)));
f.setPrivateKeyPassphrase(passphrase);
Session<LsEntry> session = f.getSession();
doTest(server, session);
} finally {
server.stop(true);
}
}
use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project testcases by coheigea.
the class SFTPTest method setup.
@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-sftp.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-sftp.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(Paths.get("target/generatedkey.pem")));
List<NamedFactory<Command>> subsystemFactories = new ArrayList<NamedFactory<Command>>(1);
subsystemFactories.add(new SftpSubsystemFactory());
sshServer.setSubsystemFactories(subsystemFactories);
sshServer.setCommandFactory(new ScpCommandFactory());
sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
sshServer.start();
// Create "storage_sftp" directory (delete it first if it exists)
File storageDirectory = new File(basedir + "/target/storage_sftp");
if (storageDirectory.exists()) {
storageDirectory.delete();
}
storageDirectory.mkdir();
}
use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project karaf by apache.
the class Activator method createSshServer.
protected SshServer createSshServer(SessionFactory sessionFactory) {
int sshPort = getInt("sshPort", 8181);
String sshHost = getString("sshHost", "0.0.0.0");
long sshIdleTimeout = getLong("sshIdleTimeout", 1800000);
int nioWorkers = getInt("nio-workers", 2);
String sshRealm = getString("sshRealm", "karaf");
String sshRole = getString("sshRole", null);
String hostKey = getString("hostKey", System.getProperty("karaf.etc") + "/host.key");
String[] authMethods = getStringArray("authMethods", "keyboard-interactive,password,publickey");
int keySize = getInt("keySize", 2048);
String algorithm = getString("algorithm", "RSA");
String[] macs = getStringArray("macs", "hmac-sha2-512,hmac-sha2-256,hmac-sha1");
String[] ciphers = getStringArray("ciphers", "aes128-ctr,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc");
String[] kexAlgorithms = getStringArray("kexAlgorithms", "diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1");
String welcomeBanner = getString("welcomeBanner", null);
String moduliUrl = getString("moduli-url", null);
boolean sftpEnabled = getBoolean("sftpEnabled", true);
Path serverKeyPath = Paths.get(hostKey);
KeyPairProvider keyPairProvider = new OpenSSHKeyPairProvider(serverKeyPath.toFile(), algorithm, keySize);
KarafJaasAuthenticator authenticator = new KarafJaasAuthenticator(sshRealm, sshRole);
UserAuthFactoriesFactory authFactoriesFactory = new UserAuthFactoriesFactory();
authFactoriesFactory.setAuthMethods(authMethods);
SshServer server = SshServer.setUpDefaultServer();
server.setPort(sshPort);
server.setHost(sshHost);
server.setMacFactories(SshUtils.buildMacs(macs));
server.setCipherFactories(SshUtils.buildCiphers(ciphers));
server.setKeyExchangeFactories(SshUtils.buildKexAlgorithms(kexAlgorithms));
server.setShellFactory(new ShellFactoryImpl(sessionFactory));
if (sftpEnabled) {
server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(cmd -> new ShellCommand(sessionFactory, cmd)).build());
server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(System.getProperty("karaf.base"))));
} else {
server.setCommandFactory(cmd -> new ShellCommand(sessionFactory, cmd));
}
server.setKeyPairProvider(keyPairProvider);
server.setPasswordAuthenticator(authenticator);
server.setPublickeyAuthenticator(authenticator);
server.setUserAuthFactories(authFactoriesFactory.getFactories());
server.setAgentFactory(KarafAgentFactory.getInstance());
server.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
server.getProperties().put(SshServer.IDLE_TIMEOUT, Long.toString(sshIdleTimeout));
server.getProperties().put(SshServer.NIO_WORKERS, Integer.toString(nioWorkers));
if (moduliUrl != null) {
server.getProperties().put(SshServer.MODULI_URL, moduliUrl);
}
if (welcomeBanner != null) {
server.getProperties().put(SshServer.WELCOME_BANNER, welcomeBanner);
}
return server;
}
use of org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory in project spring-integration-samples by spring-projects.
the class EmbeddedSftpServer method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
final PublicKey allowedKey = decodePublicKey();
this.server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey));
this.server.setPort(this.port);
this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
this.server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator;
new File(pathname).mkdirs();
server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname)));
}
Aggregations