Commit 8650f6e7 authored by Maxim's avatar Maxim
Browse files

(ftp) Require FTPS

parent 2626d645
......@@ -15,6 +15,7 @@
*/
package org.genesys.filerepository.service.ftp;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
......@@ -28,6 +29,7 @@ import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.message.MessageResource;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
......@@ -56,8 +58,20 @@ public class RepositoryFtpServer implements InitializingBean, DisposableBean {
// Idle timeout
private int idleTimeout;
private String keystorePath;
private String keystorePsw;
private FtpServer server = null;
public void setKeystorePath(final String keystorePath) {
this.keystorePath = keystorePath;
}
public void setKeystorePsw(final String keystorePsw) {
this.keystorePsw = keystorePsw;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
......@@ -84,6 +98,16 @@ public class RepositoryFtpServer implements InitializingBean, DisposableBean {
// set idle timeout
factory.setIdleTimeout(idleTimeout);
// define SSL configuration
SslConfigurationFactory ssl = new SslConfigurationFactory();
// Create store: keytool -genkey -alias testdomain -keyalg RSA -keystore ftpserver.jks -keysize 4096
ssl.setKeystoreFile(new File(keystorePath));
ssl.setKeystorePassword(keystorePsw);
// set the SSL configuration for the listener
factory.setSslConfiguration(ssl.createSslConfiguration());
factory.setImplicitSsl(true);
// replace the default listener
serverFactory.addListener("default", factory.createListener());
}
......
......@@ -39,6 +39,8 @@ public class ApplicationConfig {
RepositoryFtpServer ftpServer = new RepositoryFtpServer();
ftpServer.setFtpPort(8021);
ftpServer.setUserManager(userManager());
ftpServer.setKeystorePath("ftpserver.jks");
ftpServer.setKeystorePsw("genesys");
return ftpServer;
}
......
......@@ -23,6 +23,7 @@ import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
......@@ -35,31 +36,40 @@ public class FtpServerTest {
private String username = "user";
private String password = username + "1!";
@Test
public void serverListening() throws SocketException, IOException {
@Test(expected = IOException.class)
public void serverListening() throws IOException {
final FTPClient ftp = new FTPClient();
try {
ftp.setConnectTimeout(2000);
ftp.connect("localhost", 8021);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.disconnect();
}
@Test
public void serverListeningFTPS() throws SocketException, IOException {
final FTPSClient ftpsClient = new FTPSClient(true);
try {
ftpsClient.setConnectTimeout(2000);
ftpsClient.connect("localhost", 8021);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftpsClient.getReplyCode()), is(true));
} finally {
ftp.disconnect();
ftpsClient.disconnect();
}
}
@Test
public void userLogin() throws SocketException, IOException {
final FTPClient ftp = new FTPClient();
public void userLoginFTPS() throws SocketException, IOException {
final FTPSClient ftpsClient = new FTPSClient(true);
try {
ftp.connect("localhost", 8021);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftpsClient.setConnectTimeout(2000);
ftpsClient.connect("localhost", 8021);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftpsClient.getReplyCode()), is(true));
ftp.login(username, password);
assertThat("Login failed", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftpsClient.login(username, password);
assertThat("Login failed", FTPReply.isPositiveCompletion(ftpsClient.getReplyCode()), is(true));
} finally {
ftp.disconnect();
ftpsClient.disconnect();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment