@@ -119,9 +119,12 @@ public class SecurityInterceptorTests {
119
119
private Connection connection3 ;
120
120
private DiscoveryNode otherRemoteNode ;
121
121
private Connection connection4 ;
122
+ private DiscoveryNode remoteNodeWithCustomSerialization ;
123
+ private Connection connection5 ;
122
124
123
125
private AsyncSender sender ;
124
- private AsyncSender serializedSender ;
126
+ private AsyncSender jdkSerializedSender ;
127
+ private AsyncSender customSerializedSender ;
125
128
private AtomicReference <CountDownLatch > senderLatch = new AtomicReference <>(new CountDownLatch (1 ));
126
129
127
130
@ Before
@@ -199,7 +202,30 @@ public void setup() {
199
202
otherRemoteNode = new DiscoveryNode ("remote-node2" , new TransportAddress (remoteAddress , 9876 ), remoteNodeVersion );
200
203
connection4 = transportService .getConnection (otherRemoteNode );
201
204
202
- serializedSender = new AsyncSender () {
205
+ remoteNodeWithCustomSerialization = new DiscoveryNode (
206
+ "remote-node-with-custom-serialization" ,
207
+ new TransportAddress (localAddress , 7456 ),
208
+ Version .V_2_12_0
209
+ );
210
+ connection5 = transportService .getConnection (remoteNodeWithCustomSerialization );
211
+
212
+ jdkSerializedSender = new AsyncSender () {
213
+ @ Override
214
+ public <T extends TransportResponse > void sendRequest (
215
+ Connection connection ,
216
+ String action ,
217
+ TransportRequest request ,
218
+ TransportRequestOptions options ,
219
+ TransportResponseHandler <T > handler
220
+ ) {
221
+ String serializedUserHeader = threadPool .getThreadContext ().getHeader (ConfigConstants .OPENDISTRO_SECURITY_USER_HEADER );
222
+ User deserializedUser = (User ) Base64Helper .deserializeObject (serializedUserHeader , true );
223
+ assertThat (deserializedUser , is (user ));
224
+ senderLatch .get ().countDown ();
225
+ }
226
+ };
227
+
228
+ customSerializedSender = new AsyncSender () {
203
229
@ Override
204
230
public <T extends TransportResponse > void sendRequest (
205
231
Connection connection ,
@@ -209,7 +235,7 @@ public <T extends TransportResponse> void sendRequest(
209
235
TransportResponseHandler <T > handler
210
236
) {
211
237
String serializedUserHeader = threadPool .getThreadContext ().getHeader (ConfigConstants .OPENDISTRO_SECURITY_USER_HEADER );
212
- assertThat (serializedUserHeader , is (Base64Helper .serializeObject (user , true )));
238
+ assertThat (serializedUserHeader , is (Base64Helper .serializeObject (user , false )));
213
239
senderLatch .get ().countDown ();
214
240
}
215
241
};
@@ -265,6 +291,27 @@ final void completableRequestDecorate(
265
291
senderLatch .set (new CountDownLatch (1 ));
266
292
}
267
293
294
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
295
+ final void completableRequestDecorateWithPreviouslyPopulatedHeaders (
296
+ AsyncSender sender ,
297
+ Connection connection ,
298
+ String action ,
299
+ TransportRequest request ,
300
+ TransportRequestOptions options ,
301
+ TransportResponseHandler handler ,
302
+ DiscoveryNode localNode
303
+ ) {
304
+ securityInterceptor .sendRequestDecorate (sender , connection , action , request , options , handler , localNode );
305
+ try {
306
+ senderLatch .get ().await (1 , TimeUnit .SECONDS );
307
+ } catch (final InterruptedException e ) {
308
+ throw new RuntimeException (e );
309
+ }
310
+
311
+ // Reset the latch so another request can be processed
312
+ senderLatch .set (new CountDownLatch (1 ));
313
+ }
314
+
268
315
@ Test
269
316
public void testSendRequestDecorateLocalConnection () {
270
317
@@ -278,16 +325,44 @@ public void testSendRequestDecorateLocalConnection() {
278
325
public void testSendRequestDecorateRemoteConnection () {
279
326
280
327
// this is a remote request
281
- completableRequestDecorate (serializedSender , connection3 , action , request , options , handler , localNode );
328
+ completableRequestDecorate (jdkSerializedSender , connection3 , action , request , options , handler , localNode );
282
329
// this is a remote request where the transport address is different
283
- completableRequestDecorate (serializedSender , connection4 , action , request , options , handler , localNode );
330
+ completableRequestDecorate (jdkSerializedSender , connection4 , action , request , options , handler , localNode );
331
+ }
332
+
333
+ @ Test
334
+ public void testSendRequestDecorateRemoteConnectionUsesJDKSerialization () {
335
+ threadPool .getThreadContext ().putHeader (ConfigConstants .OPENDISTRO_SECURITY_USER_HEADER , Base64Helper .serializeObject (user , false ));
336
+ completableRequestDecorateWithPreviouslyPopulatedHeaders (
337
+ jdkSerializedSender ,
338
+ connection3 ,
339
+ action ,
340
+ request ,
341
+ options ,
342
+ handler ,
343
+ localNode
344
+ );
345
+ }
346
+
347
+ @ Test
348
+ public void testSendRequestDecorateRemoteConnectionUsesCustomSerialization () {
349
+ threadPool .getThreadContext ().putHeader (ConfigConstants .OPENDISTRO_SECURITY_USER_HEADER , Base64Helper .serializeObject (user , true ));
350
+ completableRequestDecorateWithPreviouslyPopulatedHeaders (
351
+ customSerializedSender ,
352
+ connection5 ,
353
+ action ,
354
+ request ,
355
+ options ,
356
+ handler ,
357
+ localNode
358
+ );
284
359
}
285
360
286
361
@ Test
287
362
public void testSendNoOriginNodeCausesSerialization () {
288
363
289
364
// this is a request where the local node is null; have to use the remote connection since the serialization will fail
290
- completableRequestDecorate (serializedSender , connection3 , action , request , options , handler , null );
365
+ completableRequestDecorate (jdkSerializedSender , connection3 , action , request , options , handler , null );
291
366
}
292
367
293
368
@ Test
@@ -296,7 +371,7 @@ public void testSendNoConnectionShouldThrowNPE() {
296
371
// The completable version swallows the NPE so have to call actual method
297
372
assertThrows (
298
373
java .lang .NullPointerException .class ,
299
- () -> securityInterceptor .sendRequestDecorate (serializedSender , null , action , request , options , handler , localNode )
374
+ () -> securityInterceptor .sendRequestDecorate (jdkSerializedSender , null , action , request , options , handler , localNode )
300
375
);
301
376
}
302
377
@@ -328,7 +403,7 @@ public void testCustomRemoteAddressCausesSerialization() {
328
403
ConfigConstants .OPENDISTRO_SECURITY_REMOTE_ADDRESS ,
329
404
String .valueOf (new TransportAddress (new InetSocketAddress ("8.8.8.8" , 80 )))
330
405
);
331
- completableRequestDecorate (serializedSender , connection3 , action , request , options , handler , localNode );
406
+ completableRequestDecorate (jdkSerializedSender , connection3 , action , request , options , handler , localNode );
332
407
}
333
408
334
409
@ Test
@@ -351,7 +426,7 @@ public void testFakeHeaderIsIgnored() {
351
426
// this is a local request
352
427
completableRequestDecorate (sender , connection1 , action , request , options , handler , localNode );
353
428
// this is a remote request
354
- completableRequestDecorate (serializedSender , connection3 , action , request , options , handler , localNode );
429
+ completableRequestDecorate (jdkSerializedSender , connection3 , action , request , options , handler , localNode );
355
430
}
356
431
357
432
@ Test
@@ -363,7 +438,7 @@ public void testNullHeaderIsIgnored() {
363
438
// this is a local request
364
439
completableRequestDecorate (sender , connection1 , action , request , options , handler , localNode );
365
440
// this is a remote request
366
- completableRequestDecorate (serializedSender , connection3 , action , request , options , handler , localNode );
441
+ completableRequestDecorate (jdkSerializedSender , connection3 , action , request , options , handler , localNode );
367
442
}
368
443
369
444
@ Test
0 commit comments