diff options
author | Michael Petretti | 2014-05-07 17:53:03 +0200 |
---|---|---|
committer | Michael Petretti | 2014-05-07 17:53:03 +0200 |
commit | 741e5d5fd03f5530ba116e9d1909d5888e470a7e (patch) | |
tree | d7a84c5556472eed8eb3c03beaaaf0762462b91f | |
parent | Merge branch 'master' of git.openslx.org:bwlp/satellite-daemon (diff) | |
download | satellite-daemon-741e5d5fd03f5530ba116e9d1909d5888e470a7e.tar.gz satellite-daemon-741e5d5fd03f5530ba116e9d1909d5888e470a7e.tar.xz satellite-daemon-741e5d5fd03f5530ba116e9d1909d5888e470a7e.zip |
small change
5 files changed, 50 insertions, 286 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 9eb1cd6..9f42623 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -1,9 +1,13 @@ package org.openslx.satellitedaemon; +import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.ConnectException; import java.nio.ByteBuffer; import java.security.InvalidKeyException; +import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; @@ -11,6 +15,11 @@ import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.UUID; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.commons.net.ftp.FTPSClient; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; @@ -27,28 +36,55 @@ import org.openslx.satellitedaemon.util.RndStringEncrypt; * */ public class App { - public static void main(String[] args) throws UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException, IOException, InvalidKeyException, SignatureException { + public static void main(String[] args) throws UnrecoverableKeyException, + NoSuchAlgorithmException, CertificateException, + FileNotFoundException, KeyStoreException, IOException, + InvalidKeyException, SignatureException { + String nilsIp = "132.230.4.23"; + int thriftPort = 9090; + int ftpPort = 2221; try { TTransport transport; - transport = new TSocket("132.230.4.23", 9090); //Nils IP + transport = new TSocket(nilsIp, thriftPort); // Nils IP transport.open(); TProtocol protocol = new TBinaryProtocol(transport); - ImageServer.Client client = new ImageServer.Client( - protocol); + ImageServer.Client client = new ImageServer.Client(protocol); String rnd = client.startServerAuthentication("uni-freiburg.de"); - System.out.println(rnd); - - RndStringEncrypt rse = new RndStringEncrypt("serverid", "password", "/home/michael/satellite-daemon/config/serverid.jks"); + System.out.println(rnd); + + RndStringEncrypt rse = new RndStringEncrypt("serverid", "password", + "/home/michael/satellite-daemon/config/serverid.jks"); byte[] byteArray = rse.encryptRndString(rnd); - ServerSessionData sSD = client.serverAuthenticate("uni-freiburg.de", ByteBuffer.wrap(byteArray)); - System.out.println(sSD.sessionId); - ImageData imDat = new ImageData(UUID.randomUUID().toString(), 113, "TestImage", 100, 105, "me", "anyThing", true, false, "theBest", "theVeryBest", 1024 ); - FtpCredentials ftpc = client.submitImage(sSD.sessionId, imDat); - - transport.close(); + ServerSessionData sSD = client.serverAuthenticate( + "uni-freiburg.de", ByteBuffer.wrap(byteArray)); + System.out.println(sSD.sessionId); + ImageData imDat = new ImageData(UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", true, false, + "theBest", "theVeryBest", 1024); + FtpCredentials ftpc = client.submitImage(sSD.sessionId, imDat); + FTPSClient ftpClient = new FTPSClient("SSL", true); + TrustManagerFactory trustManagerFactory = TrustManagerFactory + .getInstance(KeyManagerFactory.getDefaultAlgorithm()); + KeyStore keystore = KeyStore.getInstance("JKS"); + keystore.load(new FileInputStream(new File( + "/home/michael/satellite-daemon/config/ftpsid.jks")), + "password".toCharArray()); + trustManagerFactory.init(keystore); + TrustManager trustManager = trustManagerFactory.getTrustManagers()[0]; + ftpClient.setTrustManager(trustManager); + try { + ftpClient.connect( nilsIp, ftpPort ); + if (!ftpClient.login(ftpc.username, ftpc.password)) { + throw new ConnectException("Could not login."); + } + System.out.println( "Connected to " + nilsIp + ":" + ftpPort + ". Reply code: " + ftpClient.getReplyCode() ); + } finally { + ftpClient.disconnect(); + } + transport.close(); } catch (TException x) { x.printStackTrace(); } diff --git a/src/main/java/org/openslx/satellitedaemon/db/DbImage.java b/src/main/java/org/openslx/satellitedaemon/db/DbImage.java index b7fb67d..9004d6a 100644 --- a/src/main/java/org/openslx/satellitedaemon/db/DbImage.java +++ b/src/main/java/org/openslx/satellitedaemon/db/DbImage.java @@ -17,7 +17,7 @@ public class DbImage * @return list of images that are marked for upload, where the upload * was either not started yet, or is incomplete */ - public List<DbImage> getAllMarkedForUpload() + public static List<DbImage> getAllMarkedForUpload() { // TODO: Implement return new ArrayList<>(); diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java deleted file mode 100644 index d83c0ea..0000000 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java +++ /dev/null @@ -1,88 +0,0 @@ -package org.openslx.satellitedaemon.ftp; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -import org.apache.commons.net.ftp.FTPClient; - -public class FtpUpDownUtil { - /** - * FTP-Dateienliste. - * @return String-Array der Dateinamen auf dem FTP-Server - */ - public static String[] list( String host, int port, String usr, String pwd ) throws IOException - { - FTPClient ftpClient = new FTPClient(); - String[] filenameList; - - try { - ftpClient.connect( host, port ); - ftpClient.login( usr, pwd ); - filenameList = ftpClient.listNames(); - ftpClient.logout(); - } finally { - ftpClient.disconnect(); - } - - return filenameList; - } - - /** - * FTP-Client-Download. - * @return true falls ok - */ - public static boolean download( String localResultFile, String remoteSourceFile, - String host, int port, String usr, String pwd, boolean showMessages ) throws IOException - { - FTPClient ftpClient = new FTPClient(); - FileOutputStream fos = null; - boolean resultOk = true; - - try { - ftpClient.connect( host, port ); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - resultOk &= ftpClient.login( usr, pwd ); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - fos = new FileOutputStream( localResultFile ); - resultOk &= ftpClient.retrieveFile( remoteSourceFile, fos ); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - resultOk &= ftpClient.logout(); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - } finally { - try { if( fos != null ) { fos.close(); } } catch( IOException e ) {/* nothing to do */} - ftpClient.disconnect(); - } - - return resultOk; - } - - /** - * FTP-Client-Upload. - * @return true falls ok - */ - public static boolean upload( String localSourceFile, String remoteResultFile, - String host, int port, String usr, String pwd, boolean showMessages ) throws IOException - { - FTPClient ftpClient = new FTPClient(); - FileInputStream fis = null; - boolean resultOk = true; - - try { - ftpClient.connect( host, port ); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - resultOk &= ftpClient.login( usr, pwd ); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - fis = new FileInputStream( localSourceFile ); - resultOk &= ftpClient.storeFile( remoteResultFile, fis ); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - resultOk &= ftpClient.logout(); - if( showMessages ) { System.out.println( ftpClient.getReplyString() ); } - } finally { - try { if( fis != null ) { fis.close(); } } catch( IOException e ) {/* nothing to do */} - ftpClient.disconnect(); - } - - return resultOk; - } -} diff --git a/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java b/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java deleted file mode 100644 index 44d14c5..0000000 --- a/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.openslx.satellitedaemon.ftp; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; - -import org.apache.ftpserver.ConnectionConfigFactory; -import org.apache.ftpserver.FtpServer; -import org.apache.ftpserver.FtpServerFactory; -import org.apache.ftpserver.ftplet.Authority; -import org.apache.ftpserver.ftplet.FtpException; -import org.apache.ftpserver.ftplet.UserManager; -import org.apache.ftpserver.listener.ListenerFactory; -import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; -import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor; -import org.apache.ftpserver.usermanager.impl.BaseUser; -import org.apache.ftpserver.usermanager.impl.WritePermission; - -/** - * FTP-Test-Utility, basierend auf Apache FtpServer: {@link http - * ://www.jarvana.com - * /jarvana/view/org/apache/ftpserver/ftpserver-core/1.0.6/ftpserver - * -core-1.0.6-javadoc.jar!/org/apache/ftpserver/FtpServer.html} - */ -public class FtpTestUtil { - /** - * Erzeuge FTP-Server. - * - * @param ftpPort - * FTP-Port, z.B. 2121 - * @param ftpHomeDir - * FTP-Verzeichnis, z.B. "target/FtpHome" - * @param readUserName - * leseberechtigter Benutzer: Name - * @param readUserPwd - * leseberechtigter Benutzer: Passwort - * @param writeUserName - * schreibberechtigter Benutzer: Name - * @param writeUserPwd - * schreibberechtigter Benutzer: Passwort - * @param ftpUsersPropsFile - * kann null sein, oder z.B. "target/FtpUsers.properties" - * @param maxLogins - * maximale Anzahl von Logins (0 fuer Defaultwert) - */ - public static FtpServer createFtpServer(int ftpPort, String ftpHomeDir, - String readUserName, String readUserPwd, String writeUserName, - String writeUserPwd) throws FtpException, IOException { - return createFtpServer(ftpPort, ftpHomeDir, readUserName, readUserPwd, - writeUserName, writeUserPwd, null, 0); - } - - public static FtpServer createFtpServer(int ftpPort, String ftpHomeDir, - String readUserName, String readUserPwd, String writeUserName, - String writeUserPwd, String ftpUsersPropsFile, int maxLogins) - throws FtpException, IOException { - return createFtpServer(ftpPort, ftpHomeDir, readUserName, readUserPwd, - writeUserName, writeUserPwd, ftpUsersPropsFile, maxLogins, 0); - } - - public static FtpServer createFtpServer(int ftpPort, String ftpHomeDir, - String readUserName, String readUserPwd, String writeUserName, - String writeUserPwd, String ftpUsersPropsFile, int maxLogins, - int maxIdleTimeSec) throws FtpException, IOException { - File fhd = new File(ftpHomeDir); - if (!fhd.exists()) - fhd.mkdirs(); - - ListenerFactory listenerFactory = new ListenerFactory(); - listenerFactory.setPort(ftpPort); - - PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); - userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor()); - if (ftpUsersPropsFile != null && ftpUsersPropsFile.trim().length() > 0) { - File upf = new File(ftpUsersPropsFile); - if (!upf.exists()) - upf.createNewFile(); - userManagerFactory.setFile(upf); - } - - // Einen Nur-Lese-User und einen User mit Schreibberechtigung anlegen: - UserManager userManager = userManagerFactory.createUserManager(); - BaseUser userRd = new BaseUser(); - BaseUser userWr = new BaseUser(); - userRd.setName(readUserName); - userRd.setPassword(readUserPwd); - userRd.setHomeDirectory(ftpHomeDir); - userWr.setName(writeUserName); - userWr.setPassword(writeUserPwd); - userWr.setHomeDirectory(ftpHomeDir); - if (maxIdleTimeSec > 0) { - userRd.setMaxIdleTime(maxIdleTimeSec); - userWr.setMaxIdleTime(maxIdleTimeSec); - } - ArrayList<Authority> authorities = new ArrayList<Authority>(); - authorities.add(new WritePermission()); - userWr.setAuthorities(authorities); - userManager.save(userRd); - userManager.save(userWr); - - FtpServerFactory serverFactory = new FtpServerFactory(); - serverFactory.addListener("default", listenerFactory.createListener()); - serverFactory.setUserManager(userManager); - if (maxLogins > 0) { - ConnectionConfigFactory ccf = new ConnectionConfigFactory(); - ccf.setMaxLogins(maxLogins); - serverFactory.setConnectionConfig(ccf.createConnectionConfig()); - } - return serverFactory.createServer(); - } -} diff --git a/src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java b/src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java deleted file mode 100644 index 4caa8db..0000000 --- a/src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.openslx.satellitedaemon.ftp; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; - -import junit.framework.Assert; - -import org.apache.ftpserver.FtpServer; -import org.apache.ftpserver.ftplet.FtpException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -public class FtpUpDownUtilTest { - private static final int FTP_PORT = 2121; - private static final String FTP_HOST = "localhost"; - private static final String FTP_HOME_DIR = "target/FtpHome"; - private static final String FTPUSERSPROPS_FILE = "target/FtpUsers.properties"; - private static final String READ_USER_NAME = "ReadUserName"; - private static final String READ_USER_PWD = "ReadUserPwd"; - private static final String WRITE_USER_NAME = "WriteUserName"; - private static final String WRITE_USER_PWD = "WriteUserPwd"; - private static FtpServer ftpServer; - - @BeforeClass - public static void startFtpServer() throws FtpException, IOException - { - ftpServer = FtpTestUtil.createFtpServer( FTP_PORT, FTP_HOME_DIR, - READ_USER_NAME, READ_USER_PWD, WRITE_USER_NAME, WRITE_USER_PWD, FTPUSERSPROPS_FILE, 0 ); - ftpServer.start(); - } - - @AfterClass - public static void stoppFtpServer() - { - // Um den FTP-Server von ausserhalb des Tests eine Zeit lang ueber - // ftp://WriteUserName:WriteUserPwd@localhost:2121 - // anzusprechen, kann folgende Zeile aktiviert werden: - // try { Thread.sleep( 55000 ); } catch( InterruptedException e ) {/*ok*/} - ftpServer.stop(); - ftpServer = null; - } - - @Test - public void testFtp() throws IOException - { - final String LOCAL_SRC_FILE = "TestSrc.txt"; - final String LOCAL_DST_FILE = "target/TestDst.txt"; - final String REMOTE_FILE = "Test.txt"; - - File testFile = new File( LOCAL_SRC_FILE ); - if( !testFile.exists() ) testFile.createNewFile(); - - // Upload-Versuch ohne Schreibberechtigung muss fehlschlagen: - Assert.assertFalse( "READ_USER", FtpUpDownUtil.upload( LOCAL_SRC_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT, - READ_USER_NAME, READ_USER_PWD, false ) ); - - // Teste Upload mit Schreibberechtigung: - Assert.assertTrue( "WRITE_USER", FtpUpDownUtil.upload( LOCAL_SRC_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT, - WRITE_USER_NAME, WRITE_USER_PWD, false ) ); - Assert.assertTrue( "REMOTE_FILE.exists", new File( FTP_HOME_DIR, REMOTE_FILE ).exists() ); - - // Teste Download: - Assert.assertTrue( "Download", FtpUpDownUtil.download( LOCAL_DST_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT, - READ_USER_NAME, READ_USER_PWD, false ) ); - Assert.assertTrue( "LOCAL_DST_FILE.exists", new File( LOCAL_DST_FILE ).exists() ); - - // Teste Auflistung: - String[] remoteFilenameList = FtpUpDownUtil.list( FTP_HOST, FTP_PORT, READ_USER_NAME, READ_USER_PWD ); - Assert.assertTrue( "remoteFilenameList", Arrays.asList( remoteFilenameList ).contains( REMOTE_FILE ) ); - } -} |