Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

Commit c84c4c3

Browse files
committed
Add option to disable SSL
1 parent 60ea8d7 commit c84c4c3

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

client/src/main/java/io/netifi/proteus/DefaultBuilderConfig.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
* default.
1717
*/
1818
final class DefaultBuilderConfig {
19+
private static final Config conf = ConfigFactory.load();
1920

2021
private DefaultBuilderConfig() {}
2122

23+
static boolean isSslDisabled() {
24+
return conf.hasPath("proteus.client.ssl.disabled")
25+
&& conf.getBoolean("proteus.client.ssl.disabled");
26+
}
27+
2228
static boolean getKeepAlive() {
2329
boolean keepalive = false;
2430
try {
25-
keepalive = ConfigHolder.conf.getBoolean("proteus.client.keepalive.enable");
31+
keepalive = conf.getBoolean("proteus.client.keepalive.enable");
2632
} catch (ConfigException.Missing m) {
2733

2834
}
@@ -33,7 +39,7 @@ static boolean getKeepAlive() {
3339
static long getTickPeriodSeconds() {
3440
long tickPeriodSeconds = 60;
3541
try {
36-
tickPeriodSeconds = ConfigHolder.conf.getLong("proteus.client.keepalive.tickPeriodSeconds");
42+
tickPeriodSeconds = conf.getLong("proteus.client.keepalive.tickPeriodSeconds");
3743
} catch (ConfigException.Missing m) {
3844

3945
}
@@ -44,7 +50,7 @@ static long getTickPeriodSeconds() {
4450
static long getAckTimeoutSeconds() {
4551
long ackTimeoutSeconds = 120;
4652
try {
47-
ackTimeoutSeconds = ConfigHolder.conf.getLong("proteus.client.keepalive.ackTimeoutSeconds");
53+
ackTimeoutSeconds = conf.getLong("proteus.client.keepalive.ackTimeoutSeconds");
4854
} catch (ConfigException.Missing m) {
4955

5056
}
@@ -55,7 +61,7 @@ static long getAckTimeoutSeconds() {
5561
static int getMissedAcks() {
5662
int missedAcks = 3;
5763
try {
58-
missedAcks = ConfigHolder.conf.getInt("proteus.client.keepalive.missedAcks");
64+
missedAcks = conf.getInt("proteus.client.keepalive.missedAcks");
5965
} catch (ConfigException.Missing m) {
6066
}
6167

@@ -65,7 +71,7 @@ static int getMissedAcks() {
6571
static String getHost() {
6672
String host = null;
6773
try {
68-
host = ConfigHolder.conf.getString("proteus.client.host");
74+
host = conf.getString("proteus.client.host");
6975
} catch (ConfigException.Missing m) {
7076

7177
}
@@ -76,7 +82,7 @@ static String getHost() {
7682
static int getPort() {
7783
int port = 8001;
7884
try {
79-
port = ConfigHolder.conf.getInt("proteus.client.port");
85+
port = conf.getInt("proteus.client.port");
8086
} catch (ConfigException.Missing m) {
8187

8288
}
@@ -87,7 +93,7 @@ static int getPort() {
8793
static String getGroup() {
8894
String group = null;
8995
try {
90-
group = ConfigHolder.conf.getString("proteus.client.group");
96+
group = conf.getString("proteus.client.group");
9197
} catch (ConfigException.Missing m) {
9298

9399
}
@@ -98,7 +104,7 @@ static String getGroup() {
98104
static String getDestination() {
99105
String destination = null;
100106
try {
101-
destination = ConfigHolder.conf.getString("proteus.client.destination");
107+
destination = conf.getString("proteus.client.destination");
102108
} catch (ConfigException.Missing m) {
103109

104110
}
@@ -110,7 +116,7 @@ static Long getAccountId() {
110116
Long accountId = null;
111117

112118
try {
113-
accountId = ConfigHolder.conf.getLong("proteus.client.accountId");
119+
accountId = conf.getLong("proteus.client.accountId");
114120
} catch (ConfigException.Missing m) {
115121

116122
}
@@ -122,7 +128,7 @@ static Long getAccessKey() {
122128
Long accessKey = null;
123129

124130
try {
125-
accessKey = ConfigHolder.conf.getLong("proteus.client.accessKey");
131+
accessKey = conf.getLong("proteus.client.accessKey");
126132
} catch (ConfigException.Missing m) {
127133

128134
}
@@ -134,7 +140,7 @@ static String getAccessToken() {
134140
String accessToken = null;
135141

136142
try {
137-
accessToken = ConfigHolder.conf.getString("proteus.client.accessToken");
143+
accessToken = conf.getString("proteus.client.accessToken");
138144
} catch (ConfigException.Missing m) {
139145

140146
}
@@ -145,7 +151,7 @@ static String getAccessToken() {
145151
static int getPoolSize() {
146152
int poolSize = Math.min(4, Runtime.getRuntime().availableProcessors());
147153
try {
148-
poolSize = ConfigHolder.conf.getInt("proteus.client.poolSize");
154+
poolSize = conf.getInt("proteus.client.poolSize");
149155
} catch (ConfigException.Missing m) {
150156
}
151157
return poolSize;
@@ -154,7 +160,7 @@ static int getPoolSize() {
154160
static int getMinHostsAtStartup() {
155161
int minHostsAtStartup = 3;
156162
try {
157-
minHostsAtStartup = ConfigHolder.conf.getInt("proteus.client.minHostsAtStartup");
163+
minHostsAtStartup = conf.getInt("proteus.client.minHostsAtStartup");
158164
} catch (ConfigException.Missing m) {
159165
}
160166
return minHostsAtStartup;
@@ -165,7 +171,7 @@ static long getMinHostsAtStartupTimeoutSeconds() {
165171

166172
try {
167173
minHostsAtStartupTimeout =
168-
ConfigHolder.conf.getLong("proteus.client.minHostsAtStartupTimeout");
174+
conf.getLong("proteus.client.minHostsAtStartupTimeout");
169175
} catch (ConfigException.Missing m) {
170176
}
171177
return minHostsAtStartupTimeout;
@@ -175,7 +181,7 @@ static String getMetricHandlerGroup() {
175181
String metricHandlerGroup = "netifi.metrics";
176182
try {
177183
metricHandlerGroup =
178-
ConfigHolder.conf.getString("proteus.client.metrics.group");
184+
conf.getString("proteus.client.metrics.group");
179185
} catch (ConfigException.Missing m) {
180186
}
181187
return metricHandlerGroup;
@@ -186,7 +192,7 @@ static int getBatchSize() {
186192

187193
try {
188194
batchSize =
189-
ConfigHolder.conf.getInt("proteus.client.metrics.metricBatchSize");
195+
conf.getInt("proteus.client.metrics.metricBatchSize");
190196
} catch (ConfigException.Missing m) {
191197
}
192198
return batchSize;
@@ -197,7 +203,7 @@ static long getExportFrequencySeconds() {
197203

198204
try {
199205
exportFrequencySeconds =
200-
ConfigHolder.conf.getLong("proteus.client.metrics.frequency");
206+
conf.getLong("proteus.client.metrics.frequency");
201207
} catch (ConfigException.Missing m) {
202208
}
203209
return exportFrequencySeconds;
@@ -209,7 +215,7 @@ static boolean getExportSystemMetrics() {
209215

210216
try {
211217
exportSystemMetrics =
212-
ConfigHolder.conf.getBoolean("proteus.client.metrics.exportSystemMetrics");
218+
conf.getBoolean("proteus.client.metrics.exportSystemMetrics");
213219
} catch (ConfigException.Missing m) {
214220
}
215221
return exportSystemMetrics;
@@ -219,7 +225,7 @@ static boolean getExportSystemMetrics() {
219225
static List<SocketAddress> getSeedAddress() {
220226
List<SocketAddress> seedAddresses = null;
221227
try {
222-
String s = ConfigHolder.conf.getString("proteus.client.seedAddresses");
228+
String s = conf.getString("proteus.client.seedAddresses");
223229
if (s != null) {
224230
seedAddresses = new ArrayList<>();
225231
String[] split = s.split(",");
@@ -244,12 +250,4 @@ static List<SocketAddress> getSeedAddress() {
244250

245251
return seedAddresses;
246252
}
247-
248-
private static class ConfigHolder {
249-
private static final Config conf;
250-
251-
static {
252-
conf = ConfigFactory.load();
253-
}
254-
}
255253
}

client/src/main/java/io/netifi/proteus/Proteus.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public static class Builder {
135135
private String destination = DefaultBuilderConfig.getDestination();
136136
private String accessToken = DefaultBuilderConfig.getAccessToken();
137137
private byte[] accessTokenBytes = new byte[20];
138+
private boolean sslDisabled = DefaultBuilderConfig.isSslDisabled();
138139
private boolean keepalive = DefaultBuilderConfig.getKeepAlive();
139140
private long tickPeriodSeconds = DefaultBuilderConfig.getTickPeriodSeconds();
140141
private long ackTimeoutSeconds = DefaultBuilderConfig.getAckTimeoutSeconds();
@@ -156,6 +157,11 @@ public Builder poolSize(int poolSize) {
156157
return this;
157158
}
158159

160+
public Builder sslDisabled(boolean sslDisabled) {
161+
this.sslDisabled = sslDisabled;
162+
return this;
163+
}
164+
159165
public Builder keepalive(boolean useKeepAlive) {
160166
this.keepalive = useKeepAlive;
161167
return this;
@@ -244,30 +250,40 @@ public Proteus build() {
244250

245251
if (clientTransportFactory == null) {
246252
logger.info("Client transport factory not provided; using TCP transport.");
247-
try {
248-
final SslProvider sslProvider;
249-
if (OpenSsl.isAvailable()) {
250-
logger.info("Native SSL provider is available; will use native provider.");
251-
sslProvider = SslProvider.OPENSSL_REFCNT;
252-
} else {
253-
logger.info("Native SSL provider not available; will use JDK SSL provider.");
254-
sslProvider = SslProvider.JDK;
255-
}
256-
final SslContext sslContext =
257-
SslContextBuilder.forClient()
258-
.trustManager(InsecureTrustManagerFactory.INSTANCE)
259-
.sslProvider(sslProvider)
260-
.build();
253+
if (sslDisabled) {
261254
clientTransportFactory = address -> {
262255
TcpClient client =
263256
TcpClient.create(
264257
opts ->
265-
opts.connectAddress(() -> address)
266-
.sslContext(sslContext));
258+
opts.connectAddress(() -> address));
267259
return TcpClientTransport.create(client);
268260
};
269-
} catch (Exception sslException) {
270-
throw Exceptions.bubble(sslException);
261+
} else {
262+
try {
263+
final SslProvider sslProvider;
264+
if (OpenSsl.isAvailable()) {
265+
logger.info("Native SSL provider is available; will use native provider.");
266+
sslProvider = SslProvider.OPENSSL_REFCNT;
267+
} else {
268+
logger.info("Native SSL provider not available; will use JDK SSL provider.");
269+
sslProvider = SslProvider.JDK;
270+
}
271+
final SslContext sslContext =
272+
SslContextBuilder.forClient()
273+
.trustManager(InsecureTrustManagerFactory.INSTANCE)
274+
.sslProvider(sslProvider)
275+
.build();
276+
clientTransportFactory = address -> {
277+
TcpClient client =
278+
TcpClient.create(
279+
opts ->
280+
opts.connectAddress(() -> address)
281+
.sslContext(sslContext));
282+
return TcpClientTransport.create(client);
283+
};
284+
} catch (Exception sslException) {
285+
throw Exceptions.bubble(sslException);
286+
}
271287
}
272288
}
273289

0 commit comments

Comments
 (0)