-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Policies.go
2000 lines (1663 loc) · 79.1 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-2020 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 RuntimeTest
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/policy/api"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
"github.com/cilium/cilium/test/helpers/constants"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"github.com/sirupsen/logrus"
)
const (
// Commands
ping = "ping"
ping6 = "ping6"
http = "http"
http6 = "http6"
httpPrivate = "http_private"
http6Private = "http6_private"
httpPrivateToken = "http_private_token"
http6PrivateToken = "http6_private_token"
httpPathRewrite = "http_path_rewrite"
http6PathRewrite = "http6_path_rewrite"
// Policy files
policyJSON = "policy.json"
invalidJSON = "invalid.json"
multL7PoliciesJSON = "Policies-l7-multiple.json"
policiesL7JSON = "Policies-l7-simple.json"
imposePoliciesL7JSON = "Policies-l7-impose.json"
policiesL3JSON = "Policies-l3-policy.json"
policiesL4Json = "Policies-l4-policy.json"
policiesL3DependentL7EgressJSON = "Policies-l3-dependent-l7-egress.json"
policiesReservedInitJSON = "Policies-reserved-init.json"
)
var _ = Describe("RuntimePolicies", func() {
var (
vm *helpers.SSHMeta
monitorStop = func() error { return nil }
initContainer string
)
BeforeAll(func() {
vm = helpers.InitRuntimeHelper(helpers.Runtime, logger)
// Make sure that Cilium is started with appropriate CLI options
// (specifically to exclude the local addresses that are populated for
// CIDR policy tests).
Expect(vm.SetUpCiliumWithHubble()).To(BeNil())
ExpectCiliumReady(vm)
vm.SampleContainersActions(helpers.Create, helpers.CiliumDockerNetwork)
vm.PolicyDelAll()
initContainer = "initContainer"
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue(), "Endpoints are not ready after timeout")
})
BeforeEach(func() {
ExpectPolicyEnforcementUpdated(vm, helpers.PolicyEnforcementDefault)
})
AfterEach(func() {
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
})
JustBeforeEach(func() {
monitorStop = vm.MonitorStart()
})
JustAfterEach(func() {
vm.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
Expect(monitorStop()).To(BeNil(), "cannot stop monitor command")
})
AfterFailed(func() {
vm.ReportFailed()
})
AfterAll(func() {
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
vm.SampleContainersActions(helpers.Delete, helpers.CiliumDockerNetwork)
vm.CloseSSHClient()
})
pingRequests := []string{ping, ping6}
httpRequestsPublic := []string{http, http6}
httpRequestsPrivate := []string{httpPrivate, http6Private}
httpRequestsPrivateToken := []string{httpPrivateToken, http6PrivateToken}
httpRequestsPathRewrite := []string{httpPathRewrite, http6PathRewrite}
httpRequests := append(httpRequestsPublic, httpRequestsPrivate...)
httpRequests = append(httpRequests, httpRequestsPathRewrite...)
allRequests := append(pingRequests, httpRequests...)
connectivityTest := func(tests []string, client, server string, expectsSuccess bool) {
var assertFn func() types.GomegaMatcher
if expectsSuccess {
assertFn = BeTrue
} else {
assertFn = BeFalse
}
if client != helpers.Host {
_, err := vm.ContainerInspectNet(client)
ExpectWithOffset(1, err).Should(BeNil(), fmt.Sprintf(
"could not get container %q (client) meta", client))
}
srvIP, err := vm.ContainerInspectNet(server)
ExpectWithOffset(1, err).Should(BeNil(), fmt.Sprintf(
"could not get container %q (server) meta", server))
for _, test := range tests {
var command, commandName, dst, resultName string
switch test {
case ping:
command = helpers.Ping(srvIP[helpers.IPv4])
dst = srvIP[helpers.IPv4]
case ping6:
command = helpers.Ping6(srvIP[helpers.IPv6])
dst = srvIP[helpers.IPv6]
case http, httpPrivate, httpPrivateToken, httpPathRewrite:
dst = srvIP[helpers.IPv4]
case http6, http6Private, http6PrivateToken, http6PathRewrite:
dst = fmt.Sprintf("[%s]", srvIP[helpers.IPv6])
}
switch test {
case ping:
commandName = "ping"
case ping6:
commandName = "ping6"
case http:
commandName = "curl public IPv4 URL on"
command = helpers.CurlFail("http://%s:80/public", dst)
case http6:
commandName = "curl public IPv6 URL on"
command = helpers.CurlFail("http://%s:80/public", dst)
case httpPrivate:
commandName = "curl private IPv4 URL on"
command = helpers.CurlFail("http://%s:80/private", dst)
case http6Private:
commandName = "curl private IPv6 URL on"
command = helpers.CurlFail("http://%s:80/private", dst)
case httpPrivateToken:
commandName = "curl private IPv4 URL with an access-token 1234-09AB-5678-CDEF on"
command = helpers.CurlFail(`--header "Access-Token: 1234-09AB-5678-CDEF" http://%s:80/private`, dst)
case http6PrivateToken:
commandName = "curl private IPv6 URL with an access-token 1234-09AB-5678-CDEF on"
command = helpers.CurlFail(`--header "Access-Token: 1234-09AB-5678-CDEF" http://%s:80/private`, dst)
case httpPathRewrite:
commandName = "curl path rewrite IPv4 URL on"
command = helpers.CurlFail("http://%s:80/public/../private", dst)
case http6PathRewrite:
commandName = "curl path rewrite IPv6 URL on"
command = helpers.CurlFail("http://%s:80/public/../private", dst)
}
if expectsSuccess {
resultName = "succeed"
} else {
resultName = "fail"
}
By("%q attempting to %q %q", client, commandName, server)
var res *helpers.CmdRes
if client != helpers.Host {
res = vm.ContainerExec(client, command)
} else {
res = vm.Exec(command)
}
ExpectWithOffset(1, res.WasSuccessful()).Should(assertFn(),
fmt.Sprintf("%q expects %s %s (%s) to %s", client, commandName, server, dst, resultName))
}
}
checkProxyStatistics := func(epID string, reqsFwd, reqsReceived, reqsDenied, respFwd, respReceived int) {
epModel := vm.EndpointGet(epID)
Expect(epModel).To(Not(BeNil()), "nil model returned for endpoint %s", epID)
for _, epProxyStatistics := range epModel.Status.Policy.ProxyStatistics {
if epProxyStatistics.Location == models.ProxyStatisticsLocationEgress {
ExpectWithOffset(1, epProxyStatistics.Statistics.Requests.Forwarded).To(BeEquivalentTo(reqsFwd), "Unexpected number of forwarded requests to proxy")
ExpectWithOffset(1, epProxyStatistics.Statistics.Requests.Received).To(BeEquivalentTo(reqsReceived), "Unexpected number of received requests to proxy")
ExpectWithOffset(1, epProxyStatistics.Statistics.Requests.Denied).To(BeEquivalentTo(reqsDenied), "Unexpected number of denied requests to proxy")
ExpectWithOffset(1, epProxyStatistics.Statistics.Responses.Forwarded).To(BeEquivalentTo(respFwd), "Unexpected number of forwarded responses from proxy")
ExpectWithOffset(1, epProxyStatistics.Statistics.Responses.Received).To(BeEquivalentTo(respReceived), "Unexpected number of received responses from proxy")
}
}
}
It("L3/L4 Checks", func() {
_, err := vm.PolicyImportAndWait(vm.GetFullPath(policiesL3JSON), helpers.HelperTimeout)
Expect(err).Should(BeNil())
//APP1 can connect to all Httpd1
connectivityTest(allRequests, helpers.App1, helpers.Httpd1, true)
//APP2 can't connect to Httpd1
connectivityTest([]string{http}, helpers.App2, helpers.Httpd1, false)
// APP1 can reach using TCP HTTP2
connectivityTest(httpRequestsPublic, helpers.App1, helpers.Httpd2, true)
// APP2 can't reach using TCP to HTTP2
connectivityTest(httpRequestsPublic, helpers.App2, helpers.Httpd2, false)
// APP3 can reach using TCP to HTTP2, but can't ping due to egress rule.
connectivityTest(httpRequestsPublic, helpers.App3, helpers.Httpd2, true)
connectivityTest(pingRequests, helpers.App3, helpers.Httpd2, false)
// APP3 can't reach using TCP to HTTP3
connectivityTest(allRequests, helpers.App3, helpers.Httpd3, false)
// app2 can reach httpd3 for all requests due to l3-only label-based allow policy.
connectivityTest(allRequests, helpers.App2, helpers.Httpd3, true)
// app2 cannot reach httpd2 for all requests.
connectivityTest(allRequests, helpers.App2, helpers.Httpd2, false)
By("Deleting all policies; all tests should succeed")
status := vm.PolicyDelAll()
status.ExpectSuccess()
vm.WaitEndpointsReady()
connectivityTest(allRequests, helpers.App1, helpers.Httpd1, true)
connectivityTest(allRequests, helpers.App2, helpers.Httpd1, true)
})
It("L4Policy Checks", func() {
_, err := vm.PolicyImportAndWait(vm.GetFullPath(policiesL4Json), helpers.HelperTimeout)
Expect(err).Should(BeNil())
for _, app := range []string{helpers.App1, helpers.App2} {
connectivityTest(pingRequests, app, helpers.Httpd1, false)
connectivityTest(httpRequestsPublic, app, helpers.Httpd1, true)
connectivityTest(pingRequests, app, helpers.Httpd2, false)
connectivityTest(httpRequestsPublic, app, helpers.Httpd2, true)
}
connectivityTest(allRequests, helpers.App3, helpers.Httpd1, false)
connectivityTest(pingRequests, helpers.App1, helpers.Httpd3, false)
By("Disabling all the policies. All should work")
vm.PolicyDelAll().ExpectSuccess("cannot delete the policy")
vm.WaitEndpointsReady()
for _, app := range []string{helpers.App1, helpers.App2} {
connectivityTest(allRequests, app, helpers.Httpd1, true)
connectivityTest(allRequests, app, helpers.Httpd2, true)
}
})
It("Checks that traffic is not dropped when L4 policy is installed and deleted", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srvIP, err := vm.ContainerInspectNet(helpers.Httpd1)
Expect(err).Should(BeNil(), "Cannot get httpd1 server address")
type BackgroundTestAsserts struct {
res *helpers.CmdRes
time time.Time
}
backgroundChecks := []*BackgroundTestAsserts{}
var wg sync.WaitGroup
wg.Add(1)
go func() {
for {
select {
default:
res := vm.ContainerExec(
helpers.App1,
helpers.CurlFail("http://%s/", srvIP[helpers.IPv4]))
assert := &BackgroundTestAsserts{
res: res,
time: time.Now(),
}
backgroundChecks = append(backgroundChecks, assert)
case <-ctx.Done():
wg.Done()
return
}
}
}()
// Sleep a bit to make sure that the goroutine starts.
time.Sleep(50 * time.Millisecond)
_, err = vm.PolicyImportAndWait(vm.GetFullPath(policiesL4Json), helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot install L4 policy")
By("Uninstalling policy")
vm.PolicyDelAll().ExpectSuccess("Cannot delete all policies")
vm.WaitEndpointsReady()
By("Canceling background connections from app2 to httpd1")
cancel()
wg.Wait()
GinkgoPrint("Made %d connections in total", len(backgroundChecks))
Expect(backgroundChecks).ShouldNot(BeEmpty(), "No background connections were made")
for _, check := range backgroundChecks {
check.res.ExpectSuccess("Curl from app2 to httpd1 should work but it failed at %s", check.time)
}
})
It("L7 Checks", func() {
_, err := vm.PolicyImportAndWait(vm.GetFullPath(policiesL7JSON), helpers.HelperTimeout)
Expect(err).Should(BeNil())
By("Simple Ingress")
// app1 can connect to /public, but not to /private.
connectivityTest(httpRequestsPublic, helpers.App1, helpers.Httpd1, true)
connectivityTest(httpRequestsPrivate, helpers.App1, helpers.Httpd1, false)
// app1 can't exploit path rewrite
connectivityTest(httpRequestsPathRewrite, helpers.App1, helpers.Httpd1, false)
// Host can connect to /public, but not to /private.
connectivityTest(httpRequestsPublic, helpers.Host, helpers.Httpd1, true)
connectivityTest(httpRequestsPrivate, helpers.Host, helpers.Httpd1, false)
// Host can't exploit path rewrite
connectivityTest(httpRequestsPathRewrite, helpers.Host, helpers.Httpd1, false)
// app cannot connect to httpd1 because httpd1 only allows ingress from app1.
connectivityTest(httpRequestsPublic, helpers.App2, helpers.Httpd1, false)
By("Simple Egress")
// app2 can connect to public, but no to private
connectivityTest(httpRequestsPublic, helpers.App2, helpers.Httpd2, true)
connectivityTest(httpRequestsPrivate, helpers.App2, helpers.Httpd2, false)
// app2 can't exploit path rewrite
connectivityTest(httpRequestsPathRewrite, helpers.App2, helpers.Httpd2, false)
// TODO (1488) - uncomment when l3-dependent-l7 is merged for egress.
//connectivityTest(httpRequestsPublic, helpers.App3, helpers.Httpd3, true)
//connectivityTest(httpRequestsPrivate, helpers.App3, helpers.Httpd3, false)
//connectivityTest(allRequests, helpers.App3, helpers.Httpd2, false)
By("Disabling all the policies. All should work")
status := vm.PolicyDelAll()
status.ExpectSuccess()
vm.WaitEndpointsReady()
connectivityTest(allRequests, helpers.App1, helpers.Httpd1, true)
connectivityTest(allRequests, helpers.App2, helpers.Httpd1, true)
By("Impose header on Egress, verify on ingress")
vm.PolicyDelAll()
_, err = vm.PolicyImportAndWait(vm.GetFullPath(imposePoliciesL7JSON), helpers.HelperTimeout)
Expect(err).Should(BeNil())
// app1 can connect to public, but not to private
connectivityTest(httpRequestsPublic, helpers.App1, helpers.Httpd1, true)
connectivityTest(httpRequestsPrivate, helpers.App1, helpers.Httpd1, false)
// app1 succeeds if the right access token is used
connectivityTest(httpRequestsPrivateToken, helpers.App1, helpers.Httpd1, true)
// app2 can connect to public, and to private due to access token being inserted at egress
connectivityTest(httpRequestsPublic, helpers.App2, helpers.Httpd2, true)
connectivityTest(httpRequestsPrivate, helpers.App2, helpers.Httpd2, true)
By("Multiple Ingress")
vm.PolicyDelAll()
_, err = vm.PolicyImportAndWait(vm.GetFullPath(multL7PoliciesJSON), helpers.HelperTimeout)
Expect(err).Should(BeNil())
//APP1 can connect to public, but no to private
connectivityTest(httpRequestsPublic, helpers.App1, helpers.Httpd1, true)
connectivityTest(httpRequestsPrivate, helpers.App1, helpers.Httpd1, false)
//App2 can't connect
connectivityTest(httpRequestsPublic, helpers.App2, helpers.Httpd1, false)
By("Multiple Ingress rules on same port")
// app1 can connect to /public on httpd2
connectivityTest(httpRequestsPublic, helpers.App1, helpers.Httpd2, true)
By("Multiple Egress")
// app2 can connect to /public, but not to /private
connectivityTest(httpRequestsPublic, helpers.App2, helpers.Httpd2, true)
connectivityTest(httpRequestsPrivate, helpers.App2, helpers.Httpd2, false)
By("Disabling all the policies. All should work")
status = vm.PolicyDelAll()
status.ExpectSuccess()
vm.WaitEndpointsReady()
connectivityTest(allRequests, helpers.App1, helpers.Httpd1, true)
connectivityTest(allRequests, helpers.App2, helpers.Httpd1, true)
})
It("Tests Endpoint Connectivity Functions After Daemon Configuration Is Updated", func() {
httpd1DockerNetworking, err := vm.ContainerInspectNet(helpers.Httpd1)
Expect(err).ToNot(HaveOccurred(), "unable to get container networking metadata for %s", helpers.Httpd1)
// Importing a policy to ensure that not only does endpoint connectivity
// work after updating daemon configuration, but that policy works as well.
By("Importing policy and waiting for revision to increase for endpoints")
_, err = vm.PolicyImportAndWait(vm.GetFullPath(policiesL7JSON), helpers.HelperTimeout)
Expect(err).ToNot(HaveOccurred(), "unable to import policy after timeout")
By("Trying to access %s:80/public from %s before daemon configuration is updated (should be allowed by policy)", helpers.Httpd1, helpers.App1)
res := vm.ContainerExec(helpers.App1, helpers.CurlFail("http://%s:80/public", httpd1DockerNetworking[helpers.IPv4]))
res.ExpectSuccess("unable to access %s:80/public from %s (should have worked)", helpers.Httpd1, helpers.App1)
By("Trying to access %s:80/private from %s before daemon configuration is updated (should not be allowed by policy)", helpers.Httpd1, helpers.App1)
res = vm.ContainerExec(helpers.App1, helpers.CurlFail("http://%s:80/private", httpd1DockerNetworking[helpers.IPv4]))
res.ExpectFail("unable to access %s:80/private from %s (should not have worked)", helpers.Httpd1, helpers.App1)
By("Getting configuration for daemon")
daemonDebugConfig, err := vm.ExecCilium("config -o json").Filter("{.Debug}")
Expect(err).ToNot(HaveOccurred(), "Unable to get configuration for daemon")
daemonDebugConfigString := daemonDebugConfig.String()
var daemonDebugConfigSwitched string
switch daemonDebugConfigString {
case "Disabled":
daemonDebugConfigSwitched = "Enabled"
case "Enabled":
daemonDebugConfigSwitched = "Disabled"
default:
Fail(fmt.Sprintf("invalid configuration value for daemon: Debug=%s", daemonDebugConfigString))
}
currentRev, err := vm.PolicyGetRevision()
Expect(err).ToNot(HaveOccurred(), "unable to get policy revision")
// TODO: would be a good idea to factor out daemon configuration updates
// into a function in the future.
By("Changing daemon configuration from Debug=%s to Debug=%s to induce policy recalculation for endpoints", daemonDebugConfigString, daemonDebugConfigSwitched)
res = vm.ExecCilium(fmt.Sprintf("config Debug=%s", daemonDebugConfigSwitched))
res.ExpectSuccess("unable to change daemon configuration")
By("Getting policy revision after daemon configuration change")
revAfterConfig, err := vm.PolicyGetRevision()
Expect(err).ToNot(HaveOccurred(), "unable to get policy revision")
Expect(revAfterConfig).To(BeNumerically(">=", currentRev+1))
By("Waiting for policy revision to increase after daemon configuration change")
res = vm.PolicyWait(revAfterConfig)
res.ExpectSuccess("policy revision was not bumped after daemon configuration changes")
By("Changing daemon configuration back from Debug=%s to Debug=%s", daemonDebugConfigSwitched, daemonDebugConfigString)
res = vm.ExecCilium(fmt.Sprintf("config Debug=%s", daemonDebugConfigString))
res.ExpectSuccess("unable to change daemon configuration")
By("Getting policy revision after daemon configuration change")
revAfterSecondConfig, err := vm.PolicyGetRevision()
Expect(err).To(BeNil())
Expect(revAfterSecondConfig).To(BeNumerically(">=", revAfterConfig+1))
By("Waiting for policy revision to increase after daemon configuration change")
res = vm.PolicyWait(revAfterSecondConfig)
res.ExpectSuccess("policy revision was not bumped after daemon configuration changes")
By("Trying to access %s:80/public from %s after daemon configuration was updated (should be allowed by policy)", helpers.Httpd1, helpers.App1)
res = vm.ContainerExec(helpers.App1, helpers.CurlFail("http://%s:80/public", httpd1DockerNetworking[helpers.IPv4]))
res.ExpectSuccess("unable to access %s:80/public from %s (should have worked)", helpers.Httpd1, helpers.App1)
By("Trying to access %s:80/private from %s after daemon configuration is updated (should not be allowed by policy)", helpers.Httpd1, helpers.App1)
res = vm.ContainerExec(helpers.App1, helpers.CurlFail("http://%s:80/private", httpd1DockerNetworking[helpers.IPv4]))
res.ExpectFail("unable to access %s:80/private from %s (should not have worked)", helpers.Httpd1, helpers.App1)
})
It("L3-Dependent L7 Egress", func() {
_, err := vm.PolicyImportAndWait(vm.GetFullPath(policiesL3DependentL7EgressJSON), helpers.HelperTimeout)
Expect(err).Should(BeNil(), "unable to import %s", policiesL3DependentL7EgressJSON)
endpointIDS, err := vm.GetEndpointsIds()
Expect(err).To(BeNil(), "Unable to get IDs of endpoints")
app3EndpointID, exists := endpointIDS[helpers.App3]
Expect(exists).To(BeTrue(), "Expected endpoint ID to exist for %s", helpers.App3)
connectivityTest(httpRequestsPublic, helpers.Host, helpers.Httpd2, true)
connectivityTest(httpRequestsPublic, helpers.App3, helpers.Httpd1, true)
// Since policy allows connectivity on /public to httpd1 from app3, we
// expect:
// * two requests to get received by the proxy because connectivityTest
// connects via http / http6.
// * two requests to get forwarded by the proxy because policy allows
// connectivity via http / http6.
// * two corresponding responses forwarded / received to the aforementioned
// requests due to policy allowing connectivity via http / http6.
checkProxyStatistics(app3EndpointID, 2, 2, 0, 2, 2)
connectivityTest(httpRequestsPrivate, helpers.App3, helpers.Httpd1, false)
// Since policy does not allow connectivity on /private to httpd1 from app3, we expect:
// * two requests denied due to connectivity not being allowed via http / http6
// * the count for requests forwarded, and responses forwarded / received to be the same
// as from the prior test since the requests were denied, and thus no new requests
// were forwarded, and no new responses forwarded nor received.
checkProxyStatistics(app3EndpointID, 2, 4, 2, 2, 2)
connectivityTest(httpRequestsPublic, helpers.App3, helpers.Httpd2, true)
// Since policy allows connectivity on L3 from app3 to httpd2, and such
// packets are not forwarded to the proxy, we expect no changes in proxy
// stats.
checkProxyStatistics(app3EndpointID, 2, 4, 2, 2, 2)
connectivityTest(httpRequestsPrivate, helpers.App3, helpers.Httpd2, true)
// Since policy allows connectivity on L3 from app3 to httpd2, and such
// packets are not forwarded to the proxy, we expect no changes in proxy
// stats.
checkProxyStatistics(app3EndpointID, 2, 4, 2, 2, 2)
})
Context("CIDR L3 Policy", func() {
var (
httpd1DockerNetworking map[string]string
ipv4Prefix, ipv6Prefix string
ipv4Address, ipv4PrefixExcept string
worldIP string
)
const (
ipv4OtherHost = "192.168.254.111"
ipv4OtherNet = "99.11.0.0/16"
httpd2Label = "id.httpd2"
httpd1Label = "id.httpd1"
app3Label = "id.app3"
worldPrefix = "192.168.2.0/24"
)
// Delete the pseudo-host IPs that we added to localhost after test
// finishes. Don't care about success; this is best-effort.
cleanup := func() {
_ = vm.ContainerRm(helpers.WorldHttpd1)
_ = vm.NetworkDelete(helpers.WorldDockerNetwork)
_ = vm.RemoveIPFromLoopbackDevice(fmt.Sprintf("%s/32", helpers.FakeIPv4WorldAddress))
_ = vm.RemoveIPFromLoopbackDevice(fmt.Sprintf("%s/128", helpers.FakeIPv6WorldAddress))
}
BeforeAll(func() {
var err error
httpd1DockerNetworking, err = vm.ContainerInspectNet(helpers.Httpd1)
Expect(err).Should(BeNil(), fmt.Sprintf(
"could not get container %s Docker networking", helpers.Httpd1))
ipv6Prefix = fmt.Sprintf("%s/112", httpd1DockerNetworking["IPv6Gateway"])
ipv4Address = httpd1DockerNetworking[helpers.IPv4]
// Get prefix of node-local endpoints.
By("Getting IPv4 and IPv6 prefixes of node-local endpoints")
getIpv4Prefix := vm.Exec(fmt.Sprintf(`expr %s : '\([0-9]*\.[0-9]*\.\)'`, ipv4Address)).SingleOut()
ipv4Prefix = fmt.Sprintf("%s0.0/16", getIpv4Prefix)
getIpv4PrefixExcept := vm.Exec(fmt.Sprintf(`expr %s : '\([0-9]*\.[0-9]*\.\)'`, ipv4Address)).SingleOut()
ipv4PrefixExcept = fmt.Sprintf(`%s0.0/18`, getIpv4PrefixExcept)
By("Adding Pseudo-Host IPs to localhost")
cleanup()
vm.AddIPToLoopbackDevice(fmt.Sprintf("%s/32", helpers.FakeIPv4WorldAddress)).ExpectSuccess("Unable to add %s to pseudo-host IP to localhost", helpers.FakeIPv4WorldAddress)
vm.AddIPToLoopbackDevice(fmt.Sprintf("%s/128", helpers.FakeIPv6WorldAddress)).ExpectSuccess("Unable to add %s to pseudo-host IP to localhost", helpers.FakeIPv6WorldAddress)
netOptions := "-o com.docker.network.bridge.enable_ip_masquerade=false"
res := vm.NetworkCreateWithOptions(helpers.WorldDockerNetwork, worldPrefix, false, netOptions)
res.ExpectSuccess("Docker network for world containers could not be created")
res = vm.NetworkGet(helpers.WorldDockerNetwork)
res.ExpectSuccess("Docker network for world containers is unavailable")
vm.ContainerCreate(helpers.WorldHttpd1, constants.HttpdImage, helpers.WorldDockerNetwork, fmt.Sprintf("-l id.%s", helpers.WorldHttpd1))
res = vm.ContainerInspect(helpers.WorldHttpd1)
res.ExpectSuccess("World container is not ready")
worldNet, err := vm.ContainerInspectOtherNet(helpers.WorldHttpd1, helpers.WorldDockerNetwork)
Expect(err).Should(BeNil(), fmt.Sprintf(
"could not get container %s Docker networking", helpers.WorldHttpd1))
worldIP = worldNet[helpers.IPv4]
})
AfterAll(func() {
cleanup()
})
BeforeEach(func() {
logger.WithFields(logrus.Fields{
"IPv4_host": helpers.FakeIPv4WorldAddress,
"IPv4_other_host": ipv4OtherHost,
"IPv4_other_net": ipv4OtherNet,
"IPv6_host": helpers.FakeIPv6WorldAddress}).
Info("VM IP address configuration")
// If the pseudo host IPs have not been removed since the last run but
// Cilium was restarted, the IPs may have been picked up as valid host
// IPs. Remove them from the list so they are not regarded as localhost
// entries.
// Don't care about success or failure as the BPF endpoint may not even be
// present; this is best-effort.
_ = vm.ExecCilium(fmt.Sprintf("bpf endpoint delete %s", helpers.FakeIPv4WorldAddress))
_ = vm.ExecCilium(fmt.Sprintf("bpf endpoint delete %s", helpers.FakeIPv6WorldAddress))
By("IPV6 Prefix: %q", ipv6Prefix)
By("IPV4 Address Endpoint: %q", ipv4Address)
By("IPV4 Prefix: %q", ipv4Prefix)
By("IPV4 Prefix Except: %q", ipv4PrefixExcept)
By("Setting PolicyEnforcement to always enforce (default-deny)")
ExpectPolicyEnforcementUpdated(vm, helpers.PolicyEnforcementAlways)
})
It("validates toCIDR", func() {
By("Pinging host IPv4 from httpd2 (should NOT work due to default-deny PolicyEnforcement mode)")
res := vm.ContainerExec(helpers.Httpd2, helpers.Ping(helpers.FakeIPv4WorldAddress))
res.ExpectFail("Unexpected success pinging host (%s) from %s", helpers.FakeIPv4WorldAddress, helpers.Httpd2)
By("Importing L3 CIDR Policy for IPv4 Egress Allowing Egress to %q, %q from %q", ipv4OtherHost, ipv4OtherHost, httpd2Label)
script := fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%s":""}},
"egress":
[{
"toCIDR": [
"%s/24",
"%s/20"
]
}]
}]`, httpd2Label, ipv4OtherHost, ipv4OtherHost)
_, err := vm.PolicyRenderAndImport(script)
Expect(err).To(BeNil(), "Unable to import policy: %s", err)
res = vm.ContainerExec(helpers.Httpd2, helpers.Ping(helpers.FakeIPv4WorldAddress))
res.ExpectSuccess("Unexpected failure pinging host (%s) from %s", helpers.FakeIPv4WorldAddress, helpers.Httpd2)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
By("Pinging host IPv6 from httpd2 (should NOT work because we did not specify IPv6 CIDR of host as part of previously imported policy)")
res = vm.ContainerExec(helpers.Httpd2, helpers.Ping6(helpers.FakeIPv6WorldAddress))
res.ExpectFail("Unexpected success pinging host (%s) from %s", helpers.FakeIPv6WorldAddress, helpers.Httpd2)
By("Importing L3 CIDR Policy for IPv6 Egress")
script = fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%s":""}},
"egress": [{
"toCIDR": [
"%s"
]
}]
}]`, httpd2Label, helpers.FakeIPv6WorldAddress)
_, err = vm.PolicyRenderAndImport(script)
Expect(err).To(BeNil(), "Unable to import policy: %s", err)
By("Pinging host IPv6 from httpd2 (should work because policy allows IPv6 CIDR %q)", helpers.FakeIPv6WorldAddress)
res = vm.ContainerExec(helpers.Httpd2, helpers.Ping6(helpers.FakeIPv6WorldAddress))
res.ExpectSuccess("Unexpected failure pinging host (%s) from %s", helpers.FakeIPv6WorldAddress, helpers.Httpd2)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
// This test case checks that ping works even without explicit CIDR policies
// imported.
By("Importing L3 Label-Based Policy Allowing traffic from httpd2 to httpd1")
script = fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%[1]s":""}},
"ingress": [{
"fromEndpoints": [
{"matchLabels":{"%[2]s":""}}
]
}]
},
{
"endpointSelector": {"matchLabels":{"%[2]s":""}},
"egress": [{
"toEndpoints": [
{"matchLabels":{"%[1]s":""}}
]
}]
}]`, httpd1Label, httpd2Label)
_, err = vm.PolicyRenderAndImport(script)
Expect(err).To(BeNil(), "Unable to import policy: %s", err)
By("Pinging httpd1 IPV4 from httpd2 (should work because we allowed traffic to httpd1 labels from httpd2 labels)")
res = vm.ContainerExec(helpers.Httpd2, helpers.Ping(httpd1DockerNetworking[helpers.IPv4]))
res.ExpectSuccess("Unexpected failure pinging %s (%s) from %s", helpers.Httpd1, httpd1DockerNetworking[helpers.IPv4], helpers.Httpd2)
By("Pinging httpd1 IPv6 from httpd2 (should work because we allowed traffic to httpd1 labels from httpd2 labels)")
res = vm.ContainerExec(helpers.Httpd2, helpers.Ping6(httpd1DockerNetworking[helpers.IPv6]))
res.ExpectSuccess("Unexpected failure pinging %s (%s) from %s", helpers.Httpd1, httpd1DockerNetworking[helpers.IPv6], helpers.Httpd2)
By("Pinging httpd1 IPv4 from app3 (should NOT work because app3 hasn't been whitelisted to communicate with httpd1)")
res = vm.ContainerExec(helpers.App3, helpers.Ping(helpers.Httpd1))
res.ExpectFail("Unexpected success pinging %s IPv4 from %s", helpers.Httpd1, helpers.App3)
By("Pinging httpd1 IPv6 from app3 (should NOT work because app3 hasn't been whitelisted to communicate with httpd1)")
res = vm.ContainerExec(helpers.App3, helpers.Ping6(helpers.Httpd1))
res.ExpectFail("Unexpected success pinging %s IPv6 from %s", helpers.Httpd1, helpers.App3)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
})
It("validates fromCIDR", func() {
// Checking combined policy allowing traffic from IPv4 and IPv6 CIDR ranges.
By("Importing Policy Allowing Ingress From %q --> %q And From CIDRs %q, %q, %q", helpers.Httpd2, helpers.Httpd1, ipv4Prefix, ipv6Prefix, worldPrefix)
policy := fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%[1]s":""}},
"ingress": [{
"fromEndpoints": [
{"matchLabels":{"%[2]s":""}}
]
}, {
"fromCIDR": [
"%s",
"%s",
"%s"
]
}]
},
{
"endpointSelector": {"matchLabels":{"%[2]s":""}},
"egress": [{
"toEndpoints": [
{"matchLabels":{"%[1]s":""}}
]
}]
}]`, httpd1Label, httpd2Label, ipv4Prefix, ipv6Prefix, worldPrefix)
_, err := vm.PolicyRenderAndImport(policy)
Expect(err).To(BeNil(), "Unable to import policy: %s", err)
// Checks from a Cilium endpoint
By("Pinging httpd1 IPV4 from httpd2 (should work because we allowed traffic to httpd1 labels from httpd2 labels)")
res := vm.ContainerExec(helpers.Httpd2, helpers.Ping(httpd1DockerNetworking[helpers.IPv4]))
res.ExpectSuccess("Unexpected failure pinging %s (%s) from %s", helpers.Httpd1, httpd1DockerNetworking[helpers.IPv4], helpers.Httpd2)
By("Pinging httpd1 IPv6 from httpd2 (should work because we allowed traffic to httpd1 labels from httpd2 labels)")
res = vm.ContainerExec(helpers.Httpd2, helpers.Ping6(httpd1DockerNetworking[helpers.IPv6]))
res.ExpectSuccess("Unexpected failure pinging %s (%s) from %s", helpers.Httpd1, httpd1DockerNetworking[helpers.IPv6], helpers.Httpd2)
By("Pinging httpd1 IPv4 %q from app3 (shouldn't work because CIDR policies don't apply to endpoint-endpoint communication)", ipv4Prefix)
res = vm.ContainerExec(helpers.App3, helpers.Ping(helpers.Httpd1))
res.ExpectFail("Unexpected success pinging %s IPv4 from %s", helpers.Httpd1, helpers.App3)
By("Pinging httpd1 IPv6 %q from app3 (shouldn't work because CIDR policies don't apply to endpoint-endpoint communication)", ipv6Prefix)
res = vm.ContainerExec(helpers.App3, helpers.Ping6(helpers.Httpd1))
res.ExpectFail("Unexpected success pinging %s IPv6 from %s", helpers.Httpd1, helpers.App3)
// Ping from a source outside Cilium control
By(fmt.Sprintf("Pinging httpd1 IPV4 from world container (should work because we allowed traffic to httpd1 labels from prefix %s)", worldPrefix))
res = vm.ContainerExec(helpers.WorldHttpd1, helpers.Ping(httpd1DockerNetworking[helpers.IPv4]))
res.ExpectSuccess("Unexpected failure pinging %s (%s) from %s", helpers.Httpd1, httpd1DockerNetworking[helpers.IPv4], helpers.WorldHttpd1)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
// Make sure that combined label-based and CIDR-based policy works.
By("Importing Policy Allowing Ingress From %s --> %s And From CIDRs %s", helpers.Httpd2, helpers.Httpd1, ipv4OtherNet)
policy = fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%[1]s":""}},
"ingress": [{
"fromEndpoints": [
{"matchLabels":{"%s":""}}
]
}, {
"fromCIDR": [
"%s"
]
}]
},
{
"endpointSelector": {"matchLabels":{"%s":""}},
"egress": [{
"toEndpoints": [
{"matchLabels":{"%[1]s":""}}
]
}]
}]`, httpd1Label, httpd2Label, ipv4OtherNet, app3Label)
_, err = vm.PolicyRenderAndImport(policy)
Expect(err).To(BeNil(), "Unable to import policy: %s", err)
By("Pinging httpd1 IPv4 from app3 (should NOT work because we only allow traffic from %q to %q)", httpd2Label, httpd1Label)
res = vm.ContainerExec(helpers.App3, helpers.Ping(helpers.Httpd1))
res.ExpectFail("Unexpected success pinging %s IPv4 from %s", helpers.Httpd1, helpers.App3)
By("Pinging httpd1 IPv6 from app3 (should NOT work because we only allow traffic from %q to %q)", httpd2Label, httpd1Label)
res = vm.ContainerExec(helpers.App3, helpers.Ping6(helpers.Httpd1))
res.ExpectFail("Unexpected success pinging %s IPv6 from %s", helpers.Httpd1, helpers.App3)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
By("Testing CIDR Exceptions in Cilium Policy")
By("Importing Policy Allowing Ingress From %q --> %q And From CIDRs %q Except %q", helpers.Httpd2, helpers.Httpd1, worldPrefix, worldIP)
policy = fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%s":""}},
"ingress": [{
"fromEndpoints": [
{"matchLabels":{"%s":""}}
]
}, {
"fromCIDRSet": [ {
"cidr": "%s",
"except": [
"%s"
]
}
]
}]
}]`, httpd1Label, httpd2Label, worldPrefix, fmt.Sprintf("%s/32", worldIP))
_, err = vm.PolicyRenderAndImport(policy)
Expect(err).To(BeNil(), "Unable to import policy: %s", err)
By(fmt.Sprintf("Pinging httpd1 IPV4 from world container (should not work because the IP %s falls within the CIDR exception range)", worldIP))
res = vm.ContainerExec(helpers.WorldHttpd1, helpers.Ping(httpd1DockerNetworking[helpers.IPv4]))
res.ExpectFail("Unexpected success pinging %s IPv4 from %s", httpd1DockerNetworking[helpers.IPv4], helpers.WorldHttpd1)
})
})
It("Extended HTTP Methods tests", func() {
// This also tests L3-dependent L7.
httpMethods := []string{"GET", "POST"}
TestMethodPolicy := func(method string) {
vm.PolicyDelAll().ExpectSuccess("Cannot delete all policies")
policy := `
[{
"endpointSelector": {"matchLabels": {"id.httpd1": ""}},
"ingress": [{
"fromEndpoints": [{"matchLabels": {"id.app1": ""}}],
"toPorts": [{
"ports": [{"port": "80", "protocol": "tcp"}],
"rules": {
"HTTP": [{
"method": "%[1]s",
"path": "/public"
}]
}
}]
}]
},{
"endpointSelector": {"matchLabels": {"id.httpd1": ""}},
"ingress": [{
"fromEndpoints": [{"matchLabels": {"id.app2": ""}}],
"toPorts": [{
"ports": [{"port": "80", "protocol": "tcp"}],
"rules": {
"HTTP": [{
"method": "%[1]s",
"path": "/public",
"headers": ["X-Test: True"]
}]
}
}]
}]
}]`
_, err := vm.PolicyRenderAndImport(fmt.Sprintf(policy, method))
Expect(err).To(BeNil(), "Cannot import policy for %q", method)
srvIP, err := vm.ContainerInspectNet(helpers.Httpd1)
Expect(err).Should(BeNil(), "could not get container %q meta", helpers.Httpd1)
dest := helpers.CurlFail("http://%s/public -X %s", srvIP[helpers.IPv4], method)
destHeader := helpers.CurlFail("http://%s/public -H 'X-Test: True' -X %s",
srvIP[helpers.IPv4], method)
vm.ContainerExec(helpers.App1, dest).ExpectSuccess(
"%q cannot http request to Public", helpers.App1)
vm.ContainerExec(helpers.App2, dest).ExpectFail(
"%q can http request to Public", helpers.App2)
vm.ContainerExec(helpers.App2, destHeader).ExpectSuccess(
"%q cannot http request to Public", helpers.App2)
vm.ContainerExec(helpers.App1, destHeader).ExpectSuccess(
"%q can http request to Public", helpers.App1)
vm.ContainerExec(helpers.App3, destHeader).ExpectFail(
"%q can http request to Public", helpers.App3)
vm.ContainerExec(helpers.App3, dest).ExpectFail(
"%q can http request to Public", helpers.App3)
}
for _, method := range httpMethods {
By("Testing method %q", method)
TestMethodPolicy(method)
}
})
It("Tests Egress To World", func() {
googleDNS := "8.8.8.8"
googleHTTP := "google.com"
numberOfTries := 5
maxNumberOffFailures := 1
checkEgressToWorld := func() {
res := vm.ContainerExec(helpers.App1, helpers.Ping(helpers.App2))
ExpectWithOffset(2, res).ShouldNot(helpers.CMDSuccess(),
"unexpectedly able to ping %q", helpers.App2)
By("Testing egress access to the world from %s", helpers.App1)
pingFailures := 0
curlFailures := 0
for i := 0; i < numberOfTries; i++ {
res := vm.ContainerExec(helpers.App1, helpers.Ping(googleDNS))
if !res.WasSuccessful() {
pingFailures++
}
res = vm.ContainerExec(helpers.App1, helpers.CurlFail("-4 http://%s", googleHTTP))
if !res.WasSuccessful() {
curlFailures++
}
// If no failures, let's skip the next ones.
if (curlFailures + pingFailures) == 0 {
break
}
}
ExpectWithOffset(2, pingFailures).To(BeNumerically("<=", maxNumberOffFailures),
"%d of %d pings to %q failed", pingFailures, maxNumberOffFailures, googleDNS)
ExpectWithOffset(2, curlFailures).To(BeNumerically("<=", maxNumberOffFailures),
"%d of %d HTTP request to %q failed",
pingFailures, maxNumberOffFailures, googleHTTP)
}
setupPolicyAndTestEgressToWorld := func(policy string) {
_, err := vm.PolicyRenderAndImport(policy)
ExpectWithOffset(1, err).To(BeNil(), "Unable to import policy: %s\n%s", err, policy)
areEndpointsReady := vm.WaitEndpointsReady()
ExpectWithOffset(1, areEndpointsReady).Should(BeTrue(), "Endpoints are not ready after timeout")
checkEgressToWorld()
}
// Set policy enforcement to default deny so that we can do negative tests
// before importing policy
ExpectPolicyEnforcementUpdated(vm, helpers.PolicyEnforcementAlways)
failedPing := vm.ContainerExec(helpers.App1, helpers.Ping(googleDNS))
failedPing.ExpectFail("unexpectedly able to ping %s", googleDNS)
By("testing basic egress to world")
app1Label := fmt.Sprintf("id.%s", helpers.App1)
policy := fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%s":""}},
"egress": [{
"toEntities": [
"%s"
]
}]
}]`, app1Label, api.EntityWorld)
setupPolicyAndTestEgressToWorld(policy)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
By("testing egress to world with all entity")
policy = fmt.Sprintf(`
[{
"endpointSelector": {"matchLabels":{"%s":""}},
"egress": [{
"toEntities": [
"%s"
]
}]
}]`, app1Label, api.EntityAll)
setupPolicyAndTestEgressToWorld(policy)
vm.PolicyDelAll().ExpectSuccess("Unable to delete all policies")
By("testing basic egress to 0.0.0.0/0")
policy = fmt.Sprintf(`