11package io .split .client ;
22
3+ import io .split .client .dtos .ProxyMTLSAuth ;
34import io .split .client .impressions .ImpressionsManager ;
45import io .split .client .utils .FileTypeEnum ;
56import io .split .integrations .IntegrationsConfig ;
7+ import io .split .service .SplitHttpClientImpl ;
68import io .split .storages .enums .OperationMode ;
79import io .split .storages .pluggable .domain .UserStorageWrapper ;
810import io .split .telemetry .storage .TelemetryStorage ;
911import io .split .telemetry .synchronizer .TelemetrySynchronizer ;
1012import junit .framework .TestCase ;
13+ import org .apache .hc .client5 .http .auth .AuthScope ;
14+ import org .apache .hc .client5 .http .auth .BearerToken ;
15+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
16+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
17+ import org .apache .hc .client5 .http .impl .io .DefaultHttpClientConnectionOperator ;
18+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
19+ import org .apache .hc .client5 .http .impl .routing .DefaultProxyRoutePlanner ;
20+ import org .apache .hc .core5 .http .HttpHost ;
21+ import org .apache .hc .core5 .http .config .Registry ;
1122import org .awaitility .Awaitility ;
1223import org .junit .Assert ;
13- import org .junit .Ignore ;
1424import org .junit .Test ;
1525import org .mockito .Mockito ;
1626import static org .mockito .Mockito .when ;
2434import java .lang .reflect .Method ;
2535import java .lang .reflect .Modifier ;
2636import java .net .URISyntaxException ;
37+ import java .util .HashMap ;
38+ import java .util .concurrent .ConcurrentHashMap ;
2739import java .util .concurrent .TimeUnit ;
2840
2941
@@ -87,12 +99,12 @@ public void testFactoryInstantiationIntegrationsConfig() throws Exception {
8799 }
88100
89101 @ Test
90- public void testFactoryInstantiationWithProxy () throws Exception {
102+ public void testFactoryInstantiationWithProxyCredentials () throws Exception {
91103 SplitClientConfig splitClientConfig = SplitClientConfig .builder ()
92104 .enableDebug ()
93105 .impressionsMode (ImpressionsManager .Mode .DEBUG )
94106 .impressionsRefreshRate (1 )
95- .endpoint (ENDPOINT ,EVENTS_ENDPOINT )
107+ .endpoint (ENDPOINT , EVENTS_ENDPOINT )
96108 .telemetryURL (SplitClientConfig .TELEMETRY_ENDPOINT )
97109 .authServiceURL (AUTH_SERVICE )
98110 .setBlockUntilReadyTimeout (1000 )
@@ -102,9 +114,150 @@ public void testFactoryInstantiationWithProxy() throws Exception {
102114 .proxyHost (ENDPOINT )
103115 .build ();
104116 SplitFactoryImpl splitFactory = new SplitFactoryImpl (API_KEY , splitClientConfig );
105-
106117 assertNotNull (splitFactory .client ());
107118 assertNotNull (splitFactory .manager ());
119+
120+ Field splitHttpClientField = SplitFactoryImpl .class .getDeclaredField ("_splitHttpClient" );
121+ splitHttpClientField .setAccessible (true );
122+ SplitHttpClientImpl client = (SplitHttpClientImpl ) splitHttpClientField .get (splitFactory );
123+
124+ Field httpClientField = SplitHttpClientImpl .class .getDeclaredField ("_client" );
125+ httpClientField .setAccessible (true );
126+ Class <?> InternalHttp = Class .forName ("org.apache.hc.client5.http.impl.classic.InternalHttpClient" );
127+
128+ Field routePlannerField = InternalHttp .getDeclaredField ("routePlanner" );
129+ routePlannerField .setAccessible (true );
130+ DefaultProxyRoutePlanner routePlanner = (DefaultProxyRoutePlanner ) routePlannerField .get (InternalHttp .cast (httpClientField .get (client )));
131+
132+ Field proxyField = DefaultProxyRoutePlanner .class .getDeclaredField ("proxy" );
133+ proxyField .setAccessible (true );
134+ HttpHost proxy = (HttpHost ) proxyField .get (routePlanner );
135+
136+ Assert .assertEquals ("http" , proxy .getSchemeName ());
137+ Assert .assertEquals (ENDPOINT , proxy .getHostName ());
138+ Assert .assertEquals (6060 , proxy .getPort ());
139+
140+ Field credentialsProviderField = InternalHttp .getDeclaredField ("credentialsProvider" );
141+ credentialsProviderField .setAccessible (true );
142+ BasicCredentialsProvider credentialsProvider = (BasicCredentialsProvider ) credentialsProviderField .get (InternalHttp .cast (httpClientField .get (client )));
143+
144+ Field credMapField = BasicCredentialsProvider .class .getDeclaredField ("credMap" );
145+ credMapField .setAccessible (true );
146+ ConcurrentHashMap <AuthScope , UsernamePasswordCredentials > credMap = (ConcurrentHashMap ) credMapField .get (credentialsProvider );
147+
148+ Assert .assertEquals ("test" , credMap .entrySet ().stream ().iterator ().next ().getValue ().getUserName ());
149+ assertNotNull (credMap .entrySet ().stream ().iterator ().next ().getValue ().getUserPassword ());
150+
151+ splitFactory .destroy ();
152+ }
153+
154+ @ Test
155+ public void testFactoryInstantiationWithProxyToken () throws Exception {
156+ SplitClientConfig splitClientConfig = SplitClientConfig .builder ()
157+ .enableDebug ()
158+ .impressionsMode (ImpressionsManager .Mode .DEBUG )
159+ .impressionsRefreshRate (1 )
160+ .endpoint (ENDPOINT , EVENTS_ENDPOINT )
161+ .telemetryURL (SplitClientConfig .TELEMETRY_ENDPOINT )
162+ .authServiceURL (AUTH_SERVICE )
163+ .setBlockUntilReadyTimeout (1000 )
164+ .proxyPort (6060 )
165+ .proxyToken ("123456789" )
166+ .proxyHost (ENDPOINT )
167+ .build ();
168+ SplitFactoryImpl splitFactory2 = new SplitFactoryImpl (API_KEY , splitClientConfig );
169+ assertNotNull (splitFactory2 .client ());
170+ assertNotNull (splitFactory2 .manager ());
171+
172+ Field splitHttpClientField2 = SplitFactoryImpl .class .getDeclaredField ("_splitHttpClient" );
173+ splitHttpClientField2 .setAccessible (true );
174+ SplitHttpClientImpl client2 = (SplitHttpClientImpl ) splitHttpClientField2 .get (splitFactory2 );
175+
176+ Field httpClientField2 = SplitHttpClientImpl .class .getDeclaredField ("_client" );
177+ httpClientField2 .setAccessible (true );
178+ Class <?> InternalHttp2 = Class .forName ("org.apache.hc.client5.http.impl.classic.InternalHttpClient" );
179+
180+ Field credentialsProviderField2 = InternalHttp2 .getDeclaredField ("credentialsProvider" );
181+ credentialsProviderField2 .setAccessible (true );
182+ BasicCredentialsProvider credentialsProvider2 = (BasicCredentialsProvider ) credentialsProviderField2 .get (InternalHttp2 .cast (httpClientField2 .get (client2 )));
183+
184+ Field credMapField2 = BasicCredentialsProvider .class .getDeclaredField ("credMap" );
185+ credMapField2 .setAccessible (true );
186+ ConcurrentHashMap <AuthScope , BearerToken > credMap2 = (ConcurrentHashMap ) credMapField2 .get (credentialsProvider2 );
187+
188+ Assert .assertEquals ("123456789" , credMap2 .entrySet ().stream ().iterator ().next ().getValue ().getToken ());
189+
190+ splitFactory2 .destroy ();
191+ }
192+
193+ @ Test
194+ public void testFactoryInstantiationWithProxyMtls () throws Exception {
195+ SplitClientConfig splitClientConfig = SplitClientConfig .builder ()
196+ .enableDebug ()
197+ .impressionsMode (ImpressionsManager .Mode .DEBUG )
198+ .impressionsRefreshRate (1 )
199+ .endpoint (ENDPOINT ,EVENTS_ENDPOINT )
200+ .telemetryURL (SplitClientConfig .TELEMETRY_ENDPOINT )
201+ .authServiceURL (AUTH_SERVICE )
202+ .setBlockUntilReadyTimeout (1000 )
203+ .proxyPort (6060 )
204+ .proxyScheme ("https" )
205+ .proxyMtlsAuth (new ProxyMTLSAuth .Builder ().proxyP12File ("src/test/resources/keyStore.p12" ).proxyP12FilePassKey ("split" ).build ())
206+ .proxyHost (ENDPOINT )
207+ .build ();
208+ SplitFactoryImpl splitFactory3 = new SplitFactoryImpl (API_KEY , splitClientConfig );
209+ assertNotNull (splitFactory3 .client ());
210+ assertNotNull (splitFactory3 .manager ());
211+
212+ Field splitHttpClientField3 = SplitFactoryImpl .class .getDeclaredField ("_splitHttpClient" );
213+ splitHttpClientField3 .setAccessible (true );
214+ SplitHttpClientImpl client3 = (SplitHttpClientImpl ) splitHttpClientField3 .get (splitFactory3 );
215+
216+ Field httpClientField3 = SplitHttpClientImpl .class .getDeclaredField ("_client" );
217+ httpClientField3 .setAccessible (true );
218+ Class <?> InternalHttp3 = Class .forName ("org.apache.hc.client5.http.impl.classic.InternalHttpClient" );
219+
220+ Field connManagerField = InternalHttp3 .getDeclaredField ("connManager" );
221+ connManagerField .setAccessible (true );
222+ PoolingHttpClientConnectionManager connManager = (PoolingHttpClientConnectionManager ) connManagerField .get (InternalHttp3 .cast (httpClientField3 .get (client3 )));
223+
224+ Field connectionOperatorField = PoolingHttpClientConnectionManager .class .getDeclaredField ("connectionOperator" );
225+ connectionOperatorField .setAccessible (true );
226+ DefaultHttpClientConnectionOperator connectionOperator = (DefaultHttpClientConnectionOperator ) connectionOperatorField .get (connManager );
227+
228+ Field tlsSocketStrategyLookupField = DefaultHttpClientConnectionOperator .class .getDeclaredField ("tlsSocketStrategyLookup" );
229+ tlsSocketStrategyLookupField .setAccessible (true );
230+ Registry tlsSocketStrategyLookup = (Registry ) tlsSocketStrategyLookupField .get (connectionOperator );
231+
232+ Field mapField = Registry .class .getDeclaredField ("map" );
233+ mapField .setAccessible (true );
234+ Class <?> map = mapField .get (tlsSocketStrategyLookup ).getClass ();
235+
236+ Class <?> value = ((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ).getClass ();
237+
238+ Field arg1Field = value .getDeclaredField ("arg$1" );
239+ arg1Field .setAccessible (true );
240+ Class <?> sslConnectionSocketFactory = arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" )).getClass ();
241+
242+ Field socketFactoryField = sslConnectionSocketFactory .getDeclaredField ("socketFactory" );
243+ socketFactoryField .setAccessible (true );
244+ Class <?> socketFactory = socketFactoryField .get (arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ))).getClass ();
245+
246+ Field contextField = socketFactory .getDeclaredField ("context" );
247+ contextField .setAccessible (true );
248+ Class <?> context = Class .forName ("sun.security.ssl.SSLContextImpl" );
249+
250+ Field keyManagerField = context .getDeclaredField ("keyManager" );
251+ keyManagerField .setAccessible (true );
252+ Class <?> keyManager = keyManagerField .get (contextField .get (socketFactoryField .get (arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ))))).getClass ();
253+
254+ Field credentialsMapField = keyManager .getDeclaredField ("credentialsMap" );
255+ credentialsMapField .setAccessible (true );
256+ HashMap <String ,Object > credentialsMap = (HashMap ) credentialsMapField .get (keyManagerField .get (contextField .get (socketFactoryField .get (arg1Field .get (((ConcurrentHashMap ) map .cast (mapField .get (tlsSocketStrategyLookup ))).get ("https" ))))));
257+
258+ assertNotNull (credentialsMap .get ("1" ));
259+
260+ splitFactory3 .destroy ();
108261 }
109262
110263 @ Test
0 commit comments