package org.openslx.satellitedaemon; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.Properties; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import org.apache.log4j.Logger; public class Globals { private static Logger log = Logger.getLogger( Globals.class ); private static final Properties properties = new Properties(); private static boolean loadedProperties = false; private static SSLContext context = null; /*********************************************************************************************** * If there are more ints or Strings which should be added to config/global.properties, * add to suiting enum, add a 'case' to getPropertyInt/String() and add checks to * propertiesValid(). * * As an Example, if you want the value of the FTPSERVERIP you have to call * Globals.getPropertyString( PropString.FTPSERVERIP ) which returns a string. */ public static enum PropInt { FTPPORT, THRIFTPORT, BLOCKSIZE // More int's? Add them separated with "," } public static enum PropString { // More strings's? Add them separated with "," FTPSERVERIP, KEYSTORETYPE, FTPSKEYSTOREPATH, FTPSKEYSTOREPWD, THRIFTORGANIZATIONNAME, RNDSTRINGENCRYPTALIAS, RNDSTRINGENCRYPTPASSWORD, RNDSTRINGENCRYPTPATH } /*********************************************************************************************** * * @return * @throws IOException */ public static boolean loadProperties() throws IOException { if ( loadedProperties ) return false; // Load all entries of the config file into properties BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) ); properties.load( stream ); stream.close(); return true; } /*********************************************************************************************** * * @param props * @return */ public static int getPropertyInt( Globals.PropInt props ) { String result = null; switch ( props ) { case FTPPORT: result = properties.getProperty( "ftp_port" ); break; case BLOCKSIZE: result = properties.getProperty( "blockSize" ); break; case THRIFTPORT: result = properties.getProperty( "ThriftPort" ); break; default: result = "0"; break; } if ( result == null ) return 0; return Integer.valueOf( result ); } /*********************************************************************************************** * * @param props * @return */ public static String getPropertyString( Globals.PropString props ) { String result = null; switch ( props ) { case FTPSERVERIP: result = properties.getProperty( "ftp_server_ip" ); break; case KEYSTORETYPE: result = properties.getProperty( "keyStore_type" ); break; case FTPSKEYSTOREPATH: result = properties.getProperty( "path_to_ftps_keyStore" ); break; case FTPSKEYSTOREPWD: result = properties.getProperty( "ftps_keyStore_password" ); break; case THRIFTORGANIZATIONNAME: result = properties.getProperty( "organization_name" ); break; case RNDSTRINGENCRYPTALIAS: result = properties.getProperty( "RndStringEncrypt_alias" ); break; case RNDSTRINGENCRYPTPASSWORD: result = properties.getProperty( "RndStringEncrypt_password" ); break; case RNDSTRINGENCRYPTPATH: result = properties.getProperty( "RndStringEncrypt_path" ); break; default: result = ""; break; } return result; } // TODO: add real checks /*********************************************************************************************** * * @return */ public static boolean propertiesValid() { if ( Globals.getPropertyInt( PropInt.FTPPORT ) == 0 || Globals.getPropertyInt( PropInt.BLOCKSIZE ) == 0 || Globals.getPropertyInt( PropInt.THRIFTPORT ) == 0 || Globals.getPropertyString( PropString.FTPSERVERIP ).isEmpty() || Globals.getPropertyString( PropString.FTPSERVERIP ) == null || Globals.getPropertyString( PropString.KEYSTORETYPE ).isEmpty() || Globals.getPropertyString( PropString.KEYSTORETYPE ) == null || Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ).isEmpty() || Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ) == null || Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).isEmpty() || Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ) == null || Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ).isEmpty() || Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ) == null || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ).isEmpty() || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ) == null || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ).isEmpty() || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ) == null || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ).isEmpty() || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ) == null ) { return false; } else { return true; } } /*********************************************************************************************** * * @return */ public static boolean masterServerSslContextInit() { char[] passphrase = Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).toCharArray(); KeyStore keystore; try { keystore = KeyStore.getInstance( "JKS" ); keystore.load( new FileInputStream( Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ) ), passphrase ); TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); tmf.init( keystore ); context = SSLContext.getInstance( "SSLv3" ); TrustManager[] trustManagers = tmf.getTrustManagers(); context.init( null, trustManagers, null ); } catch ( KeyStoreException e ) { log.error("KeyStoreException"); return false; } catch ( NoSuchAlgorithmException e ) { log.error("NoSuchAlgorithmException"); return false; } catch ( CertificateException e ) { log.error("CertificateException"); return false; } catch ( FileNotFoundException e ) { log.error("FileNotFoundException"); return false; } catch ( IOException e ) { log.error("IOException"); return false; } catch ( KeyManagementException e ) { log.error("KeyManagementException"); return false; } return true; } /*********************************************************************************************** * * @return */ public static SSLContext getMasterServerSslContext(){ return Globals.context; } }