Skip to content

Commit af59d67

Browse files
committed
MQTTS test simplified by using a default trusted ca-certificates by Java. This approach requires no craft for JKS file
1 parent 048b7cb commit af59d67

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/main/java/org/thingsboard/tools/service/shared/BaseMqttAPITest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import io.netty.handler.codec.mqtt.MqttQoS;
2222
import io.netty.handler.ssl.SslContext;
2323
import io.netty.handler.ssl.SslContextBuilder;
24+
import io.netty.handler.ssl.SslProvider;
2425
import io.netty.util.concurrent.Future;
2526
import lombok.extern.slf4j.Slf4j;
2627
import org.springframework.beans.factory.annotation.Value;
2728
import org.thingsboard.mqtt.MqttClient;
2829
import org.thingsboard.mqtt.MqttClientConfig;
2930
import org.thingsboard.mqtt.MqttConnectResult;
31+
import org.thingsboard.server.common.data.StringUtils;
3032
import org.thingsboard.tools.service.mqtt.DeviceClient;
3133
import org.thingsboard.tools.service.msg.Msg;
3234

@@ -195,19 +197,26 @@ private MqttClient initClient(String token) throws Exception {
195197

196198
private SslContext getSslContext() {
197199
if (mqttSslEnabled) {
200+
if (StringUtils.isNotBlank(mqttSslKeyStore)) {
201+
try {
202+
TrustManagerFactory trustFact = TrustManagerFactory.getInstance("SunX509");
203+
KeyStore trustStore = KeyStore.getInstance("JKS");
204+
FileInputStream stream = new FileInputStream(mqttSslKeyStore);
205+
trustStore.load(stream, mqttSslKeyStorePassword.toCharArray());
206+
trustFact.init(trustStore);
207+
return SslContextBuilder.forClient().trustManager(trustFact).build();
208+
} catch (Exception e) {
209+
log.warn("Error while initializing SSL context for keystore [{}]. Will try default SSLContext", mqttSslKeyStore, e);
210+
}
211+
}
212+
198213
try {
199-
TrustManagerFactory trustFact = TrustManagerFactory.getInstance("SunX509");
200-
KeyStore trustStore = KeyStore.getInstance("JKS");
201-
FileInputStream stream = new FileInputStream(mqttSslKeyStore);
202-
trustStore.load(stream, mqttSslKeyStorePassword.toCharArray());
203-
trustFact.init(trustStore);
204-
return SslContextBuilder.forClient().trustManager(trustFact).build();
214+
return SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build();
205215
} catch (Exception e) {
206-
throw new RuntimeException("Exception while creating SslContext", e);
216+
throw new RuntimeException("Error while initializing default SSL context", e);
207217
}
208-
} else {
209-
return null;
210218
}
219+
return null;
211220
}
212221

213222
protected void reportMqttClientsStats() {

src/main/resources/tb-ce-performance-tests.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ endpoints:
3535

3636
rest:
3737
connect_server: "${REST_CONNECT_SERVER:true}"
38+
# Http or https. Port can be omitted. Use the same web link as you usually login with your browser
3839
url: "${REST_URL:http://localhost:8080}"
3940
username: "${REST_USERNAME:tenant@thingsboard.org}"
4041
password: "${REST_PASSWORD:tenant}"
4142
pool_size: "${REST_POOL_SIZE:4}"
4243
mqtt:
44+
# For MQTT use hostname or IP. For MQTTS use only hostname
4345
host: "${MQTT_HOST:localhost}"
46+
# Usually 1883 for MQTT or 8883 for MQTTS
4447
port: "${MQTT_PORT:1883}"
4548
ssl:
4649
enabled: "${MQTT_SSL_ENABLED:false}"
47-
key_store: "${MQTT_SSL_KEY_STORE:mqttclient.jks}"
50+
# Java keystore file mqttclient.jks. If not set, it will use a default trusted certs for Java like ca-certificates pre-installed. Use custom JKS truststore only to deal with self-managed certificates
51+
key_store: "${MQTT_SSL_KEY_STORE:}"
4852
key_store_password: "${MQTT_SSL_KEY_STORE_PASSWORD:password}"
4953
lwm2m:
5054
recommended_ciphers: "${LWM2M_RECOMMENDED_CIPHERS:false}"

0 commit comments

Comments
 (0)