From f593a42eae01cbec9c64c1a497205b3b74749438 Mon Sep 17 00:00:00 2001 From: Michael Petretti Date: Mon, 19 May 2014 17:33:17 +0200 Subject: Added the new FtpUploadWorker-Class --- src/main/java/org/openslx/satellitedaemon/App.java | 14 ++- .../openslx/satellitedaemon/ftp/FtpConnection.java | 124 --------------------- .../satellitedaemon/ftp/FtpUploadWorker.java | 87 +++++++++++++++ .../satellitedaemon/ftp/ThriftConnection.java | 123 ++++++++++++++++++++ 4 files changed, 221 insertions(+), 127 deletions(-) delete mode 100644 src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java create mode 100644 src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java create mode 100644 src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 073e2e8..a0de29a 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -2,14 +2,19 @@ package org.openslx.satellitedaemon; import java.io.FileNotFoundException; import java.io.IOException; +import java.security.InvalidKeyException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import java.util.UUID; import org.openslx.imagemaster.thrift.iface.FtpCredentials; +import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException; import org.openslx.satellitedaemon.ftp.FtpImageUploader; -import org.openslx.satellitedaemon.ftp.FtpConnection; +import org.openslx.satellitedaemon.ftp.ThriftConnection; import org.openslx.satellitedaemon.util.Util; /** @@ -18,12 +23,15 @@ import org.openslx.satellitedaemon.util.Util; */ public class App { - public static void main( String[] args ) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, ServerAuthenticationException + public static void main( String[] args ) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, ServerAuthenticationException, UnrecoverableKeyException, InvalidKeyException, SignatureException { // TODO: A Thread that starts the call for new credentials and the upload // whenever a new image was sceduled in the db. - FtpCredentials ftpc = FtpConnection.getFtpCredentials(); + ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + true, false, "best", "theVeryBest", 1024 ); + FtpCredentials ftpc = ThriftConnection.getFtpCredentials(imDat); Util.notNullFatal( ftpc, "ftpc was null" ); FtpImageUploader ftpIU = new FtpImageUploader( ftpc ); Util.notNullFatal( ftpIU, "ftpIU was null" ); diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java deleted file mode 100644 index fa0b040..0000000 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java +++ /dev/null @@ -1,124 +0,0 @@ -package org.openslx.satellitedaemon.ftp; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.security.InvalidKeyException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.SignatureException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.UUID; - -import org.apache.thrift.TException; -import org.apache.thrift.protocol.TBinaryProtocol; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.transport.TSocket; -import org.apache.thrift.transport.TTransport; -import org.openslx.imagemaster.thrift.iface.FtpCredentials; -import org.openslx.imagemaster.thrift.iface.ImageData; -import org.openslx.imagemaster.thrift.iface.ImageServer; -import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException; -import org.openslx.imagemaster.thrift.iface.ServerSessionData; -import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey; -import org.openslx.satellitedaemon.util.Util; - -/** - * Handles the authentication with the Satellite Server and sends the FtpCredentials, which - * are necessary for the upload of the image. - */ -public class FtpConnection -{ - private static ImageServer.Client client = null; - // TODO: All of the Strings and int's should not fall from sky. - static String nilsIp = "132.230.4.23"; - static int thriftPort = 9090; - - private static ImageServer.Client getConnection() - { - ImageServer.Client theClient = null; - if ( client == null ) { - theClient = newClient(); - } else { - theClient = client; - } - boolean isAuthenticated = false; - try { - isAuthenticated = theClient.ping(); - } catch ( TException x ) { - theClient = newClient(); - if (theClient==null){ - return null; - } - } - if ( !isAuthenticated ) { -// TODO: a server authentication. - } - return theClient; - } - - private static ImageServer.Client newClient() - { - ImageServer.Client newClient = null; - try { - TTransport transport; - transport = new TSocket( nilsIp, thriftPort ); // Nils IP - transport.open(); - TProtocol protocol = new TBinaryProtocol( transport ); - newClient = new ImageServer.Client( protocol ); - } catch ( TException x ) { - x.printStackTrace(); - return null; - } - return newClient; - } - - public static FtpCredentials getFtpCredentials() throws ServerAuthenticationException - { - try { - client = getConnection(); - Util.notNullFatal( client, "Client is null. Maybe a Network error." ); - String toEncrypt = client.startServerAuthentication( "uni-freiburg.de" ); - // System.out.println( toEncrypt ); - EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( "serverid", "password", - "/home/michael/satellite-daemon/config/serverid.jks" ); - byte[] byteArray = rse.encryptString( toEncrypt ); - ServerSessionData sSD = client.serverAuthenticate( - "uni-freiburg.de", ByteBuffer.wrap( byteArray ) ); - ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, - "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", - true, false, "best", "theVeryBest", 1024 ); - - return client.submitImage( sSD.sessionId, imDat ); - } catch ( InvalidKeyException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( NoSuchAlgorithmException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( SignatureException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( UnrecoverableKeyException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( CertificateException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( FileNotFoundException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( KeyStoreException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( IOException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( TException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } -} diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java new file mode 100644 index 0000000..4f90467 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java @@ -0,0 +1,87 @@ +package org.openslx.satellitedaemon.ftp; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.ConnectException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.util.ArrayList; +import java.util.List; +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.openslx.imagemaster.thrift.iface.FtpCredentials; +import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.satellitedaemon.db.DbImage; + +public class FtpUploadWorker implements Runnable +{ + static String nilsIp = "132.230.4.23"; + static int ftpPort = 2221; + + @Override + public void run() + { + List imageList = new ArrayList(); + while ( true ) { + imageList = DbImage.getAllMarkedForUpload(); + while ( !imageList.isEmpty() ) { + // TODO: imDat should be filled by the first entry of imageList. + // imageList.get(0); + ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + true, false, "best", "theVeryBest", 1024 ); + + FtpCredentials ftpc = ThriftConnection.getFtpCredentials( imDat ); + + try { + 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]; + FTPSClient ftpClient = new FTPSClient( "SSL", true ); + 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() ); + + // TODO: Where do I find the path to the db-image? + File file = new File( "/path/to/File" ); + FileInputStream fis = new FileInputStream( file ); + + // TODO: What is the path where it should be stored? + ftpClient.storeFile( "/path/where/theImage/belongs", fis ); + + } finally { + ftpClient.disconnect(); + } + } catch ( NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e ) { + } + imageList.remove( 0 ); + } + try { + Thread.sleep( 5 * 60 * 1000 ); + } catch ( InterruptedException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + +} diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java new file mode 100644 index 0000000..810a496 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java @@ -0,0 +1,123 @@ +package org.openslx.satellitedaemon.ftp; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.InvalidKeyException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; + +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.openslx.imagemaster.thrift.iface.FtpCredentials; +import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.imagemaster.thrift.iface.ImageServer; +import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException; +import org.openslx.imagemaster.thrift.iface.ServerSessionData; +import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey; +import org.openslx.satellitedaemon.util.Util; + +/** + * Handles the authentication with the Satellite Server and sends the FtpCredentials, which + * are necessary for the upload of the image. + */ +public class ThriftConnection +{ + private static ImageServer.Client client = null; + private static ServerSessionData sSD = null; + // TODO: All of the Strings and int's should not fall from sky. + static String nilsIp = "132.230.4.23"; + static int thriftPort = 9090; + + private static ImageServer.Client getConnection() + throws ServerAuthenticationException, TException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException, + IOException, InvalidKeyException, SignatureException + { + ImageServer.Client theClient = null; + if ( client == null ) { + theClient = newClient(); + } else { + theClient = client; + } + boolean isAuthenticated = false; + try { + isAuthenticated = theClient.ping(); + } catch ( TException x ) { + theClient = newClient(); + if ( theClient == null ) { + return null; + } + } + if ( !isAuthenticated ) { + String toEncrypt = client.startServerAuthentication( "uni-freiburg.de" ); + // System.out.println( toEncrypt ); + EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( "serverid", "password", + "/home/michael/satellite-daemon/config/serverid.jks" ); + byte[] byteArray = rse.encryptString( toEncrypt ); + sSD = client.serverAuthenticate( + "uni-freiburg.de", ByteBuffer.wrap( byteArray ) ); + } + return theClient; + } + + private static ImageServer.Client newClient() + { + ImageServer.Client newClient = null; + try { + TTransport transport; + transport = new TSocket( nilsIp, thriftPort ); // Nils IP + transport.open(); + TProtocol protocol = new TBinaryProtocol( transport ); + newClient = new ImageServer.Client( protocol ); + } catch ( TException x ) { + x.printStackTrace(); + return null; + } + return newClient; + } + + public static FtpCredentials getFtpCredentials(ImageData imDat) + + { + try { + client = getConnection(); + Util.notNullFatal( client, "Client is null. Maybe a Network error." ); + + return client.submitImage( sSD.sessionId, imDat ); + } catch ( TException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( UnrecoverableKeyException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( InvalidKeyException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( NoSuchAlgorithmException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( CertificateException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( FileNotFoundException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( KeyStoreException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( SignatureException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( IOException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } +} -- cgit v1.2.3-55-g7522