summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java
blob: e7fe0b8aa177febd6bed41b2b750ff1b5b981ecf (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                        









                                               
 
                               







                                                           
                                                                          
                                                              


                                                      
                                                                     
 



                                                                                           
                             
 
                                                        
                                                    
                                                                               
 









                                                                             
                                               
                                                                

                                            







































                                                                                   
                                                         
                                                                                                                                                   
                                                                            











                                                           
                                                  



                                            


















                                                                                                                            






                                                     
                     

                                                                                                                                                          

                                                                              
                                                                       
                                          
                                                                                   
                                            
                                    
                 
                                 

         
 
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.log4j.Logger;
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.Globals;
import org.openslx.satellitedaemon.Globals.PropInt;
import org.openslx.satellitedaemon.Globals.PropString;
import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey;

/**
 * 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;
	private static Logger log = Logger.getLogger( ThriftConnection.class );

	/**
	 * The method calls getConnection() to check if the connection is ok,
	 * if so, it returns ftpCredential.
	 * 
	 * @return returns 'null' if there is a problem.
	 */
	public static FtpCredentials getFtpCredentials( ImageData imDat )
	{
		try {
			client = getConnection();
			if ( client == null ) {
				log.error( "Client was null!" );
				return null;
			}

			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;
	}

	/**
	 * This method checks if there is already a working connection. If not,
	 * newClient() establishes one. Also it does the Authentication if not done
	 * yet.
	 * 
	 * @return returns the client if successful.
	 */
	private static ImageServer.Client getConnection()
			throws 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 ) {
			log.info( "ThriftConnection: Client not yet Authenticated. Trying..." );
			String toEncrypt;
			try {
				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 ) );
			} catch ( ServerAuthenticationException e ) {
				log.error( "ThriftConnection: Server Authetication was not sucessful." );
				e.printStackTrace();
			} catch ( TException e ) {
				log.error( "ThriftConnection: Server Authetication was not sucessful." );
				e.printStackTrace();
			}
			log.info( "is Authenticated." );

		}
		return theClient;
	}

	private static ImageServer.Client newClient()
	{
		ImageServer.Client newClient = null;
		try {
			TTransport transport;							// Is it really always the same IP:Port as from FTPServer?
			transport = new TSocket( Globals.getPropertyString( PropString.FTPSERVERIP ), Globals.getPropertyInt( PropInt.FTPPORT ) );
			transport.open();
			TProtocol protocol = new TBinaryProtocol( transport );
			newClient = new ImageServer.Client( protocol );
		} catch ( TException x ) {
			log.error( "ThriftConnection coudn't create new client." );
			x.printStackTrace();
			return null;
		}
		return newClient;
	}

}