Skip to content

Commit f133ee6

Browse files
committed
Support TLS options on the command line
E.g. to support client/server authentication. Fixes #35
1 parent 169faa6 commit f133ee6

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
package com.rabbitmq.perf;
1717

18+
import java.security.NoSuchAlgorithmException;
1819
import java.text.SimpleDateFormat;
19-
import java.util.Arrays;
2020
import java.util.Calendar;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.Properties;
2324

2425
import org.apache.commons.cli.CommandLine;
2526
import org.apache.commons.cli.CommandLineParser;
@@ -30,11 +31,17 @@
3031
import org.apache.commons.cli.ParseException;
3132

3233
import com.rabbitmq.client.ConnectionFactory;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
36+
37+
import javax.net.ssl.SSLContext;
3338

3439
import static java.util.Arrays.asList;
3540

3641
public class PerfTest {
3742

43+
private static final Logger LOGGER = LoggerFactory.getLogger(PerfTest.class);
44+
3845
public static void main(String[] args) {
3946
Options options = getOptions();
4047
CommandLineParser parser = new GnuParser();
@@ -100,7 +107,12 @@ public static void main(String[] args) {
100107
flags.contains("immediate")),
101108
confirm != -1);
102109

110+
SSLContext sslContext = getSslContextIfNecessary(cmd, System.getProperties());
111+
103112
ConnectionFactory factory = new ConnectionFactory();
113+
if (sslContext != null) {
114+
factory.useSslProtocol(sslContext);
115+
}
104116
factory.setShutdownTimeout(0); // So we still shut down even with slow consumers
105117
factory.setUri(uris.get(0));
106118
factory.setRequestedFrameMax(frameMax);
@@ -150,6 +162,26 @@ public static void main(String[] args) {
150162
}
151163
}
152164

165+
private static SSLContext getSslContextIfNecessary(CommandLine cmd, Properties systemProperties) throws NoSuchAlgorithmException {
166+
SSLContext sslContext = null;
167+
if (cmd.hasOption("useDefaultSslContext")) {
168+
LOGGER.info("Using default SSL context as per command line option");
169+
sslContext = SSLContext.getDefault();
170+
}
171+
for (String propertyName : systemProperties.stringPropertyNames()) {
172+
if (propertyName != null && isPropertyTlsRelated(propertyName)) {
173+
LOGGER.info("TLS related system properties detected, using default SSL context");
174+
sslContext = SSLContext.getDefault();
175+
break;
176+
}
177+
}
178+
return sslContext;
179+
}
180+
181+
private static boolean isPropertyTlsRelated(String propertyName) {
182+
return propertyName.startsWith("javax.net.ssl") || propertyName.startsWith("jdk.tls");
183+
}
184+
153185
private static void usage(Options options) {
154186
HelpFormatter formatter = new HelpFormatter();
155187
formatter.printHelp("<program>", options);
@@ -192,7 +224,7 @@ private static Options getOptions() {
192224
options.addOption(new Option("p", "predeclared", false,"allow use of predeclared objects"));
193225
options.addOption(new Option("B", "body", true, "comma-separated list of files to use in message bodies"));
194226
options.addOption(new Option("T", "bodyContenType", true, "body content-type"));
195-
227+
options.addOption(new Option("useDefaultSslContext", "useDefaultSslContext", false,"use JVM default SSL context"));
196228
return options;
197229
}
198230

0 commit comments

Comments
 (0)