Skip to content

Commit 2bc5767

Browse files
committed
Updated SplitClientConfig
1 parent 193f3c4 commit 2bc5767

File tree

3 files changed

+196
-1
lines changed

3 files changed

+196
-1
lines changed

client/src/main/java/io/split/client/SplitClientConfig.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.split.client;
22

3+
import io.split.client.dtos.ProxyMTLSAuth;
34
import io.split.client.impressions.ImpressionListener;
45
import io.split.client.impressions.ImpressionsManager;
56
import io.split.client.utils.FileTypeEnum;
@@ -85,6 +86,8 @@ public class SplitClientConfig {
8586
private final HttpHost _proxy;
8687
private final String _proxyUsername;
8788
private final String _proxyPassword;
89+
private final String _proxyToken;
90+
private final ProxyMTLSAuth _proxyMtlsAuth;
8891

8992
// To be set during startup
9093
public static String splitSdkVersion;
@@ -118,6 +121,8 @@ private SplitClientConfig(String endpoint,
118121
HttpHost proxy,
119122
String proxyUsername,
120123
String proxyPassword,
124+
String proxyToken,
125+
ProxyMTLSAuth proxyMtlsAuth,
121126
int eventsQueueSize,
122127
long eventSendIntervalInMillis,
123128
int maxStringLength,
@@ -171,6 +176,8 @@ private SplitClientConfig(String endpoint,
171176
_proxy = proxy;
172177
_proxyUsername = proxyUsername;
173178
_proxyPassword = proxyPassword;
179+
_proxyToken = proxyToken;
180+
_proxyMtlsAuth = proxyMtlsAuth;
174181
_eventsQueueSize = eventsQueueSize;
175182
_eventSendIntervalInMillis = eventSendIntervalInMillis;
176183
_maxStringLength = maxStringLength;
@@ -302,6 +309,14 @@ public String proxyPassword() {
302309
return _proxyPassword;
303310
}
304311

312+
public String proxyToken() {
313+
return _proxyToken;
314+
}
315+
316+
public ProxyMTLSAuth proxyMTLSAuth() {
317+
return _proxyMtlsAuth;
318+
}
319+
305320
public long eventSendIntervalInMillis() {
306321
return _eventSendIntervalInMillis;
307322
}
@@ -417,8 +432,8 @@ public boolean isSdkEndpointOverridden() {
417432
}
418433

419434
public CustomHttpModule alternativeHTTPModule() { return _alternativeHTTPModule; }
420-
public static final class Builder {
421435

436+
public static final class Builder {
422437
private String _endpoint = SDK_ENDPOINT;
423438
private boolean _endpointSet = false;
424439
private String _eventsEndpoint = EVENTS_ENDPOINT;
@@ -442,6 +457,8 @@ public static final class Builder {
442457
private int _proxyPort = -1;
443458
private String _proxyUsername;
444459
private String _proxyPassword;
460+
private String _proxyToken;
461+
private ProxyMTLSAuth _proxyMtlsAuth;
445462
private int _eventsQueueSize = 500;
446463
private long _eventSendIntervalInMillis = 30 * (long)1000;
447464
private int _maxStringLength = 250;
@@ -776,6 +793,28 @@ public Builder proxyPassword(String proxyPassword) {
776793
return this;
777794
}
778795

796+
/**
797+
* Set the token for authentication against the proxy (if proxy settings are enabled). (Optional).
798+
*
799+
* @param proxyToken
800+
* @return this builder
801+
*/
802+
public Builder proxyToken(String proxyToken) {
803+
_proxyToken = proxyToken;
804+
return this;
805+
}
806+
807+
/**
808+
* Set the mtls authentication against the proxy (if proxy settings are enabled). (Optional).
809+
*
810+
* @param proxyMtlsAuth
811+
* @return this builder
812+
*/
813+
public Builder proxyMtlsAuth(ProxyMTLSAuth proxyMtlsAuth) {
814+
_proxyMtlsAuth = proxyMtlsAuth;
815+
return this;
816+
}
817+
779818
/**
780819
* Disables running destroy() on shutdown by default.
781820
*
@@ -1096,6 +1135,34 @@ private void verifyAlternativeClient() {
10961135
}
10971136
}
10981137

1138+
private void verifyProxy() {
1139+
if (_proxyPort == -1) {
1140+
return;
1141+
}
1142+
1143+
if (_proxyUsername == null && _proxyToken == null && _proxyMtlsAuth == null) {
1144+
return;
1145+
}
1146+
1147+
if (_proxyUsername != null && _proxyToken != null) {
1148+
throw new IllegalArgumentException("Proxy user and Proxy token params are updated, set only one param.");
1149+
}
1150+
1151+
if (_proxyUsername != null && _proxyMtlsAuth != null) {
1152+
throw new IllegalArgumentException("Proxy user and Proxy mTLS params are updated, set only one param.");
1153+
}
1154+
1155+
if (_proxyToken != null && _proxyMtlsAuth != null) {
1156+
throw new IllegalArgumentException("Proxy token and Proxy mTLS params are updated, set only one param.");
1157+
}
1158+
1159+
if (_proxyMtlsAuth != null) {
1160+
if (_proxyMtlsAuth.getP12File() == null || _proxyMtlsAuth.getP12FilePassKey() == null) {
1161+
throw new IllegalArgumentException("Proxy mTLS must have p12 file path and name, and pass phrase.");
1162+
}
1163+
}
1164+
}
1165+
10991166
public SplitClientConfig build() {
11001167

11011168
verifyRates();
@@ -1108,6 +1175,8 @@ public SplitClientConfig build() {
11081175

11091176
verifyAlternativeClient();
11101177

1178+
verifyProxy();
1179+
11111180
if (_numThreadsForSegmentFetch <= 0) {
11121181
throw new IllegalArgumentException("Number of threads for fetching segments MUST be greater than zero");
11131182
}
@@ -1133,6 +1202,8 @@ public SplitClientConfig build() {
11331202
proxy(),
11341203
_proxyUsername,
11351204
_proxyPassword,
1205+
_proxyToken,
1206+
_proxyMtlsAuth,
11361207
_eventsQueueSize,
11371208
_eventSendIntervalInMillis,
11381209
_maxStringLength,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.split.client.dtos;
2+
3+
public class ProxyMTLSAuth {
4+
private final String _proxyP12File;
5+
private final String _proxyP12FilePassKey;
6+
7+
private ProxyMTLSAuth(String proxyP12File, String proxyP12FilePassKey) {
8+
_proxyP12File = proxyP12File;
9+
_proxyP12FilePassKey = proxyP12FilePassKey;
10+
}
11+
12+
public String getP12File() { return _proxyP12File; }
13+
14+
public String getP12FilePassKey() { return _proxyP12FilePassKey; }
15+
16+
public static ProxyMTLSAuth.Builder builder() {
17+
return new ProxyMTLSAuth.Builder();
18+
}
19+
20+
public static class Builder {
21+
private String _p12File;
22+
private String _p12FilePassKey;
23+
24+
public Builder() {
25+
}
26+
27+
public ProxyMTLSAuth.Builder proxyP12File(String p12File) {
28+
_p12File = p12File;
29+
return this;
30+
}
31+
32+
public ProxyMTLSAuth.Builder proxyP12FilePassKey(String p12FilePassKey) {
33+
_p12FilePassKey = p12FilePassKey;
34+
return this;
35+
}
36+
37+
public ProxyMTLSAuth build() {
38+
return new ProxyMTLSAuth(_p12File, _p12FilePassKey);
39+
}
40+
}
41+
}

client/src/test/java/io/split/client/SplitClientConfigTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.split.client;
22

33
import com.google.common.util.concurrent.ThreadFactoryBuilder;
4+
import io.split.client.dtos.ProxyMTLSAuth;
45
import io.split.client.impressions.Impression;
56
import io.split.client.impressions.ImpressionListener;
67
import io.split.client.impressions.ImpressionsManager;
@@ -252,6 +253,88 @@ public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
252253

253254
SplitClientConfig config2 = SplitClientConfig.builder().build();
254255
Assert.assertNull(config2.customHeaderDecorator());
256+
}
257+
258+
@Test
259+
public void checkProxyParams() {
260+
SplitClientConfig config = SplitClientConfig.builder()
261+
.proxyHost("proxy-host")
262+
.proxyPort(8888).build();
263+
Assert.assertEquals("proxy-host", config.proxy().getHostName());
264+
Assert.assertEquals(8888, config.proxy().getPort());
265+
266+
config = SplitClientConfig.builder()
267+
.proxyHost("proxy-host")
268+
.proxyPort(8888)
269+
.proxyUsername("user")
270+
.proxyPassword("pass")
271+
.build();
272+
Assert.assertEquals("user", config.proxyUsername());
273+
Assert.assertEquals("pass", config.proxyPassword());
255274

275+
config = SplitClientConfig.builder()
276+
.proxyHost("proxy-host")
277+
.proxyPort(8888)
278+
.proxyToken("my-token")
279+
.build();
280+
Assert.assertEquals("my-token", config.proxyToken());
281+
282+
config = SplitClientConfig.builder()
283+
.proxyHost("proxy-host")
284+
.proxyPort(8888)
285+
.proxyMtlsAuth(new ProxyMTLSAuth.Builder().proxyP12File("path/to/file").proxyP12FilePassKey("pass-key").build())
286+
.build();
287+
Assert.assertEquals("path/to/file", config.proxyMTLSAuth().getP12File());
288+
Assert.assertEquals("pass-key", config.proxyMTLSAuth().getP12FilePassKey());
289+
}
290+
291+
@Test(expected = IllegalArgumentException.class)
292+
public void cannotUseProxyTokenAndProxyUsername() {
293+
SplitClientConfig.builder()
294+
.proxyHost("proxy-host")
295+
.proxyPort(8888)
296+
.proxyUsername("user")
297+
.proxyPassword("pass")
298+
.proxyToken("my-token")
299+
.build();
300+
}
301+
302+
@Test(expected = IllegalArgumentException.class)
303+
public void cannotUseProxyUserAndProxyMtls() {
304+
SplitClientConfig.builder()
305+
.proxyHost("proxy-host")
306+
.proxyPort(8888)
307+
.proxyUsername("user")
308+
.proxyPassword("pass")
309+
.proxyMtlsAuth(new ProxyMTLSAuth.Builder().proxyP12File("path/to/file").proxyP12FilePassKey("pass-key").build())
310+
.build();
311+
}
312+
313+
@Test(expected = IllegalArgumentException.class)
314+
public void cannotUseProxyTokenAndProxyMtls() {
315+
SplitClientConfig.builder()
316+
.proxyHost("proxy-host")
317+
.proxyPort(8888)
318+
.proxyToken("my-token")
319+
.proxyMtlsAuth(new ProxyMTLSAuth.Builder().proxyP12File("path/to/file").proxyP12FilePassKey("pass-key").build())
320+
.build();
321+
}
322+
323+
@Test(expected = IllegalArgumentException.class)
324+
public void mustUseP12FileWithProxyMtls() {
325+
SplitClientConfig.builder()
326+
.proxyHost("proxy-host")
327+
.proxyPort(8888)
328+
.proxyMtlsAuth(new ProxyMTLSAuth.Builder().proxyP12FilePassKey("pass-key").build())
329+
.build();
330+
}
331+
332+
@Test(expected = IllegalArgumentException.class)
333+
public void mustUseP12PassKeyWithProxyMtls() {
334+
SplitClientConfig.builder()
335+
.proxyHost("proxy-host")
336+
.proxyPort(8888)
337+
.proxyMtlsAuth(new ProxyMTLSAuth.Builder().proxyP12File("path/to/file").build())
338+
.build();
256339
}
257340
}

0 commit comments

Comments
 (0)