forked from antrea-io/antrea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantreapolicy_test.go
1539 lines (1411 loc) · 59.5 KB
/
antreapolicy_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 2020 Antrea 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 e2e
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
v1net "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
secv1alpha1 "github.com/vmware-tanzu/antrea/pkg/apis/security/v1alpha1"
"github.com/vmware-tanzu/antrea/pkg/features"
. "github.com/vmware-tanzu/antrea/test/e2e/utils"
)
// common for all tests.
var (
allPods []Pod
k8sUtils *KubernetesUtils
allTestList []*TestCase
pods, namespaces []string
podIPs map[string]string
p80, p81, p8080, p8082 int
p8081, p8085 int32
)
const (
// provide enough time for policies to be enforced & deleted by the CNI plugin.
networkPolicyDelay = 2 * time.Second
// audit log directory on Antrea Agent
logDir = "/var/log/antrea/networkpolicy/"
logfileName = "np.log"
defaultTierName = "application"
)
func failOnError(err error, t *testing.T) {
if err != nil {
log.Errorf("%+v", err)
k8sUtils.Cleanup(namespaces)
t.Fatalf("test failed: %v", err)
}
}
// TestCase is a collection of TestSteps to be tested against.
type TestCase struct {
Name string
Steps []*TestStep
}
// TestStep is a single unit of testing spec. It includes the ACNP specs that need to be
// applied for this test, the port to test traffic on and the expected Reachability matrix.
type TestStep struct {
Name string
Reachability *Reachability
Policies []metav1.Object
Port []int
Duration time.Duration
}
func initialize(t *testing.T, data *TestData) {
p80 = 80
p81 = 81
p8080 = 8080
p8081 = 8081
p8082 = 8082
p8085 = 8085
pods = []string{"a", "b", "c"}
namespaces = []string{"x", "y", "z"}
for _, podName := range pods {
for _, ns := range namespaces {
allPods = append(allPods, NewPod(ns, podName))
}
}
skipIfAntreaPolicyDisabled(t, data)
var err error
// k8sUtils is a global var
k8sUtils, err = NewKubernetesUtils(data)
failOnError(err, t)
ips, err := k8sUtils.Bootstrap(namespaces, pods)
failOnError(err, t)
podIPs = *ips
}
func skipIfAntreaPolicyDisabled(tb testing.TB, data *TestData) {
if featureGate, err := data.GetControllerFeatures(antreaNamespace); err != nil {
tb.Fatalf("Cannot determine if ACNP enabled: %v", err)
} else if !featureGate.Enabled(features.AntreaPolicy) {
tb.Skipf("Skipping test as it required ACNP to be enabled")
}
}
func applyDefaultDenyToAllNamespaces(k8s *KubernetesUtils, namespaces []string) error {
if err := k8s.CleanNetworkPolicies(namespaces); err != nil {
return err
}
for _, ns := range namespaces {
builder := &NetworkPolicySpecBuilder{}
builder = builder.SetName(ns, "default-deny-namespace")
builder.SetTypeIngress()
if _, err := k8s.CreateOrUpdateNetworkPolicy(ns, builder.Get()); err != nil {
return err
}
}
time.Sleep(networkPolicyDelay)
r := NewReachability(allPods, false)
k8s.Validate(allPods, r, p80)
_, wrong, _ := r.Summary()
if wrong != 0 {
return fmt.Errorf("error when creating default deny k8s NetworkPolicies")
}
return nil
}
func cleanupDefaultDenyNPs(k8s *KubernetesUtils, namespaces []string) error {
if err := k8s.CleanNetworkPolicies(namespaces); err != nil {
return err
}
time.Sleep(networkPolicyDelay * 2)
r := NewReachability(allPods, true)
k8s.Validate(allPods, r, p80)
_, wrong, _ := r.Summary()
if wrong != 0 {
return fmt.Errorf("error when cleaning default deny k8s NetworkPolicies")
}
return nil
}
func testMutateACNPNoTier(t *testing.T) {
invalidNpErr := fmt.Errorf("ACNP tier not mutated to default tier")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-no-tier").
SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil).
SetPriority(10.0)
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
acnp, err := k8sUtils.CreateOrUpdateACNP(acnp)
if err != nil {
failOnError(fmt.Errorf("ACNP create failed %v", err), t)
}
if acnp.Spec.Tier != defaultTierName {
failOnError(invalidNpErr, t)
}
failOnError(k8sUtils.CleanACNPs(), t)
}
func testMutateANPNoTier(t *testing.T) {
invalidNpErr := fmt.Errorf("ANP tier not mutated to default tier")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("x", "anp-no-tier").
SetAppliedToGroup(map[string]string{"pod": "a"}, nil).
SetPriority(10.0)
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
anp, err := k8sUtils.CreateOrUpdateANP(anp)
if err != nil {
failOnError(fmt.Errorf("ANP create failed %v", err), t)
}
if anp.Spec.Tier != defaultTierName {
failOnError(invalidNpErr, t)
}
failOnError(k8sUtils.CleanANPs([]string{anp.Namespace}), t)
}
func testMutateACNPNoRuleName(t *testing.T) {
mutateErr := fmt.Errorf("ACNP Rule name not mutated automatically")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-no-rule-name").
SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil).
SetPriority(10.0).
AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, map[string]string{"pod": "b"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
acnp, err := k8sUtils.CreateOrUpdateACNP(acnp)
if err != nil {
failOnError(fmt.Errorf("ACNP create failed %v", err), t)
}
ir := acnp.Spec.Ingress
if len(ir) != 1 {
failOnError(fmt.Errorf("unexpected number of rules present in ACNP: %d rules present instead of 1", len(ir)), t)
}
// Here we created a single rule
if ir[0].Name == "" {
failOnError(mutateErr, t)
}
failOnError(k8sUtils.CleanACNPs(), t)
}
func testMutateANPNoRuleName(t *testing.T) {
mutateErr := fmt.Errorf("ANP Rule name not mutated automatically")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("x", "anp-no-rule-name").
SetAppliedToGroup(map[string]string{"pod": "a"}, nil).
SetPriority(10.0).
AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, map[string]string{"pod": "b"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
anp, err := k8sUtils.CreateOrUpdateANP(anp)
if err != nil {
failOnError(fmt.Errorf("ANP create failed %v", err), t)
}
ir := anp.Spec.Ingress
if len(ir) != 1 {
failOnError(fmt.Errorf("unexpected number of rules present in ANP: %d rules present instead of 1", len(ir)), t)
}
// Here we created a single rule
if ir[0].Name == "" {
failOnError(mutateErr, t)
}
failOnError(k8sUtils.CleanANPs([]string{anp.Namespace}), t)
}
func testInvalidACNPNoPriority(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea ClusterNetworkPolicy without a priority accepted")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-no-priority").SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil)
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
if _, err := k8sUtils.CreateOrUpdateACNP(acnp); err == nil {
// Above creation of ACNP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidACNPRuleNameNotUnique(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea ClusterNetworkPolicy without unique rule names accepted")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-rule-name-not-unique").SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil).
AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, map[string]string{"pod": "b"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "not-unique").
AddIngress(v1.ProtocolTCP, &p81, nil, nil, nil, map[string]string{"pod": "c"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "not-unique")
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
if _, err := k8sUtils.CreateOrUpdateACNP(acnp); err == nil {
// Above creation of ACNP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidACNPTierDoesNotExist(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea ClusterNetworkPolicy without existing Tier accepted")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-tier-not-exist").SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil).
SetTier("i-dont-exist")
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
if _, err := k8sUtils.CreateOrUpdateACNP(acnp); err == nil {
// Above creation of ACNP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidACNPPortRangePortUnset(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea ClusterNetworkPolicy egress rule with endPort but no port accepted")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-egress-port-range-port-unset").
SetPriority(1.0).
SetAppliedToGroup(map[string]string{"pod": "b"}, nil, nil, nil)
builder.AddEgress(v1.ProtocolTCP, nil, nil, &p8085, nil, map[string]string{"pod": "c"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "acnp-port-range")
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
if _, err := k8sUtils.CreateOrUpdateACNP(acnp); err == nil {
// Above creation of ACNP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidACNPPortRangeEndPortSmall(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea ClusterNetworkPolicy egress rule with endPort smaller than port accepted")
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-egress-port-range-endport-small").
SetPriority(1.0).
SetAppliedToGroup(map[string]string{"pod": "b"}, nil, nil, nil)
builder.AddEgress(v1.ProtocolTCP, &p8082, nil, &p8081, nil, map[string]string{"pod": "c"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "acnp-port-range")
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
if _, err := k8sUtils.CreateOrUpdateACNP(acnp); err == nil {
// Above creation of ACNP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidANPNoPriority(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea NetworkPolicy without a priority accepted")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("x", "anp-no-priority").SetAppliedToGroup(map[string]string{"pod": "a"}, nil)
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
if _, err := k8sUtils.CreateOrUpdateANP(anp); err == nil {
// Above creation of ANP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidANPRuleNameNotUnique(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea NetworkPolicy without unique rule names accepted")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("x", "anp-rule-name-not-unique").SetAppliedToGroup(map[string]string{"pod": "a"}, nil).
AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, map[string]string{"pod": "b"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "not-unique").
AddIngress(v1.ProtocolTCP, &p81, nil, nil, nil, map[string]string{"pod": "c"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "not-unique")
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
if _, err := k8sUtils.CreateOrUpdateANP(anp); err == nil {
// Above creation of ANP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidANPTierDoesNotExist(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea NetworkPolicy without existing Tier accepted")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("x", "anp-tier-not-exist").SetAppliedToGroup(map[string]string{"pod": "a"}, nil).
SetTier("i-dont-exist")
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
if _, err := k8sUtils.CreateOrUpdateANP(anp); err == nil {
// Above creation of ANP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidANPPortRangePortUnset(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea NetworkPolicy egress rule with endPort but no port accepted")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("y", "anp-egress-port-range-port-unset").
SetPriority(1.0).
SetAppliedToGroup(map[string]string{"pod": "b"}, nil)
builder.AddEgress(v1.ProtocolTCP, nil, nil, &p8085, nil, map[string]string{"pod": "c"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "anp-port-range")
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
if _, err := k8sUtils.CreateOrUpdateANP(anp); err == nil {
// Above creation of ANP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidANPPortRangeEndPortSmall(t *testing.T) {
invalidNpErr := fmt.Errorf("invalid Antrea NetworkPolicy egress rule with endPort smaller than port accepted")
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("y", "anp-egress-port-range-endport-small").
SetPriority(1.0).
SetAppliedToGroup(map[string]string{"pod": "b"}, nil)
builder.AddEgress(v1.ProtocolTCP, &p8082, nil, &p8081, nil, map[string]string{"pod": "c"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "anp-port-range")
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
if _, err := k8sUtils.CreateOrUpdateANP(anp); err == nil {
// Above creation of ANP must fail as it is an invalid spec.
failOnError(invalidNpErr, t)
}
}
func testInvalidTierReservedDelete(t *testing.T) {
invalidErr := fmt.Errorf("reserved Tier deleted")
if err := k8sUtils.DeleteTier("emergency"); err == nil {
// Above deletion of reserved Tier must fail.
failOnError(invalidErr, t)
}
}
func testInvalidTierPriorityUpdate(t *testing.T) {
invalidErr := fmt.Errorf("tier priority updated")
oldTier, err := k8sUtils.CreateNewTier("prio-updated-tier", 21)
if err != nil {
failOnError(fmt.Errorf("create Tier failed for tier prio-updated-tier: %v", err), t)
}
// Update this tier with new priority
newTier := secv1alpha1.Tier{
ObjectMeta: oldTier.ObjectMeta,
Spec: oldTier.Spec,
}
// Attempt to update Tier's priority
newTier.Spec.Priority = 31
// Above update of Tier must fail as it is an invalid case.
if _, err = k8sUtils.UpdateTier(&newTier); err == nil {
failOnError(invalidErr, t)
}
failOnError(k8sUtils.DeleteTier(oldTier.Name), t)
}
func testInvalidTierPriorityOverlap(t *testing.T) {
invalidErr := fmt.Errorf("tiers created with overlapping priorities")
tr, err := k8sUtils.CreateNewTier("tier-prio-20", 20)
if err != nil {
failOnError(fmt.Errorf("create Tier failed for tier tier-prio-20: %v", err), t)
}
// Attempt to create Tier with same priority.
if _, err = k8sUtils.CreateNewTier("another-tier-prio-20", 20); err == nil {
// Above creation of Tier must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
failOnError(k8sUtils.DeleteTier(tr.Name), t)
}
func testInvalidTierReservedPriority(t *testing.T) {
invalidErr := fmt.Errorf("tier created with reserved priority")
if _, err := k8sUtils.CreateNewTier("tier-reserved-prio", 251); err == nil {
// Above creation of Tier must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidTierACNPRefDelete(t *testing.T) {
invalidErr := fmt.Errorf("tier deleted with referenced ACNPs")
tr, err := k8sUtils.CreateNewTier("tier-acnp", 10)
if err != nil {
failOnError(fmt.Errorf("create Tier failed for tier tier-acnp: %v", err), t)
}
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-for-tier").
SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil).
SetTier("tier-acnp").
SetPriority(13.0)
acnp := builder.Get()
log.Debugf("creating ACNP %v", acnp.Name)
if _, err = k8sUtils.CreateOrUpdateACNP(acnp); err != nil {
failOnError(fmt.Errorf("create ACNP failed for ACNP %s: %v", acnp.Name, err), t)
}
// Deleting this Tier must fail as it has referenced ACNP
if err = k8sUtils.DeleteTier(tr.Name); err == nil {
failOnError(invalidErr, t)
}
failOnError(k8sUtils.CleanACNPs(), t)
failOnError(k8sUtils.DeleteTier(tr.Name), t)
}
func testInvalidTierANPRefDelete(t *testing.T) {
invalidErr := fmt.Errorf("tier deleted with referenced ANPs")
tr, err := k8sUtils.CreateNewTier("tier-anp", 10)
if err != nil {
failOnError(fmt.Errorf("create Tier failed for tier tier-anp: %v", err), t)
}
builder := &AntreaNetworkPolicySpecBuilder{}
builder = builder.SetName("x", "anp-for-tier").
SetAppliedToGroup(map[string]string{"pod": "a"}, nil).
SetTier("tier-anp").
SetPriority(13.0)
anp := builder.Get()
log.Debugf("creating ANP %v", anp.Name)
if _, err = k8sUtils.CreateOrUpdateANP(anp); err != nil {
failOnError(fmt.Errorf("create ANP failed for ANP %s: %v", anp.Name, err), t)
}
// Deleting this Tier must fail as it has referenced ANP
if err = k8sUtils.DeleteTier(tr.Name); err == nil {
failOnError(invalidErr, t)
}
failOnError(k8sUtils.CleanANPs([]string{anp.Namespace}), t)
failOnError(k8sUtils.DeleteTier(tr.Name), t)
}
// testACNPAllowXBtoA tests traffic from X/B to pods with label A, after applying the default deny
// k8s NetworkPolicies in all namespaces and ACNP to allow X/B to A.
func testACNPAllowXBtoA(t *testing.T) {
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-allow-xb-to-a").
SetPriority(1.0).
SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil)
builder.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, map[string]string{"pod": "b"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
reachability := NewReachability(allPods, false)
reachability.Expect(Pod("x/b"), Pod("x/a"), true)
reachability.Expect(Pod("x/b"), Pod("y/a"), true)
reachability.Expect(Pod("x/b"), Pod("z/a"), true)
reachability.ExpectSelf(allPods, true)
testStep := []*TestStep{
{
"Port 80",
reachability,
[]metav1.Object{builder.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP Allow X/B to A", testStep},
}
executeTests(t, testCase)
}
// testACNPAllowXBtoYA tests traffic from X/B to Y/A on named port 81, after applying the default deny
// k8s NetworkPolicies in all namespaces and ACNP to allow X/B to Y/A.
func testACNPAllowXBtoYA(t *testing.T) {
port81Name := "serve-81"
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-allow-xb-to-ya").
SetPriority(2.0).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "y"}, nil, nil)
builder.AddIngress(v1.ProtocolTCP, nil, &port81Name, nil, nil, map[string]string{"pod": "b"}, map[string]string{"ns": "x"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
reachability := NewReachability(allPods, false)
reachability.Expect(Pod("x/b"), Pod("y/a"), true)
reachability.ExpectSelf(allPods, true)
testStep := []*TestStep{
{
"NamedPort 81",
reachability,
[]metav1.Object{builder.Get()},
[]int{81},
0,
},
}
testCase := []*TestCase{
{"ACNP Allow X/B to Y/A", testStep},
}
executeTests(t, testCase)
}
// testACNPPriorityOverrideDefaultDeny tests priority override in ACNP. It applies a higher priority ACNP to drop
// traffic from namespace Z to X/A, and in the meantime applies a lower priority ACNP to allow traffic from Z to X.
// It is tested with default deny k8s NetworkPolicies in all namespaces.
func testACNPPriorityOverrideDefaultDeny(t *testing.T) {
builder1 := &ClusterNetworkPolicySpecBuilder{}
builder1 = builder1.SetName("acnp-priority2").
SetPriority(2).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
builder1.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
builder2 := &ClusterNetworkPolicySpecBuilder{}
builder2 = builder2.SetName("acnp-priority1").
SetPriority(1).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "x"}, nil, nil)
builder2.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
// Ingress from ns:z to x/a will be dropped since acnp-priority1 has higher precedence.
reachabilityBothACNP := NewReachability(allPods, false)
reachabilityBothACNP.Expect(Pod("z/a"), Pod("x/b"), true)
reachabilityBothACNP.Expect(Pod("z/a"), Pod("x/c"), true)
reachabilityBothACNP.Expect(Pod("z/b"), Pod("x/b"), true)
reachabilityBothACNP.Expect(Pod("z/b"), Pod("x/c"), true)
reachabilityBothACNP.Expect(Pod("z/c"), Pod("x/b"), true)
reachabilityBothACNP.Expect(Pod("z/c"), Pod("x/c"), true)
reachabilityBothACNP.ExpectSelf(allPods, true)
testStep := []*TestStep{
{
"Both ACNP",
reachabilityBothACNP,
[]metav1.Object{builder1.Get(), builder2.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP PriorityOverride Default Deny", testStep},
}
executeTests(t, testCase)
}
// testACNPAllowNoDefaultIsolation tests that no default isolation rules are created for Policies.
func testACNPAllowNoDefaultIsolation(t *testing.T) {
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-allow-x-ingress-y-egress-z").
SetPriority(1.1).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
builder.AddIngress(v1.ProtocolTCP, &p81, nil, nil, nil, nil, map[string]string{"ns": "y"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
builder.AddEgress(v1.ProtocolTCP, &p81, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
reachability := NewReachability(allPods, true)
testStep := []*TestStep{
{
"Port 81",
reachability,
[]metav1.Object{builder.Get()},
[]int{81},
0,
},
}
testCase := []*TestCase{
{"ACNP Allow No Default Isolation", testStep},
}
executeTests(t, testCase)
}
// testACNPDropEgress tests that a ACNP is able to drop egress traffic from pods labelled A to namespace Z.
func testACNPDropEgress(t *testing.T) {
builder := &ClusterNetworkPolicySpecBuilder{}
builder = builder.SetName("acnp-deny-a-to-z-egress").
SetPriority(1.0).
SetAppliedToGroup(map[string]string{"pod": "a"}, nil, nil, nil)
builder.AddEgress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
reachability := NewReachability(allPods, true)
reachability.Expect(Pod("x/a"), Pod("z/a"), false)
reachability.Expect(Pod("x/a"), Pod("z/b"), false)
reachability.Expect(Pod("x/a"), Pod("z/c"), false)
reachability.Expect(Pod("y/a"), Pod("z/a"), false)
reachability.Expect(Pod("y/a"), Pod("z/b"), false)
reachability.Expect(Pod("y/a"), Pod("z/c"), false)
reachability.Expect(Pod("z/a"), Pod("z/b"), false)
reachability.Expect(Pod("z/a"), Pod("z/c"), false)
testStep := []*TestStep{
{
"Port 80",
reachability,
[]metav1.Object{builder.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP Drop Egress From All Pod:a to NS:z", testStep},
}
executeTests(t, testCase)
}
// testBaselineNamespaceIsolation tests that a ACNP in the baseline Tier is able to enforce default namespace isolation,
// which can be later overridden by developer K8s NetworkPolicies.
func testBaselineNamespaceIsolation(t *testing.T) {
builder := &ClusterNetworkPolicySpecBuilder{}
nsExpOtherThanX := metav1.LabelSelectorRequirement{
Key: "ns",
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{"x"},
}
builder = builder.SetName("acnp-baseline-isolate-ns-x").
SetTier("baseline").
SetPriority(1.0).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
builder.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, nil,
nil, &[]metav1.LabelSelectorRequirement{nsExpOtherThanX},
nil, secv1alpha1.RuleActionDrop, "")
// create a K8s NetworkPolicy for Pods in namespace x to allow ingress traffic from Pods in the same namespace,
// as well as from the y/a Pod. It should open up ingress from y/a since it's evaluated before the baseline tier.
k8sNPBuilder := &NetworkPolicySpecBuilder{}
k8sNPBuilder = k8sNPBuilder.SetName("x", "allow-ns-x-and-y-a").
SetTypeIngress().
AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil,
nil, map[string]string{"ns": "x"}, nil, nil).
AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil,
map[string]string{"pod": "a"}, map[string]string{"ns": "y"}, nil, nil)
reachability := NewReachability(allPods, true)
reachability.Expect(Pod("y/b"), Pod("x/a"), false)
reachability.Expect(Pod("y/c"), Pod("x/a"), false)
reachability.Expect(Pod("z/a"), Pod("x/a"), false)
reachability.Expect(Pod("z/b"), Pod("x/a"), false)
reachability.Expect(Pod("z/c"), Pod("x/a"), false)
reachability.Expect(Pod("y/b"), Pod("x/b"), false)
reachability.Expect(Pod("y/c"), Pod("x/b"), false)
reachability.Expect(Pod("z/a"), Pod("x/b"), false)
reachability.Expect(Pod("z/b"), Pod("x/b"), false)
reachability.Expect(Pod("z/c"), Pod("x/b"), false)
reachability.Expect(Pod("y/b"), Pod("x/c"), false)
reachability.Expect(Pod("y/c"), Pod("x/c"), false)
reachability.Expect(Pod("z/a"), Pod("x/c"), false)
reachability.Expect(Pod("z/b"), Pod("x/c"), false)
reachability.Expect(Pod("z/c"), Pod("x/c"), false)
testStep := []*TestStep{
{
"Port 80",
reachability,
[]metav1.Object{builder.Get(), k8sNPBuilder.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP baseline tier namespace isolation", testStep},
}
executeTests(t, testCase)
// Cleanup the K8s NetworkPolicy created for this test.
failOnError(k8sUtils.CleanNetworkPolicies([]string{"x"}), t)
time.Sleep(networkPolicyDelay)
}
// testACNPPriorityOverride tests priority overriding in three Policies. Those three Policies are applied in a specific order to
// test priority reassignment, and each controls a smaller set of traffic patterns as priority increases.
func testACNPPriorityOverride(t *testing.T) {
builder1 := &ClusterNetworkPolicySpecBuilder{}
builder1 = builder1.SetName("acnp-priority1").
SetPriority(1.001).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "x"}, nil, nil)
podZBIP, _ := podIPs["z/b"]
cidr := podZBIP + "/32"
// Highest priority. Drops traffic from z/b to x/a.
builder1.AddIngress(v1.ProtocolTCP, &p80, nil, nil, &cidr, nil, nil,
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
builder2 := &ClusterNetworkPolicySpecBuilder{}
builder2 = builder2.SetName("acnp-priority2").
SetPriority(1.002).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "x"}, nil, nil)
// Medium priority. Allows traffic from z to x/a.
builder2.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
builder3 := &ClusterNetworkPolicySpecBuilder{}
builder3 = builder3.SetName("acnp-priority3").
SetPriority(1.003).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
// Lowest priority. Drops traffic from z to x.
builder3.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
reachabilityTwoACNPs := NewReachability(allPods, true)
reachabilityTwoACNPs.Expect(Pod("z/a"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/a"), Pod("x/c"), false)
reachabilityTwoACNPs.Expect(Pod("z/b"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/b"), Pod("x/c"), false)
reachabilityTwoACNPs.Expect(Pod("z/c"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/c"), Pod("x/c"), false)
reachabilityAllACNPs := NewReachability(allPods, true)
reachabilityAllACNPs.Expect(Pod("z/a"), Pod("x/b"), false)
reachabilityAllACNPs.Expect(Pod("z/a"), Pod("x/c"), false)
reachabilityAllACNPs.Expect(Pod("z/b"), Pod("x/a"), false)
reachabilityAllACNPs.Expect(Pod("z/b"), Pod("x/b"), false)
reachabilityAllACNPs.Expect(Pod("z/b"), Pod("x/c"), false)
reachabilityAllACNPs.Expect(Pod("z/c"), Pod("x/b"), false)
reachabilityAllACNPs.Expect(Pod("z/c"), Pod("x/c"), false)
testStepTwoACNP := []*TestStep{
{
"Two Policies with different priorities",
reachabilityTwoACNPs,
[]metav1.Object{builder3.Get(), builder2.Get()},
[]int{80},
0,
},
}
// Create the Policies in specific order to make sure that priority re-assignments work as expected.
testStepAll := []*TestStep{
{
"All three Policies",
reachabilityAllACNPs,
[]metav1.Object{builder3.Get(), builder1.Get(), builder2.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP PriorityOverride Intermediate", testStepTwoACNP},
{"ACNP PriorityOverride All", testStepAll},
}
executeTests(t, testCase)
}
// testACNPTierOverride tests tier priority overriding in three Policies.
// Each ACNP controls a smaller set of traffic patterns as tier priority increases.
func testACNPTierOverride(t *testing.T) {
builder1 := &ClusterNetworkPolicySpecBuilder{}
builder1 = builder1.SetName("acnp-tier-emergency").
SetTier("emergency").
SetPriority(100).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "x"}, nil, nil)
podZBIP, _ := podIPs["z/b"]
cidr := podZBIP + "/32"
// Highest priority tier. Drops traffic from z/b to x/a.
builder1.AddIngress(v1.ProtocolTCP, &p80, nil, nil, &cidr, nil, nil,
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
builder2 := &ClusterNetworkPolicySpecBuilder{}
builder2 = builder2.SetName("acnp-tier-securityops").
SetTier("securityops").
SetPriority(10).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "x"}, nil, nil)
// Medium priority tier. Allows traffic from z to x/a.
builder2.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
builder3 := &ClusterNetworkPolicySpecBuilder{}
builder3 = builder3.SetName("acnp-tier-application").
SetTier("application").
SetPriority(1).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
// Lowest priority tier. Drops traffic from z to x.
builder3.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
reachabilityTwoACNPs := NewReachability(allPods, true)
reachabilityTwoACNPs.Expect(Pod("z/a"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/a"), Pod("x/c"), false)
reachabilityTwoACNPs.Expect(Pod("z/b"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/b"), Pod("x/c"), false)
reachabilityTwoACNPs.Expect(Pod("z/c"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/c"), Pod("x/c"), false)
reachabilityAllACNPs := NewReachability(allPods, true)
reachabilityAllACNPs.Expect(Pod("z/a"), Pod("x/b"), false)
reachabilityAllACNPs.Expect(Pod("z/a"), Pod("x/c"), false)
reachabilityAllACNPs.Expect(Pod("z/b"), Pod("x/a"), false)
reachabilityAllACNPs.Expect(Pod("z/b"), Pod("x/b"), false)
reachabilityAllACNPs.Expect(Pod("z/b"), Pod("x/c"), false)
reachabilityAllACNPs.Expect(Pod("z/c"), Pod("x/b"), false)
reachabilityAllACNPs.Expect(Pod("z/c"), Pod("x/c"), false)
testStepTwoACNP := []*TestStep{
{
"Two Policies in different tiers",
reachabilityTwoACNPs,
[]metav1.Object{builder3.Get(), builder2.Get()},
[]int{80},
0,
},
}
testStepAll := []*TestStep{
{
"All three Policies in different tiers",
reachabilityAllACNPs,
[]metav1.Object{builder3.Get(), builder1.Get(), builder2.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP TierOverride Intermediate", testStepTwoACNP},
{"ACNP TierOverride All", testStepAll},
}
executeTests(t, testCase)
}
// testACNPTierOverride tests tier priority overriding in three Policies with custom created tiers.
// Each ACNP controls a smaller set of traffic patterns as tier priority increases.
func testACNPCustomTiers(t *testing.T) {
// Create two custom tiers with tier priority immediately next to each other.
_, err := k8sUtils.CreateNewTier("high-priority", 245)
failOnError(err, t)
_, err = k8sUtils.CreateNewTier("low-priority", 246)
failOnError(err, t)
builder1 := &ClusterNetworkPolicySpecBuilder{}
builder1 = builder1.SetName("acnp-tier-high").
SetTier("high-priority").
SetPriority(100).
SetAppliedToGroup(map[string]string{"pod": "a"}, map[string]string{"ns": "x"}, nil, nil)
// Medium priority tier. Allows traffic from z to x/a.
builder1.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
builder2 := &ClusterNetworkPolicySpecBuilder{}
builder2 = builder2.SetName("acnp-tier-low").
SetTier("low-priority").
SetPriority(1).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
// Lowest priority tier. Drops traffic from z to x.
builder2.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
reachabilityTwoACNPs := NewReachability(allPods, true)
reachabilityTwoACNPs.Expect(Pod("z/a"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/a"), Pod("x/c"), false)
reachabilityTwoACNPs.Expect(Pod("z/b"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/b"), Pod("x/c"), false)
reachabilityTwoACNPs.Expect(Pod("z/c"), Pod("x/b"), false)
reachabilityTwoACNPs.Expect(Pod("z/c"), Pod("x/c"), false)
testStepTwoACNP := []*TestStep{
{
"Two Policies in different tiers",
reachabilityTwoACNPs,
[]metav1.Object{builder2.Get(), builder1.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP Custom Tier priority", testStepTwoACNP},
}
executeTests(t, testCase)
// Cleanup customed tiers. ACNPs created in those tiers need to be deleted first.
failOnError(k8sUtils.CleanACNPs(), t)
time.Sleep(networkPolicyDelay)
failOnError(k8sUtils.DeleteTier("high-priority"), t)
failOnError(k8sUtils.DeleteTier("low-priority"), t)
}
// testACNPPriorityConflictingRule tests that if there are two Policies in the cluster with rules that conflicts with
// each other, the ACNP with higher priority will prevail.
func testACNPPriorityConflictingRule(t *testing.T) {
builder1 := &ClusterNetworkPolicySpecBuilder{}
builder1 = builder1.SetName("acnp-drop").
SetPriority(1).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
builder1.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
builder2 := &ClusterNetworkPolicySpecBuilder{}
builder2 = builder2.SetName("acnp-allow").
SetPriority(2).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
// The following ingress rule will take no effect as it is exactly the same as ingress rule of cnp-drop,
// but cnp-allow has lower priority.
builder2.AddIngress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
reachabilityBothACNP := NewReachability(allPods, true)
reachabilityBothACNP.Expect(Pod("z/a"), Pod("x/a"), false)
reachabilityBothACNP.Expect(Pod("z/a"), Pod("x/b"), false)
reachabilityBothACNP.Expect(Pod("z/a"), Pod("x/c"), false)
reachabilityBothACNP.Expect(Pod("z/b"), Pod("x/a"), false)
reachabilityBothACNP.Expect(Pod("z/b"), Pod("x/b"), false)
reachabilityBothACNP.Expect(Pod("z/b"), Pod("x/c"), false)
reachabilityBothACNP.Expect(Pod("z/c"), Pod("x/a"), false)
reachabilityBothACNP.Expect(Pod("z/c"), Pod("x/b"), false)
reachabilityBothACNP.Expect(Pod("z/c"), Pod("x/c"), false)
testStep := []*TestStep{
{
"Both ACNP",
reachabilityBothACNP,
[]metav1.Object{builder1.Get(), builder2.Get()},
[]int{80},
0,
},
}
testCase := []*TestCase{
{"ACNP Priority Conflicting Rule", testStep},
}
executeTests(t, testCase)
}
// testACNPPriorityConflictingRule tests that if there are two rules in the cluster that conflicts with
// each other, the rule with higher precedence will prevail.
func testACNPRulePrioirty(t *testing.T) {
builder1 := &ClusterNetworkPolicySpecBuilder{}
// acnp-deny will apply to all pods in namespace x
builder1 = builder1.SetName("acnp-deny").
SetPriority(5).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
builder1.AddEgress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "y"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
// This rule should take no effect as it will be overridden by the first rule of cnp-allow
builder1.AddEgress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionDrop, "")
builder2 := &ClusterNetworkPolicySpecBuilder{}
// acnp-allow will also apply to all pods in namespace x
builder2 = builder2.SetName("acnp-allow").
SetPriority(5).
SetAppliedToGroup(nil, map[string]string{"ns": "x"}, nil, nil)
builder2.AddEgress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "z"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
// This rule should take no effect as it will be overridden by the first rule of cnp-drop
builder2.AddEgress(v1.ProtocolTCP, &p80, nil, nil, nil, nil, map[string]string{"ns": "y"},
nil, nil, nil, secv1alpha1.RuleActionAllow, "")
// Only egress from pods in namespace x to namespace y should be denied
reachabilityBothACNP := NewReachability(allPods, true)
reachabilityBothACNP.Expect(Pod("x/a"), Pod("y/a"), false)
reachabilityBothACNP.Expect(Pod("x/b"), Pod("y/a"), false)
reachabilityBothACNP.Expect(Pod("x/c"), Pod("y/a"), false)
reachabilityBothACNP.Expect(Pod("x/a"), Pod("y/b"), false)
reachabilityBothACNP.Expect(Pod("x/b"), Pod("y/b"), false)
reachabilityBothACNP.Expect(Pod("x/c"), Pod("y/b"), false)
reachabilityBothACNP.Expect(Pod("x/a"), Pod("y/c"), false)
reachabilityBothACNP.Expect(Pod("x/b"), Pod("y/c"), false)
reachabilityBothACNP.Expect(Pod("x/c"), Pod("y/c"), false)
testStep := []*TestStep{
{
"Both ACNP",
reachabilityBothACNP,
[]metav1.Object{builder2.Get(), builder1.Get()},
[]int{80},
0,