File
Blob: tun/client/tunnel_test.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "testing" |
| 11 | |
| 12 | "go.miragespace.co/specter/spec/chord" |
| 13 | "go.miragespace.co/specter/spec/mocks" |
| 14 | "go.miragespace.co/specter/spec/pki" |
| 15 | "go.miragespace.co/specter/spec/protocol" |
| 16 | "go.miragespace.co/specter/spec/rpc" |
| 17 | |
| 18 | "github.com/stretchr/testify/mock" |
| 19 | "github.com/stretchr/testify/require" |
| 20 | "go.uber.org/zap/zaptest" |
| 21 | ) |
| 22 | |
| 23 | func TestPublishHostnameReuse(t *testing.T) { |
| 24 | as := require.New(t) |
| 25 | logger := zaptest.NewLogger(t) |
| 26 | |
| 27 | file, err := os.CreateTemp("", "client") |
| 28 | as.NoError(err) |
| 29 | defer os.Remove(file.Name()) |
| 30 | |
| 31 | ctx := t.Context() |
| 32 | |
| 33 | token := &protocol.ClientToken{ |
| 34 | Token: []byte("test"), |
| 35 | } |
| 36 | cl := &protocol.Node{ |
| 37 | Id: chord.Random(), |
| 38 | } |
| 39 | |
| 40 | der, cert, key := makeCertificate(as, logger, cl, token, nil) |
| 41 | cfg := &Config{ |
| 42 | path: file.Name(), |
| 43 | Apex: testApex, |
| 44 | Certificate: cert, |
| 45 | PrivKey: key, |
| 46 | Tunnels: []Tunnel{ |
| 47 | { |
| 48 | Target: "tcp://127.0.0.1:5432", |
| 49 | }, |
| 50 | { |
| 51 | Target: "tcp://127.0.0.1:2345", |
| 52 | }, |
| 53 | { |
| 54 | Target: "https://example.com", |
| 55 | }, |
| 56 | }, |
| 57 | } |
| 58 | as.NoError(cfg.validate()) |
| 59 | |
| 60 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 61 | resp := &protocol.RegisteredHostnamesResponse{ |
| 62 | Hostnames: []string{testHostname, "bastion.example.com"}, |
| 63 | } |
| 64 | s.On("RegisteredHostnames", mock.Anything, mock.Anything).Return(resp, nil) |
| 65 | transportHelper(t1, der) |
| 66 | } |
| 67 | |
| 68 | client, _, assertion := setupClient(t, as, ctx, logger, nil, cfg, nil, m, true, len(cfg.Tunnels)) |
| 69 | defer assertion() |
| 70 | defer client.Close() |
| 71 | |
| 72 | // assert that we are not reusing the custom domain |
| 73 | for _, tunnel := range client.Configuration.Tunnels { |
| 74 | as.Equal(testHostname, tunnel.Hostname) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestRegisterAndProxy(t *testing.T) { |
| 79 | as := require.New(t) |
| 80 | logger := zaptest.NewLogger(t) |
| 81 | |
| 82 | file, err := os.CreateTemp("", "client") |
| 83 | as.NoError(err) |
| 84 | defer os.Remove(file.Name()) |
| 85 | |
| 86 | ctx := t.Context() |
| 87 | |
| 88 | tcpListener, err := net.Listen("tcp", "127.0.0.1:0") |
| 89 | as.NoError(err) |
| 90 | defer tcpListener.Close() |
| 91 | |
| 92 | go func() { |
| 93 | conn, err := tcpListener.Accept() |
| 94 | as.NoError(err) |
| 95 | conn.Write([]byte("hi")) |
| 96 | }() |
| 97 | |
| 98 | token := &protocol.ClientToken{ |
| 99 | Token: []byte("test"), |
| 100 | } |
| 101 | cl := &protocol.Node{ |
| 102 | Id: chord.Random(), |
| 103 | } |
| 104 | |
| 105 | // no token |
| 106 | cfg := &Config{ |
| 107 | path: file.Name(), |
| 108 | Apex: testApex, |
| 109 | Tunnels: []Tunnel{ |
| 110 | { |
| 111 | Target: fmt.Sprintf("tcp://%s", tcpListener.Addr()), |
| 112 | }, |
| 113 | }, |
| 114 | } |
| 115 | as.NoError(cfg.validate()) |
| 116 | |
| 117 | key, err := pki.UnmarshalPrivateKey([]byte(cfg.PrivKey)) |
| 118 | as.NoError(err) |
| 119 | der, cert, _ := makeCertificate(as, logger, cl, token, key) |
| 120 | pkiClient := new(mocks.PKIClient) |
| 121 | pkiClient.On("RequestCertificate", mock.Anything, mock.Anything).Return(&protocol.CertificateResponse{ |
| 122 | CertDer: der, |
| 123 | CertPem: []byte(cert), |
| 124 | }, nil).Once() |
| 125 | |
| 126 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 127 | s.On("RegisterIdentity", mock.Anything, mock.Anything).Return(&protocol.RegisterIdentityResponse{ |
| 128 | Apex: testApex, |
| 129 | }, nil) |
| 130 | |
| 131 | defaultNoHostnames(s) |
| 132 | transportHelper(t1, der) |
| 133 | |
| 134 | t1.Identify = cl |
| 135 | } |
| 136 | |
| 137 | client, t2, assertion := setupClient(t, as, ctx, logger, pkiClient, cfg, nil, m, false, 1) |
| 138 | defer assertion() |
| 139 | defer client.Close() |
| 140 | |
| 141 | client.Start(ctx) |
| 142 | |
| 143 | conn, err := t2.DialStream(ctx, cl, protocol.Stream_DIRECT) |
| 144 | as.NoError(err) |
| 145 | as.NoError(rpc.Send(conn, &protocol.Link{ |
| 146 | Alpn: protocol.Link_TCP, |
| 147 | Hostname: testHostname, |
| 148 | })) |
| 149 | |
| 150 | status := &protocol.TunnelStatus{} |
| 151 | rpc.BoundedReceive(conn, status, 1024) |
| 152 | as.Equal(protocol.TunnelStatusCode_STATUS_OK, status.GetStatus()) |
| 153 | |
| 154 | buf := make([]byte, 2) |
| 155 | n, err := io.ReadFull(conn, buf) |
| 156 | as.NoError(err) |
| 157 | as.Equal(2, n) |
| 158 | as.Equal("hi", string(buf)) |
| 159 | } |
| 160 | |
| 161 | func TestUnpublishTunnel(t *testing.T) { |
| 162 | as := require.New(t) |
| 163 | logger := zaptest.NewLogger(t) |
| 164 | |
| 165 | file, err := os.CreateTemp("", "client") |
| 166 | as.NoError(err) |
| 167 | defer os.Remove(file.Name()) |
| 168 | |
| 169 | ctx := t.Context() |
| 170 | |
| 171 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 172 | fmt.Fprintf(w, "ok") |
| 173 | })) |
| 174 | defer ts.Close() |
| 175 | |
| 176 | token := &protocol.ClientToken{ |
| 177 | Token: []byte("test"), |
| 178 | } |
| 179 | cl := &protocol.Node{ |
| 180 | Id: chord.Random(), |
| 181 | } |
| 182 | |
| 183 | der, cert, key := makeCertificate(as, logger, cl, token, nil) |
| 184 | cfg := &Config{ |
| 185 | path: file.Name(), |
| 186 | Apex: testApex, |
| 187 | Certificate: cert, |
| 188 | PrivKey: key, |
| 189 | Tunnels: []Tunnel{ |
| 190 | { |
| 191 | Hostname: testHostname, |
| 192 | Target: ts.URL, |
| 193 | }, |
| 194 | }, |
| 195 | } |
| 196 | as.NoError(cfg.validate()) |
| 197 | |
| 198 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 199 | s.On("UnpublishTunnel", mock.Anything, mock.MatchedBy(func(req *protocol.UnpublishTunnelRequest) bool { |
| 200 | return req.GetHostname() == testHostname |
| 201 | })).Return(&protocol.UnpublishTunnelResponse{}, nil).NotBefore(publishCall) |
| 202 | |
| 203 | defaultNoHostnames(s) |
| 204 | transportHelper(t1, der) |
| 205 | } |
| 206 | |
| 207 | client, _, assertion := setupClient(t, as, ctx, logger, nil, cfg, nil, m, false, 1) |
| 208 | defer assertion() |
| 209 | defer client.Close() |
| 210 | |
| 211 | client.Start(ctx) |
| 212 | |
| 213 | err = client.UnpublishTunnel(ctx, Tunnel{Hostname: testHostname}) |
| 214 | as.NoError(err) |
| 215 | |
| 216 | curr := client.GetCurrentConfig() |
| 217 | as.Len(curr.Tunnels, 0) |
| 218 | } |
| 219 | |
| 220 | func TestReleaseTunnel(t *testing.T) { |
| 221 | as := require.New(t) |
| 222 | logger := zaptest.NewLogger(t) |
| 223 | |
| 224 | file, err := os.CreateTemp("", "client") |
| 225 | as.NoError(err) |
| 226 | defer os.Remove(file.Name()) |
| 227 | |
| 228 | ctx := t.Context() |
| 229 | |
| 230 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 231 | fmt.Fprintf(w, "ok") |
| 232 | })) |
| 233 | defer ts.Close() |
| 234 | |
| 235 | token := &protocol.ClientToken{ |
| 236 | Token: []byte("test"), |
| 237 | } |
| 238 | cl := &protocol.Node{ |
| 239 | Id: chord.Random(), |
| 240 | } |
| 241 | |
| 242 | der, cert, key := makeCertificate(as, logger, cl, token, nil) |
| 243 | cfg := &Config{ |
| 244 | path: file.Name(), |
| 245 | Apex: testApex, |
| 246 | Certificate: cert, |
| 247 | PrivKey: key, |
| 248 | Tunnels: []Tunnel{ |
| 249 | { |
| 250 | Hostname: testHostname, |
| 251 | Target: ts.URL, |
| 252 | }, |
| 253 | }, |
| 254 | } |
| 255 | as.NoError(cfg.validate()) |
| 256 | |
| 257 | m := func(s *mocks.TunnelService, t1 *mocks.MemoryTransport, publishCall *mock.Call) { |
| 258 | s.On("ReleaseTunnel", mock.Anything, mock.MatchedBy(func(req *protocol.ReleaseTunnelRequest) bool { |
| 259 | return req.GetHostname() == testHostname |
| 260 | })).Return(&protocol.ReleaseTunnelResponse{}, nil).NotBefore(publishCall) |
| 261 | |
| 262 | defaultNoHostnames(s) |
| 263 | transportHelper(t1, der) |
| 264 | } |
| 265 | |
| 266 | client, _, assertion := setupClient(t, as, ctx, logger, nil, cfg, nil, m, false, 1) |
| 267 | defer assertion() |
| 268 | defer client.Close() |
| 269 | |
| 270 | client.Start(ctx) |
| 271 | |
| 272 | err = client.ReleaseTunnel(ctx, Tunnel{Hostname: testHostname}) |
| 273 | as.NoError(err) |
| 274 | |
| 275 | curr := client.GetCurrentConfig() |
| 276 | as.Len(curr.Tunnels, 0) |
| 277 | } |