mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.3 KiB
Go
113 lines
3.3 KiB
Go
5 months ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
|
|
||
|
//go:build !plan9
|
||
|
|
||
4 months ago
|
package sessionrecording
|
||
5 months ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"net/netip"
|
||
|
"net/url"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
"tailscale.com/client/tailscale/apitype"
|
||
4 months ago
|
"tailscale.com/k8s-operator/sessionrecording/fakes"
|
||
5 months ago
|
"tailscale.com/tailcfg"
|
||
|
"tailscale.com/tsnet"
|
||
|
"tailscale.com/tstest"
|
||
|
)
|
||
|
|
||
4 months ago
|
func Test_Hijacker(t *testing.T) {
|
||
5 months ago
|
zl, err := zap.NewDevelopment()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
failOpen bool
|
||
|
failRecorderConnect bool // fail initial connect to the recorder
|
||
|
failRecorderConnPostConnect bool // send error down the error channel
|
||
|
wantsConnClosed bool
|
||
|
wantsSetupErr bool
|
||
|
}{
|
||
|
{
|
||
|
name: "setup succeeds, conn stays open",
|
||
|
},
|
||
|
{
|
||
|
name: "setup fails, policy is to fail open, conn stays open",
|
||
|
failOpen: true,
|
||
|
failRecorderConnect: true,
|
||
|
},
|
||
|
{
|
||
|
name: "setup fails, policy is to fail closed, conn is closed",
|
||
|
failRecorderConnect: true,
|
||
|
wantsSetupErr: true,
|
||
|
wantsConnClosed: true,
|
||
|
},
|
||
|
{
|
||
|
name: "connection fails post-initial connect, policy is to fail open, conn stays open",
|
||
|
failRecorderConnPostConnect: true,
|
||
|
failOpen: true,
|
||
|
},
|
||
|
{
|
||
|
name: "connection fails post-initial connect, policy is to fail closed, conn is closed",
|
||
|
failRecorderConnPostConnect: true,
|
||
|
wantsConnClosed: true,
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
4 months ago
|
tc := &fakes.TestConn{}
|
||
5 months ago
|
ch := make(chan error)
|
||
4 months ago
|
h := &Hijacker{
|
||
5 months ago
|
connectToRecorder: func(context.Context, []netip.AddrPort, func(context.Context, string, string) (net.Conn, error)) (wc io.WriteCloser, rec []*tailcfg.SSHRecordingAttempt, _ <-chan error, err error) {
|
||
|
if tt.failRecorderConnect {
|
||
|
err = errors.New("test")
|
||
|
}
|
||
|
return wc, rec, ch, err
|
||
|
},
|
||
|
failOpen: tt.failOpen,
|
||
|
who: &apitype.WhoIsResponse{Node: &tailcfg.Node{}, UserProfile: &tailcfg.UserProfile{}},
|
||
|
log: zl.Sugar(),
|
||
|
ts: &tsnet.Server{},
|
||
|
req: &http.Request{URL: &url.URL{}},
|
||
|
}
|
||
|
ctx := context.Background()
|
||
|
_, err := h.setUpRecording(ctx, tc)
|
||
|
if (err != nil) != tt.wantsSetupErr {
|
||
|
t.Errorf("spdyHijacker.setupRecording() error = %v, wantErr %v", err, tt.wantsSetupErr)
|
||
|
return
|
||
|
}
|
||
|
if tt.failRecorderConnPostConnect {
|
||
|
select {
|
||
|
case ch <- errors.New("err"):
|
||
|
case <-time.After(time.Second * 15):
|
||
|
t.Errorf("error from recorder conn was not read within 15 seconds")
|
||
|
}
|
||
|
}
|
||
|
timeout := time.Second * 20
|
||
|
// TODO (irbekrm): cover case where an error is received
|
||
|
// over channel and the failure policy is to fail open
|
||
|
// (test that connection remains open over some period
|
||
|
// of time).
|
||
|
if err := tstest.WaitFor(timeout, func() (err error) {
|
||
4 months ago
|
if tt.wantsConnClosed != tc.IsClosed() {
|
||
|
return fmt.Errorf("got connection state: %t, wants connection state: %t", tc.IsClosed(), tt.wantsConnClosed)
|
||
5 months ago
|
}
|
||
|
return nil
|
||
|
}); err != nil {
|
||
|
t.Errorf("connection did not reach the desired state within %s", timeout.String())
|
||
|
}
|
||
|
ctx.Done()
|
||
|
})
|
||
|
}
|
||
|
}
|