File: port_forwarding_test.go

package info (click to toggle)
golang-github-containers-gvisor-tap-vsocks 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 800 kB
  • sloc: sh: 95; makefile: 59
file content (269 lines) | stat: -rw-r--r-- 9,551 bytes parent folder | download
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
package e2e

import (
	"context"
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"time"

	gvproxyclient "github.com/containers/gvisor-tap-vsock/pkg/client"
	"github.com/containers/gvisor-tap-vsock/pkg/transport"
	"github.com/containers/gvisor-tap-vsock/pkg/types"
	"github.com/onsi/ginkgo"
	"github.com/onsi/gomega"
	log "github.com/sirupsen/logrus"
)

var _ = ginkgo.Describe("port forwarding", func() {
	client := gvproxyclient.New(&http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", sock)
			},
		},
	}, "http://base")

	ginkgo.It("should reach a http server on the host", func() {
		ln, err := net.Listen("tcp", "127.0.0.1:9090")
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
		defer ln.Close()

		mux := http.NewServeMux()
		mux.HandleFunc("/", func(writer http.ResponseWriter, _ *http.Request) {
			_, _ = writer.Write([]byte("Hello from the host"))
		})
		go func() {
			s := &http.Server{
				Handler:      mux,
				ReadTimeout:  10 * time.Second,
				WriteTimeout: 10 * time.Second,
			}
			err := s.Serve(ln)
			if err != nil {
				log.Error(err)
			}
		}()

		out, err := sshExec("curl http://host.containers.internal:9090")
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
		gomega.Expect(string(out)).To(gomega.ContainSubstring("Hello from the host"))

		out, err = sshExec("curl http://host.docker.internal:9090")
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
		gomega.Expect(string(out)).To(gomega.ContainSubstring("Hello from the host"))
	})

	ginkgo.It("should reach a http server in the VM using dynamic port forwarding", func() {
		_, err := net.Dial("tcp", "127.0.0.1:9090")
		gomega.Expect(err).Should(gomega.HaveOccurred())
		gomega.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))

		gomega.Expect(client.Expose(&types.ExposeRequest{
			Local:  "127.0.0.1:9090",
			Remote: "192.168.127.2:8080",
		})).Should(gomega.Succeed())

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := http.Get("http://127.0.0.1:9090")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
		}).Should(gomega.Succeed())

		gomega.Expect(client.Unexpose(&types.UnexposeRequest{
			Local: "127.0.0.1:9090",
		})).Should(gomega.Succeed())

		gomega.Eventually(func(g gomega.Gomega) {
			_, err = net.Dial("tcp", "127.0.0.1:9090")
			g.Expect(err).Should(gomega.HaveOccurred())
			g.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))
		}).Should(gomega.Succeed())
	})

	ginkgo.It("should reach a dns server in the VM using dynamic port forwarding", func() {
		gomega.Expect(client.Expose(&types.ExposeRequest{
			Local:    ":1053",
			Remote:   "192.168.127.2:53",
			Protocol: "udp",
		})).Should(gomega.Succeed())

		gomega.Eventually(func(g gomega.Gomega) {
			cmd := exec.Command("nslookup", "-timeout=1", "-port=1053", "foobar", "127.0.0.1")
			out, err := cmd.CombinedOutput()
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(string(out)).To(gomega.ContainSubstring("Address: 1.2.3.4"))
		}).Should(gomega.Succeed())

		gomega.Expect(client.Unexpose(&types.UnexposeRequest{
			Local:    ":1053",
			Protocol: "udp",
		})).Should(gomega.Succeed())
	})

	ginkgo.It("should reach a http server in the VM using the tunneling of the daemon", func() {
		httpClient := &http.Client{
			Transport: &http.Transport{
				DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
					conn, err := net.Dial("unix", sock)
					if err != nil {
						return nil, err
					}
					return conn, transport.Tunnel(conn, "192.168.127.2", 8080)
				},
			},
		}

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := httpClient.Get("http://placeholder/")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
		}).Should(gomega.Succeed())
	})

	ginkgo.It("should reach a http server in the VM using dynamic port forwarding configured within the VM", func() {
		_, err := net.Dial("tcp", "127.0.0.1:9090")
		gomega.Expect(err).Should(gomega.HaveOccurred())
		gomega.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))

		_, err = sshExec(`curl http://gateway.containers.internal/services/forwarder/expose -X POST -d'{"local":":9090", "remote":":8080"}'`)
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := http.Get("http://127.0.0.1:9090")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
		}).Should(gomega.Succeed())

		_, err = sshExec(`curl http://gateway.containers.internal/services/forwarder/unexpose -X POST -d'{"local":":9090"}'`)
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

		gomega.Eventually(func(g gomega.Gomega) {
			_, err = net.Dial("tcp", "127.0.0.1:9090")
			g.Expect(err).Should(gomega.HaveOccurred())
			g.Expect(err.Error()).To(gomega.HaveSuffix("connection refused"))
		}).Should(gomega.Succeed())
	})

	ginkgo.It("should reach rootless podman API using unix socket forwarding over ssh", func() {
		httpClient := &http.Client{
			Transport: &http.Transport{
				DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
					return net.Dial("unix", forwardSock)
				},
			},
		}

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := httpClient.Get("http://host/_ping")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
			g.Expect(resp.ContentLength).To(gomega.Equal(int64(2)))

			reply := make([]byte, resp.ContentLength)
			_, err = io.ReadAtLeast(resp.Body, reply, len(reply))

			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(string(reply)).To(gomega.Equal("OK"))
		}).Should(gomega.Succeed())
	})

	ginkgo.It("should reach rootful podman API using unix socket forwarding over ssh", func() {
		httpClient := &http.Client{
			Transport: &http.Transport{
				DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
					return net.Dial("unix", forwardRootSock)
				},
			},
		}

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := httpClient.Get("http://host/_ping")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
			g.Expect(resp.ContentLength).To(gomega.Equal(int64(2)))

			reply := make([]byte, resp.ContentLength)
			_, err = io.ReadAtLeast(resp.Body, reply, len(reply))

			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(string(reply)).To(gomega.Equal("OK"))
		}).Should(gomega.Succeed())
	})

	ginkgo.It("should expose and reach an http service using unix to tcp forwarding", func() {
		if runtime.GOOS == "windows" {
			ginkgo.Skip("AF_UNIX not supported on Windows")
		}

		unix2tcpfwdsock, _ := filepath.Abs(filepath.Join(tmpDir, "podman-unix-to-unix-forwarding.sock"))

		out, err := sshExec(`curl http://gateway.containers.internal/services/forwarder/expose -X POST -d'{"protocol":"unix","local":"` + unix2tcpfwdsock + `","remote":"tcp://192.168.127.2:8080"}'`)
		gomega.Expect(string(out)).Should(gomega.Equal(""))
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

		gomega.Eventually(func(g gomega.Gomega) {
			sockfile, err := os.Stat(unix2tcpfwdsock)
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(sockfile.Mode().Type().String()).To(gomega.Equal(os.ModeSocket.String()))
		}).Should(gomega.Succeed())

		httpClient := &http.Client{
			Transport: &http.Transport{
				DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
					return net.Dial("unix", unix2tcpfwdsock)
				},
			},
		}

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := httpClient.Get("http://placeholder/")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
		}).Should(gomega.Succeed())
	})

	ginkgo.It("should expose and reach rootless podman API using unix to unix forwarding over ssh", func() {
		if runtime.GOOS == "windows" {
			ginkgo.Skip("AF_UNIX not supported on Windows")
		}

		unix2unixfwdsock, _ := filepath.Abs(filepath.Join(tmpDir, "podman-unix-to-unix-forwarding.sock"))

		remoteuri := fmt.Sprintf(`ssh-tunnel://root@%s:%d%s?key=%s`, "192.168.127.2", 22, podmanSock, privateKeyFile)
		_, err := sshExec(`curl http://192.168.127.1/services/forwarder/expose -X POST -d'{"protocol":"unix","local":"` + unix2unixfwdsock + `","remote":"` + remoteuri + `"}'`)
		gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

		gomega.Eventually(func(g gomega.Gomega) {
			sockfile, err := os.Stat(unix2unixfwdsock)
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(sockfile.Mode().Type().String()).To(gomega.Equal(os.ModeSocket.String()))
		}).Should(gomega.Succeed())

		httpClient := &http.Client{
			Transport: &http.Transport{
				DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
					return net.Dial("unix", unix2unixfwdsock)
				},
			},
		}

		gomega.Eventually(func(g gomega.Gomega) {
			resp, err := httpClient.Get("http://host/_ping")
			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
			g.Expect(resp.ContentLength).To(gomega.Equal(int64(2)))

			reply := make([]byte, resp.ContentLength)
			_, err = io.ReadAtLeast(resp.Body, reply, len(reply))

			g.Expect(err).ShouldNot(gomega.HaveOccurred())
			g.Expect(string(reply)).To(gomega.Equal("OK"))
		}).Should(gomega.Succeed())
	})
})