Skip to content

Commit 3bc45af

Browse files
committed
add TLS support
1 parent a64f23d commit 3bc45af

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

src/main/java/ru/cinimex/exporter/Config.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public Config(String path) {
6868
this.usePCFWildcards = (boolean) pcfParameters.get("usePCFWildcards");
6969
this.scrapeInterval = (Integer) pcfParameters.get("scrapeInterval");
7070
boolean useTLS = (boolean) qmgrConnectionParams.get("useTLS");
71-
if (useTLS)
71+
if (useTLS) {
72+
logger.info("secured connection to queue manager will be used");
7273
mqSecurityProperties = new MQSecurityProperties(
7374
useTLS,
7475
(String) qmgrConnectionParams.get("keystorePath"),
@@ -78,6 +79,9 @@ public Config(String path) {
7879
(String) qmgrConnectionParams.get("sslProtocol"),
7980
(String) qmgrConnectionParams.get("cipherSuite")
8081
);
82+
} else {
83+
logger.info("unsecured connection to queue manager will be used");
84+
}
8185
logger.info("Successfully parsed configuration file!");
8286
}
8387

src/main/java/ru/cinimex/exporter/mq/MQConnection.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.HashMap;
2525
import java.util.Hashtable;
2626
import java.util.Map;
27-
import java.util.Optional;
2827

2928
/**
3029
* Class represents MQ connection.
@@ -36,7 +35,7 @@ public class MQConnection {
3635
/**
3736
* Method creates connection properties Hashtable from connection parameters.
3837
*
39-
* @param config - config.
38+
* @param config - object containing different properties.
4039
* @return - returns prepared structure with all parameters transformed into queue manager's format.
4140
*/
4241
public static Map<String, Object> createMQConnectionParams(Config config) {
@@ -52,30 +51,36 @@ public static Map<String, Object> createMQConnectionParams(Config config) {
5251
}
5352
MQSecurityProperties mqSecurityProperties = config.getMqSecurityProperties();
5453
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-
7254
properties.put(MQConstants.SSL_CIPHER_SUITE_PROPERTY, mqSecurityProperties.getCipherSuite());
73-
properties.put(MQConstants.SSL_SOCKET_FACTORY_PROPERTY, sslSocketFactory);
55+
properties.put(MQConstants.SSL_SOCKET_FACTORY_PROPERTY, getSslSocketFactory(mqSecurityProperties));
7456
System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
7557
}
7658
return properties;
7759
}
7860

61+
/**
62+
* Method creates SSLSocketFactory from connection parameters.
63+
*
64+
* @param mqSecurityProperties - object containing security properties.
65+
* @return - returns prepared SSLSocketFactory.
66+
*/
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+
7984
private static KeyStore getStore(String storePath, String storePassword) {
8085
KeyStore keyStore = null;
8186
try (FileInputStream keyStoreInput = new FileInputStream(storePath)) {
@@ -91,7 +96,7 @@ private static KeyStore getStore(String storePath, String storePassword) {
9196
* Method establishes connection with queue manager.
9297
*
9398
* @param qmNqme - queue manager's name.
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.
99+
* @param connectionProperties - prepared structure with all parameters transformed into queue manager's format. See {@link #createMQConnectionParams(Config config)} for more info.
95100
*/
96101
public void establish(String qmNqme, Map<String, Object> connectionProperties) throws MQException {
97102
queueManager = new MQQueueManager(qmNqme, new Hashtable<>(connectionProperties));

src/main/java/ru/cinimex/exporter/mq/MQSubscriberManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MQSubscriberManager {
2424
/**
2525
* Constructor sets params for connecting to target queue manager.
2626
*
27-
* @param config - config.
27+
* @param config - object containing different properties
2828
*/
2929
public MQSubscriberManager(Config config) {
3030
connectionProperties = MQConnection.createMQConnectionParams(config);
@@ -154,12 +154,12 @@ private void addPCFSubscribers(Map<MQObject.MQType, ArrayList<MQObject>> objects
154154
*/
155155
private void addPCFSubscribers(List<MQObject> objects, int interval) {
156156
int corePoolSize = objects.size();
157-
ScheduledExecutorService executor = Executors.newScheduledThreadPool(corePoolSize);
157+
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(corePoolSize);
158158
for (MQObject object : objects) {
159159
MQPCFSubscriber subscriber = new MQPCFSubscriber(queueManagerName, new Hashtable<>(connectionProperties), object);
160160
subscribers.add(subscriber);
161161
logger.debug("Starting subscriber for sending direct PCF commands to retrieve statistics about object with type {} and name {}.", object.getType().name(), object.getName());
162-
executor.scheduleAtFixedRate(subscriber, 0, interval, TimeUnit.SECONDS);
162+
scheduledExecutorService.scheduleAtFixedRate(subscriber, 0, interval, TimeUnit.SECONDS);
163163
logger.debug("Subscriber for sending direct PCF commands to retrieve statistics about object with type {} and name {} successfully started.", object.getType().name(), object.getName());
164164
}
165165
}

src/main/java/ru/cinimex/exporter/mq/MQTopicSubscriber.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void stopProcessing() {
8484
/**
8585
* Starts subscriber.
8686
*/
87+
@Override
8788
public void run() {
8889
try {
8990
topic = connection.createTopic(element.getTopicString());

src/main/resources/exporter_config.yaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ qmgrConnectionParams:
1919
# How long to wait until metrics are published by queue manager (milliseconds).
2020
# Value must be at least 10000 (periodicity with which metrics are published by MQ).
2121
connTimeout: 12000
22-
#
22+
# Use TLS connection to queue manager?
2323
useTLS: true
24-
#
25-
keystorePath: /path
26-
#
27-
keystorePassword: qweqwe
28-
#
29-
truststorePath: /path
30-
#
31-
truststorePassword: qweqwe
32-
#
24+
# Path to keystore file
25+
keystorePath: /opt/mq_exporter/keystores/keystore.jks
26+
# keystore password
27+
keystorePassword: testpass2
28+
# path to truststore file
29+
truststorePath: /opt/mq_exporter/keystores/truststore.jks
30+
# truststore password
31+
truststorePassword: testpass2
32+
# SSL protocol
3333
sslProtocol: TLSv1.2
34-
# or SSL_RSA_WITH_AES_256_CBC_SHA256?
34+
# cipherSuite
3535
cipherSuite: TLS_RSA_WITH_AES_256_CBC_SHA256
3636

3737
# Prometheus connection information -------------------------------

0 commit comments

Comments
 (0)