7
7
import com .ibm .mq .constants .MQConstants ;
8
8
import org .apache .logging .log4j .LogManager ;
9
9
import org .apache .logging .log4j .Logger ;
10
+ import ru .cinimex .exporter .Config ;
10
11
12
+ import javax .net .ssl .KeyManagerFactory ;
13
+ import javax .net .ssl .SSLContext ;
14
+ import javax .net .ssl .SSLSocketFactory ;
15
+ import javax .net .ssl .TrustManagerFactory ;
16
+ import java .io .FileInputStream ;
17
+ import java .io .IOException ;
18
+ import java .security .KeyManagementException ;
19
+ import java .security .KeyStore ;
20
+ import java .security .KeyStoreException ;
21
+ import java .security .NoSuchAlgorithmException ;
22
+ import java .security .UnrecoverableKeyException ;
23
+ import java .security .cert .CertificateException ;
24
+ import java .util .HashMap ;
11
25
import java .util .Hashtable ;
26
+ import java .util .Map ;
27
+ import java .util .Optional ;
12
28
13
29
/**
14
30
* Class represents MQ connection.
@@ -20,42 +36,55 @@ public class MQConnection {
20
36
/**
21
37
* Method creates connection properties Hashtable from connection parameters.
22
38
*
23
- * @param host - host, where queue manager is located.
24
- * @param port - queue manager's port.
25
- * @param channel - queue manager's channel.
26
- * @param user - user, which has enough privilege on the queue manager (optional).
27
- * @param password - password, which is required to establish connection with queue manager (optional).
28
- * @param useMQCSP - flag, which indicates, if MQCSP auth should be used.
39
+ * @param config - config.
29
40
* @return - returns prepared structure with all parameters transformed into queue manager's format.
30
41
*/
31
- protected static Hashtable <String , Object > createMQConnectionParams (String host , int port , String channel , String user , String password , boolean useMQCSP ) {
32
- Hashtable <String , Object > properties = new Hashtable <>();
33
- properties .put (MQConstants .TRANSPORT_PROPERTY , host == null ? MQConstants .TRANSPORT_MQSERIES_BINDINGS : MQConstants .TRANSPORT_MQSERIES_CLIENT );
34
- if (host != null ) properties .put (MQConstants .HOST_NAME_PROPERTY , host );
35
- if (port != 0 ) properties .put (MQConstants .PORT_PROPERTY , port );
36
- if (channel != null ) properties .put (MQConstants .CHANNEL_PROPERTY , channel );
37
- if (user != null || password != null ) {
38
- if (useMQCSP ) properties .put (MQConstants .USE_MQCSP_AUTHENTICATION_PROPERTY , true );
39
- if (user != null ) properties .put (MQConstants .USER_ID_PROPERTY , user );
40
- if (password != null ) properties .put (MQConstants .PASSWORD_PROPERTY , password );
42
+ public static Map <String , Object > createMQConnectionParams (Config config ) {
43
+ Map <String , Object > properties = new HashMap <>();
44
+ properties .put (MQConstants .TRANSPORT_PROPERTY , config .getQmgrHost () == null ? MQConstants .TRANSPORT_MQSERIES_BINDINGS : MQConstants .TRANSPORT_MQSERIES_CLIENT );
45
+ if (config .getQmgrHost () != null ) properties .put (MQConstants .HOST_NAME_PROPERTY , config .getQmgrHost ());
46
+ if (config .getQmgrPort () != 0 ) properties .put (MQConstants .PORT_PROPERTY , config .getQmgrPort ());
47
+ if (config .getQmgrChannel () != null ) properties .put (MQConstants .CHANNEL_PROPERTY , config .getQmgrChannel ());
48
+ if (config .getUser () != null || config .getPassword () != null ) {
49
+ if (config .useMqscp ()) properties .put (MQConstants .USE_MQCSP_AUTHENTICATION_PROPERTY , true );
50
+ if (config .getUser () != null ) properties .put (MQConstants .USER_ID_PROPERTY , config .getUser ());
51
+ if (config .getPassword () != null ) properties .put (MQConstants .PASSWORD_PROPERTY , config .getPassword ());
52
+ }
53
+ MQSecurityProperties mqSecurityProperties = config .getMqSecurityProperties ();
54
+ if (mqSecurityProperties != null && mqSecurityProperties .isUseTLS ()) {
55
+ KeyStore keyStore = getStore (mqSecurityProperties .getKeystorePath (), mqSecurityProperties .getKeystorePassword ());
56
+ KeyStore trustStore = getStore (mqSecurityProperties .getTruststorePath (), mqSecurityProperties .getTruststorePassword ());
57
+
58
+ SSLContext sslContext = null ;
59
+ try {
60
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
61
+ KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
62
+ trustManagerFactory .init (trustStore );
63
+ keyManagerFactory .init (keyStore , mqSecurityProperties .getKeystorePassword ().toCharArray ());
64
+ sslContext = SSLContext .getInstance (mqSecurityProperties .getSslProtocol ());
65
+ sslContext .init (keyManagerFactory .getKeyManagers (), trustManagerFactory .getTrustManagers (), null );
66
+ } catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyManagementException e1 ) {
67
+ logger .error ("Failed!" , e1 );
68
+ }
69
+
70
+ SSLSocketFactory sslSocketFactory = sslContext .getSocketFactory ();
71
+
72
+ properties .put (MQConstants .SSL_CIPHER_SUITE_PROPERTY , mqSecurityProperties .getCipherSuite ());
73
+ properties .put (MQConstants .SSL_SOCKET_FACTORY_PROPERTY , sslSocketFactory );
74
+ System .setProperty ("com.ibm.mq.cfg.useIBMCipherMappings" , "false" );
41
75
}
42
76
return properties ;
43
77
}
44
78
45
- /**
46
- * Method establishes connection with queue manager.
47
- *
48
- * @param host - host, where queue manager is located.
49
- * @param port - queue manager's port.
50
- * @param channel - queue manager's channel.
51
- * @param qmName - queue manager's name.
52
- * @param user - user, which has enough privilege on the queue manager (optional).
53
- * @param password - password, which is required to establish connection with queue manager (optional).
54
- * @param useMQCSP - flag, which indicates, if MQCSP auth should be used.
55
- */
56
- public void establish (String host , int port , String channel , String qmName , String user , String password , boolean useMQCSP ) throws MQException {
57
- Hashtable <String , Object > connectionProperties = createMQConnectionParams (host , port , channel , user , password , useMQCSP );
58
- queueManager = new MQQueueManager (qmName , connectionProperties );
79
+ private static KeyStore getStore (String storePath , String storePassword ) {
80
+ KeyStore keyStore = null ;
81
+ try (FileInputStream keyStoreInput = new FileInputStream (storePath )) {
82
+ keyStore = KeyStore .getInstance ("JKS" );
83
+ keyStore .load (keyStoreInput , storePassword .toCharArray ());
84
+ } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e ) {
85
+ logger .error ("Failed to get key or trust store: " , e );
86
+ }
87
+ return keyStore ;
59
88
}
60
89
61
90
/**
@@ -64,8 +93,8 @@ public void establish(String host, int port, String channel, String qmName, Stri
64
93
* @param qmNqme - queue manager's name.
65
94
* @param connectionProperties - prepared structure with all parameters transformed into queue manager's format. See {@link #createMQConnectionParams(String, int, String, String, String, boolean)} for more info.
66
95
*/
67
- public void establish (String qmNqme , Hashtable <String , Object > connectionProperties ) throws MQException {
68
- queueManager = new MQQueueManager (qmNqme , connectionProperties );
96
+ public void establish (String qmNqme , Map <String , Object > connectionProperties ) throws MQException {
97
+ queueManager = new MQQueueManager (qmNqme , new Hashtable <>( connectionProperties ) );
69
98
}
70
99
71
100
/**
@@ -100,4 +129,5 @@ public MQTopic createTopic(String topic) throws MQException {
100
129
public MQQueueManager getQueueManager () {
101
130
return this .queueManager ;
102
131
}
132
+
103
133
}
0 commit comments