forked from cilium/cilium
/
Policies.go
1051 lines (843 loc) · 39.7 KB
/
Policies.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 2017-2019 Authors of Cilium
//
// 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 k8sTest
import (
"context"
"fmt"
"time"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"github.com/sirupsen/logrus"
)
var _ = Describe("K8sPolicyTest", func() {
var (
kubectl *helpers.Kubectl
demoPath = helpers.ManifestGet("demo.yaml")
l3Policy = helpers.ManifestGet("l3-l4-policy.yaml")
l7Policy = helpers.ManifestGet("l7-policy.yaml")
l7PolicyKafka = helpers.ManifestGet("l7-policy-kafka.yaml")
serviceAccountPolicy = helpers.ManifestGet("service-account.yaml")
knpDenyIngress = helpers.ManifestGet("knp-default-deny-ingress.yaml")
knpDenyEgress = helpers.ManifestGet("knp-default-deny-egress.yaml")
knpDenyIngressEgress = helpers.ManifestGet("knp-default-deny-ingress-egress.yaml")
cnpDenyIngress = helpers.ManifestGet("cnp-default-deny-ingress.yaml")
cnpDenyEgress = helpers.ManifestGet("cnp-default-deny-egress.yaml")
knpAllowIngress = helpers.ManifestGet("knp-default-allow-ingress.yaml")
knpAllowEgress = helpers.ManifestGet("knp-default-allow-egress.yaml")
cnpMatchExpression = helpers.ManifestGet("cnp-matchexpressions.yaml")
app1Service = "app1-service"
microscopeErr error
microscopeCancel = func() error { return nil }
backgroundCancel context.CancelFunc = func() { return }
backgroundError error
apps = []string{helpers.App1, helpers.App2, helpers.App3}
)
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
DeployCiliumAndDNS(kubectl)
})
AfterEach(func() {
ExpectAllPodsTerminated(kubectl)
})
AfterFailed(func() {
kubectl.CiliumReport(helpers.KubeSystemNamespace,
"cilium service list",
"cilium endpoint list")
})
AfterAll(func() {
ExpectAllPodsTerminated(kubectl)
kubectl.CloseSSHClient()
})
JustBeforeEach(func() {
microscopeErr, microscopeCancel = kubectl.MicroscopeStart()
Expect(microscopeErr).To(BeNil(), "Microscope cannot be started")
backgroundCancel, backgroundError = kubectl.BackgroundReport("uptime")
Expect(backgroundError).To(BeNil(), "Cannot start background report process")
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
Expect(microscopeCancel()).To(BeNil(), "cannot stop microscope")
backgroundCancel()
})
Context("Basic Test", func() {
var (
ciliumPod string
clusterIP string
appPods map[string]string
namespaceForTest string
)
importPolicy := func(file, name string) {
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, file, helpers.KubectlApply, helpers.HelperTimeout)
ExpectWithOffset(1, err).Should(BeNil(),
"policy %s cannot be applied in %q namespace", file, namespaceForTest)
}
// getMatcher returns a helper.CMDSucess() matcher for success or
// failure situations.
getMatcher := func(val bool) types.GomegaMatcher {
if val {
return helpers.CMDSuccess()
}
return Not(helpers.CMDSuccess())
}
validateConnectivity := func(expectWorldSuccess, expectClusterSuccess bool) {
for _, pod := range []string{appPods[helpers.App2], appPods[helpers.App3]} {
By("HTTP connectivity to 1.1.1.1")
res := kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.CurlFail("http://1.1.1.1/"))
ExpectWithOffset(1, res).To(getMatcher(expectWorldSuccess),
"HTTP egress connectivity to 1.1.1.1 from pod %q", pod)
By("ICMP connectivity to 8.8.8.8")
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.Ping("8.8.8.8"))
ExpectWithOffset(1, res).To(getMatcher(expectWorldSuccess),
"ICMP egress connectivity to 8.8.8.8 from pod %q", pod)
By("DNS lookup of kubernetes.default.svc.cluster.local")
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
"host -v kubernetes.default.svc.cluster.local")
// kube-dns is always whitelisted so this should always work
ExpectWithOffset(1, res).To(getMatcher(expectWorldSuccess || expectClusterSuccess),
"DNS connectivity of kubernetes.default.svc.cluster.local from pod %q", pod)
By("HTTP connectivity from pod to pod")
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
ExpectWithOffset(1, res).To(getMatcher(expectClusterSuccess),
"HTTP connectivity to clusterIP %q of app1 from pod %q", clusterIP, appPods[helpers.App2])
}
}
BeforeAll(func() {
namespaceForTest = helpers.GenerateNamespaceForTest()
kubectl.NamespaceCreate(namespaceForTest).ExpectSuccess("could not create namespace")
kubectl.Apply(helpers.ApplyOptions{FilePath: demoPath, Namespace: namespaceForTest}).ExpectSuccess("could not create resource")
err := kubectl.WaitforPods(namespaceForTest, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Test pods are not ready after timeout")
ciliumPod, err = kubectl.GetCiliumPodOnNode(helpers.KubeSystemNamespace, helpers.K8s1)
Expect(err).Should(BeNil(), "cannot get CiliumPod")
clusterIP, _, err = kubectl.GetServiceHostPort(namespaceForTest, app1Service)
Expect(err).To(BeNil(), "Cannot get service in %q namespace", namespaceForTest)
appPods = helpers.GetAppPods(apps, namespaceForTest, kubectl, "id")
logger.WithFields(logrus.Fields{
"ciliumPod": ciliumPod,
"clusterIP": clusterIP}).Info("Initial data")
})
AfterAll(func() {
kubectl.NamespaceDelete(namespaceForTest)
kubectl.Delete(demoPath)
})
BeforeEach(func() {
status := kubectl.CiliumExec(
ciliumPod, fmt.Sprintf("cilium config %s=%s",
helpers.PolicyEnforcement, helpers.PolicyEnforcementDefault))
status.ExpectSuccess()
err := kubectl.CiliumEndpointWaitReady()
Expect(err).To(BeNil(), "Endpoints are not ready after timeout")
err = kubectl.WaitforPods(namespaceForTest, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).Should(BeNil())
})
AfterEach(func() {
cmd := fmt.Sprintf("%s delete --all cnp,netpol -n %s", helpers.KubectlCmd, namespaceForTest)
_ = kubectl.Exec(cmd)
})
It("checks all kind of Kubernetes policies", func() {
logger.Infof("PolicyRulesTest: cluster service ip '%s'", clusterIP)
By("Testing L3/L4 rules")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, l3Policy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil())
for _, appName := range []string{helpers.App1, helpers.App2, helpers.App3} {
err = kubectl.WaitForCEPIdentity(namespaceForTest, appPods[appName])
Expect(err).Should(BeNil())
}
trace := kubectl.CiliumExec(ciliumPod, fmt.Sprintf(
"cilium policy trace --src-k8s-pod %s:%s --dst-k8s-pod %s:%s --dport 80/TCP",
namespaceForTest, appPods[helpers.App2], namespaceForTest, appPods[helpers.App1]))
trace.ExpectSuccess(trace.CombineOutput().String())
trace.ExpectContains("Final verdict: ALLOWED", "Policy trace output mismatch")
trace = kubectl.CiliumExec(ciliumPod, fmt.Sprintf(
"cilium policy trace --src-k8s-pod %s:%s --dst-k8s-pod %s:%s",
namespaceForTest, appPods[helpers.App3], namespaceForTest, appPods[helpers.App1]))
trace.ExpectSuccess(trace.CombineOutput().String())
trace.ExpectContains("Final verdict: DENIED", "Policy trace output mismatch")
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectSuccess("%q cannot curl clusterIP %q", appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("%q can curl to %q", appPods[helpers.App3], clusterIP)
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, l3Policy,
helpers.KubectlDelete, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot delete L3 Policy")
By("Testing L7 Policy")
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, l7Policy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot install %q policy", l7Policy)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail("http://%s/public", clusterIP))
res.ExpectSuccess("Cannot connect from %q to 'http://%s/public'",
appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/private", clusterIP)))
res.ExpectFail("Unexpected connection from %q to 'http://%s/private'",
appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("Unexpected connection from %q to 'http://%s/public'",
appPods[helpers.App3], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail("http://%s/private", clusterIP))
res.ExpectFail("Unexpected connection from %q to 'http://%s/private'",
appPods[helpers.App3], clusterIP)
}, 500)
It("Invalid Policy report status correctly", func() {
manifest := helpers.ManifestGet("invalid_cnp.yaml")
cnpName := "foo"
kubectl.Apply(helpers.ApplyOptions{FilePath: manifest, Namespace: namespaceForTest}).ExpectSuccess("Cannot apply policy manifest")
body := func() bool {
cnp := kubectl.GetCNP(namespaceForTest, cnpName)
if cnp != nil && len(cnp.Status.Nodes) > 0 {
for _, node := range cnp.Status.Nodes {
if node.Error == "" {
return false
}
}
return true
}
return false
}
err := helpers.WithTimeout(
body,
fmt.Sprintf("CNP %q does not report the status correctly after timeout", cnpName),
&helpers.TimeoutConfig{Timeout: 100 * time.Second})
Expect(err).To(BeNil(), "CNP status for invalid policy did not update correctly")
})
It("ServiceAccount Based Enforcement", func() {
// Load policy allowing serviceAccount of app2 to talk
// to app1 on port 80 TCP
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, serviceAccountPolicy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil())
for _, appName := range []string{helpers.App1, helpers.App2, helpers.App3} {
err = kubectl.WaitForCEPIdentity(namespaceForTest, appPods[appName])
Expect(err).Should(BeNil())
}
trace := kubectl.CiliumExec(ciliumPod, fmt.Sprintf(
"cilium policy trace --src-k8s-pod %s:%s --dst-k8s-pod %s:%s --dport 80/TCP",
namespaceForTest, appPods[helpers.App2], namespaceForTest, appPods[helpers.App1]))
trace.ExpectSuccess(trace.CombineOutput().String())
trace.ExpectContains("Final verdict: ALLOWED", "Policy trace output mismatch")
trace = kubectl.CiliumExec(ciliumPod, fmt.Sprintf(
"cilium policy trace --src-k8s-pod %s:%s --dst-k8s-pod %s:%s",
namespaceForTest, appPods[helpers.App3], namespaceForTest, appPods[helpers.App1]))
trace.ExpectSuccess(trace.CombineOutput().String())
trace.ExpectContains("Final verdict: DENIED", "Policy trace output mismatch")
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectSuccess("%q cannot curl clusterIP %q", appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("%q can curl to %q", appPods[helpers.App3], clusterIP)
}, 500)
It("CNP test MatchExpressions key", func() {
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, cnpMatchExpression, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "cannot install policy %s", cnpMatchExpression)
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectSuccess("%q cannot curl clusterIP %q", appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("%q can curl to %q", appPods[helpers.App3], clusterIP)
})
It("Denies traffic with k8s default-deny ingress policy", func() {
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectSuccess("%q cannot curl clusterIP %q", appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectSuccess("%q cannot curl to %q", appPods[helpers.App3], clusterIP)
By("Installing knp ingress default-deny")
// Import the policy and wait for all required endpoints to enforce the policy
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, knpDenyIngress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 deny-ingress Policy cannot be applied in %q namespace", namespaceForTest)
By("Testing connectivity with ingress default-deny policy loaded")
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("Ingress connectivity should be denied by policy")
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("Ingress connectivity should be denied by policy")
})
It("Denies traffic with k8s default-deny egress policy", func() {
By("Installing knp egress default-deny")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, knpDenyEgress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 deny-egress Policy cannot be applied in %q namespace", namespaceForTest)
By("Testing if egress policy enforcement is enabled on the endpoint")
for _, pod := range []string{appPods[helpers.App2], appPods[helpers.App3]} {
res := kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.CurlFail("http://1.1.1.1/"))
res.ExpectFail("Egress connectivity should be denied for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.Ping("8.8.8.8"))
res.ExpectFail("Egress ping connectivity should be denied for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
"host kubernetes.default.svc.cluster.local")
res.ExpectFail("Egress DNS connectivity should be denied for pod %q", pod)
}
})
It("Denies traffic with k8s default-deny ingress-egress policy", func() {
By("Installing knp ingress-egress default-deny")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, knpDenyIngressEgress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 deny-ingress-egress policy cannot be applied in %q namespace", namespaceForTest)
By("Testing if egress and ingress policy enforcement is enabled on the endpoint")
for _, pod := range []string{appPods[helpers.App2], appPods[helpers.App3]} {
res := kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.CurlFail("http://1.1.1.1/"))
res.ExpectFail("Egress connectivity should be denied for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.Ping("8.8.8.8"))
res.ExpectFail("Egress ping connectivity should be denied for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
"host kubernetes.default.svc.cluster.local")
res.ExpectFail("Egress DNS connectivity should be denied for pod %q", pod)
}
By("Testing ingress connectivity with default-deny policy loaded")
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("Ingress connectivity should be denied by policy")
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("Ingress connectivity should be denied by policy")
})
It("Denies traffic with cnp default-deny ingress policy", func() {
By("Installing cnp ingress default-deny")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, cnpDenyIngress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 deny-ingress Policy cannot be applied in %q namespace", namespaceForTest)
By("Testing connectivity with ingress default-deny policy loaded")
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("Ingress connectivity should be denied by policy")
By("Testing egress connnectivity works correctly")
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.Ping("8.8.8.8"))
res.ExpectSuccess("Egress ping connectivity should work")
})
It("Denies traffic with cnp default-deny egress policy", func() {
By("Installing cnp egress default-deny")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, cnpDenyEgress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 deny-egress Policy cannot be applied in %q namespace", namespaceForTest)
for _, pod := range apps {
res := kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.CurlFail("http://1.1.1.1/"))
res.ExpectFail("Egress connectivity should be denied for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.Ping("8.8.8.8"))
res.ExpectFail("Egress ping connectivity should be denied for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
"host kubernetes.default.svc.cluster.local")
res.ExpectFail("Egress DNS connectivity should be denied for pod %q", pod)
}
})
It("Allows traffic with k8s default-allow ingress policy", func() {
By("Installing ingress default-allow")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, knpAllowIngress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 allow-ingress Policy cannot be applied in %q namespace", namespaceForTest)
By("Testing connectivity with ingress default-allow policy loaded")
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail("http://%s/public", clusterIP))
res.ExpectSuccess("Ingress connectivity should be allowed by policy")
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail("http://%s/public", clusterIP))
res.ExpectSuccess("Ingress connectivity should be allowed by policy")
})
It("Allows traffic with k8s default-allow egress policy", func() {
By("Installing egress default-allow")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, knpAllowEgress, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"L3 allow-egress Policy cannot be applied in %q namespace", namespaceForTest)
By("Checking connectivity between pods and external services after installing egress policy")
for _, pod := range []string{appPods[helpers.App2], appPods[helpers.App3]} {
res := kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.CurlFail("http://1.1.1.1/"))
res.ExpectSuccess("Egress connectivity should be allowed for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
helpers.Ping("8.8.8.8"))
res.ExpectSuccess("Egress ping connectivity should be allowed for pod %q", pod)
res = kubectl.ExecPodCmd(
namespaceForTest, pod,
"host kubernetes.default.svc.cluster.local")
res.ExpectSuccess("Egress DNS connectivity should be allowed for pod %q", pod)
}
})
Context("Validate to-entities policies", func() {
const (
WorldConnectivityDeny = false
WorldConnectivityAllow = true
ClusterConnectivityDeny = false
ClusterConnectivityAllow = true
)
var (
cnpToEntitiesAll = helpers.ManifestGet("cnp-to-entities-all.yaml")
cnpToEntitiesWorld = helpers.ManifestGet("cnp-to-entities-world.yaml")
cnpToEntitiesCluster = helpers.ManifestGet("cnp-to-entities-cluster.yaml")
cnpToEntitiesHost = helpers.ManifestGet("cnp-to-entities-host.yaml")
)
It("Validate toEntities All", func() {
By("Installing toEntities All")
importPolicy(cnpToEntitiesAll, "to-entities-all")
By("Verifying policy correctness")
validateConnectivity(WorldConnectivityAllow, ClusterConnectivityAllow)
})
It("Validate toEntities World", func() {
By("Installing toEntities World")
importPolicy(cnpToEntitiesWorld, "to-entities-world")
By("Verifying policy correctness")
validateConnectivity(WorldConnectivityAllow, ClusterConnectivityDeny)
})
It("Validate toEntities Cluster", func() {
By("Installing toEntities Cluster")
importPolicy(cnpToEntitiesCluster, "to-entities-cluster")
By("Verifying policy correctness")
validateConnectivity(WorldConnectivityDeny, ClusterConnectivityAllow)
})
It("Validate toEntities Host", func() {
By("Installing toEntities Host")
importPolicy(cnpToEntitiesHost, "to-entities-host")
By("Verifying policy correctness")
validateConnectivity(WorldConnectivityDeny, ClusterConnectivityDeny)
})
})
Context("Validate CNP update", func() {
const (
allowAll = true
denyFromApp3 = false
)
var (
cnpUpdateAllow = helpers.ManifestGet("cnp-update-allow-all.yaml")
cnpUpdateDeny = helpers.ManifestGet("cnp-update-deny-ingress.yaml")
cnpUpdateNoSpecs = helpers.ManifestGet("cnp-update-no-specs.yaml")
cnpUpdateDenyLabelled = helpers.ManifestGet("cnp-update-deny-ingress-labelled.yaml")
)
validateL3L4 := func(allowApp3 bool) {
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"%q cannot curl clusterIP %q",
appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
ExpectWithOffset(1, res).To(getMatcher(allowApp3),
"%q curl clusterIP %q (expected to allow: %t)",
appPods[helpers.App3], clusterIP, allowApp3)
}
It("Enforces connectivity correctly when the same L3/L4 CNP is updated", func() {
By("Applying default allow policy")
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, cnpUpdateAllow, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "%q Policy cannot be applied", cnpUpdateAllow)
validateL3L4(allowAll)
By("Applying l3-l4 policy")
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, cnpUpdateDeny, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "%q Policy cannot be applied", cnpUpdateDeny)
validateL3L4(denyFromApp3)
By("Applying no-specs policy")
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, cnpUpdateNoSpecs, helpers.KubectlApply, helpers.HelperTimeout)
switch helpers.GetCurrentK8SEnv() {
// In k8s 1.15 no-specs policy is not allowed by kube-apiserver
case "1.8", "1.9", "1.10", "1.11", "1.12", "1.13", "1.14":
Expect(err).Should(BeNil(), "%q Policy cannot be applied", cnpUpdateAllow)
validateL3L4(allowAll)
default:
Expect(err).Should(Not(BeNil()), "%q Policy cannot be applied", cnpUpdateAllow)
validateL3L4(denyFromApp3)
}
By("Applying l3-l4 policy with user-specified labels")
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, cnpUpdateDenyLabelled, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "%q Policy cannot be applied", cnpUpdateDeny)
validateL3L4(denyFromApp3)
By("Applying default allow policy (should remove policy with user labels)")
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, cnpUpdateAllow, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "%q Policy cannot be applied", cnpUpdateAllow)
validateL3L4(allowAll)
})
It("Verifies that a CNP with L7 HTTP rules can be replaced with L7 Kafka rules", func() {
By("Installing L7 Policy")
// This HTTP policy was already validated in the
// test "checks all kind of Kubernetes policies".
// Install it then move on.
_, err := kubectl.CiliumPolicyAction(
namespaceForTest, l7Policy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot install %q policy", l7Policy)
// Update existing policy on port 80 from http to kafka
// to test ability to change L7 parser type of a port.
// Traffic cannot flow but policy must be able to be
// imported and applied to the endpoints.
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, l7PolicyKafka, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot update L7 policy (%q) from parser http to kafka", l7PolicyKafka)
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App3],
helpers.CurlFail("http://%s/public", clusterIP))
res.ExpectFail("Unexpected connection from %q to 'http://%s/public'",
appPods[helpers.App3], clusterIP)
res = kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail("http://%s/public", clusterIP))
res.ExpectFail("Unexpected connection from %q to 'http://%s/public'",
appPods[helpers.App2], clusterIP)
})
})
})
Context("GuestBook Examples", func() {
var (
deployment = "guestbook_deployment.yaml"
groupLabel = "zgroup=guestbook"
redisPolicy = "guestbook-policy-redis.json"
redisPolicyName = "guestbook-policy-redis"
redisPolicyDeprecated = "guestbook-policy-redis-deprecated.json"
redisPolicyDeprecatedName = "guestbook-redis-deprecated"
webPolicy = "guestbook-policy-web.yaml"
webPolicyName = "guestbook-policy-web"
)
var ciliumPods []string
var err error
BeforeEach(func() {
kubectl.ApplyDefault(helpers.ManifestGet(deployment))
ciliumPods, err := kubectl.GetCiliumPods(helpers.KubeSystemNamespace)
Expect(err).To(BeNil(), "cannot retrieve Cilium Pods")
Expect(ciliumPods).ShouldNot(BeEmpty(), "cannot retrieve Cilium pods")
})
getPolicyCmd := func(policy string) string {
return fmt.Sprintf("%s=%s %s=%s",
helpers.KubectlPolicyNameLabel, policy,
helpers.KubectlPolicyNameSpaceLabel, helpers.DefaultNamespace)
}
AfterEach(func() {
kubectl.Delete(helpers.ManifestGet(webPolicy)).ExpectSuccess(
"Web policy cannot be deleted")
k8sVersion := helpers.GetCurrentK8SEnv()
switch k8sVersion {
case "1.10", "1.11", "1.12", "1.13", "1.14", "1.15":
kubectl.Delete(helpers.ManifestGet(redisPolicyDeprecated)).ExpectSuccess(
"Redis deprecated policy cannot be deleted")
default:
}
kubectl.Delete(helpers.ManifestGet(deployment)).ExpectSuccess(
"Guestbook deployment cannot be deleted")
// This policy shouldn't be there, but test can fail before delete
// the policy and we want to make sure that it's deleted
kubectl.Delete(helpers.ManifestGet(redisPolicy))
for _, ciliumPod := range ciliumPods {
err := kubectl.WaitPolicyDeleted(ciliumPod, getPolicyCmd(webPolicyName))
Expect(err).To(
BeNil(), "WebPolicy is not deleted")
err = kubectl.WaitPolicyDeleted(ciliumPod, getPolicyCmd(redisPolicyName))
Expect(err).To(
BeNil(), "RedisPolicy is not deleted")
}
ExpectAllPodsTerminated(kubectl)
})
waitforPods := func() {
err = kubectl.WaitforPods(
helpers.DefaultNamespace,
fmt.Sprintf("-l %s", groupLabel), helpers.HelperTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "Bookinfo pods are not ready after timeout")
err := kubectl.WaitForServiceEndpoints(
helpers.DefaultNamespace, "", "redis-master", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "error waiting for redis-master service to be ready")
err = kubectl.WaitForServiceEndpoints(
helpers.DefaultNamespace, "", "redis-slave", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "error waiting for redis-slave service to be ready")
}
policyCheckStatus := func(policyCheck string) {
for _, ciliumPod := range ciliumPods {
ExpectWithOffset(1, kubectl.CiliumIsPolicyLoaded(ciliumPod, policyCheck)).To(BeTrue(),
"Policy %q is not in cilium pod %s", policyCheck, ciliumPod)
}
}
testConnectivitytoRedis := func() {
webPods, err := kubectl.GetPodsNodes(helpers.DefaultNamespace, "-l k8s-app.guestbook=web")
Expect(err).To(BeNil(), "Cannot get web pods")
serviceIP, port, err := kubectl.GetServiceHostPort(helpers.DefaultNamespace, "redis-master")
Expect(err).To(BeNil(), "Cannot get hostPort of redis-master")
serviceName := "redis-master"
err = kubectl.WaitForKubeDNSEntry(serviceName, helpers.DefaultNamespace)
Expect(err).To(BeNil(), "DNS entry is not ready after timeout")
for pod := range webPods {
redisMetadata := map[string]int{serviceIP: port, serviceName: port}
for k, v := range redisMetadata {
command := fmt.Sprintf(`nc %s %d <<EOF
PING
EOF`, k, v)
res := kubectl.ExecPodCmd(helpers.DefaultNamespace, pod, command)
ExpectWithOffset(1, res).To(helpers.CMDSuccess(),
"Web pod %q cannot connect to redis-master on '%s:%d'", pod, k, v)
}
}
}
It("checks policy example", func() {
waitforPods()
By("Apply policy to web")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, helpers.ManifestGet(webPolicy),
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot apply web-policy")
policyCheck := fmt.Sprintf("%s=%s %s=%s",
helpers.KubectlPolicyNameLabel, webPolicyName,
helpers.KubectlPolicyNameSpaceLabel, helpers.DefaultNamespace)
policyCheckStatus(policyCheck)
By("Apply policy to Redis")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, helpers.ManifestGet(redisPolicy),
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot apply redis policy")
policyCheck = fmt.Sprintf("%s=%s %s=%s",
helpers.KubectlPolicyNameLabel, redisPolicyName,
helpers.KubectlPolicyNameSpaceLabel, helpers.DefaultNamespace)
policyCheckStatus(policyCheck)
testConnectivitytoRedis()
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, helpers.ManifestGet(redisPolicy),
helpers.KubectlDelete, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot apply redis policy")
k8sVersion := helpers.GetCurrentK8SEnv()
switch k8sVersion {
case "1.10", "1.11", "1.12", "1.13", "1.14", "1.15":
default:
Skip(fmt.Sprintf("K8s %s doesn't support extensions/v1beta1 NetworkPolicies, skipping test", k8sVersion))
}
By("Apply deprecated policy to Redis")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, helpers.ManifestGet(redisPolicyDeprecated),
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot apply redis deprecated policy err: %q", err)
policyCheck = fmt.Sprintf("%s=%s %s=%s",
helpers.KubectlPolicyNameLabel, redisPolicyDeprecatedName,
helpers.KubectlPolicyNameSpaceLabel, helpers.DefaultNamespace)
policyCheckStatus(policyCheck)
testConnectivitytoRedis()
})
})
Context("Namespaces policies", func() {
var (
err error
secondNS = "second"
appPods map[string]string
appPodsNS map[string]string
clusterIP string
secondNSclusterIP string
demoPath = helpers.ManifestGet("demo.yaml")
l3L4Policy = helpers.ManifestGet("l3-l4-policy.yaml")
cnpSecondNS = helpers.ManifestGet("cnp-second-namespaces.yaml")
netpolNsSelector = fmt.Sprintf("%s -n %s", helpers.ManifestGet("netpol-namespace-selector.yaml"), secondNS)
l3l4PolicySecondNS = fmt.Sprintf("%s -n %s", l3L4Policy, secondNS)
demoManifest = fmt.Sprintf("%s -n %s", demoPath, secondNS)
)
BeforeAll(func() {
res := kubectl.NamespaceCreate(secondNS)
res.ExpectSuccess("unable to create namespace %q", secondNS)
res = kubectl.Exec(fmt.Sprintf("kubectl label namespaces/%[1]s nslabel=%[1]s", secondNS))
res.ExpectSuccess("cannot create namespace labels")
res = kubectl.ApplyDefault(demoManifest)
res.ExpectSuccess("unable to apply manifest")
res = kubectl.ApplyDefault(demoPath)
res.ExpectSuccess("unable to apply manifest")
err := kubectl.WaitforPods(secondNS, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).To(BeNil(),
"testapp pods are not ready after timeout in namspace %q", secondNS)
err = kubectl.WaitforPods(helpers.DefaultNamespace, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).To(BeNil(),
"testapp pods are not ready after timeout in %q namespace", helpers.DefaultNamespace)
appPods = helpers.GetAppPods(apps, helpers.DefaultNamespace, kubectl, "id")
appPodsNS = helpers.GetAppPods(apps, secondNS, kubectl, "id")
clusterIP, _, err = kubectl.GetServiceHostPort(helpers.DefaultNamespace, app1Service)
Expect(err).To(BeNil(), "Cannot get service on %q namespace", helpers.DefaultNamespace)
secondNSclusterIP, _, err = kubectl.GetServiceHostPort(secondNS, app1Service)
Expect(err).To(BeNil(), "Cannot get service on %q namespace", secondNS)
})
AfterEach(func() {
// Explicitly do not check results to avoid incomplete teardown of test.
_ = kubectl.Delete(l3l4PolicySecondNS)
_ = kubectl.Delete(l3L4Policy)
_ = kubectl.Delete(netpolNsSelector)
})
AfterAll(func() {
_ = kubectl.Delete(demoPath)
_ = kubectl.Delete(demoManifest)
_ = kubectl.Delete(cnpSecondNS)
_ = kubectl.NamespaceDelete(secondNS)
})
It("Tests the same Policy in different namespaces", func() {
// Tests that the same policy(name,labels) can enforce based on the
// namespace and all works as expected.
By("Applying Policy in %q namespace", secondNS)
_, err = kubectl.CiliumPolicyAction(
secondNS, l3l4PolicySecondNS, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"%q Policy cannot be applied in %q namespace", l3l4PolicySecondNS, secondNS)
By("Applying Policy in default namespace")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, l3L4Policy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(),
"%q Policy cannot be applied in %q namespace", l3L4Policy, helpers.DefaultNamespace)
By("Testing connectivity in %q namespace", secondNS)
res := kubectl.ExecPodCmd(
secondNS, appPodsNS[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", secondNSclusterIP)))
res.ExpectSuccess("%q cannot curl service", appPods[helpers.App2])
res = kubectl.ExecPodCmd(
secondNS, appPodsNS[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", secondNSclusterIP)))
res.ExpectFail("%q can curl to service", appPods[helpers.App3])
By("Testing connectivity in 'default' namespace")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectSuccess("%q cannot curl clusterIP %q", appPods[helpers.App2], clusterIP)
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App3],
helpers.CurlFail(fmt.Sprintf("http://%s/public", clusterIP)))
res.ExpectFail("%q can curl to %q", appPods[helpers.App3], clusterIP)
})
It("Kubernetes Network Policy by namespace selector", func() {
// Use namespace selector using Kubernetes Network Policy to make
// sure that it is translated correctly to Cilium and applies the
// policies to the right endpoints.
// KNP reference:
// https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#networkpolicyspec-v1-networking-k8s-io
By("Testing connectivity across Namespaces without policy")
for _, pod := range []string{helpers.App2, helpers.App3} {
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[pod],
helpers.CurlFail(fmt.Sprintf("http://%s/public", secondNSclusterIP)))
res.ExpectSuccess("%q cannot curl service", appPods[pod])
res = kubectl.ExecPodCmd(
secondNS, appPodsNS[pod],
helpers.CurlFail(fmt.Sprintf("http://%s/public", secondNSclusterIP)))
res.ExpectSuccess("%q cannot curl service", appPodsNS[pod])
}
By("Applying Policy in %q namespace", secondNS)
_, err = kubectl.CiliumPolicyAction(
secondNS, netpolNsSelector, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Policy cannot be applied")
for _, pod := range []string{helpers.App2, helpers.App3} {
// Make sure that the Default namespace can NOT connect to
// second namespace.
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[pod],
helpers.CurlFail(fmt.Sprintf("http://%s/public", secondNSclusterIP)))
res.ExpectFail("%q can curl to service, policy is not blocking"+
"communication to %q namespace", appPods[pod], secondNS)
// Second namespace pods can connect to the same namespace based on policy.
res = kubectl.ExecPodCmd(
secondNS, appPodsNS[pod],
helpers.CurlFail(fmt.Sprintf("http://%s/public", secondNSclusterIP)))
res.ExpectSuccess("%q cannot curl service", appPodsNS[pod])
}