summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Globals.java
blob: d75459bf41a6cb6d9968485e0de93fb74fe4dad0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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.KeyFactory;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
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;
import org.openslx.satellitedaemon.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" );
	}
	
	public static String getProxyConf()
	{
		return properties.getProperty( "PROXY_CONF" );
	}
	
	public static String getProxyAddress()
	{
		return properties.getProperty( "PROXY_ADDR" );
	}
	
	public static String getProxyUsername()
	{
		return properties.getProperty( "PROXY_USERNAME" );
	}

	public static String getProxyPassword()
	{
		return properties.getProperty( "PROXY_PASSWORD" );
	}

	// Integers //

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

	public static int getProxyPort()
	{
		return tryToParseInt( properties.getProperty( "PROXY_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.getDefault();
			} catch ( NoSuchAlgorithmException 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( "SSLv3" );
			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;
	}

	/**
	 * Tries to parse an int. Returns 0 on error.
	 * 
	 * @param s
	 *           The string to parse
	 * @return The parsed int or 0 on error
	 */
	public static int tryToParseInt( String s )
	{
		try {
			return Integer.parseInt( s );
		} catch ( NumberFormatException e ) {
			return 0;
		}
	}
	
	public static boolean checkProxySettings() {
		return (
				(getProxyAddress() != "") &&
				(getProxyPort() != 0));
	}
}