/
rbac_engine_test.go
1021 lines (1003 loc) · 33.1 KB
/
rbac_engine_test.go
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 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 rbac
import (
"context"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"net"
"net/url"
"testing"
v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
v3rbacpb "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3"
v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
v3matcherpb "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3"
wrapperspb "github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
type s struct {
grpctest.Tester
}
func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}
type addr struct {
ipAddress string
}
func (addr) Network() string { return "" }
func (a *addr) String() string { return a.ipAddress }
// TestNewChainEngine tests the construction of the ChainEngine. Due to some
// types of RBAC configuration being logically wrong and returning an error
// rather than successfully constructing the RBAC Engine, this test tests both
// RBAC Configurations deemed successful and also RBAC Configurations that will
// raise errors.
func (s) TestNewChainEngine(t *testing.T) {
tests := []struct {
name string
policies []*v3rbacpb.RBAC
wantErr bool
}{
{
name: "SuccessCaseAnyMatchSingular",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"anyone": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
{
name: "SuccessCaseAnyMatchMultiple",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"anyone": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
{
Action: v3rbacpb.RBAC_DENY,
Policies: map[string]*v3rbacpb.Policy{
"anyone": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
{
name: "SuccessCaseSimplePolicySingular",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"localhost-fan": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationPort{DestinationPort: 8080}},
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "localhost-fan-page"}}}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
// SuccessCaseSimplePolicyTwoPolicies tests the construction of the
// chained engines in the case where there are two policies in a list,
// one with an allow policy and one with a deny policy. A situation
// where two policies (allow and deny) is a very common use case for
// this API, and should successfully build.
{
name: "SuccessCaseSimplePolicyTwoPolicies",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"localhost-fan": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationPort{DestinationPort: 8080}},
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "localhost-fan-page"}}}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
{
Action: v3rbacpb.RBAC_DENY,
Policies: map[string]*v3rbacpb.Policy{
"localhost-fan": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationPort{DestinationPort: 8080}},
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "localhost-fan-page"}}}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
{
name: "SuccessCaseEnvoyExampleSingular",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"service-admin": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Authenticated_{Authenticated: &v3rbacpb.Principal_Authenticated{PrincipalName: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "cluster.local/ns/default/sa/admin"}}}}},
{Identifier: &v3rbacpb.Principal_Authenticated_{Authenticated: &v3rbacpb.Principal_Authenticated{PrincipalName: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "cluster.local/ns/default/sa/superuser"}}}}},
},
},
"product-viewer": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_AndRules{AndRules: &v3rbacpb.Permission_Set{
Rules: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_ExactMatch{ExactMatch: "GET"}}}},
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Prefix{Prefix: "/products"}}}}}},
{Rule: &v3rbacpb.Permission_OrRules{OrRules: &v3rbacpb.Permission_Set{
Rules: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationPort{DestinationPort: 80}},
{Rule: &v3rbacpb.Permission_DestinationPort{DestinationPort: 443}},
},
},
},
},
},
},
},
},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
{
name: "SourceIpMatcherSuccessSingular",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"certain-source-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_DirectRemoteIp{DirectRemoteIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
},
},
},
},
},
{
name: "SourceIpMatcherFailureSingular",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"certain-source-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_DirectRemoteIp{DirectRemoteIp: &v3corepb.CidrRange{AddressPrefix: "not a correct address", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
},
},
},
},
wantErr: true,
},
{
name: "DestinationIpMatcherSuccess",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"certain-destination-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationIp{DestinationIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
{
name: "DestinationIpMatcherFailure",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"certain-destination-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationIp{DestinationIp: &v3corepb.CidrRange{AddressPrefix: "not a correct address", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
wantErr: true,
},
{
name: "MatcherToNotPolicy",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"not-secret-content": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_NotRule{NotRule: &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Prefix{Prefix: "/secret-content"}}}}}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
{
name: "MatcherToNotPrinicipal",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"not-from-certain-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_NotId{NotId: &v3rbacpb.Principal{Identifier: &v3rbacpb.Principal_DirectRemoteIp{DirectRemoteIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}}}},
},
},
},
},
},
},
// PrinicpalProductViewer tests the construction of a chained engine
// with a policy that allows any downstream to send a GET request on a
// certain path.
{
name: "PrincipalProductViewer",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"product-viewer": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{
Identifier: &v3rbacpb.Principal_AndIds{AndIds: &v3rbacpb.Principal_Set{Ids: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_ExactMatch{ExactMatch: "GET"}}}},
{Identifier: &v3rbacpb.Principal_OrIds{OrIds: &v3rbacpb.Principal_Set{
Ids: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Prefix{Prefix: "/books"}}}}}},
{Identifier: &v3rbacpb.Principal_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Prefix{Prefix: "/cars"}}}}}},
},
}}},
}}},
},
},
},
},
},
},
},
// Certain Headers tests the construction of a chained engine with a
// policy that allows any downstream to send an HTTP request with
// certain headers.
{
name: "CertainHeaders",
policies: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"certain-headers": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{
Identifier: &v3rbacpb.Principal_OrIds{OrIds: &v3rbacpb.Principal_Set{Ids: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_ExactMatch{ExactMatch: "GET"}}}},
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_SafeRegexMatch{SafeRegexMatch: &v3matcherpb.RegexMatcher{Regex: "GET"}}}}},
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_RangeMatch{RangeMatch: &v3typepb.Int64Range{
Start: 0,
End: 64,
}}}}},
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_PresentMatch{PresentMatch: true}}}},
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_PrefixMatch{PrefixMatch: "GET"}}}},
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_SuffixMatch{SuffixMatch: "GET"}}}},
{Identifier: &v3rbacpb.Principal_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_ContainsMatch{ContainsMatch: "GET"}}}},
}}},
},
},
},
},
},
},
},
{
name: "LogAction",
policies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_LOG,
Policies: map[string]*v3rbacpb.Policy{
"anyone": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
wantErr: true,
},
{
name: "ActionNotSpecified",
policies: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"anyone": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if _, err := NewChainEngine(test.policies); (err != nil) != test.wantErr {
t.Fatalf("NewChainEngine(%+v) returned err: %v, wantErr: %v", test.policies, err, test.wantErr)
}
})
}
}
// TestChainEngine tests the chain of RBAC Engines by configuring the chain of
// engines in a certain way in different scenarios. After configuring the chain
// of engines in a certain way, this test pings the chain of engines with
// different types of data representing incoming RPC's (piped into a context),
// and verifies that it works as expected.
func (s) TestChainEngine(t *testing.T) {
defer func(gc func(ctx context.Context) net.Conn) {
getConnection = gc
}(getConnection)
tests := []struct {
name string
rbacConfigs []*v3rbacpb.RBAC
rbacQueries []struct {
rpcData *rpcData
wantStatusCode codes.Code
}
}{
// SuccessCaseAnyMatch tests a single RBAC Engine instantiated with
// a config with a policy with any rules for both permissions and
// principals, meaning that any data about incoming RPC's that the RBAC
// Engine is queried with should match that policy.
{
name: "SuccessCaseAnyMatch",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"anyone": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
{
rpcData: &rpcData{
fullMethod: "some method",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.OK,
},
},
},
// SuccessCaseSimplePolicy is a test that tests a single policy
// that only allows an rpc to proceed if the rpc is calling with a certain
// path.
{
name: "SuccessCaseSimplePolicy",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"localhost-fan": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "localhost-fan-page"}}}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This RPC should match with the local host fan policy. Thus,
// this RPC should be allowed to proceed.
{
rpcData: &rpcData{
fullMethod: "localhost-fan-page",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.OK,
},
// This RPC shouldn't match with the local host fan policy. Thus,
// this rpc shouldn't be allowed to proceed.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
// SuccessCaseEnvoyExample is a test based on the example provided
// in the EnvoyProxy docs. The RBAC Config contains two policies,
// service admin and product viewer, that provides an example of a real
// RBAC Config that might be configured for a given for a given backend
// service.
{
name: "SuccessCaseEnvoyExample",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"service-admin": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Authenticated_{Authenticated: &v3rbacpb.Principal_Authenticated{PrincipalName: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "//cluster.local/ns/default/sa/admin"}}}}},
{Identifier: &v3rbacpb.Principal_Authenticated_{Authenticated: &v3rbacpb.Principal_Authenticated{PrincipalName: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "//cluster.local/ns/default/sa/superuser"}}}}},
},
},
"product-viewer": {
Permissions: []*v3rbacpb.Permission{
{
Rule: &v3rbacpb.Permission_AndRules{AndRules: &v3rbacpb.Permission_Set{
Rules: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Header{Header: &v3routepb.HeaderMatcher{Name: ":method", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_ExactMatch{ExactMatch: "GET"}}}},
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Prefix{Prefix: "/products"}}}}}},
},
},
},
},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This incoming RPC Call should match with the service admin
// policy.
{
rpcData: &rpcData{
fullMethod: "some method",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
AuthInfo: credentials.TLSInfo{
State: tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{
URIs: []*url.URL{
{
Host: "cluster.local",
Path: "/ns/default/sa/admin",
},
},
},
},
},
},
},
},
wantStatusCode: codes.OK,
},
// These incoming RPC calls should not match any policy.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
{
rpcData: &rpcData{
fullMethod: "get-product-list",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
AuthInfo: credentials.TLSInfo{
State: tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{
Subject: pkix.Name{
CommonName: "localhost",
},
},
},
},
},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
{
name: "NotMatcher",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"not-secret-content": {
Permissions: []*v3rbacpb.Permission{
{
Rule: &v3rbacpb.Permission_NotRule{
NotRule: &v3rbacpb.Permission{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Prefix{Prefix: "/secret-content"}}}}}},
},
},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This incoming RPC Call should match with the not-secret-content policy.
{
rpcData: &rpcData{
fullMethod: "/regular-content",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.OK,
},
// This incoming RPC Call shouldn't match with the not-secret-content-policy.
{
rpcData: &rpcData{
fullMethod: "/secret-content",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
{
name: "DirectRemoteIpMatcher",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"certain-direct-remote-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_DirectRemoteIp{DirectRemoteIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This incoming RPC Call should match with the certain-direct-remote-ip policy.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.OK,
},
// This incoming RPC Call shouldn't match with the certain-direct-remote-ip policy.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "10.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
// This test tests a RBAC policy configured with a remote-ip policy.
// This should be logically equivalent to configuring a Engine with a
// direct-remote-ip policy, as per A41 - "allow equating RBAC's
// direct_remote_ip and remote_ip."
{
name: "RemoteIpMatcher",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"certain-remote-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_RemoteIp{RemoteIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This incoming RPC Call should match with the certain-remote-ip policy.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.OK,
},
// This incoming RPC Call shouldn't match with the certain-remote-ip policy.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "10.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
{
name: "DestinationIpMatcher",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"certain-destination-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_DestinationIp{DestinationIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This incoming RPC Call shouldn't match with the
// certain-destination-ip policy, as the test listens on local
// host.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "10.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
// AllowAndDenyPolicy tests a policy with an allow (on path) and
// deny (on port) policy chained together. This represents how a user
// configured interceptor would use this, and also is a potential
// configuration for a dynamic xds interceptor.
{
name: "AllowAndDenyPolicy",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"certain-source-ip": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_DirectRemoteIp{DirectRemoteIp: &v3corepb.CidrRange{AddressPrefix: "0.0.0.0", PrefixLen: &wrapperspb.UInt32Value{Value: uint32(10)}}}},
},
},
},
Action: v3rbacpb.RBAC_ALLOW,
},
{
Policies: map[string]*v3rbacpb.Policy{
"localhost-fan": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_UrlPath{UrlPath: &v3matcherpb.PathMatcher{Rule: &v3matcherpb.PathMatcher_Path{Path: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: "localhost-fan-page"}}}}}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
},
},
},
Action: v3rbacpb.RBAC_DENY,
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This RPC should match with the allow policy, and shouldn't
// match with the deny and thus should be allowed to proceed.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.OK,
},
// This RPC should match with both the allow policy and deny policy
// and thus shouldn't be allowed to proceed as matched with deny.
{
rpcData: &rpcData{
fullMethod: "localhost-fan-page",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
// This RPC shouldn't match with either policy, and thus
// shouldn't be allowed to proceed as didn't match with allow.
{
rpcData: &rpcData{
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "10.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
// This RPC shouldn't match with allow, match with deny, and
// thus shouldn't be allowed to proceed.
{
rpcData: &rpcData{
fullMethod: "localhost-fan-page",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "10.0.0.0"},
},
},
wantStatusCode: codes.PermissionDenied,
},
},
},
// This test tests that when there are no SANs or Subject's
// distinguished name in incoming RPC's, that authenticated matchers
// match against the empty string.
{
name: "default-matching-no-credentials",
rbacConfigs: []*v3rbacpb.RBAC{
{
Policies: map[string]*v3rbacpb.Policy{
"service-admin": {
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Authenticated_{Authenticated: &v3rbacpb.Principal_Authenticated{PrincipalName: &v3matcherpb.StringMatcher{MatchPattern: &v3matcherpb.StringMatcher_Exact{Exact: ""}}}}},
},
},
},
},
},
rbacQueries: []struct {
rpcData *rpcData
wantStatusCode codes.Code
}{
// This incoming RPC Call should match with the service admin
// policy. No authentication info is provided, so the
// authenticated matcher should match to the string matcher on
// the empty string, matching to the service-admin policy.
{
rpcData: &rpcData{
fullMethod: "some method",
peerInfo: &peer.Peer{
Addr: &addr{ipAddress: "0.0.0.0"},
AuthInfo: credentials.TLSInfo{
State: tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{
URIs: []*url.URL{
{
Host: "cluster.local",
Path: "/ns/default/sa/admin",
},
},
},
},
},
},
},
},
wantStatusCode: codes.OK,
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Instantiate the chainedRBACEngine with different configurations that are
// interesting to test and to query.
cre, err := NewChainEngine(test.rbacConfigs)
if err != nil {
t.Fatalf("Error constructing RBAC Engine: %v", err)
}
// Query the created chain of RBAC Engines with different args to see
// if the chain of RBAC Engines configured as such works as intended.
for _, data := range test.rbacQueries {
func() {
// Construct the context with three data points that have enough
// information to represent incoming RPC's. This will be how a
// user uses this API. A user will have to put MD, PeerInfo, and
// the connection the RPC is sent on in the context.
ctx := metadata.NewIncomingContext(context.Background(), data.rpcData.md)
// Make a TCP connection with a certain destination port. The
// address/port of this connection will be used to populate the
// destination ip/port in RPCData struct. This represents what
// the user of ChainEngine will have to place into
// context, as this is only way to get destination ip and port.
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Error listening: %v", err)
}
defer lis.Close()
connCh := make(chan net.Conn, 1)
go func() {
conn, err := lis.Accept()
if err != nil {
t.Errorf("Error accepting connection: %v", err)
return
}
connCh <- conn
}()
_, err = net.Dial("tcp", lis.Addr().String())
if err != nil {
t.Fatalf("Error dialing: %v", err)
}
conn := <-connCh
defer conn.Close()
getConnection = func(context.Context) net.Conn {
return conn
}
ctx = peer.NewContext(ctx, data.rpcData.peerInfo)
stream := &ServerTransportStreamWithMethod{
method: data.rpcData.fullMethod,
}
ctx = grpc.NewContextWithServerTransportStream(ctx, stream)
err = cre.IsAuthorized(ctx)
if gotCode := status.Code(err); gotCode != data.wantStatusCode {
t.Fatalf("IsAuthorized(%+v, %+v) returned (%+v), want(%+v)", ctx, data.rpcData.fullMethod, gotCode, data.wantStatusCode)
}
}()
}
})
}