-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathup_test.go
802 lines (609 loc) · 33.8 KB
/
up_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package main_test
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/localip"
"code.cloudfoundry.org/winc/network/netinterface"
"code.cloudfoundry.org/winc/network/netrules"
"golang.org/x/sys/windows"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
var _ = Describe("Up", func() {
var (
bundleSpec specs.Spec
n netinterface.NetInterface
localIp string
)
BeforeEach(func() {
bundleSpec = helpers.GenerateRuntimeSpec(helpers.CreateVolume(rootfsURI, containerId))
n = netinterface.NetInterface{}
var err error
localIp, err = localip.LocalIP()
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
failed = failed || CurrentSpecReport().Failed()
})
Context("default network config", func() {
BeforeEach(func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
networkConfig = helpers.GenerateNetworkConfig()
helpers.CreateNetwork(networkConfig, networkConfigFile)
})
AfterEach(func() {
deleteContainerAndNetwork(containerId, networkConfig)
})
It("sets the host MTU in the container", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {} ,"netin": []}`, networkConfigFile)
containerMtu, err := n.GetMTU(fmt.Sprintf("vEthernet (%s)", containerId), windows.AF_INET)
Expect(err).ToNot(HaveOccurred())
hostAdapter, err := n.ByIP(localIp)
Expect(err).ToNot(HaveOccurred())
hostMtu, err := n.GetMTU(hostAdapter.Name, windows.AF_INET)
Expect(err).ToNot(HaveOccurred())
Expect(containerMtu).To(Equal(hostMtu))
})
Context("stdin contains a net in rule", func() {
var (
hostPort1 uint32
hostPort2 uint32
containerPort1 uint32
containerPort2 uint32
client http.Client
)
BeforeEach(func() {
hostPort1 = 0
hostPort2 = uint32(randomPort())
containerPort1 = 12345
containerPort2 = 9876
client = *http.DefaultClient
client.Timeout = 5 * time.Second
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "server.exe"), serverBin)
_, _, err := helpers.ExecInContainer(containerId, []string{"c:\\server.exe", strconv.Itoa(int(containerPort1))}, true)
Expect(err).NotTo(HaveOccurred())
_, _, err = helpers.ExecInContainer(containerId, []string{"c:\\server.exe", strconv.Itoa(int(containerPort2))}, true)
Expect(err).NotTo(HaveOccurred())
})
It("generates the correct port mappings and binds them to the container", func() {
outputs := helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %d},{"host_port": %d, "container_port": %d}]}`, hostPort1, containerPort1, hostPort2, containerPort2), networkConfigFile)
mappedPorts := []netrules.PortMapping{}
Expect(json.Unmarshal([]byte(outputs.Properties.MappedPorts), &mappedPorts)).To(Succeed())
Expect(len(mappedPorts)).To(Equal(2))
Expect(mappedPorts[0].ContainerPort).To(Equal(containerPort1))
Expect(mappedPorts[0].HostPort).NotTo(Equal(hostPort1))
Expect(mappedPorts[1].ContainerPort).To(Equal(containerPort2))
Expect(mappedPorts[1].HostPort).To(Equal(hostPort2))
hostPort1 = mappedPorts[0].HostPort
hostIP, err := localip.LocalIP()
Expect(err).NotTo(HaveOccurred())
address := fmt.Sprintf("http://%s:%d", hostIP, hostPort1)
var resp http.Response
Eventually(httpGetInto(address, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %d", containerPort1)))
address = fmt.Sprintf("http://%s:%d", hostIP, hostPort2)
Eventually(httpGetInto(address, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %d", containerPort2)))
})
It("can hit a port on the container directly", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %d},{"host_port": %d, "container_port": %d}]}`, hostPort1, containerPort1, hostPort2, containerPort2), networkConfigFile)
address := fmt.Sprintf("http://%s:%d", getContainerIp(containerId), containerPort1)
var resp http.Response
Eventually(httpGetInto(address, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %d", containerPort1)))
address = fmt.Sprintf("http://%s:%d", getContainerIp(containerId), containerPort2)
Eventually(httpGetInto(address, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %d", containerPort2)))
})
It("creates the correct urlacl in the container", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {"ports": "8080"} ,"netin": [{"host_port": 0, "container_port": 1234}]}`, networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"cmd.exe", "/C", "netsh http show urlacl url=http://*:8080/"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(MatchRegexp(`Reserved URL[ ]*: http://\*:8080/.*`))
})
Context("stdin does not contain a port mapping request", func() {
It("cannot listen on any ports", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {"ports": ""} }`, networkConfigFile)
_, err := client.Get(fmt.Sprintf("http://%s:%d", getContainerIp(containerId), containerPort1))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(`Get "http://%s:%d":`, getContainerIp(containerId), containerPort1)))
Expect(err.Error()).To(ContainSubstring(`(Client.Timeout exceeded while awaiting headers)`))
_, err = client.Get(fmt.Sprintf("http://%s:%d", getContainerIp(containerId), containerPort2))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(`Get "http://%s:%d":`, getContainerIp(containerId), containerPort2)))
Expect(err.Error()).To(ContainSubstring(`(Client.Timeout exceeded while awaiting headers)`))
})
It("prints an empty list of mapped ports", func() {
outputs := helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {} }`, networkConfigFile)
Expect(outputs.Properties.MappedPorts).To(Equal("[]"))
Expect(outputs.Properties.DeprecatedHostIP).To(Equal("255.255.255.255"))
_, network, err := net.ParseCIDR(networkConfig.SubnetRange)
Expect(err).NotTo(HaveOccurred())
ip := net.ParseIP(outputs.Properties.ContainerIP)
Expect(ip).NotTo(BeNil())
Expect(network.Contains(ip)).To(BeTrue())
})
})
})
Context("stdin does not contain net out rules", func() {
BeforeEach(func() {
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "netout.exe"), netoutBin)
})
It("cannot resolve DNS", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {}}`, networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "dns", "--addr", "www.google.com"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("lookup www.google.com: no such host"))
})
It("cannot connect to a remote host over TCP", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {}}`, networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
errStr := "dial tcp 8.8.8.8:53: connectex: An attempt was made to access a socket in a way forbidden by its access permissions."
Expect(strings.TrimSpace(stdout.String())).To(Equal(errStr))
})
It("cannot connect to a remote host over UDP", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {}}`, networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("failed to exchange: read udp"))
Expect(stdout.String()).To(ContainSubstring("8.8.8.8:53: i/o timeout"))
})
It("cannot connect to a remote host over ICMP", func() {
if windowsBuild == 16299 {
Skip("ping.exe elevates to admin, breaking this test on 1709")
}
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {}}`, networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.8.8"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.8.8"))
Expect(stdout.String()).To(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
})
})
Context("stdin contains net out rules", func() {
var (
netOutRules []byte
netOutRule netrules.NetOut
)
BeforeEach(func() {
netOutRule = netrules.NetOut{
Networks: []netrules.IPRange{
{Start: net.ParseIP("8.8.5.5"), End: net.ParseIP("9.0.0.0")},
},
Ports: []netrules.PortRange{{Start: 40, End: 60}},
}
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "netout.exe"), netoutBin)
})
Context("netout allows udp", func() {
BeforeEach(func() {
var err error
netOutRule.Protocol = netrules.ProtocolUDP
netOutRules, err = json.Marshal([]netrules.NetOut{netOutRule})
Expect(err).NotTo(HaveOccurred())
})
It("can connect to a remote host over UDP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("recieved response to DNS query from 8.8.8.8:53 over UDP"))
})
It("cannot connect to a remote host over TCP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
errStr := "dial tcp 8.8.8.8:53: connectex: An attempt was made to access a socket in a way forbidden by its access permissions."
Expect(strings.TrimSpace(stdout.String())).To(Equal(errStr))
})
It("cannot connect to a remote host over ICMP", func() {
if windowsBuild == 16299 {
Skip("ping.exe elevates to admin, breaking this test on 1709")
}
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.8.8"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.8.8"))
Expect(stdout.String()).To(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
})
It("cannot connect to a remote host over UDP prohibited by netout", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.4.4", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("failed to exchange: read udp"))
Expect(stdout.String()).To(ContainSubstring("8.8.4.4:53: i/o timeout"))
})
Context("netout allows udp on port 53", func() {
BeforeEach(func() {
var err error
netOutRule.Networks = []netrules.IPRange{
{Start: net.ParseIP("0.0.0.0"), End: net.ParseIP("255.255.255.255")},
}
netOutRule.Ports = []netrules.PortRange{{Start: 53, End: 53}}
netOutRules, err = json.Marshal([]netrules.NetOut{netOutRule})
Expect(err).NotTo(HaveOccurred())
})
It("can resolve DNS", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "dns", "--addr", "www.google.com"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("found addr"))
})
})
})
Context("netout allows tcp", func() {
BeforeEach(func() {
var err error
netOutRule.Protocol = netrules.ProtocolTCP
netOutRules, err = json.Marshal([]netrules.NetOut{netOutRule})
Expect(err).NotTo(HaveOccurred())
})
It("can connect to a remote host over TCP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(strings.TrimSpace(stdout.String())).To(Equal("connected to 8.8.8.8:53 over tcp"))
})
It("cannot connect to a remote host over UDP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("failed to exchange: read udp"))
Expect(stdout.String()).To(ContainSubstring("8.8.8.8:53: i/o timeout"))
})
It("cannot connect to a remote host over ICMP", func() {
if windowsBuild == 16299 {
Skip("ping.exe elevates to admin, breaking this test on 1709")
}
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.8.8"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.8.8"))
Expect(stdout.String()).To(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
})
It("cannot connect to a remote server over TCP prohibited by netout", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.4.4", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
errStr := "dial tcp 8.8.4.4:53: connectex: An attempt was made to access a socket in a way forbidden by its access permissions."
Expect(strings.TrimSpace(stdout.String())).To(Equal(errStr))
})
})
Context("netout allows icmp", func() {
BeforeEach(func() {
var err error
netOutRule.Protocol = netrules.ProtocolICMP
netOutRules, err = json.Marshal([]netrules.NetOut{netOutRule})
Expect(err).NotTo(HaveOccurred())
})
It("can connect to a remote host over ICMP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.8.8"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.8.8"))
Expect(stdout.String()).NotTo(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
Expect(stdout.String()).To(ContainSubstring("Packets: Sent = 4, Received ="))
})
It("cannot connect to a remote host over TCP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
errStr := "dial tcp 8.8.8.8:53: connectex: An attempt was made to access a socket in a way forbidden by its access permissions."
Expect(strings.TrimSpace(stdout.String())).To(Equal(errStr))
})
It("cannot connect to a remote host over UDP", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("failed to exchange: read udp"))
Expect(stdout.String()).To(ContainSubstring("8.8.8.8:53: i/o timeout"))
})
It("cannot connect to a remote host over ICMP prohibited by netout", func() {
if windowsBuild == 16299 {
Skip("ping.exe elevates to admin, breaking this test on 1709")
}
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.4.4"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.4.4"))
Expect(stdout.String()).To(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
})
})
Context("netout allows all", func() {
BeforeEach(func() {
var err error
netOutRule.Protocol = netrules.ProtocolAll
netOutRules, err = json.Marshal([]netrules.NetOut{netOutRule})
Expect(err).NotTo(HaveOccurred())
})
It("allows access over all protocols to valid remote hosts", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("recieved response to DNS query from 8.8.8.8:53 over UDP"))
stdout, _, err = helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(strings.TrimSpace(stdout.String())).To(Equal("connected to 8.8.8.8:53 over tcp"))
stdout, _, err = helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.8.8"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.8.8"))
Expect(stdout.String()).To(ContainSubstring("Reply from 8.8.8.8: bytes=32"))
Expect(stdout.String()).NotTo(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
})
It("blocks access over all protocols to prohibited remote hosts", func() {
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(netOutRules)), networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "udp", "--addr", "8.8.4.4", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("failed to exchange: read udp"))
Expect(stdout.String()).To(ContainSubstring("8.8.4.4:53: i/o timeout"))
stdout, _, err = helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.4.4", "--port", "53"}, false)
Expect(err).To(HaveOccurred())
errStr := "dial tcp 8.8.4.4:53: connectex: An attempt was made to access a socket in a way forbidden by its access permissions."
Expect(strings.TrimSpace(stdout.String())).To(Equal(errStr))
if windowsBuild != 16299 {
// this test works on 1803
stdout, _, err = helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "icmp", "--addr", "8.8.4.4"}, false)
Expect(err).To(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Ping statistics for 8.8.4.4"))
Expect(stdout.String()).To(ContainSubstring("Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)"))
}
})
})
})
})
Context("custom MTU", func() {
BeforeEach(func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
networkConfig = helpers.GenerateNetworkConfig()
networkConfig.MTU = 1405
helpers.CreateNetwork(networkConfig, networkConfigFile)
})
AfterEach(func() {
deleteContainerAndNetwork(containerId, networkConfig)
})
It("sets the network MTU on the internal container NIC", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {} ,"netin": []}`, networkConfigFile)
containerMtu, err := n.GetMTU(fmt.Sprintf("vEthernet (%s)", containerId), windows.AF_INET)
Expect(err).ToNot(HaveOccurred())
Expect(containerMtu).To(Equal(uint32(1405)))
})
})
Context("custom DNS Servers", func() {
BeforeEach(func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
networkConfig = helpers.GenerateNetworkConfig()
networkConfig.DNSServers = []string{"8.8.8.8", "8.8.4.4"}
helpers.CreateNetwork(networkConfig, networkConfigFile)
})
AfterEach(func() {
deleteContainerAndNetwork(containerId, networkConfig)
})
It("uses those IP addresses as DNS servers", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {} ,"netin": []}`, networkConfigFile)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"powershell.exe", "-Command", `(Get-DnsClientServerAddress -InterfaceAlias 'vEthernet*' -AddressFamily IPv4).ServerAddresses -join ","`}, false)
Expect(err).NotTo(HaveOccurred())
Expect(strings.TrimSpace(stdout.String())).To(Equal("8.8.8.8,8.8.4.4"))
})
It("allows traffic to those servers", func() {
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {} ,"netin": []}`, networkConfigFile)
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "netout.exe"), netoutBin)
stdout, _, err := helpers.ExecInContainer(containerId, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "8.8.8.8", "--port", "53"}, false)
Expect(err).NotTo(HaveOccurred())
Expect(strings.TrimSpace(stdout.String())).To(Equal("connected to 8.8.8.8:53 over tcp"))
})
})
Context("two containers are running", func() {
var (
bundlePath2 string
bundleSpec2 specs.Spec
containerId2 string
containerPort string
hostIP string
client http.Client
)
BeforeEach(func() {
var err error
bundlePath2, err = os.MkdirTemp("", "winccontainer-2")
Expect(err).NotTo(HaveOccurred())
containerId2 = filepath.Base(bundlePath2)
bundleSpec2 = helpers.GenerateRuntimeSpec(helpers.CreateVolume(rootfsURI, containerId2))
containerPort = "12345"
hostIP, err = localip.LocalIP()
Expect(err).NotTo(HaveOccurred())
client = *http.DefaultClient
client.Timeout = 5 * time.Second
networkConfig = helpers.GenerateNetworkConfig()
helpers.CreateNetwork(networkConfig, networkConfigFile)
})
AfterEach(func() {
helpers.NetworkDown(containerId2, networkConfigFile)
helpers.DeleteContainer(containerId2)
helpers.DeleteVolume(containerId2)
deleteContainerAndNetwork(containerId, networkConfig)
Expect(os.RemoveAll(bundlePath)).To(Succeed())
Expect(os.RemoveAll(bundlePath2)).To(Succeed())
})
It("does not allow traffic between containers", func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
outputs := helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %s}]}`, 0, containerPort), networkConfigFile)
hostIp := outputs.Properties.ContainerIP
Expect(helpers.ContainerExists(containerId)).To(BeTrue())
hostPort := findExternalPort(outputs.Properties.MappedPorts, containerPort)
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "server.exe"), serverBin)
_, _, err := helpers.ExecInContainer(containerId, []string{"c:\\server.exe", containerPort}, true)
Expect(err).NotTo(HaveOccurred())
helpers.RunContainer(bundleSpec2, bundlePath2, containerId2)
helpers.NetworkUp(containerId2, `{"Pid": 123, "Properties": {}}`, networkConfigFile)
pid = helpers.GetContainerState(containerId2).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "netout.exe"), netoutBin)
stdOut, _, err := helpers.ExecInContainer(containerId2, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", hostIp, "--port", strconv.Itoa(hostPort)}, false)
Expect(err).To(HaveOccurred())
Expect(stdOut.String()).To(ContainSubstring("An attempt was made to access a socket in a way forbidden by its access permissions"))
})
It("can route traffic to the remaining container after the other is deleted", func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
outputs := helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %s}]}`, 0, containerPort), networkConfigFile)
mappedPorts := []netrules.PortMapping{}
Expect(json.Unmarshal([]byte(outputs.Properties.MappedPorts), &mappedPorts)).To(Succeed())
Expect(len(mappedPorts)).To(Equal(1))
hostPort1 := mappedPorts[0].HostPort
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "server.exe"), serverBin)
_, _, err := helpers.ExecInContainer(containerId, []string{"c:\\server.exe", containerPort}, true)
Expect(err).NotTo(HaveOccurred())
address := fmt.Sprintf("http://%s:%d", hostIP, hostPort1)
var resp http.Response
Eventually(httpGetInto(address, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %s", containerPort)))
helpers.RunContainer(bundleSpec2, bundlePath2, containerId2)
outputs = helpers.NetworkUp(containerId2, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %s}]}`, 0, containerPort), networkConfigFile)
Expect(json.Unmarshal([]byte(outputs.Properties.MappedPorts), &mappedPorts)).To(Succeed())
Expect(len(mappedPorts)).To(Equal(1))
hostPort2 := mappedPorts[0].HostPort
Expect(hostPort2).NotTo(Equal(hostPort1))
pid = helpers.GetContainerState(containerId2).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "server.exe"), serverBin)
_, _, err = helpers.ExecInContainer(containerId2, []string{"c:\\server.exe", containerPort}, true)
Expect(err).NotTo(HaveOccurred())
helpers.DeleteContainer(containerId)
helpers.NetworkDown(containerId, networkConfigFile)
helpers.DeleteVolume(containerId)
address = fmt.Sprintf("http://%s:%d", hostIP, hostPort2)
Eventually(httpGetInto(address, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %s", containerPort)))
})
Context("the max outgoing bandwidth is set in the config file", func() {
var (
serverURL string
clientNetOutRules []byte
)
const (
tinyBandwidth = 1024 * 1024
giantBandwidth = 10 * 1024 * 1024
fileSize = 10 * 1024 * 1024
)
BeforeEach(func() {
var err error
helpers.RunContainer(bundleSpec, bundlePath, containerId)
outputs := helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %s}]}`, 0, containerPort), networkConfigFile)
hostIp, err := localip.LocalIP()
Expect(err).NotTo(HaveOccurred())
Expect(helpers.ContainerExists(containerId)).To(BeTrue())
hostPort := findExternalPort(outputs.Properties.MappedPorts, containerPort)
serverURL = fmt.Sprintf("http://%s:%d", hostIp, hostPort)
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "server.exe"), serverBin)
_, _, err = helpers.ExecInContainer(containerId, []string{"c:\\server.exe", containerPort}, true)
Expect(err).NotTo(HaveOccurred())
netOutRule := netrules.NetOut{
Protocol: netrules.ProtocolAll,
Networks: []netrules.IPRange{
{Start: net.ParseIP(hostIp), End: net.ParseIP(hostIp)},
},
Ports: []netrules.PortRange{{Start: uint16(hostPort), End: uint16(hostPort)}},
}
clientNetOutRules, err = json.Marshal([]netrules.NetOut{netOutRule})
Expect(err).NotTo(HaveOccurred())
var resp http.Response
Eventually(httpGetInto(serverURL, &resp), "30s").Should(Succeed())
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(Equal(fmt.Sprintf("Response from server on port %s", containerPort)))
})
It("applies the bandwidth limit on the container to outgoing traffic", func() {
helpers.RunContainer(bundleSpec2, bundlePath2, containerId2)
pid := helpers.GetContainerState(containerId2).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "client.exe"), clientBin)
networkConfig.MaximumOutgoingBandwidth = tinyBandwidth
helpers.WriteNetworkConfig(networkConfig, networkConfigFile)
helpers.NetworkUp(containerId2, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(clientNetOutRules)), networkConfigFile)
tinyTime := uploadFile(containerId2, fileSize, serverURL)
helpers.NetworkDown(containerId2, networkConfigFile)
networkConfig.MaximumOutgoingBandwidth = giantBandwidth
helpers.WriteNetworkConfig(networkConfig, networkConfigFile)
helpers.NetworkUp(containerId2, fmt.Sprintf(`{"Pid": 123, "Properties": {}, "netout_rules": %s}`, string(clientNetOutRules)), networkConfigFile)
giantTime := uploadFile(containerId2, fileSize, serverURL)
Expect(tinyTime).To(BeNumerically(">", giantTime*7))
})
})
Context("when the containers share a network namespace", func() {
BeforeEach(func() {
bundleSpec2.Windows.Network = &specs.WindowsNetwork{NetworkSharedContainerName: containerId}
containerPort = "23456"
})
It("allows traffic between the containers", func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
helpers.NetworkUp(containerId, fmt.Sprintf(`{"Pid": 123, "Properties": {} ,"netin": [{"host_port": %d, "container_port": %s}]}`, 0, containerPort), networkConfigFile)
pid := helpers.GetContainerState(containerId).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "server.exe"), serverBin)
_, _, err := helpers.ExecInContainer(containerId, []string{"c:\\server.exe", containerPort}, true)
Expect(err).NotTo(HaveOccurred())
helpers.RunContainer(bundleSpec2, bundlePath2, containerId2)
helpers.NetworkUp(containerId2, `{"Pid": 123, "Properties": {}}`, networkConfigFile)
pid = helpers.GetContainerState(containerId2).Pid
helpers.CopyFile(filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root", "netout.exe"), netoutBin)
stdOut, _, err := helpers.ExecInContainer(containerId2, []string{"c:\\netout.exe", "--protocol", "tcp", "--addr", "127.0.0.1", "--port", containerPort}, false)
Expect(err).NotTo(HaveOccurred())
Expect(stdOut.String()).To(Equal(fmt.Sprintf("connected to 127.0.0.1:%s over tcp", containerPort)))
})
Context("when deleting the first container", func() {
BeforeEach(func() {
helpers.RunContainer(bundleSpec, bundlePath, containerId)
helpers.NetworkUp(containerId, `{"Pid": 123, "Properties": {} }`, networkConfigFile)
helpers.RunContainer(bundleSpec2, bundlePath2, containerId2)
})
It("deletes the main container and the pea container", func() {
Expect(helpers.ContainerExists(containerId2)).To(BeTrue())
helpers.DeleteContainer(containerId)
Expect(helpers.ContainerExists(containerId)).To(BeFalse())
Expect(helpers.ContainerExists(containerId2)).To(BeFalse())
})
It("does not delete the bundle directories", func() {
helpers.DeleteContainer(containerId)
Expect(bundlePath).To(BeADirectory())
Expect(bundlePath2).To(BeADirectory())
})
It("unmounts sandbox.vhdx", func() {
pid := helpers.GetContainerState(containerId2).Pid
helpers.DeleteContainer(containerId)
rootPath := filepath.Join("c:\\", "proc", strconv.Itoa(pid), "root")
Expect(rootPath).NotTo(BeADirectory())
// if not cleanly unmounted, the mount point is left as a symlink
_, err := os.Lstat(rootPath)
Expect(err).NotTo(BeNil())
})
})
})
})
})