summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java
blob: c831435f30a643e8e99a057d93ca79039cfad761 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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.DownloadInfos;
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.imagemaster.thrift.iface.UploadInfos;
import org.openslx.satellitedaemon.Globals;
import org.openslx.satellitedaemon.Globals.PropInt;
import org.openslx.satellitedaemon.Globals.PropString;
import org.openslx.satellitedaemon.db.DbImage;
import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey;

// TODO: Handle all the auto-generated catch blocks in a meaningful way

/**
 * 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 
	 * and to get the ServerSessionData. If connection is ok, it returns ftpCredential.
	 * 
	 * @return returns 'null' if there is a problem.
	 */
	public static UploadInfos getUploadInfos( ImageData imDat )
	{
		ImageServer.Client theClient = null;
		try {
			theClient = getConnection();
			if ( theClient == null ) {
				log.error( "Client was null!" );
				return null;
			}

			return theClient.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;
	}
	
	/**
	 * The method calls getConnection() to check if the connection is ok 
	 * and to get the DownloadeInfos. If connection is ok, it returns ftpCredential.
	 * 
	 * @return returns 'null' if there is a problem.
	 */
	public static DownloadInfos getDownloadInfos( DbImage imDat )
	{
		ImageServer.Client theClient = null;
		try {
			theClient = getConnection();
			if ( theClient == null ) {
				log.error( "Client was null!" );
				return null;
			}

			return theClient.getImage( imDat.guid, sSD.sessionId );
		} 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;
		boolean isAuthenticated = false;
		if ( client == null ) {
			log.info( "The global client was null. Making a new client ..." );
			theClient = newClient();
			if ( theClient == null ) {
				log.debug( "ThriftConnection: The client was null after newClient()" );
				return null;
			}
		} else {
			log.info( "The global Client was already used. Setting isAuthenticated = true." );
			theClient = client;
			isAuthenticated = true;
		}
		//		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;
			if ( theClient == null ) {
				log.debug( "The client was null" );
				return null;
			}
			try {
				toEncrypt = theClient.startServerAuthentication( Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME) );
				log.info( "The random String we want to encrypt: " + toEncrypt );
				EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ),
						Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ),
						Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ) );
				byte[] byteArray = rse.encryptString( toEncrypt );
				sSD = theClient.serverAuthenticate(
						Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME), ByteBuffer.wrap( byteArray ) );
			} catch ( ServerAuthenticationException e ) {
				log.error( "ThriftConnection: ServerAuthenticationException: Server Authetication was not sucessful." );
				e.printStackTrace();
				return null;
			} catch ( TException e ) {
				log.error( "ThriftConnection: TException: Server Authetication was not sucessful." );
				e.printStackTrace();
				return null;
			}
			log.info( "is Authenticated." );

		}
		client = theClient;
		return theClient;
	}

	private static ImageServer.Client newClient()
	{
		ImageServer.Client newClient = null;
		try {
			TTransport transport;							
			transport = new TSocket( Globals.getPropertyString( PropString.FTPSERVERIP ), Globals.getPropertyInt( PropInt.THRIFTPORT ) );
			transport.open();
			TProtocol protocol = new TBinaryProtocol( transport );
			newClient = new ImageServer.Client( protocol );
			log.debug( "ThriftConnection: Made a new Client" );
		} catch ( TException x ) {
			log.error( "ThriftConnection coudn't create new client." );
			x.printStackTrace();
			return null;
		}
		return newClient;
	}
}