/
test_policy.py
3480 lines (3361 loc) · 203 KB
/
test_policy.py
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
from common.k8s.base import BaseK8sTest
from k8s.network_policy import NetworkPolicyFixture
from tcutils.wrappers import preposttest_wrapper
from test import BaseTestCase
from k8s.namespace import NamespaceFixture
from k8s.pod import PodFixture
from tcutils.util import get_random_name, get_random_cidr
import time
import test
from tcutils.util import skip_because
class TestNetworkPolicy(BaseK8sTest):
@classmethod
def setUpClass(cls):
super(TestNetworkPolicy, cls).setUpClass()
try:
cls.ns1 = NamespaceFixture(connections=cls.connections, name="default")
#cls.ns1.setUp()
cls.ns2 = NamespaceFixture(connections=cls.connections, name="non-default")
cls.ns2.setUp()
cls.ns3 = NamespaceFixture(connections=cls.connections, name="temp-ns")
cls.ns3.setUp()
#cls.ns1.set_labels({'project': cls.ns1.name})
cls.ns1.set_labels({'site': cls.ns1.name})
cls.ns2.set_labels({'site': cls.ns2.name})
cls.ns3.set_labels({'new_site': cls.ns3.name})
web_label_ns1, web_label_ns2 = 'webns1', 'webns2'
client1_label_ns1, client1_label_ns2, client1_label_ns3 = 'client1_ns1', 'client1_ns2', 'client1_ns3'
client2_label_ns1, client2_label_ns2, client2_label_ns3 = 'client2_ns1', 'client2_ns2', 'client2_ns3'
client3_label_ns3 = 'client3_ns3'
nginx_spec_1 = {'containers': [{'image': 'nginx',
'ports': [{'container_port': 80}]}]}
nginx_spec_2 = {'containers': [{'image': 'nginx',
'ports': [{'container_port': 80}]}]}
nginx_metadata_ns1 = {'labels': {'app': web_label_ns1}}
nginx_metadata_ns2 = {'labels': {'app': web_label_ns2}}
cls.web_pod_ns1 = PodFixture(connections=cls.connections,
namespace=cls.ns1.name,
metadata=nginx_metadata_ns1,
spec=nginx_spec_1)
cls.web_pod_ns1.setUp()
cls.web_pod_ns2 = PodFixture(connections=cls.connections,
namespace=cls.ns2.name,
metadata=nginx_metadata_ns2,
spec=nginx_spec_2)
cls.web_pod_ns2.setUp()
busybox_spec_1 = {'containers': [{'image': 'busybox','command': ['sleep', '1000000'],
'image_pull_policy': 'IfNotPresent',}],
'restart_policy': 'Always'}
busybox_spec_2 = dict(busybox_spec_1)
busybox_spec_3 = dict(busybox_spec_1)
busybox_spec_4 = dict(busybox_spec_1)
busybox_spec_5 = dict(busybox_spec_1)
busybox_spec_6 = dict(busybox_spec_1)
busybox_spec_7 = dict(busybox_spec_1)
busybox_metadata_c1_ns1 = {'labels': {'app': client1_label_ns1}}
busybox_metadata_c1_ns2 = {'labels': {'app': client1_label_ns2}}
busybox_metadata_c1_ns3 = {'labels': {'app': client1_label_ns3}}
busybox_metadata_c2_ns1 = {'labels': {'app': client2_label_ns1}}
busybox_metadata_c2_ns2 = {'labels': {'app': client2_label_ns2}}
busybox_metadata_c2_ns3 = {'labels': {'app': client2_label_ns3}}
busybox_metadata_c3_ns3 = {'labels': {'app': client3_label_ns3}}
cls.client1_pod_ns1 = PodFixture(connections=cls.connections,
namespace=cls.ns1.name,
metadata=busybox_metadata_c1_ns1,
spec=busybox_spec_1)
cls.client1_pod_ns1.setUp()
cls.client2_pod_ns1 = PodFixture(connections=cls.connections,
namespace=cls.ns1.name,
metadata=busybox_metadata_c2_ns1,
spec=busybox_spec_2)
cls.client2_pod_ns1.setUp()
cls.client1_pod_ns2 = PodFixture(connections=cls.connections,
namespace=cls.ns2.name,
metadata=busybox_metadata_c1_ns2,
spec=busybox_spec_3)
cls.client1_pod_ns2.setUp()
cls.client2_pod_ns2 = PodFixture(connections=cls.connections,
namespace=cls.ns2.name,
metadata=busybox_metadata_c2_ns2,
spec=busybox_spec_4)
cls.client2_pod_ns2.setUp()
cls.client1_pod_ns3 = PodFixture(connections=cls.connections,
namespace=cls.ns3.name,
metadata=busybox_metadata_c1_ns3,
spec=busybox_spec_5)
cls.client1_pod_ns3.setUp()
cls.client2_pod_ns3 = PodFixture(connections=cls.connections,
namespace=cls.ns3.name,
metadata=busybox_metadata_c2_ns3,
spec=busybox_spec_6)
cls.client2_pod_ns3.setUp()
cls.client3_pod_ns3 = PodFixture(connections=cls.connections,
namespace=cls.ns3.name,
metadata=busybox_metadata_c3_ns3,
spec=busybox_spec_7)
cls.client3_pod_ns3.setUp()
assert cls.ns1.verify_on_setup()
assert cls.ns2.verify_on_setup()
assert cls.ns3.verify_on_setup()
assert cls.web_pod_ns1.verify_on_setup()
assert cls.web_pod_ns2.verify_on_setup()
assert cls.client1_pod_ns1.verify_on_setup()
assert cls.client1_pod_ns2.verify_on_setup()
assert cls.client1_pod_ns3.verify_on_setup()
assert cls.client2_pod_ns1.verify_on_setup()
assert cls.client2_pod_ns2.verify_on_setup()
assert cls.client2_pod_ns3.verify_on_setup()
assert cls.client3_pod_ns3.verify_on_setup()
except:
cls.tearDownClass()
raise
@classmethod
def tearDownClass(cls):
if getattr(cls, 'web_pod_ns1', None):
cls.web_pod_ns1.cleanUp()
if getattr(cls, 'web_pod_ns2', None):
cls.web_pod_ns2.cleanUp()
if getattr(cls, 'client1_pod_ns1', None):
cls.client1_pod_ns1.cleanUp()
if getattr(cls, 'client2_pod_ns1', None):
cls.client2_pod_ns1.cleanUp()
if getattr(cls, 'client1_pod_ns2', None):
cls.client1_pod_ns2.cleanUp()
if getattr(cls, 'client2_pod_ns2', None):
cls.client2_pod_ns2.cleanUp()
if getattr(cls, 'client1_pod_ns3', None):
cls.client1_pod_ns3.cleanUp()
if getattr(cls, 'client2_pod_ns3', None):
cls.client2_pod_ns3.cleanUp()
if getattr(cls, 'client3_pod_ns3', None):
cls.client3_pod_ns3.cleanUp()
if getattr(cls, 'ns2', None):
cls.ns2.cleanUp()
if getattr(cls, 'ns3', None):
cls.ns3.cleanUp()
super(TestNetworkPolicy, cls).tearDownClass()
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_allow_all_ingress(self):
"""
Verify a network policy with Ingress rule as allow all.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace
3. Veify that all pods are still reachable from all namespaces
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
self.setup_update_simple_policy(name="allow-all-ingress",
namespace= self.ns1.name,
ingress_all =True)
# All traffic should still work as it is ingress allow all policy
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
#end test_allow_all_ingress
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_deny_all_ingress(self):
"""
Verify a network policy with Ingress rule as deny all.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace.
3. Verify that ingress is not allowed to pods of namespace "default"
4. Verify that egress is allowed from pods of namespace "default"
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
policy_types = ["Ingress"]
self.setup_update_simple_policy(name="deny-all-ingress",
namespace= self.ns1.name,
policy_types = policy_types)
#All ingress traffic to all pods of namespace "default" should be dropped.
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip,
expectation=False)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
assert self.client2_pod_ns2.ping_with_certainty(self.client1_pod_ns2.pod_ip)
#end test_deny_all_ingress
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_podselector_for_pod(self):
"""
Verify a network policy for a Pod with Ingress Podselector rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular pod for a particular pod.
3. Verify that ingress to the pod is allowed from configured PodSelector only.
4. Verify that ingress from all other pods is dropped.
5. Verify that other pods of same namespace are not affected by the policy
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
self.setup_update_simple_policy(name="ingress-pod-to-pod",
pod_selector = self.web_pod_ns1.labels,
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_pods= self.client1_pod_ns1.labels)
# All ingress traffic to pod cls.web_pod_ns1 will be dropped except from pod self.client1_pod_ns1.labels
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
#end test_ingress_podselector_for_pod
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_namespaceselector_for_pod(self):
"""
Verify a network policy for a Pod with Ingress NamespaceSelector rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular namespace for a particular pod.
3. Verify that ingress to the pod is allowed from pods of configured Namespace only.
4. Verify that ingress from all other pods is dropped.
5. Verify that other pods of same namespace are not affected by the policy
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
self.setup_update_simple_policy(name="ingress-ns-to-pod",
pod_selector = self.web_pod_ns1.labels,
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_namespaces= self.ns2.labels)
# All ingress traffic to pod cls.web_pod_ns1 will be allowed only from all pods of namespace "non-default"
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client2_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
# Ingress traffic from Pods of same namespace "default" should not be allowed.
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
# Ingress to other pods of namespace "default" should not be affected.
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
#end test_ingress_namespaceselector_for_pod
@preposttest_wrapper
def test_ingress_ipblock_for_pod(self):
"""
Verify a network policy for a Pod with Ingress IpBlock rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular IpBlock CIDR for a particular pod.
Also mention to deny traffic from another cidr towards a particular pod.
3. Verify that ingress to the pod is allowed from pods lying in the configured cidr
4. Verify that ingress to other pods of "default" namespac eis not affacted.
5. Verify that ingress from Pod mentioned in "except" block of policy is not allowed.
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
allowed_ip = self.client1_pod_ns2.pod_ip.split(".")[:3]
allowed_ip.append("0/24")
ingress_allow_cidr = ".".join(allowed_ip)
deny_cidr = self.client1_pod_ns1.pod_ip + "/32"
self.setup_update_simple_policy(name="ingress-ipblock-to-pod",
pod_selector = self.web_pod_ns1.labels,
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_ipblock= {"cidr" : ingress_allow_cidr,
"_except" : [deny_cidr]})
# INgress traffic should be allowed from pods lying in "ingress_allow_cidr" but not from host ip of self.client1_pod_ns1
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
if self.client2_pod_ns1.pod_ip.split(".")[:3] == self.web_pod_ns1.pod_ip.split(".")[:3]:
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
# Ingress to other pods of "default" namespace is not affected
assert self.client2_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
# INgress from cidr configured in "except" block of the policy towards self.web_pod_ns1 is not allowed
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
#end test_ingress_ipblock_for_pod
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_port_for_pod(self):
"""
Verify a network policy for a Pod with Ingress Port/Protocol rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular port/protocol for a particular pod.
3. Verify that ingress to the pod of default namespace is allowed from the
port mentioned in the policy
4. Verify that ingress from any other port to pods of "default" namespace is not allowed.
5. Verify that egress from default namespace is not affected.
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
self.setup_update_simple_policy(name="ingress-port-to-pod",
pod_selector = self.web_pod_ns1.labels,
policy_types = ["Ingress"],
namespace= self.ns1.name,
ports=['TCP/80'],
ingress_all =True)
# Ingress TCP traffic should be allowed and ICMP traffic should drop
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
# Egress from Pods of "default" namespace should not be affected
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
assert self.validate_wget(self.client2_pod_ns1, url2)
#end test_ingress_port_for_pod
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_podselector_for_namespace(self):
"""
Verify a network policy for a Namespace with Ingress Podselector rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular pod of "default" namespace.
3. Verify that ingress to the namespace "default" is allowed from configured
PodSelector only.
4. Verify that ingress from all other pods in namespace "default" is dropped.
5. Verify that egress from namespace "default" to other namespace is not affected
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
self.setup_update_simple_policy(name="ingress-pod-to-ns",
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_pods= self.client1_pod_ns1.labels)
# Traffic should only be allowed from self.client1_pod_ns1 inside namespace default
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
# Traffic from any other Pod inside defaut namespace should not be allowed
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client2_pod_ns1, url, expectation=False)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client1_pod_ns2, url, expectation=False)
# Traffic from pods of "default" namespace should be able to reach pods outside the namespace
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
assert self.client2_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip)
#end test_ingress_podselector_for_namespace
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_podselector_for_namespace_negative(self):
"""
Verify a network policy for a Namespace with Ingress Podselector rule where the
Pod lies in different namespace. It is a negative test.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular pod of "non-default" namespace.
3. Verify that no ingress is allowed in "default" namespace as it will search for
the pod in its own namespace("default") and it will not find it.
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
self.setup_update_simple_policy(name="ingress-pod-to-ns",
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_pods= self.client1_pod_ns2.labels)
# Being a negative case where PodSelector "ingress_pods" is from different namespace, this policy is similar as deny all policy on namespace "default"
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client1_pod_ns2, url,
expectation=False)
# Traffic from any other Pod inside defaut namespace should not be allowed
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client2_pod_ns1, url, expectation=False)
# Traffic from any other Pod from "non-default" namespace should not be allowed
assert self.client2_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client2_pod_ns2, url, expectation=False)
#end test_ingress_podselector_for_namespace_negative
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_namespaceselector_for_namespace(self):
"""
Verify a network policy for a Namespace with Ingress NamespaceSelector rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular namespace.
3. Verify that ingress to the pods of "default" namespace is allowed from pods of configured
NamespaceSelector only.
4. Verify that ingress to "default" namespace from pods of same namespace is not allowed.
5. Verify that egress from "default" namespace to other namespace is not affected.
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
self.setup_update_simple_policy(name="ingress-ns-to-ns",
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_namespaces= self.ns2.labels)
# Traffic from "non-default" namespace to "default" namespace should work
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
# Traffic from any other Pod inside default namespace should not be allowed
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client1_pod_ns1, url, expectation=False)
# Traffic from "default" namespace to "non-default" should not be affected
assert self.client2_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip)
#end test_ingress_namespaceselector_for_namespace
@preposttest_wrapper
def test_ingress_ipblock_for_namespace(self):
"""
Verify a network policy for a Namespace with Ingress IpBlock rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow ingress
from a particular IpBlock CIDR.
Also mention to deny traffic from another cidr towards namespace "default".
3. Verify that ingress to all pods of namespace "default" is allowed from pods
lying in the configured cidr
4. Verify that ingress to namespace "default" from Pod mentioned in "except"
block of policy is not allowed.
5. Verify that egress from namespace "default" is not affected.
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
allowed_ip = self.client1_pod_ns2.pod_ip.split(".")[:3]
allowed_ip.append("0/24")
ingress_allow_cidr = ".".join(allowed_ip)
deny_cidr = self.client2_pod_ns2.pod_ip + "/32"
self.setup_update_simple_policy(name="ingress-ipblock-to-ns",
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_ipblock= {"cidr" : ingress_allow_cidr,
"_except" : [deny_cidr]})
# Ingress traffic should be allowed from pods lying in "ingress_allow_cidr"
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
# Ingress traffic should not be allowed from host ip of self.client2_pod_ns2
assert self.validate_wget(self.client2_pod_ns2, url, expectation=False)
assert self.client2_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip,
expectation=False)
# Egress traffic from "default" to "non-default" domain should not be affacetd
assert self.validate_wget(self.client1_pod_ns1, url2)
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
#end test_ingress_ipblock_for_namespace
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_ingress_port_for_namespace(self):
"""
Verify a network policy for a Namespace with Ingress Port/Protocol rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "non-default" namespace to allow ingress
from a particular port/protocol.
3. Verify that ingress to all pods of "non-default" namespace is allowed from the
port mentioned in the policy
4. Verify that ingress from any other port to pods of "non-default" namespace is not allowed.
5. Verify that egress from "non-default" namespace is not affected.
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.validate_wget(self.client1_pod_ns1, url2)
self.setup_update_simple_policy(name="ingress-port-to-ns",
policy_types = ["Ingress"],
namespace= self.ns2.name,
ports=['TCP/80'],
ingress_all =True)
# Ingress TCP traffic should be allowed in namespace "non default"
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client1_pod_ns1, url2)
# ingress ICMP traffic should drop in namespace "non-default"
assert self.client1_pod_ns2.ping_with_certainty(self.client2_pod_ns2.pod_ip,
expectation=False)
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip,
expectation=False)
# Egress ICMP trafic should be allowed from namespace "non-default"
assert self.client1_pod_ns2.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
#end test_ingress_port_for_namespace
@preposttest_wrapper
def test_policy_with_multiple_ingress_rules(self):
"""
Verify a network policy with multiple ingress rules specified.
Steps:
1. Create a network policy with ingress "from" rule mentioning "podSelector",
"NamespaceSelector" and "port"
2. Verify the accessibility between pods as per the rule
3. Delete the policy and create another policy with ingress "from" rule
mentioning "podSelector", "IpBlock" and "port"
4. Verify the accessibility between pods as per the rule
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.validate_wget(self.client1_pod_ns1, url2)
# Enable all traffic to ns1
policy_types = ["Ingress"]
ingress_list = [
{'from': [
{'pod_selector': self.client1_pod_ns2.labels},
{'namespace_selector': self.ns1.labels}
],
'ports': [ 'TCP/80', 'UDP/53' ]
},
]
policy1 = self.setup_update_policy(pod_selector = self.web_pod_ns2.labels,
name="many-selector-ingress",
namespace = self.ns2.name,
policy_types = policy_types,
ingress= ingress_list)
# Ingress traffic to self.web_pod_ns2 should only be allowed from self.client1_pod_ns2 within namespace "non-defaut"
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client2_pod_ns2, url2, expectation = False)
# Ingress traffic to self.web_pod_ns2 should be allowed from pods of namespace "default"
assert self.validate_wget(self.client2_pod_ns1, url2)
# INgress traffic from any other protocol than TCP/UDP should be dropped
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
self.perform_cleanup(policy1)
allowed_ip = self.client2_pod_ns1.pod_ip.split(".")[:3]
allowed_ip.append("0/24")
ingress_allow_cidr = ".".join(allowed_ip)
ingress_list = [
{'from': [
{'pod_selector': self.client1_pod_ns2.labels},
{'ip_block': { "cidr" : ingress_allow_cidr,
"_except" : [self.client1_pod_ns1.pod_ip + "/32"] }}
],
'ports': [ 'TCP/80', 'UDP/53' ]
},
]
policy2 = self.setup_update_policy(pod_selector = self.web_pod_ns2.labels,
name="many-selector-ingress",
namespace = self.ns2.name,
policy_types = policy_types,
ingress= ingress_list)
# INgess traffic to self.web_pod_ns2 should be allowed from self.client2_pod_ns1 as per cidr rule
assert self.validate_wget(self.client2_pod_ns1, url2)
# INgess traffic to self.web_pod_ns2 should not be allowed from self.client1_pod_ns1 as per except rule
assert self.validate_wget(self.client1_pod_ns1, url2, expectation = False)
#INgress traffic from pod self.client1_pod_ns2 should be allowed as per podSelector rule
assert self.validate_wget(self.client1_pod_ns2, url2)
#INgress traffic from any other pod of non-default namespace should not be allowed until it lies in cidr range
if self.client2_pod_ns2.pod_ip.split(".")[:3] == ingress_allow_cidr.split(".")[:3]:
assert self.validate_wget(self.client2_pod_ns2, url2)
else:
assert self.validate_wget(self.client2_pod_ns2, url2, expectation=False)
# INgress traffic from any other protocol than TCP/UDP should be dropped
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
# Ingress Traffic for any other pod of namespace "non-default" should not be affected
assert self.client1_pod_ns2.ping_with_certainty(self.client2_pod_ns2.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip)
self.perform_cleanup(policy2)
#end test_policy_with_multiple_ingress_rules
@preposttest_wrapper
def test_ingress_rules_edit(self):
"""
Verify that Contrail FW policies are updated on editing the k8s netwok poliy
Steps:
1. Create a netwok policy on one of the podSelector of namespace "non-default" with
ingress from "pdSelector", "namespaceSelector" and ports.
2. Verify that policy works as expected
3. Update the netwok policy on same podSelector of namespace "non-default" with
updated "podSelector", removing "namespaceSelector", ipblock and updated ports.
4. Verify that policy works as expected
5. Again update the network policy with updated PodSelctor which corresponds to
a different Pod of namespace "non-default"
6. Verify that policy works as expected
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.validate_wget(self.client1_pod_ns1, url2)
# Enable all traffic to ns1
policy_types = ["Ingress"]
ingress_list = [
{'from': [
{'pod_selector': self.client1_pod_ns2.labels},
{'namespace_selector': self.ns1.labels}
],
'ports': [ 'TCP/80', 'UDP/53' ]
},
]
policy1 = self.setup_update_policy(pod_selector = self.web_pod_ns2.labels,
name="ingress-rule-edit",
namespace = self.ns2.name,
policy_types = policy_types,
ingress= ingress_list)
# Ingress traffic to self.web_pod_ns2 should only be allowed from self.client1_pod_ns2 within namespace "non-defaut"
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client2_pod_ns2, url2, expectation = False)
# Ingress traffic to self.web_pod_ns2 should be allowed from pods of namespace "default"
assert self.validate_wget(self.client2_pod_ns1, url2)
# INgress traffic from any other protocol than TCP/UDP should be dropped
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
#Updating the same network policy
allowed_ip = self.client2_pod_ns1.pod_ip.split(".")[:3]
allowed_ip.append("0/24")
ingress_allow_cidr = ".".join(allowed_ip)
ingress_list = [
{'from': [
{'pod_selector': self.client2_pod_ns2.labels},
{'ip_block': { "cidr" : ingress_allow_cidr,
"_except" : [self.client1_pod_ns1.pod_ip + "/32"] }}
],
'ports': [ 'TCP/80' ]
},
]
self.setup_update_policy(update = True,
np_fixture = policy1,
ingress= ingress_list)
# INgess traffic to self.web_pod_ns2 should be allowed from self.client2_pod_ns1 as per cidr rule
assert self.validate_wget(self.client2_pod_ns1, url2)
# INgess traffic to self.web_pod_ns2 should not be allowed from self.client1_pod_ns1 as per except rule
assert self.validate_wget(self.client1_pod_ns1, url2, expectation = False)
#INgress traffic from pod self.client1_pod_ns2 should be allowed as per podSelector rule
assert self.validate_wget(self.client2_pod_ns2, url2)
#INgress traffic from any other pod of non-default namespace should not be allowed until it lies in cidr range
if self.client1_pod_ns2.pod_ip.split(".")[:3] == ingress_allow_cidr.split(".")[:3]:
assert self.validate_wget(self.client1_pod_ns2, url2)
else:
assert self.validate_wget(self.client1_pod_ns2, url2, expectation=False)
# INgress traffic from any other protocol than TCP/UDP should be dropped
assert self.client2_pod_ns2.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns2.pod_ip,
expectation=False)
# Ingress Traffic for any other pod of namespace "non-default" should not be affected
assert self.client2_pod_ns2.ping_with_certainty(self.client1_pod_ns2.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
#Updating the policy again:
ingress_list = [
{'from': [
{'namespace_selector': self.ns1.labels}
],
},
]
self.setup_update_policy(pod_selector = self.client1_pod_ns2.labels,
update = True,
np_fixture = policy1,
ingress= ingress_list)
assert self.client2_pod_ns2.ping_with_certainty(self.client1_pod_ns2.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
assert self.validate_wget(self.client2_pod_ns2, url2)
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client1_pod_ns2, url)
#end test_ingress_rules_edit
@preposttest_wrapper
def test_multiple_ingress_policies(self):
"""
Verify that Contrail FW policies are effective when multiple ingress policies coexist
Steps:
1. Create few policies for namespace "default"
2. Create few policies for namespace "non-default"
3. Verify that FW rules created are behaving as expected
4. Start deleting the policies 1 by 1 and verify the reachability
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.validate_wget(self.client1_pod_ns1, url2)
# Enable all traffic to ns1
policy1 = self.setup_update_simple_policy(name="ingress-pod-to-pod",
pod_selector = self.web_pod_ns1.labels,
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_pods= self.client1_pod_ns1.labels)
policy2 = self.setup_update_simple_policy(name="ingress-ns-to-pod",
pod_selector = self.web_pod_ns1.labels,
policy_types = ["Ingress"],
namespace= self.ns1.name,
ingress_namespaces= self.ns2.labels)
ingress_list = [
{'from': [
{'pod_selector': self.client2_pod_ns1.labels},
],
'ports': ["TCP/80"]
},
]
policy3 = self.setup_update_policy(pod_selector = self.web_pod_ns1.labels,
name="ingress-port-to-pod",
namespace = self.ns1.name,
policy_types = ["Ingress"],
ingress= ingress_list)
policy4 = self.setup_update_simple_policy(name="ingress-pod-to-ns",
policy_types = ["Ingress"],
namespace= self.ns2.name,
ingress_pods= self.client1_pod_ns2.labels)
allowed_ip = self.client1_pod_ns1.pod_ip.split(".")[:3]
allowed_ip.append("0/24")
ingress_allow_cidr = ".".join(allowed_ip)
deny_cidr = self.client2_pod_ns1.pod_ip + "/32"
policy5 = self.setup_update_simple_policy(name="ingress-ipblock-to-ns",
policy_types = ["Ingress"],
namespace= self.ns2.name,
ingress_ipblock= {"cidr" : ingress_allow_cidr,
"_except" : [deny_cidr]})
# Verifying ingress behavior on namspace "default" as per 1st 3 policies
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client2_pod_ns1, url)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.validate_wget(self.client2_pod_ns2, url)
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
# Verifying ingress behavior on namspace "non-default" as per policy 4 and 5
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client1_pod_ns1, url2)
assert self.validate_wget(self.client2_pod_ns1, url2, expectation=False)
assert self.client1_pod_ns2.ping_with_certainty(self.client2_pod_ns2.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip)
assert self.client2_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip,
expectation = False)
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip,
expectation = False)
self.perform_cleanup(policy1)
assert self.validate_wget(self.client1_pod_ns1, url, expectation=False)
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client2_pod_ns1, url)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client1_pod_ns2, url)
self.perform_cleanup(policy2)
assert self.validate_wget(self.client2_pod_ns1, url)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.validate_wget(self.client1_pod_ns2, url, expectation=False)
self.perform_cleanup(policy3)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.validate_wget(self.client2_pod_ns1, url)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
self.perform_cleanup(policy4)
self.perform_cleanup(policy5)
assert self.validate_wget(self.client2_pod_ns1, url2)
assert self.client2_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip)
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
#end test_multiple_ingress_policies
@test.attr(type=['k8s_sanity'])
@preposttest_wrapper
def test_ingress_rules_label_edit(self):
"""
Verify that pod reachability takes effect correctly on editing the label of
pods and namespace specified in network policy.
Steps:
1. Change the label of the pod mentioned in podSelector rule and verify reachability
2. Change the label of the pod of namespace mentioned in namespaceSelector
and verify the reachability.
3. Change the label of namespace mentioned in namespaceSelector and verify reachability.
4. Edit the network policy to comply with the podSelector and namespaceSelector
label and verify the reachability is as expected.
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
url2 = 'http://%s' % (self.web_pod_ns2.pod_ip)
assert self.validate_wget(self.client1_pod_ns2, url)
assert self.validate_wget(self.client1_pod_ns1, url2)
# Enable all traffic to ns1
policy_types = ["Ingress"]
ingress_list = [
{'from': [
{'pod_selector': self.client1_pod_ns2.labels},
{'namespace_selector': self.ns1.labels}
],
},
]
policy1 = self.setup_update_policy(pod_selector = self.web_pod_ns2.labels,
name="ingress-policy-test",
namespace = self.ns2.name,
policy_types = policy_types,
ingress= ingress_list)
# Ingress behavior as per the above policy should be as follows:
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client2_pod_ns2, url2, expectation = False)
assert self.validate_wget(self.client2_pod_ns1, url2)
# Change the label of the pod mentione in podSelector rule and verify that it cant reach self.web_pod_ns2 anymore
self.client1_pod_ns2.set_labels({'app': 'client2_ns2'})
self.addCleanup(self.client1_pod_ns2.set_labels, {'app': 'client1_ns2'})
assert self.validate_wget(self.client1_pod_ns2, url2, expectation = False)
assert self.validate_wget(self.client2_pod_ns1, url2)
# Change the label of a pods which is part of namespace mentioned in namespaceSelector. Verify that it does not matter.
self.client2_pod_ns1.set_labels({'app': 'test'})
self.addCleanup(self.client2_pod_ns1.set_labels, {'app': 'client2_ns1'})
assert self.validate_wget(self.client2_pod_ns1, url2)
# Change the label of namespace mentioned in namespaceSelector. Verify that pods of that namespace are not reachable anymore
self.ns1.set_labels({'site': 'test'})
self.addCleanup(self.ns1.set_labels, {'site': 'default'})
assert self.validate_wget(self.client2_pod_ns1, url2, expectation = False)
self.client2_pod_ns2.set_labels({'app': 'client1_ns2'})
self.addCleanup(self.client2_pod_ns2.set_labels, {'app': 'client2_ns2'})
assert self.validate_wget(self.client2_pod_ns1, url2, expectation = False)
assert self.validate_wget(self.client2_pod_ns2, url2)
# Update the policy corresponding to the labels changed and verify that all reachability is same as before
ingress_list = [
{'from': [
{'pod_selector': {'app': 'client2_ns2'}},
{'namespace_selector': {'site': 'test'}}
],
},
]
self.setup_update_policy(update = True,
np_fixture = policy1,
ingress= ingress_list)
assert self.validate_wget(self.client1_pod_ns2, url2)
assert self.validate_wget(self.client2_pod_ns2, url2, expectation = False)
assert self.validate_wget(self.client2_pod_ns1, url2)
#end test_ingress_rules_label_edit
@preposttest_wrapper
def test_allow_all_egress(self):
"""
Verify a network policy with Egress rule as allow all.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace
3. Veify that all pods are still reachable from all namespaces
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
policy_types = ["Egress"]
egress_list = [{}]
policy1 = self.setup_update_policy(name="allow-all-egress",
namespace = self.ns1.name,
policy_types = policy_types,
egress= egress_list)
# All traffic should still work as it is ingress allow all policy
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
#end test_allow_all_egress
def test_deny_all_egress(self):
"""
Verify a network policy with Egress rule as deny all.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace.
3. Verify that egress is not allowed from pods of namespace "default"
4. Verify that ingress is allowed to the pods of namespace "default"
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
policy_types = ["Egress"]
self.setup_update_simple_policy(name="deny-all-egress",
namespace= self.ns1.name,
policy_types = policy_types)
#All egress traffic from all pods of namespace "default" should be dropped.
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip,
expectation=False)
#All ingress traffic from pods of namespace "non-default" should be allowed.
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client2_pod_ns2.ping_with_certainty(self.client2_pod_ns1.pod_ip)
#end test_deny_all_egress
@preposttest_wrapper
def test_egress_podselector_for_pod(self):
"""
Verify a network policy for a Pod with Egress Podselector rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow egress
to a particular pod for a podSelector.
3. Verify that egress from the pod is allowed from configured PodSelector only.
4. Verify that egress to all other pods is dropped.
5. Verify that other pods of same namespace are not affected by the policy
"""
# All traffic between everyone should work
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns2.ping_with_certainty(self.web_pod_ns1.pod_ip)
self.setup_update_simple_policy(name="egress-pod-to-pod",
pod_selector = self.client2_pod_ns1.labels,
policy_types = ["Egress"],
namespace= self.ns1.name,
egress_pods= self.client1_pod_ns1.labels)
# All egress traffic from pod cls.client2_pod_ns1 will be dropped except to pod self.client1_pod_ns1.labels
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client2_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip,
expectation=False)
# Traffic of othe rpods should not be affected
assert self.client1_pod_ns1.ping_with_certainty(self.web_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip)
#Ingress to pod cls.client2_pod_ns1 should not be affected
assert self.client1_pod_ns2.ping_with_certainty(self.client2_pod_ns1.pod_ip)
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip)
#end test_egress_podselector_for_pod
@preposttest_wrapper
def test_egress_podselector_for_namespace(self):
"""
Verify a network policy for a Namespace with Egress Podselector rule.
Steps:
1. Verify that all pods are reachable from all namespaces
2. Create a Network policy on "default" namespace to allow egress
to a particular pod of "default" namespace.
3. Verify that egress from the namespace "default" is allowed to the configured
pod only.
4. Verify that egress to all other pods in namespace "default" is dropped.
5. Verify that egress from namespace "default" to other namespace is not affected
"""
# All traffic between everyone should work
url = 'http://%s' % (self.web_pod_ns1.pod_ip)
assert self.validate_wget(self.client1_pod_ns1, url)
assert self.validate_wget(self.client1_pod_ns2, url)
self.setup_update_simple_policy(name="egress-pod-to-ns",
policy_types = ["Egress"],
namespace= self.ns1.name,
egress_pods= self.client1_pod_ns1.labels)
# Traffic should only be allowed to self.client1_pod_ns1 inside namespace default
assert self.client2_pod_ns1.ping_with_certainty(self.client1_pod_ns1.pod_ip)
# Traffic to any other Pod inside defaut namespace should not be allowed
assert self.validate_wget(self.client2_pod_ns1, url, expectation=False)
assert self.validate_wget(self.client1_pod_ns1, url, expectation=False)
assert self.client1_pod_ns1.ping_with_certainty(self.client2_pod_ns1.pod_ip,
expectation=False)
# Traffic to pods of "non-default" namespace should not be allowed as well
assert self.client1_pod_ns1.ping_with_certainty(self.client1_pod_ns2.pod_ip,
expectation=False)
assert self.client2_pod_ns1.ping_with_certainty(self.client2_pod_ns2.pod_ip,
expectation=False)
# Ingress traffic to pods of namespace "default" should not be affected
assert self.client1_pod_ns2.ping_with_certainty(self.client1_pod_ns1.pod_ip)
assert self.client2_pod_ns2.ping_with_certainty(self.client2_pod_ns1.pod_ip)