Skip to content

Commit

Permalink
Fix apache#3730 improve paho-mqtt5 ssl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng committed Apr 14, 2022
1 parent 6dfc138 commit ffb0f3c
Showing 1 changed file with 27 additions and 15 deletions.
Expand Up @@ -16,10 +16,12 @@
*/
package org.apache.camel.quarkus.component.paho.mqtt5.it;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.TimeUnit;

import javax.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -68,13 +70,17 @@ public class PahoMqtt5Resource {
public String consumePahoMessage(
@PathParam("protocol") String protocol,
@PathParam("queueName") String queueName) {
String tmpKeystore = null;

if ("ssl".equals(protocol)) {
setKeyStore(keystore, password);
tmpKeystore = setKeyStore(keystore, password);
}

String result = consumerTemplate.receiveBody("paho-mqtt5:" + queueName + "?brokerUrl=" + brokerUrl(protocol), 5000,
String.class);
if ("ssl".equals(protocol)) {
removeKeyStore(keystore);

if ("ssl".equals(protocol) && tmpKeystore != null) {
removeKeyStore(tmpKeystore);
}
return result;
}
Expand All @@ -86,14 +92,16 @@ public Response producePahoMessage(
@PathParam("protocol") String protocol,
@PathParam("queueName") String queueName,
String message) throws Exception {
String tmpKeystore = null;

if ("ssl".equals(protocol)) {
setKeyStore(keystore, password);
tmpKeystore = setKeyStore(keystore, password);
}
try {
producerTemplate.sendBody("paho-mqtt5:" + queueName + "?retained=true&brokerUrl=" + brokerUrl(protocol), message);
} finally {
if ("ssl".equals(protocol)) {
removeKeyStore(keystore);
if ("ssl".equals(protocol) && tmpKeystore != null) {
removeKeyStore(tmpKeystore);
}
}
return Response.created(new URI("https://camel.apache.org/")).build();
Expand Down Expand Up @@ -159,18 +167,22 @@ private String brokerUrl(String protocol) {
return ConfigProvider.getConfig().getValue("paho5.broker." + protocol + ".url", String.class);
}

private void setKeyStore(String keystore, String password) {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(keystore);
private String setKeyStore(String keystore, String password) {
String tmpKeystore = null;

try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(keystore);) {
tmpKeystore = File.createTempFile("keystore-", ".jks").getPath();
Files.copy(in, Paths.get(tmpKeystore), StandardCopyOption.REPLACE_EXISTING);

System.setProperty("javax.net.ssl.keyStore", tmpKeystore);
System.setProperty("javax.net.ssl.keyStorePassword", password);
System.setProperty("javax.net.ssl.trustStore", tmpKeystore);
System.setProperty("javax.net.ssl.trustStorePassword", password);

try {
Files.copy(in, Paths.get(keystore));
} catch (Exception e) {
} finally {
return tmpKeystore;
}

System.setProperty("javax.net.ssl.keyStore", keystore);
System.setProperty("javax.net.ssl.keyStorePassword", password);
System.setProperty("javax.net.ssl.trustStore", keystore);
System.setProperty("javax.net.ssl.trustStorePassword", password);
}

private void removeKeyStore(String keystore) {
Expand Down

0 comments on commit ffb0f3c

Please sign in to comment.