-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproxy_test.go
More file actions
1676 lines (1474 loc) · 61.6 KB
/
Copy pathproxy_test.go
File metadata and controls
1676 lines (1474 loc) · 61.6 KB
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
//go:build e2e
package e2e
import (
"context"
"encoding/json"
"fmt"
"time"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/utils/pointer"
"github.com/authzed/spicedb-kubeapi-proxy/pkg/authz/distributedtx"
"github.com/authzed/spicedb-kubeapi-proxy/pkg/config/proxyrule"
"github.com/authzed/spicedb-kubeapi-proxy/pkg/failpoints"
"github.com/authzed/spicedb-kubeapi-proxy/pkg/rules"
)
var _ = Describe("Proxy", func() {
When("there are two users", func() {
var paulClient, chaniClient, adminClient kubernetes.Interface
var paulDynamicClient, chaniDynamicClient, adminDynamicClient dynamic.Interface
var paulRestConfig, chaniRestConfig, adminRestConfig *rest.Config
var paulNamespace, chaniNamespace, sharedNamespace string
var chaniPod, paulPod string
var paulCustomResource, chaniCustomResource string
var lockMode proxyrule.LockMode
BeforeEach(func() {
var err error
paulRestConfig, err = clientcmd.NewDefaultClientConfig(
*clientCA.GenerateUserConfig("paul"), nil,
).ClientConfig()
Expect(err).To(Succeed())
paulClient, err = kubernetes.NewForConfig(paulRestConfig)
Expect(err).To(Succeed())
paulDynamicClient, err = dynamic.NewForConfig(paulRestConfig)
Expect(err).To(Succeed())
chaniRestConfig, err = clientcmd.NewDefaultClientConfig(
*clientCA.GenerateUserConfig("chani"), nil,
).ClientConfig()
Expect(err).To(Succeed())
chaniClient, err = kubernetes.NewForConfig(chaniRestConfig)
Expect(err).To(Succeed())
chaniDynamicClient, err = dynamic.NewForConfig(chaniRestConfig)
Expect(err).To(Succeed())
adminClient, err = kubernetes.NewForConfig(adminUser.Config())
Expect(err).To(Succeed())
adminRestConfig = adminUser.Config()
adminDynamicClient, err = dynamic.NewForConfig(adminRestConfig)
Expect(err).To(Succeed())
paulNamespace = names.SimpleNameGenerator.GenerateName("paul-")
chaniNamespace = names.SimpleNameGenerator.GenerateName("chani-")
// pods are used for tests that require deletes, since the GC
// controller can clean them up in tests (namespaces can't be GCd)
sharedNamespace = names.SimpleNameGenerator.GenerateName("shared-")
paulPod = names.SimpleNameGenerator.GenerateName("paul-pod-")
chaniPod = names.SimpleNameGenerator.GenerateName("chani-pod-")
paulCustomResource = names.SimpleNameGenerator.GenerateName("paul-cr-")
chaniCustomResource = names.SimpleNameGenerator.GenerateName("chani-cr-")
})
AfterEach(func(ctx context.Context) {
orphan := metav1.DeletePropagationOrphan
_ = adminClient.CoreV1().Namespaces().Delete(ctx, paulNamespace, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminClient.CoreV1().Namespaces().Delete(ctx, chaniNamespace, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminClient.CoreV1().Pods(sharedNamespace).Delete(ctx, paulPod, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminClient.CoreV1().Pods(sharedNamespace).Delete(ctx, chaniPod, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminClient.CoreV1().Namespaces().Delete(ctx, sharedNamespace, metav1.DeleteOptions{PropagationPolicy: &orphan})
// Clean up custom resources
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
_ = adminDynamicClient.Resource(gvr).Namespace(paulNamespace).Delete(ctx, paulCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminDynamicClient.Resource(gvr).Namespace(chaniNamespace).Delete(ctx, chaniCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminDynamicClient.Resource(gvr).Namespace(sharedNamespace).Delete(ctx, paulCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminDynamicClient.Resource(gvr).Namespace(sharedNamespace).Delete(ctx, chaniCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
gvr = schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "anothertestresources"}
_ = adminDynamicClient.Resource(gvr).Namespace(paulNamespace).Delete(ctx, paulCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminDynamicClient.Resource(gvr).Namespace(chaniNamespace).Delete(ctx, chaniCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminDynamicClient.Resource(gvr).Namespace(sharedNamespace).Delete(ctx, paulCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
_ = adminDynamicClient.Resource(gvr).Namespace(sharedNamespace).Delete(ctx, chaniCustomResource, metav1.DeleteOptions{PropagationPolicy: &orphan})
// ensure there are no remaining locks
Expect(len(GetAllTuples(ctx, &v1.RelationshipFilter{
ResourceType: "lock",
OptionalRelation: "workflow",
OptionalSubjectFilter: &v1.SubjectFilter{SubjectType: "workflow"},
}))).To(BeZero())
// prevent failpoints from bleeding between tests
failpoints.DisableAll()
})
CreateNamespace := func(ctx context.Context, client kubernetes.Interface, namespace string) error {
_, err := client.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: namespace},
}, metav1.CreateOptions{})
return err
}
GetNamespace := func(ctx context.Context, client kubernetes.Interface, namespace string) error {
_, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
return err
}
ListNamespaces := func(ctx context.Context, client kubernetes.Interface) []string {
visibleNamespaces, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
Expect(err).To(Succeed())
return lo.Map(visibleNamespaces.Items, func(item corev1.Namespace, index int) string {
return item.Name
})
}
WatchNamespaces := func(ctx context.Context, client kubernetes.Interface, expected int) []string {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
got := make([]string, 0, expected)
watcher, err := client.CoreV1().Namespaces().Watch(ctx, metav1.ListOptions{})
Expect(err).To(Succeed())
defer watcher.Stop()
for e := range watcher.ResultChan() {
ns, ok := e.Object.(*corev1.Namespace)
if !ok {
return got
}
got = append(got, ns.Name)
if len(got) == expected {
return got
}
}
return got
}
CreatePod := func(ctx context.Context, client kubernetes.Interface, namespace, name string) error {
_, err := client.CoreV1().Pods(namespace).Create(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: corev1.PodSpec{Containers: []corev1.Container{{
Name: "nginx",
Image: "nginx:1.14.2",
}}},
}, metav1.CreateOptions{})
return err
}
DeletePod := func(ctx context.Context, client kubernetes.Interface, namespace, name string) error {
return client.CoreV1().Pods(namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
GetPod := func(ctx context.Context, client kubernetes.Interface, namespace, name string) error {
_, err := client.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
return err
}
GetAndUpdatePodLabels := func(ctx context.Context, client kubernetes.Interface, namespace, name string, labels map[string]string) error {
pod, err := client.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}
pod.Labels = labels
_, err = client.CoreV1().Pods(namespace).Update(ctx, pod, metav1.UpdateOptions{})
return err
}
PatchPodLabels := func(ctx context.Context, client kubernetes.Interface, namespace, name string, labels map[string]string) error {
patch := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
}
data, err := json.Marshal(patch)
if err != nil {
return err
}
_, err = client.CoreV1().Pods(namespace).Patch(ctx, name, types.ApplyPatchType, data, metav1.PatchOptions{FieldManager: "Test", Force: pointer.Bool(true)})
return err
}
ListPods := func(ctx context.Context, client kubernetes.Interface, namespace string) []string {
visibleNamespaces, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
Expect(err).To(Succeed())
return lo.Map(visibleNamespaces.Items, func(item corev1.Pod, index int) string {
return item.Name
})
}
ListPodsAsTable := func(ctx context.Context, config *rest.Config, namespace string) (*metav1.Table, error) {
cfg := rest.CopyConfig(config)
gv := corev1.SchemeGroupVersion
cfg.GroupVersion = &gv
cfg.APIPath = "/api"
cfg.NegotiatedSerializer = rest.CodecFactoryForGeneratedClient(scheme.Scheme, scheme.Codecs).WithoutConversion()
if cfg.UserAgent == "" {
cfg.UserAgent = rest.DefaultKubernetesUserAgent()
}
client, err := rest.RESTClientFor(cfg)
Expect(err).To(Succeed())
req := client.Get().
Resource("pods").
Namespace(namespace).
SetHeader("Accept", "application/json;as=Table;v=v1;g=meta.k8s.io")
result := req.Do(ctx)
if result.Error() != nil {
return nil, result.Error()
}
body, err := result.Raw()
if err != nil {
return nil, err
}
var table metav1.Table
err = json.Unmarshal(body, &table)
if err != nil {
return nil, err
}
return &table, nil
}
GetPodAsTable := func(ctx context.Context, config *rest.Config, namespace, name string) (*metav1.Table, error) {
cfg := rest.CopyConfig(config)
gv := corev1.SchemeGroupVersion
cfg.GroupVersion = &gv
cfg.APIPath = "/api"
cfg.NegotiatedSerializer = rest.CodecFactoryForGeneratedClient(scheme.Scheme, scheme.Codecs).WithoutConversion()
if cfg.UserAgent == "" {
cfg.UserAgent = rest.DefaultKubernetesUserAgent()
}
client, err := rest.RESTClientFor(cfg)
Expect(err).To(Succeed())
req := client.Get().
Resource("pods").
Namespace(namespace).
Name(name).
SetHeader("Accept", "application/json;as=Table;v=v1;g=meta.k8s.io")
result := req.Do(ctx)
if result.Error() != nil {
return nil, result.Error()
}
body, err := result.Raw()
if err != nil {
return nil, err
}
var table metav1.Table
err = json.Unmarshal(body, &table)
if err != nil {
return nil, err
}
return &table, nil
}
WatchPods := func(ctx context.Context, client kubernetes.Interface, namespace string, expected int, timeout time.Duration) []string {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
got := make([]string, 0, expected)
watcher, err := client.CoreV1().Pods(namespace).Watch(ctx, metav1.ListOptions{
ResourceVersion: "0",
})
Expect(err).To(Succeed())
defer watcher.Stop()
for e := range watcher.ResultChan() {
pod, ok := e.Object.(*corev1.Pod)
if !ok {
return got
}
got = append(got, pod.Name)
if len(got) == expected {
return got
}
}
return got
}
// Custom Resource helper functions
CreateTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
resource := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "example.com/v1",
"kind": "TestResource",
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
"spec": map[string]interface{}{
"message": "test message",
},
},
}
_, err := client.Resource(gvr).Namespace(namespace).Create(ctx, resource, metav1.CreateOptions{})
return err
}
DeleteTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
return client.Resource(gvr).Namespace(namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
GetTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
_, err := client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
return err
}
UpdateTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string, message string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
resource, err := client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}
spec, ok := resource.Object["spec"].(map[string]interface{})
if !ok {
spec = make(map[string]interface{})
resource.Object["spec"] = spec
}
spec["message"] = message
_, err = client.Resource(gvr).Namespace(namespace).Update(ctx, resource, metav1.UpdateOptions{})
return err
}
ListTestResources := func(ctx context.Context, client dynamic.Interface, namespace string) []string {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
list, err := client.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
Expect(err).To(Succeed())
return lo.Map(list.Items, func(item unstructured.Unstructured, index int) string {
return item.GetName()
})
}
CreateAnotherTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "anothertestresources"}
resource := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "example.com/v1",
"kind": "AnotherTestResource",
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
"spec": map[string]interface{}{
"message": "another test message",
},
},
}
_, err := client.Resource(gvr).Namespace(namespace).Create(ctx, resource, metav1.CreateOptions{})
return err
}
ListAnotherTestResources := func(ctx context.Context, client dynamic.Interface, namespace string) []string {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "anothertestresources"}
list, err := client.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
Expect(err).To(Succeed())
return lo.Map(list.Items, func(item unstructured.Unstructured, index int) string {
return item.GetName()
})
}
DeleteAnotherTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "anothertestresources"}
return client.Resource(gvr).Namespace(namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
GetAnotherTestResource := func(ctx context.Context, client dynamic.Interface, namespace, name string) error {
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "anothertestresources"}
_, err := client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
return err
}
WatchTestResources := func(ctx context.Context, client dynamic.Interface, namespace string, expected int, timeout time.Duration) []string {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
gvr := schema.GroupVersionResource{Group: "example.com", Version: "v1", Resource: "testresources"}
got := make([]string, 0, expected)
watcher, err := client.Resource(gvr).Namespace(namespace).Watch(ctx, metav1.ListOptions{
ResourceVersion: "0",
})
Expect(err).To(Succeed())
defer watcher.Stop()
for e := range watcher.ResultChan() {
fmt.Println("Event received:", e.Type, e.Object)
resource, ok := e.Object.(*unstructured.Unstructured)
if !ok {
return got
}
got = append(got, resource.GetName())
if len(got) == expected {
return got
}
}
return got
}
JustBeforeEach(func(ctx context.Context) {
// before every test, assert no access
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, paulClient, paulNamespace))).To(BeTrue())
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, paulClient, chaniNamespace))).To(BeTrue())
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, chaniClient, paulNamespace))).To(BeTrue())
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, chaniClient, chaniNamespace))).To(BeTrue())
})
AssertDualWriteBehavior := func() {
It("supports rules for every verb", func(ctx context.Context) {
// watch
var wg errgroup.Group
defer wg.Wait()
wg.Go(func() error {
defer GinkgoRecover()
Expect(WatchPods(ctx, paulClient, paulNamespace, 1, 10*time.Second)).To(ContainElement(paulPod))
return nil
})
// create
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// get
Expect(GetPod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
Expect(GetPod(ctx, chaniClient, paulNamespace, paulPod)).To(Not(Succeed()))
// update
Expect(GetAndUpdatePodLabels(ctx, paulClient, paulNamespace, paulPod, map[string]string{"a": "label"})).To(Succeed())
Expect(GetAndUpdatePodLabels(ctx, chaniClient, paulNamespace, paulPod, map[string]string{"a": "label"})).To(Not(Succeed()))
// patch
Expect(PatchPodLabels(ctx, paulClient, paulNamespace, paulPod, map[string]string{"b": "label"})).To(Succeed())
Expect(PatchPodLabels(ctx, chaniClient, paulNamespace, paulPod, map[string]string{"b": "label"})).To(Not(Succeed()))
// list
Expect(ListPods(ctx, paulClient, paulNamespace)).To(Equal([]string{paulPod}))
Expect(ListPods(ctx, chaniClient, paulNamespace)).To(Equal([]string{}))
// delete
Expect(DeletePod(ctx, chaniClient, paulNamespace, paulPod)).To(Not(Succeed()))
Expect(DeletePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
})
It("supports rules for every verb on custom resources", func(ctx context.Context) {
// watch
var wg errgroup.Group
defer wg.Wait()
wg.Go(func() error {
defer GinkgoRecover()
Expect(WatchTestResources(ctx, paulDynamicClient, paulNamespace, 1, 10*time.Second)).To(ContainElement(paulCustomResource))
return nil
})
// create
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreateTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
// get
Expect(GetTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
Expect(GetTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
// update
Expect(UpdateTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource, "updated message")).To(Succeed())
Expect(UpdateTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource, "unauthorized update")).To(Not(Succeed()))
// list
Expect(ListTestResources(ctx, paulDynamicClient, paulNamespace)).To(Equal([]string{paulCustomResource}))
Expect(ListTestResources(ctx, chaniDynamicClient, paulNamespace)).To(Equal([]string{}))
// delete
Expect(DeleteTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
Expect(DeleteTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
})
It("supports postchecks and deletions by filters", func(ctx context.Context) {
// create
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreateAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
// get
Expect(GetAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
Expect(GetAnotherTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
// list
Expect(ListAnotherTestResources(ctx, paulDynamicClient, paulNamespace)).To(Equal([]string{paulCustomResource}))
// delete
Expect(DeleteAnotherTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
Expect(DeleteAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
Expect(GetAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
})
It("recovers when there is a failure in reading relationships for deletions by filters", func(ctx context.Context) {
// create
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreateAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
// get
Expect(GetAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
Expect(GetAnotherTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
// delete
failpoints.EnableFailPoint("panicReadSpiceDB", 1)
Expect(DeleteAnotherTestResource(ctx, chaniDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
Expect(DeleteAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Succeed())
Expect(GetAnotherTestResource(ctx, paulDynamicClient, paulNamespace, paulCustomResource)).To(Not(Succeed()))
})
It("filters table format responses for get and list operations", func(ctx context.Context) {
// Create namespaces and pods for both users
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreateNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
Expect(CreatePod(ctx, chaniClient, chaniNamespace, chaniPod)).To(Succeed())
// Paul should be able to get his pod as a table
paulTable, err := GetPodAsTable(ctx, paulRestConfig, paulNamespace, paulPod)
Expect(err).To(Succeed())
Expect(paulTable.Kind).To(Equal("Table"))
Expect(len(paulTable.Rows)).To(Equal(1))
// Extract the pod name from the table row
var podObj corev1.Pod
err = json.Unmarshal(paulTable.Rows[0].Object.Raw, &podObj)
Expect(err).To(Succeed())
Expect(podObj.Name).To(Equal(paulPod))
// Paul should not be able to get Chani's pod as a table (should get unauthorized)
chaniTable, err := GetPodAsTable(ctx, paulRestConfig, chaniNamespace, chaniPod)
Expect(k8serrors.IsUnauthorized(err)).To(BeTrue())
Expect(chaniTable).To(BeNil())
// Test LIST operations with table format
// Create a shared namespace with pods from both users
Expect(CreateNamespace(ctx, adminClient, sharedNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, sharedNamespace, paulPod)).To(Succeed())
Expect(CreatePod(ctx, chaniClient, sharedNamespace, chaniPod)).To(Succeed())
// Paul should only see his pod in the table list
paulListTable, err := ListPodsAsTable(ctx, paulRestConfig, sharedNamespace)
Expect(err).To(Succeed())
Expect(paulListTable.Kind).To(Equal("Table"))
Expect(len(paulListTable.Rows)).To(Equal(1))
var paulPodFromList corev1.Pod
err = json.Unmarshal(paulListTable.Rows[0].Object.Raw, &paulPodFromList)
Expect(err).To(Succeed())
Expect(paulPodFromList.Name).To(Equal(paulPod))
// Chani should only see her pod in the table list
chaniListTable, err := ListPodsAsTable(ctx, chaniRestConfig, sharedNamespace)
Expect(err).To(Succeed())
Expect(chaniListTable.Kind).To(Equal("Table"))
Expect(len(chaniListTable.Rows)).To(Equal(1))
var chaniPodFromList corev1.Pod
err = json.Unmarshal(chaniListTable.Rows[0].Object.Raw, &chaniPodFromList)
Expect(err).To(Succeed())
Expect(chaniPodFromList.Name).To(Equal(chaniPod))
// Admin should see both pods in the table list
adminListTable, err := ListPodsAsTable(ctx, adminRestConfig, sharedNamespace)
Expect(err).To(Succeed())
Expect(adminListTable.Kind).To(Equal("Table"))
Expect(len(adminListTable.Rows)).To(Equal(2))
// Extract pod names from admin's view
adminPodNames := make([]string, 0, 2)
for _, row := range adminListTable.Rows {
var pod corev1.Pod
err = json.Unmarshal(row.Object.Raw, &pod)
Expect(err).To(Succeed())
adminPodNames = append(adminPodNames, pod.Name)
}
Expect(adminPodNames).To(ContainElements(paulPod, chaniPod))
})
It("doesn't show users namespaces the other has created", func(ctx context.Context) {
var wg errgroup.Group
defer wg.Wait()
wg.Go(func() error {
defer GinkgoRecover()
Expect(WatchNamespaces(ctx, paulClient, 1)).To(ContainElement(paulNamespace))
return nil
})
wg.Go(func() error {
defer GinkgoRecover()
Expect(WatchNamespaces(ctx, chaniClient, 1)).To(ContainElement(chaniNamespace))
return nil
})
// each creates their respective namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreateNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
// each can get their respective namespace
Expect(GetNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(GetNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
// neither can get each other's namespace
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, paulClient, chaniNamespace))).To(BeTrue())
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, chaniClient, paulNamespace))).To(BeTrue())
// neither can see each other's namespace in the list
paulList := ListNamespaces(ctx, paulClient)
chaniList := ListNamespaces(ctx, chaniClient)
Expect(paulList).ToNot(ContainElement(chaniNamespace))
Expect(paulList).To(ContainElement(paulNamespace))
Expect(chaniList).ToNot(ContainElement(paulNamespace))
Expect(chaniList).To(ContainElement(chaniNamespace))
})
It("recovers when there are kube write failures", func(ctx context.Context) {
// paul creates his namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
// make kube write fail for chani's namespace, spicedb write will have
// succeeded
if lockMode == proxyrule.PessimisticLockMode {
// the locking version retries if the connection fails
failpoints.EnableFailPoint("panicKubeWrite", distributedtx.MaxKubeAttempts+1)
} else {
failpoints.EnableFailPoint("panicKubeWrite", 1)
}
// Chani's write panics, but is retried
Expect(CreateNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
// paul isn't able to create chanis namespace
Expect(CreateNamespace(ctx, paulClient, chaniNamespace)).ToNot(BeNil())
// paul can only get his namespace
Expect(GetNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(GetNamespace(ctx, paulClient, chaniNamespace)).ToNot(BeNil())
// chani can get her namespace - this indicates the workflow was retried and eventually succeeded
Expect(GetNamespace(ctx, chaniClient, paulNamespace)).ToNot(BeNil())
Expect(GetNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
})
It("recovers when there are kube delete failures", func(ctx context.Context) {
// paul creates his pod
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// make kube delete fail, spicedb write will have succeeded
if lockMode == proxyrule.PessimisticLockMode {
// the locking version retries if the connection fails
failpoints.EnableFailPoint("panicKubeWrite", distributedtx.MaxKubeAttempts+1)
} else {
failpoints.EnableFailPoint("panicKubeWrite", 1)
}
// delete panics, but is retried
Expect(DeletePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// the pod is gone on subsequent calls
Expect(k8serrors.IsUnauthorized(GetPod(ctx, paulClient, paulNamespace, paulPod))).To(BeTrue())
Expect(k8serrors.IsNotFound(GetPod(ctx, adminClient, paulNamespace, paulPod))).To(BeTrue())
})
It("recovers when kube write succeeds but crashes", func(ctx context.Context) {
// paul creates his namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
// make kube write succeed, but crash process before it can be recorded
failpoints.EnableFailPoint("panicKubeReadResp", 1)
Expect(CreateNamespace(ctx, chaniClient, chaniNamespace)).ToNot(BeNil())
// Chani can get her namespace - the workflow has resolved the write
// Pessimistic locking retried the kube request and got an "already exists" err
// Optimistic locking checked kube and saw that the object already existed
Expect(GetNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
})
It("recovers when kube delete succeeds but crashes", func(ctx context.Context) {
// paul creates his pod
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// make kube delete succeed, but crash process before it can be recorded
failpoints.EnableFailPoint("panicKubeReadResp", 1)
Expect(DeletePod(ctx, paulClient, paulNamespace, paulPod)).ToNot(BeNil())
// Make sure tuples are gone
owners := GetAllTuples(ctx, &v1.RelationshipFilter{
ResourceType: "pod",
OptionalResourceId: paulNamespace + "/" + paulPod,
OptionalRelation: "creator",
OptionalSubjectFilter: &v1.SubjectFilter{SubjectType: "user"},
})
Expect(len(owners)).To(BeZero())
// the pod is gone on subsequent calls
Expect(k8serrors.IsUnauthorized(GetPod(ctx, paulClient, paulNamespace, paulPod))).To(BeTrue())
Expect(k8serrors.IsNotFound(GetPod(ctx, adminClient, paulNamespace, paulPod))).To(BeTrue())
})
It("prevents ownership stealing when crashing", func(ctx context.Context) {
// paul creates chani's namespace, but crashes before returning
failpoints.EnableFailPoint("panicKubeReadResp", 1)
Expect(CreateNamespace(ctx, paulClient, chaniNamespace)).ToNot(BeNil())
// chani attempts to create chani's namespace
err := CreateNamespace(ctx, chaniClient, chaniNamespace)
Expect(k8serrors.IsConflict(err) || k8serrors.IsAlreadyExists(err)).To(BeTrue())
// Chani can't get her namespace - paul created it first and hasn't shared it
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, chaniClient, chaniNamespace))).To(BeTrue())
})
It("prevents ownership stealing when retrying second write", func(ctx context.Context) {
// paul creates chani's namespace,
Expect(CreateNamespace(ctx, paulClient, chaniNamespace)).To(Succeed())
// chani attempts to create chani's namespace, but crashes before returning
failpoints.EnableFailPoint("panicKubeReadResp", 1)
err := CreateNamespace(ctx, chaniClient, chaniNamespace)
Expect(k8serrors.IsConflict(err) || k8serrors.IsAlreadyExists(err)).To(BeTrue())
// Chani can't get her namespace - paul created it first and hasn't shared it
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, chaniClient, chaniNamespace))).To(BeTrue())
})
It("recovers writes when there are spicedb write failures", func(ctx context.Context) {
// paul creates his namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
// make spicedb write crash on chani's namespace write, eventually succeeds
failpoints.EnableFailPoint("panicWriteSpiceDB", 1)
Expect(CreateNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
// paul is unable to create chani's namespace as it's already claimed
Expect(CreateNamespace(ctx, paulClient, chaniNamespace)).ToNot(BeNil())
// Check Chani is able to get namespace
Expect(GetNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
// confirm the relationship exists
Expect(len(GetAllTuples(ctx, &v1.RelationshipFilter{
ResourceType: "namespace",
OptionalResourceId: chaniNamespace,
OptionalRelation: "creator",
OptionalSubjectFilter: &v1.SubjectFilter{SubjectType: "user", OptionalSubjectId: "chani"},
}))).ToNot(BeZero())
})
It("recovers deletes when there are spicedb write failures", func(ctx context.Context) {
// paul creates his namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// make spicedb write crash on pod delete
failpoints.EnableFailPoint("panicWriteSpiceDB", 1)
Expect(DeletePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// chani is able to create pauls's pod, the delete succeeded
Expect(CreatePod(ctx, chaniClient, paulNamespace, paulPod)).To(Succeed())
// confirm the relationship exists
owners := GetAllTuples(ctx, &v1.RelationshipFilter{
ResourceType: "pod",
OptionalResourceId: paulNamespace + "/" + paulPod,
OptionalRelation: "creator",
OptionalSubjectFilter: &v1.SubjectFilter{SubjectType: "user"},
})
Expect(len(owners)).ToNot(BeZero())
Expect(owners[0].Relationship.Subject.Object.ObjectId).To(Equal("chani"))
})
It("recovers a `create` operation after a SpiceDB write followed by crash, retries and succeeds", func(ctx context.Context) {
// paul creates his namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
// make spicedb write crash on chani's namespace write, it should be retried idempotently and succeed
failpoints.EnableFailPoint("panicSpiceDBWriteResp", 1)
err := CreateNamespace(ctx, chaniClient, chaniNamespace)
Expect(err).To(Succeed())
// check that chani can get her namespace
Expect(GetNamespace(ctx, chaniClient, chaniNamespace)).To(Succeed())
// check that paul can't read chani's namespace
Expect(k8serrors.IsUnauthorized(GetNamespace(ctx, paulClient, chaniNamespace))).To(BeTrue())
// confirm the relationship doesn't exist
Expect(len(GetAllTuples(ctx, &v1.RelationshipFilter{
ResourceType: "namespace",
OptionalResourceId: chaniNamespace,
OptionalRelation: "creator",
OptionalSubjectFilter: &v1.SubjectFilter{SubjectType: "user", OptionalSubjectId: "chani"},
}))).To(Equal(1))
})
It("recovers a `delete` operation after a SpiceDB write followed by crash, retries and succeeds", func(ctx context.Context) {
// paul creates his namespace
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
Expect(CreatePod(ctx, paulClient, paulNamespace, paulPod)).To(Succeed())
// chani can't create the same pod
Expect(k8serrors.IsAlreadyExists(CreatePod(ctx, chaniClient, paulNamespace, paulPod))).To(BeTrue())
// chani isn't authorized to get the pod
err := GetPod(ctx, chaniClient, paulNamespace, paulPod)
Expect(k8serrors.IsUnauthorized(err)).To(BeTrue())
// make spicedb write crash on pod delete
failpoints.EnableFailPoint("panicSpiceDBWriteResp", 1)
err = DeletePod(ctx, paulClient, paulNamespace, paulPod)
Expect(err).To(Succeed())
// chani can now re-create paul's pod and take ownership
Expect(CreatePod(ctx, chaniClient, paulNamespace, paulPod)).To(Succeed())
// check that paul can't get the pod that chani created
Expect(k8serrors.IsUnauthorized(GetPod(ctx, paulClient, paulNamespace, paulPod))).To(BeTrue())
// confirm the relationship exists
owners := GetAllTuples(ctx, &v1.RelationshipFilter{
ResourceType: "pod",
OptionalResourceId: paulNamespace + "/" + paulPod,
OptionalRelation: "creator",
OptionalSubjectFilter: &v1.SubjectFilter{SubjectType: "user"},
})
Expect(len(owners)).ToNot(BeZero())
Expect(owners[0].Relationship.Subject.Object.ObjectId).To(Equal("chani"))
// confirm chani can get the pod
Expect(GetPod(ctx, chaniClient, paulNamespace, paulPod)).To(Succeed())
})
It("ensures only one write at a time happens for a given object", MustPassRepeatedly(5), func(ctx context.Context) {
// both attempt to create the namespace
start := make(chan struct{})
errs := make(chan error, 2)
var wg errgroup.Group
// in theory, these two requests could be run serially, but in
// practice they seem to always actually run in parallel as
// intended.
wg.Go(func() error {
defer GinkgoRecover()
<-start
errs <- CreateNamespace(ctx, paulClient, paulNamespace)
return nil
})
wg.Go(func() error {
defer GinkgoRecover()
<-start
errs <- CreateNamespace(ctx, chaniClient, paulNamespace)
return nil
})
start <- struct{}{}
start <- struct{}{}
_ = wg.Wait()
close(errs)
allErrs := make([]error, 0)
for err := range errs {
if err != nil {
allErrs = append(allErrs, err)
}
}
Expect(len(allErrs)).To(Equal(1))
Expect(k8serrors.IsConflict(allErrs[0]) || // pessimistic lock
k8serrors.IsAlreadyExists(allErrs[0]), // optimistic lock
).To(BeTrue())
})
It("revokes access during watch", func(ctx context.Context) {
Expect(CreateNamespace(ctx, adminClient, sharedNamespace)).To(Succeed())
Expect(CreatePod(ctx, chaniClient, sharedNamespace, chaniPod)).To(Succeed())
// Chani deletes her pod, which will remove her access
// to that pod in spicedb
Expect(DeletePod(ctx, chaniClient, sharedNamespace, chaniPod)).To(Succeed())
Eventually(func(g Gomega) {
g.Expect(GetPod(ctx, adminClient, sharedNamespace, chaniPod)).To(Not(Succeed()))
}).Should(Succeed())
// start a watch waiting for one result, but expect 0 results
// after the watch times out
var wg errgroup.Group
wg.Go(func() error {
defer GinkgoRecover()
Expect(len(WatchPods(ctx, chaniClient, sharedNamespace, 1, 2*time.Second))).To(BeZero())
return nil
})
// paul should get an event for the pod he creates
wg.Go(func() error {
defer GinkgoRecover()
Expect(len(WatchPods(ctx, paulClient, sharedNamespace, 1, 2*time.Second))).To(Equal(1))
return nil
})
// Paul creates chani's pod, will generate kube events that
// Chani shouldn't see
Expect(CreatePod(ctx, paulClient, sharedNamespace, chaniPod)).To(Succeed())
// wait for Chani's watch to time out, which means she didn't
// see the events from pauls writes
wg.Wait()
})
}
When("optimistic locking is used", func() {
BeforeEach(func() {
*proxySrv.Matcher = testOptimisticMatcher()
lockMode = proxyrule.OptimisticLockMode
})
AssertDualWriteBehavior()
})
When("pessimistic locking is used", func() {
BeforeEach(func() {
*proxySrv.Matcher = testPessimisticMatcher()
lockMode = proxyrule.PessimisticLockMode
})
AssertDualWriteBehavior()
})
When("no rules match the request", func() {
It("returns unauthenticated error", func(ctx context.Context) {
*proxySrv.Matcher = rules.MatcherFunc(func(match *request.RequestInfo) []*rules.RunnableRule {
return nil
})
Expect(GetNamespace(ctx, paulClient, paulNamespace)).NotTo(Succeed())
})
})
When("PostChecks are used in rules", func() {
It("allows get operations when postchecks pass", func(ctx context.Context) {
getNamespaceWithPostCheck := proxyrule.Config{
Spec: proxyrule.Spec{
Matches: []proxyrule.Match{{
GroupVersion: "v1",
Resource: "namespaces",
Verbs: []string{"get"},
}},
Checks: []proxyrule.StringOrTemplate{{
Template: "namespace:{{name}}#view@user:{{user.name}}",
}},
PostChecks: []proxyrule.StringOrTemplate{{
Template: "namespace:{{name}}#edit@user:{{user.name}}",
}},
},
}
// Set up matcher with postcheck rule
matcher, err := rules.NewMapMatcher([]proxyrule.Config{
createNamespace(),
getNamespaceWithPostCheck,
})
Expect(err).To(Succeed())
*proxySrv.Matcher = matcher
// Create namespace and required relationships
Expect(CreateNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
// Add viewer relation for Paul (so postcheck will pass)
WriteTuples(ctx, []*v1.Relationship{{
Resource: &v1.ObjectReference{ObjectType: "namespace", ObjectId: paulNamespace},
Relation: "viewer",
Subject: &v1.SubjectReference{Object: &v1.ObjectReference{ObjectType: "user", ObjectId: "paul"}},
}})
// Paul should be able to get the namespace (postchecks pass)
Expect(GetNamespace(ctx, paulClient, paulNamespace)).To(Succeed())
})
It("blocks get operations when postchecks fail", func(ctx context.Context) {
getNamespaceWithPostCheck := proxyrule.Config{
Spec: proxyrule.Spec{
Matches: []proxyrule.Match{{