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 ;
12
27
13
28
/**
14
29
* Class represents MQ connection.
@@ -20,52 +35,71 @@ public class MQConnection {
20
35
/**
21
36
* Method creates connection properties Hashtable from connection parameters.
22
37
*
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.
38
+ * @param config - object containing different properties.
29
39
* @return - returns prepared structure with all parameters transformed into queue manager's format.
30
40
*/
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 );
41
+ public static Map <String , Object > createMQConnectionParams (Config config ) {
42
+ Map <String , Object > properties = new HashMap <>();
43
+ properties .put (MQConstants .TRANSPORT_PROPERTY , config .getQmgrHost () == null ? MQConstants .TRANSPORT_MQSERIES_BINDINGS : MQConstants .TRANSPORT_MQSERIES_CLIENT );
44
+ if (config .getQmgrHost () != null ) properties .put (MQConstants .HOST_NAME_PROPERTY , config .getQmgrHost ());
45
+ if (config .getQmgrPort () != 0 ) properties .put (MQConstants .PORT_PROPERTY , config .getQmgrPort ());
46
+ if (config .getQmgrChannel () != null ) properties .put (MQConstants .CHANNEL_PROPERTY , config .getQmgrChannel ());
47
+ if (config .getUser () != null || config .getPassword () != null ) {
48
+ if (config .useMqscp ()) properties .put (MQConstants .USE_MQCSP_AUTHENTICATION_PROPERTY , true );
49
+ if (config .getUser () != null ) properties .put (MQConstants .USER_ID_PROPERTY , config .getUser ());
50
+ if (config .getPassword () != null ) properties .put (MQConstants .PASSWORD_PROPERTY , config .getPassword ());
51
+ }
52
+ MQSecurityProperties mqSecurityProperties = config .getMqSecurityProperties ();
53
+ if (mqSecurityProperties != null && mqSecurityProperties .isUseTLS ()) {
54
+ properties .put (MQConstants .SSL_CIPHER_SUITE_PROPERTY , mqSecurityProperties .getCipherSuite ());
55
+ properties .put (MQConstants .SSL_SOCKET_FACTORY_PROPERTY , getSslSocketFactory (mqSecurityProperties ));
56
+ System .setProperty ("com.ibm.mq.cfg.useIBMCipherMappings" , "false" );
41
57
}
42
58
return properties ;
43
59
}
44
60
45
61
/**
46
- * Method establishes connection with queue manager .
62
+ * Method creates SSLSocketFactory from connection parameters .
47
63
*
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.
64
+ * @param mqSecurityProperties - object containing security properties.
65
+ * @return - returns prepared SSLSocketFactory.
55
66
*/
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 );
67
+ private static SSLSocketFactory getSslSocketFactory (MQSecurityProperties mqSecurityProperties ) {
68
+ KeyStore keyStore = getStore (mqSecurityProperties .getKeystorePath (), mqSecurityProperties .getKeystorePassword ());
69
+ KeyStore trustStore = getStore (mqSecurityProperties .getTruststorePath (), mqSecurityProperties .getTruststorePassword ());
70
+ SSLContext sslContext = null ;
71
+ try {
72
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
73
+ KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
74
+ trustManagerFactory .init (trustStore );
75
+ keyManagerFactory .init (keyStore , mqSecurityProperties .getKeystorePassword ().toCharArray ());
76
+ sslContext = SSLContext .getInstance (mqSecurityProperties .getSslProtocol ());
77
+ sslContext .init (keyManagerFactory .getKeyManagers (), trustManagerFactory .getTrustManagers (), null );
78
+ } catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyManagementException e1 ) {
79
+ logger .error ("Failed!" , e1 );
80
+ }
81
+ return sslContext .getSocketFactory ();
82
+ }
83
+
84
+ private static KeyStore getStore (String storePath , String storePassword ) {
85
+ KeyStore keyStore = null ;
86
+ try (FileInputStream keyStoreInput = new FileInputStream (storePath )) {
87
+ keyStore = KeyStore .getInstance ("JKS" );
88
+ keyStore .load (keyStoreInput , storePassword .toCharArray ());
89
+ } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e ) {
90
+ logger .error ("Failed to get key or trust store: " , e );
91
+ }
92
+ return keyStore ;
59
93
}
60
94
61
95
/**
62
96
* Method establishes connection with queue manager.
63
97
*
64
98
* @param qmNqme - queue manager's name.
65
- * @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.
99
+ * @param connectionProperties - prepared structure with all parameters transformed into queue manager's format. See {@link #createMQConnectionParams(Config config )} for more info.
66
100
*/
67
- public void establish (String qmNqme , Hashtable <String , Object > connectionProperties ) throws MQException {
68
- queueManager = new MQQueueManager (qmNqme , connectionProperties );
101
+ public void establish (String qmNqme , Map <String , Object > connectionProperties ) throws MQException {
102
+ queueManager = new MQQueueManager (qmNqme , new Hashtable <>( connectionProperties ) );
69
103
}
70
104
71
105
/**
@@ -100,4 +134,5 @@ public MQTopic createTopic(String topic) throws MQException {
100
134
public MQQueueManager getQueueManager () {
101
135
return this .queueManager ;
102
136
}
137
+
103
138
}
0 commit comments