Skip to content

Commit ad724ab

Browse files
committed
Fix MQTT SSL Helpers to better handle misconfigurations
1 parent be8cfb4 commit ad724ab

File tree

1 file changed

+27
-22
lines changed
  • smallrye-reactive-messaging-mqtt/src/main/java/io/smallrye/reactive/messaging/mqtt

1 file changed

+27
-22
lines changed

smallrye-reactive-messaging-mqtt/src/main/java/io/smallrye/reactive/messaging/mqtt/MqttHelpers.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.smallrye.reactive.messaging.mqtt;
22

3+
import java.util.Optional;
34
import java.util.concurrent.TimeUnit;
45

56
import io.vertx.core.net.JksOptions;
@@ -46,17 +47,18 @@ static MqttClientOptions createMqttClientOptions(MqttConnectorCommonConfiguratio
4647
* Description: Set whether keystore type, location and password. In case of pem type the location and password are the cert
4748
* and key path.
4849
* Default Value: PfxOptions
49-
*
50+
*
5051
* @return the KeyCertOptions
5152
*/
5253
private static KeyCertOptions getKeyCertOptions(MqttConnectorCommonConfiguration config) {
53-
54-
if (config.getSsl() && config.getSslKeystoreLocation().isPresent()) {
55-
String keyStoreLocation = config.getSslKeystoreLocation().get();
54+
Optional<String> sslKeystoreLocation = config.getSslKeystoreLocation();
55+
Optional<String> sslKeystorePassword = config.getSslKeystorePassword();
56+
if (config.getSsl() && sslKeystoreLocation.isPresent()) {
57+
String keyStoreLocation = sslKeystoreLocation.get();
5658
String sslKeystoreType = config.getSslKeystoreType();
5759

58-
if (config.getSslKeystorePassword().isPresent()) {
59-
String keyStorePassword = config.getSslKeystorePassword().get();
60+
if (sslKeystorePassword.isPresent()) {
61+
String keyStorePassword = sslKeystorePassword.get();
6062
if ("jks".equalsIgnoreCase(sslKeystoreType)) {
6163
return new JksOptions()
6264
.setPath(keyStoreLocation)
@@ -84,34 +86,37 @@ private static KeyCertOptions getKeyCertOptions(MqttConnectorCommonConfiguration
8486
* Attribute Name: ssl.truststore
8587
* Description: Set whether keystore type, location and password. In case of pem type the location is the cert path.
8688
* Default Value: PfxOptions
87-
*
89+
*
8890
* @return the TrustOptions
8991
*/
9092

9193
private static TrustOptions getTrustOptions(MqttConnectorCommonConfiguration config) {
92-
93-
if (config.getSsl() && config.getSslTruststoreLocation().isPresent()) {
94-
String truststoreLocation = config.getSslTruststoreLocation().get();
94+
Optional<String> sslTruststoreLocation = config.getSslTruststoreLocation();
95+
Optional<String> sslTruststorePassword = config.getSslTruststorePassword();
96+
if (config.getSsl() && sslTruststoreLocation.isPresent()) {
97+
String truststoreLocation = sslTruststoreLocation.get();
9598
String truststoreType = config.getSslTruststoreType();
9699

97100
if ("pem".equalsIgnoreCase(truststoreType)) {
98101
return new PemTrustOptions()
99102
.addCertPath(truststoreLocation);
100-
} else if (config.getSslTruststorePassword().isPresent()) {
101-
String truststorePassword = config.getSslTruststorePassword().get();
102-
if ("jks".equalsIgnoreCase(truststoreType)) {
103-
return new JksOptions()
103+
} else {
104+
if (sslTruststorePassword.isPresent()) {
105+
String truststorePassword = sslTruststorePassword.get();
106+
if ("jks".equalsIgnoreCase(truststoreType)) {
107+
return new JksOptions()
108+
.setPath(truststoreLocation)
109+
.setPassword(truststorePassword);
110+
}
111+
// Default
112+
return new PfxOptions()
104113
.setPath(truststoreLocation)
105114
.setPassword(truststorePassword);
115+
} else {
116+
throw new IllegalArgumentException(
117+
"The attribute `ssl.keystore.password` on connector 'smallrye-mqtt' (channel: "
118+
+ config.getChannel() + ") must be set for `ssl.keystore.type`" + truststoreType);
106119
}
107-
// Default
108-
return new PfxOptions()
109-
.setPath(truststoreLocation)
110-
.setPassword(truststorePassword);
111-
} else {
112-
throw new IllegalArgumentException(
113-
"The attribute `ssl.keystore.password` on connector 'smallrye-mqtt' (channel: "
114-
+ config.getChannel() + ") must be set for `ssl.keystore.type`" + truststoreType);
115120
}
116121
}
117122
return null;

0 commit comments

Comments
 (0)