forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterChainMatchingProtocolNegotiatorsTest.java
1221 lines (1127 loc) · 65.4 KB
/
FilterChainMatchingProtocolNegotiatorsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.xds;
import static com.google.common.truth.Truth.assertThat;
import static io.grpc.xds.XdsServerWrapper.ATTR_SERVER_ROUTING_CONFIG;
import static io.grpc.xds.internal.sds.SdsProtocolNegotiators.ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.ServerInterceptor;
import io.grpc.internal.TestUtils.NoopChannelLogger;
import io.grpc.netty.GrpcHttp2ConnectionHandler;
import io.grpc.netty.InternalProtocolNegotiationEvent;
import io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator;
import io.grpc.netty.ProtocolNegotiationEvent;
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
import io.grpc.xds.EnvoyServerProtoData.FilterChain;
import io.grpc.xds.Filter.FilterConfig;
import io.grpc.xds.Filter.NamedFilterConfig;
import io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler;
import io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector;
import io.grpc.xds.VirtualHost.Route;
import io.grpc.xds.XdsServerWrapper.ServerRoutingConfig;
import io.grpc.xds.internal.sds.CommonTlsContextTestsUtil;
import io.grpc.xds.internal.sds.SslContextProviderSupplier;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http2.DefaultHttp2Connection;
import io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder;
import io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder;
import io.netty.handler.codec.http2.DefaultHttp2FrameReader;
import io.netty.handler.codec.http2.DefaultHttp2FrameWriter;
import io.netty.handler.codec.http2.Http2ConnectionDecoder;
import io.netty.handler.codec.http2.Http2ConnectionEncoder;
import io.netty.handler.codec.http2.Http2Settings;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
@RunWith(JUnit4.class)
public class FilterChainMatchingProtocolNegotiatorsTest {
@Rule
public final MockitoRule mocks = MockitoJUnit.rule();
private final GrpcHttp2ConnectionHandler grpcHandler =
FakeGrpcHttp2ConnectionHandler.newHandler();
@Mock private TlsContextManager tlsContextManager;
private ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
private ChannelPipeline pipeline;
private EmbeddedChannel channel;
private ChannelHandlerContext channelHandlerCtx;
@Mock
private ProtocolNegotiator mockDelegate;
private static final HttpConnectionManager HTTP_CONNECTION_MANAGER = createRds("routing-config");
private static final String LOCAL_IP = "10.1.2.3"; // dest
private static final String REMOTE_IP = "10.4.2.3"; // source
private static final int PORT = 7000;
private final ServerRoutingConfig noopConfig = ServerRoutingConfig.create(
new AtomicReference<ImmutableList<VirtualHost>>(),
new AtomicReference<ImmutableMap<Route, ServerInterceptor>>());
@Test
public void nofilterChainMatch_defaultSslContext() throws Exception {
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
SslContextProviderSupplier defaultSsl = new SslContextProviderSupplier(createTls(),
tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
new HashMap<FilterChain, ServerRoutingConfig>(), defaultSsl, noopConfig);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
setupChannel("172.168.1.1", "172.168.1.2", 80, filterChainMatchingHandler);
ChannelHandlerContext channelHandlerCtx = pipeline.context(filterChainMatchingHandler);
assertThat(channelHandlerCtx).isNotNull();
pipeline.fireUserEventTriggered(event);
channelHandlerCtx = pipeline.context(filterChainMatchingHandler);
assertThat(channelHandlerCtx).isNull();
channel.runPendingTasks();
assertThat(sslSet.isDone()).isTrue();
assertThat(sslSet.get()).isEqualTo(defaultSsl);
assertThat(routingSettable.get()).isEqualTo(noopConfig);
channelHandlerCtx = pipeline.context(next);
assertThat(channelHandlerCtx).isNotNull();
}
@Test
public void noFilterChainMatch_noDefaultSslContext() {
FilterChainSelector selector = new FilterChainSelector(
new HashMap<FilterChain, ServerRoutingConfig>(), null, null);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
setupChannel("172.168.1.1", "172.168.2.2", 90, filterChainMatchingHandler);
channelHandlerCtx = pipeline.context(filterChainMatchingHandler);
assertThat(channelHandlerCtx).isNotNull();
assertThat(channel.closeFuture().isDone()).isFalse();
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(channel.closeFuture().isDone()).isTrue();
}
@Test
public void singleFilterChainWithoutAlpn() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.DownstreamTlsContext tlsContext =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChain filterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatch, HTTP_CONNECTION_MANAGER, tlsContext,
tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(ImmutableMap.of(filterChain, noopConfig),
null, null);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.isDone()).isTrue();
assertThat(sslSet.get()).isEqualTo(filterChain.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContext);
}
@Test
public void singleFilterChainWithAlpn() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.asList("managed-mtls"),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.DownstreamTlsContext tlsContext =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChain filterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatch, HTTP_CONNECTION_MANAGER, tlsContext,
tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext defaultTlsContext =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER, defaultTlsContext,
tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChain, randomConfig("no-match")),
defaultFilterChain.getSslContextProviderSupplier(), noopConfig);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(defaultFilterChain.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(defaultTlsContext);
}
@Test
public void destPortFails_returnDefaultFilterChain() throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextWithDestPort =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithDestPort =
new EnvoyServerProtoData.FilterChainMatch(
PORT,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.asList("managed-mtls"),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainWithDestPort =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchWithDestPort, HTTP_CONNECTION_MANAGER,
tlsContextWithDestPort, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextForDefaultFilterChain =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER,
tlsContextForDefaultFilterChain, tlsContextManager);
ServerRoutingConfig routingConfig = ServerRoutingConfig.create(new AtomicReference<>(
ImmutableList.of(createVirtualHost("virtual"))),
new AtomicReference<ImmutableMap<Route, ServerInterceptor>>());
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainWithDestPort, routingConfig),
defaultFilterChain.getSslContextProviderSupplier(), noopConfig);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(defaultFilterChain.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext())
.isSameInstanceAs(tlsContextForDefaultFilterChain);
}
@Test
public void destPrefixRangeMatch() throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextMatch =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.1.2.0", 24)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainWithMatch = new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchWithMatch, HTTP_CONNECTION_MANAGER,
tlsContextMatch, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextForDefaultFilterChain =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER,
tlsContextForDefaultFilterChain, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainWithMatch, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("no-match"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChainWithMatch.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMatch);
}
@Test
public void destPrefixRangeMismatch_returnDefaultFilterChain()
throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextMismatch =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
// 10.2.2.0/24 doesn't match LOCAL_IP
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMismatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.2.2.0", 24)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainWithMismatch =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchWithMismatch, HTTP_CONNECTION_MANAGER,
tlsContextMismatch, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextForDefaultFilterChain =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER,
tlsContextForDefaultFilterChain, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainWithMismatch, randomConfig("no-match")),
defaultFilterChain.getSslContextProviderSupplier(), noopConfig);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.isDone()).isTrue();
assertThat(sslSet.get()).isEqualTo(defaultFilterChain.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextForDefaultFilterChain);
}
@Test
public void dest0LengthPrefixRange()
throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContext0Length =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
// 10.2.2.0/24 doesn't match LOCAL_IP
EnvoyServerProtoData.FilterChainMatch filterChainMatch0Length =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.2.2.0", 0)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain0Length = new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatch0Length, HTTP_CONNECTION_MANAGER,
tlsContext0Length, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextForDefaultFilterChain =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER,
tlsContextForDefaultFilterChain, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChain0Length, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), null);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChain0Length.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContext0Length);
}
@Test
public void destPrefixRange_moreSpecificWins()
throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextLessSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.1.2.0", 24)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainLessSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchLessSpecific, HTTP_CONNECTION_MANAGER,
tlsContextLessSpecific, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextMoreSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.1.2.2", 31)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainMoreSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatchMoreSpecific, HTTP_CONNECTION_MANAGER,
tlsContextMoreSpecific,
tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainLessSpecific, randomConfig("no-match"),
filterChainMoreSpecific, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChainMoreSpecific.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMoreSpecific);
}
@Test
public void destPrefixRange_emptyListLessSpecific()
throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextLessSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainLessSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchLessSpecific, HTTP_CONNECTION_MANAGER,
tlsContextLessSpecific, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextMoreSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("8.0.0.0", 5)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainMoreSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatchMoreSpecific, HTTP_CONNECTION_MANAGER,
tlsContextMoreSpecific,
tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainLessSpecific, randomConfig("no-match"),
filterChainMoreSpecific, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChainMoreSpecific.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMoreSpecific);
}
@Test
public void destPrefixRangeIpv6_moreSpecificWins()
throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextLessSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("FE80:0:0:0:0:0:0:0", 60)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainLessSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchLessSpecific, HTTP_CONNECTION_MANAGER,
tlsContextLessSpecific, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextMoreSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("FE80:0000:0000:0000:0202:0:0:0", 80)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainMoreSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatchMoreSpecific, HTTP_CONNECTION_MANAGER,
tlsContextMoreSpecific, tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainLessSpecific, randomConfig("no-match"),
filterChainMoreSpecific, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel("FE80:0000:0000:0000:0202:B3FF:FE1E:8329", "2001:DB8::8:800:200C:417A",
15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChainMoreSpecific.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMoreSpecific);
}
@Test
public void destPrefixRange_moreSpecificWith2Wins()
throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextMoreSpecificWith2 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecificWith2 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(
new EnvoyServerProtoData.CidrRange("10.1.2.0", 24),
new EnvoyServerProtoData.CidrRange(LOCAL_IP, 32)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainMoreSpecificWith2 =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchMoreSpecificWith2, HTTP_CONNECTION_MANAGER,
tlsContextMoreSpecificWith2, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextLessSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.1.2.2", 31)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainLessSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatchLessSpecific, HTTP_CONNECTION_MANAGER,
tlsContextLessSpecific, tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainMoreSpecificWith2, noopConfig,
filterChainLessSpecific, randomConfig("no-match")),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(
filterChainMoreSpecificWith2.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMoreSpecificWith2);
}
@Test
public void sourceTypeMismatch_returnDefaultFilterChain() throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextMismatch =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMismatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.SAME_IP_OR_LOOPBACK,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainWithMismatch =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchWithMismatch, HTTP_CONNECTION_MANAGER,
tlsContextMismatch, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextForDefaultFilterChain =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER,tlsContextForDefaultFilterChain,
tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainWithMismatch, randomConfig("no-match")),
defaultFilterChain.getSslContextProviderSupplier(), noopConfig);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(defaultFilterChain.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextForDefaultFilterChain);
}
@Test
public void sourceTypeLocal() throws Exception {
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
EnvoyServerProtoData.DownstreamTlsContext tlsContextMatch =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.SAME_IP_OR_LOOPBACK,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainWithMatch = new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchWithMatch, HTTP_CONNECTION_MANAGER, tlsContextMatch,
tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextForDefaultFilterChain =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", null, HTTP_CONNECTION_MANAGER, tlsContextForDefaultFilterChain,
tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainWithMatch, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
setupChannel(LOCAL_IP, LOCAL_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChainWithMatch.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMatch);
}
@Test
public void sourcePrefixRange_moreSpecificWith2Wins()
throws Exception {
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
EnvoyServerProtoData.DownstreamTlsContext tlsContextMoreSpecificWith2 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecificWith2 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(
new EnvoyServerProtoData.CidrRange("10.4.2.0", 24),
new EnvoyServerProtoData.CidrRange(REMOTE_IP, 32)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainMoreSpecificWith2 =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchMoreSpecificWith2, HTTP_CONNECTION_MANAGER,
tlsContextMoreSpecificWith2, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextLessSpecific =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.4.2.2", 31)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainLessSpecific =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatchLessSpecific, HTTP_CONNECTION_MANAGER,
tlsContextLessSpecific, tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainMoreSpecificWith2, noopConfig,
filterChainLessSpecific, randomConfig("no-match")),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(
filterChainMoreSpecificWith2.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextMoreSpecificWith2);
}
@Test
public void sourcePrefixRange_2Matchers_expectException()
throws UnknownHostException {
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
ChannelHandler next = new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
ProtocolNegotiationEvent e = (ProtocolNegotiationEvent)evt;
sslSet.set(InternalProtocolNegotiationEvent.getAttributes(e)
.get(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER));
}
};
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
EnvoyServerProtoData.DownstreamTlsContext tlsContext1 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatch1 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(
new EnvoyServerProtoData.CidrRange("10.4.2.0", 24),
new EnvoyServerProtoData.CidrRange("192.168.10.2", 32)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain1 = new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatch1, HTTP_CONNECTION_MANAGER, tlsContext1,
tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContext2 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatch2 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.4.2.0", 24)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain2 = new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatch2, HTTP_CONNECTION_MANAGER, tlsContext2,
tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, null);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChain1, noopConfig, filterChain2, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), noopConfig);
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
try {
channel.checkException();
fail("expect exception!");
} catch (IllegalStateException ise) {
assertThat(ise).hasMessageThat().isEqualTo("Found more than one matching filter chains. This "
+ "should not be possible as ClientXdsClient validated the chains for uniqueness.");
assertThat(sslSet.isDone()).isFalse();
channelHandlerCtx = pipeline.context(filterChainMatchingHandler);
assertThat(channelHandlerCtx).isNotNull();
}
}
@Test
public void sourcePortMatch_exactMatchWinsOverEmptyList() throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContextEmptySourcePorts =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.FilterChainMatch filterChainMatchEmptySourcePorts =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(
new EnvoyServerProtoData.CidrRange("10.4.2.0", 24),
new EnvoyServerProtoData.CidrRange("10.4.2.2", 31)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainEmptySourcePorts =
new EnvoyServerProtoData.FilterChain(
"filter-chain-foo", filterChainMatchEmptySourcePorts, HTTP_CONNECTION_MANAGER,
tlsContextEmptySourcePorts, tlsContextManager);
EnvoyServerProtoData.DownstreamTlsContext tlsContextSourcePortMatch =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.FilterChainMatch filterChainMatchSourcePortMatch =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.4.2.2", 31)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.asList(7000, 15000),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChainSourcePortMatch =
new EnvoyServerProtoData.FilterChain(
"filter-chain-bar", filterChainMatchSourcePortMatch, HTTP_CONNECTION_MANAGER,
tlsContextSourcePortMatch, tlsContextManager);
EnvoyServerProtoData.FilterChain defaultFilterChain = new EnvoyServerProtoData.FilterChain(
"filter-chain-baz", null, HTTP_CONNECTION_MANAGER, null, tlsContextManager);
FilterChainSelector selector = new FilterChainSelector(
ImmutableMap.of(filterChainEmptySourcePorts, randomConfig("no-match"),
filterChainSourcePortMatch, noopConfig),
defaultFilterChain.getSslContextProviderSupplier(), randomConfig("default"));
FilterChainMatchingHandler filterChainMatchingHandler =
new FilterChainMatchingHandler(grpcHandler, selector, mockDelegate);
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
final SettableFuture<ServerRoutingConfig> routingSettable = SettableFuture.create();
ChannelHandler next = captureAttrHandler(sslSet, routingSettable);
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
setupChannel(LOCAL_IP, REMOTE_IP, 15000, filterChainMatchingHandler);
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
assertThat(sslSet.get()).isEqualTo(filterChainSourcePortMatch.getSslContextProviderSupplier());
assertThat(routingSettable.get()).isEqualTo(noopConfig);
assertThat(sslSet.get().getTlsContext()).isSameInstanceAs(tlsContextSourcePortMatch);
}
/**
* Create 6 filterChains: - 1st filter chain has dest port & specific prefix range but is
* eliminated due to dest port - 5 advance to next step: 1 is eliminated due to being less
* specific than the remaining 4. - 4 advance to 3rd step: source type external eliminates one
* with local source_type. - 3 advance to 4th step: more specific 2 get picked based on
* source-prefix range. - 5th step: out of 2 one with matching source port gets picked
*/
@Test
public void filterChain_5stepMatch() throws Exception {
EnvoyServerProtoData.DownstreamTlsContext tlsContext1 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
EnvoyServerProtoData.DownstreamTlsContext tlsContext2 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT2", "VA2");
EnvoyServerProtoData.DownstreamTlsContext tlsContext3 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT3", "VA3");
EnvoyServerProtoData.DownstreamTlsContext tlsContext4 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT4", "VA4");
EnvoyServerProtoData.DownstreamTlsContext tlsContext5 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT5", "VA5");
EnvoyServerProtoData.DownstreamTlsContext tlsContext6 =
CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT6", "VA6");
// has dest port and specific prefix ranges: gets eliminated in step 1
EnvoyServerProtoData.FilterChainMatch filterChainMatch1 =
new EnvoyServerProtoData.FilterChainMatch(
PORT,
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
Arrays.<String>asList(),
Arrays.asList(new EnvoyServerProtoData.CidrRange(REMOTE_IP, 32)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain1 = new EnvoyServerProtoData.FilterChain(
"filter-chain-1", filterChainMatch1, HTTP_CONNECTION_MANAGER, tlsContext1,
tlsContextManager);
// next 5 use prefix range: 4 with prefixLen of 30 and last one with 29
// has single prefix range: and less specific source prefix range: gets eliminated in step 4
EnvoyServerProtoData.FilterChainMatch filterChainMatch2 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.1.2.0", 30)),
Arrays.<String>asList(),
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.4.0.0", 16)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain2 = new EnvoyServerProtoData.FilterChain(
"filter-chain-2", filterChainMatch2, HTTP_CONNECTION_MANAGER, tlsContext2,
tlsContextManager);
// has prefix ranges with one not matching and source type local: gets eliminated in step 3
EnvoyServerProtoData.FilterChainMatch filterChainMatch3 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(
new EnvoyServerProtoData.CidrRange("192.168.2.0", 24),
new EnvoyServerProtoData.CidrRange("10.1.2.0", 30)),
Arrays.<String>asList(),
Arrays.<EnvoyServerProtoData.CidrRange>asList(),
EnvoyServerProtoData.ConnectionSourceType.SAME_IP_OR_LOOPBACK,
Arrays.<Integer>asList(),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain3 = new EnvoyServerProtoData.FilterChain(
"filter-chain-3", filterChainMatch3, HTTP_CONNECTION_MANAGER, tlsContext3,
tlsContextManager);
// has prefix ranges with both matching and source type external but non matching source port:
// gets eliminated in step 5
EnvoyServerProtoData.FilterChainMatch filterChainMatch4 =
new EnvoyServerProtoData.FilterChainMatch(
0,
Arrays.asList(
new EnvoyServerProtoData.CidrRange("10.1.0.0", 16),
new EnvoyServerProtoData.CidrRange("10.1.2.0", 30)),
Arrays.<String>asList(),
Arrays.asList(new EnvoyServerProtoData.CidrRange("10.4.2.0", 24)),
EnvoyServerProtoData.ConnectionSourceType.EXTERNAL,
Arrays.asList(16000, 9000),
Arrays.<String>asList(),
null);
EnvoyServerProtoData.FilterChain filterChain4 =
new EnvoyServerProtoData.FilterChain(
"filter-chain-4", filterChainMatch4, HTTP_CONNECTION_MANAGER, tlsContext4,
tlsContextManager);
// has prefix ranges with both matching and source type external and matching source port: this
// gets selected
EnvoyServerProtoData.FilterChainMatch filterChainMatch5 =