summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Globals.java
blob: bb6e6da51f3ad9af7796b179f7587a81d1295e35 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                    
                               
                                     
                           

                                         
                                            
                              
                                              

                            




                                         
                             
 


                                                                      
                                                                      
                                                 
 

                                                                                



                                                                                                         
 
                           
 


                                                                     

         
                                                
         
                                                                   
         
 


                                                                
         

                      
 





                                                                                                                                              

                                         
                                                                                     

         
           
                          
           
                
                                                

                                                                              
                                                       

                                                                                                                    
                                       
                                           
                                                                                  
                                         

                                                   
                 
 
                                                                                                          
                                                                                                
         
 

                                                                                                         
                                                                               
           
                                                  
           

                                                          



                                                                                     


                                                                                         




                                                                                             

                                  
                                                                 
                                                                                          
                                                                     

                                                                                                  
                                                                      
                                                                              
                                                                  
                                                     

                                                                                                   
                                     

                                                                            



                                     
 

                                                            
                               
         
 
package org.openslx.satellitedaemon;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import org.apache.log4j.Logger;
import org.openslx.util.Util;

public class Globals
{
	private static Logger log = Logger.getLogger( Globals.class );
	private static final Properties properties = new Properties();
	private static SSLContext context = null;

	public static final int BLOCKSIZE = 16 * 1024 * 1024; // 16 MB blocksize

	/***********************************************************************************************/
	/**
	 * A call of Globals.getXXXXXX() returns the corresponding entry in config/global.properties
	 */

	// * Properties *//

	public static String getMasterserverHost()
	{
		return properties.getProperty( "MASTERSERVER_HOST" );
	}

	public static String getTruststorePath()
	{
		return properties.getProperty( "TRUSTSTORE_PATH" );
	}

	public static String getImageFolder()
	{
		return properties.getProperty( "IMAGE_FOLDER" );
	}

	// Integers //

	public static boolean getThriftTls()
	{
		String s = properties.getProperty( "THRIFT_TLS", "yes" );
		return s.equalsIgnoreCase( "yes" ) || s.equalsIgnoreCase( "true" ) || s.equalsIgnoreCase( "1" ) || s.equalsIgnoreCase( "on" );
	}

	public static int getThriftPort()
	{
		return Util.tryToParseInt( properties.getProperty( "THRIFT_PORT" ) );
	}

	/**
	 * Load properties
	 */
	static {
		InputStreamReader stream = null;
		try {
			// Load all entries of the config file into properties
			stream = new InputStreamReader(
					new FileInputStream( "config/global.properties" ), StandardCharsets.UTF_8 );
			properties.load( stream );
			stream.close();
		} catch ( IOException e ) {
			log.error( "Could not load global.properties. Exiting." );
			System.exit( 2 );
		} finally {
			Util.streamClose( stream );
		}

		Util.notNullOrEmptyFatal( getMasterserverHost(), "Masterserver Host must not be empty!" );
		Util.notNullOrEmptyFatal( getImageFolder(), "Image Folder must not be empty!" );
	}

	/***********************************************************************************************/
	/**
	 * Initialize the ssl context used everywhere for outgoing connections.
	 * 
	 * @return true on success, false on error
	 */
	public static boolean masterServerSslContextInit()
	{
		if ( context != null )
			return true;
		if ( getTruststorePath() == null || getTruststorePath().isEmpty() ) {
			try {
				context = SSLContext.getInstance( "TLSv1.2" );
				context.init( null, null, null );
			} catch ( NoSuchAlgorithmException | KeyManagementException e ) {
				log.error( "could not load system default ssl context.", e );
				return false;
			}
			return true;
		}
		KeyStore keystore;
		try {
			keystore = KeyStore.getInstance( "JKS" );
			keystore.load( new FileInputStream( getTruststorePath() ), null );
			TrustManagerFactory tmf = TrustManagerFactory
					.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
			tmf.init( keystore );
			context = SSLContext.getInstance( "TLSv1.2" );
			TrustManager[] trustManagers = tmf.getTrustManagers();
			context.init( null, trustManagers, null );
		} catch ( FileNotFoundException e ) {
			log.error( "Could not find the keystore for the filetransfer. Path was '" +
					getTruststorePath() + "'" );
			return false;
		} catch ( Exception e ) {
			log.error( "Could not initialize SSL context.", e );
			return false;
		}
		return true;
	}

	public static SSLContext getMasterServerSslContext()
	{
		return context;
	}
}