From b7e8b0af8f761b9fe501ba210d1c24c863e418fe Mon Sep 17 00:00:00 2001 From: Michael Petretti Date: Thu, 8 May 2014 13:30:30 +0200 Subject: Making it look more pretty. --- src/main/java/org/openslx/satellitedaemon/App.java | 84 +++++++------------- .../util/EncryptWithServerIdPublicKey.java | 50 ++++++++++++ .../satellitedaemon/util/GetFtpCredentials.java | 92 ++++++++++++++++++++++ .../satellitedaemon/util/RndStringEncrypt.java | 50 ------------ 4 files changed, 169 insertions(+), 107 deletions(-) create mode 100644 src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java create mode 100644 src/main/java/org/openslx/satellitedaemon/util/GetFtpCredentials.java delete mode 100644 src/main/java/org/openslx/satellitedaemon/util/RndStringEncrypt.java diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 9f42623..6df6cff 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -5,7 +5,6 @@ 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; @@ -13,80 +12,51 @@ import java.security.NoSuchAlgorithmException; import java.security.SignatureException; 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; -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.ServerSessionData; -import org.openslx.satellitedaemon.util.RndStringEncrypt; +import org.openslx.satellitedaemon.util.GetFtpCredentials; /** - * HS Server. + * Main class for uploading images from the HS-Server to the Satellite Server. * */ -public class App { - public static void main(String[] args) throws UnrecoverableKeyException, +public class App +{ + public static void main( String[] args ) throws UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException, IOException, - InvalidKeyException, SignatureException { + InvalidKeyException, SignatureException + { String nilsIp = "132.230.4.23"; - int thriftPort = 9090; int ftpPort = 2221; - try { - TTransport transport; - - transport = new TSocket(nilsIp, thriftPort); // Nils IP - transport.open(); - - TProtocol protocol = new TBinaryProtocol(transport); - 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"); - 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", 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 { + // TODO: A Thread that starts the call for new credentials and the upload + // whenever a new image was sceduled in the db. + FtpCredentials ftpc = GetFtpCredentials.now(); + 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(); + if ( !ftpClient.login( ftpc.username, ftpc.password ) ) { + throw new ConnectException( "Could not login." ); } - transport.close(); - } catch (TException x) { - x.printStackTrace(); + System.out.println( "Connected to " + nilsIp + ":" + ftpPort + + ". Reply code: " + ftpClient.getReplyCode() ); + } finally { + ftpClient.disconnect(); } } } diff --git a/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java b/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java new file mode 100644 index 0000000..357472d --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java @@ -0,0 +1,50 @@ +package org.openslx.satellitedaemon.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.KeyPair; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.Signature; +import java.security.SignatureException; +import java.security.UnrecoverableKeyException; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; + +public class EncryptWithServerIdPublicKey { + + KeyPair pair; + + public EncryptWithServerIdPublicKey(String alias, String password, String file) + throws NoSuchAlgorithmException, CertificateException, + FileNotFoundException, IOException, KeyStoreException, + UnrecoverableKeyException { + KeyStore keystore = KeyStore.getInstance("JKS"); + keystore.load(new FileInputStream(new File(file)), + password.toCharArray()); + Certificate cert = null; + + Key key = keystore.getKey(alias, password.toCharArray()); + + if (key instanceof PrivateKey) { + cert = keystore.getCertificate(alias); + PublicKey publicKey = cert.getPublicKey(); + pair = new KeyPair(publicKey, (PrivateKey) key); + } + } + + public byte[] encryptString(String message) throws NoSuchAlgorithmException, + InvalidKeyException, SignatureException { + Signature signature = Signature.getInstance("SHA256WITHRSA"); + signature.initSign(pair.getPrivate()); + signature.update(message.getBytes()); + return signature.sign(); + } +} diff --git a/src/main/java/org/openslx/satellitedaemon/util/GetFtpCredentials.java b/src/main/java/org/openslx/satellitedaemon/util/GetFtpCredentials.java new file mode 100644 index 0000000..27fea93 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/util/GetFtpCredentials.java @@ -0,0 +1,92 @@ +package org.openslx.satellitedaemon.util; + +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.ServerSessionData; + +public class GetFtpCredentials +{ + private static FtpCredentials ftpc = 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; + + /** + * Handles the authentication with the Satellite Server and sends the FtpCredentials, which + * are necessary for the upload of the image. + */ + static { + try { + TTransport transport; + transport = new TSocket( nilsIp, thriftPort ); // Nils IP + transport.open(); + TProtocol protocol = new TBinaryProtocol( transport ); + + ImageServer.Client client = new ImageServer.Client( protocol ); + 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 ) ); + + // TODO: Should be able to get the necessary strings ect. from the DB. + ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + true, false, "theBest", "theVeryBest", 1024 ); + + ftpc = client.submitImage( sSD.sessionId, imDat ); + + transport.close(); + } catch ( TException x ) { + x.printStackTrace(); + } 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(); + } + } + + public static FtpCredentials now() + { + return ftpc; + } +} diff --git a/src/main/java/org/openslx/satellitedaemon/util/RndStringEncrypt.java b/src/main/java/org/openslx/satellitedaemon/util/RndStringEncrypt.java deleted file mode 100644 index c99a768..0000000 --- a/src/main/java/org/openslx/satellitedaemon/util/RndStringEncrypt.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.openslx.satellitedaemon.util; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.security.InvalidKeyException; -import java.security.Key; -import java.security.KeyPair; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.Signature; -import java.security.SignatureException; -import java.security.UnrecoverableKeyException; -import java.security.cert.Certificate; -import java.security.cert.CertificateException; - -public class RndStringEncrypt { - - KeyPair pair; - - public RndStringEncrypt(String alias, String password, String file) - throws NoSuchAlgorithmException, CertificateException, - FileNotFoundException, IOException, KeyStoreException, - UnrecoverableKeyException { - KeyStore keystore = KeyStore.getInstance("JKS"); - keystore.load(new FileInputStream(new File(file)), - password.toCharArray()); - Certificate cert = null; - - Key key = keystore.getKey(alias, password.toCharArray()); - - if (key instanceof PrivateKey) { - cert = keystore.getCertificate(alias); - PublicKey publicKey = cert.getPublicKey(); - pair = new KeyPair(publicKey, (PrivateKey) key); - } - } - - public byte[] encryptRndString(String message) throws NoSuchAlgorithmException, - InvalidKeyException, SignatureException { - Signature signature = Signature.getInstance("SHA256WITHRSA"); - signature.initSign(pair.getPrivate()); - signature.update(message.getBytes()); - return signature.sign(); - } -} -- cgit v1.2.3-55-g7522