mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-01 06:02:32 -08:00
* k8s-operator/sessionrecording: harden recording upload end handling
The session recording hijacker treated a recording upload that ended
without an error as finished, whether or not its session had ended.
This fixes this by checking that the session's connection was closed
first, and otherwise handling the end like an upload error (so the
session's recording failure policy applies), as ssh/tailssh does.
Updates tailscale/corp#51365
Change-Id: I909c6e48f43dc295b7c4b1e5762abe4e8aa6a484
* k8s-operator/sessionrecording: test recording upload end handling
This adds Test_Hijacker cases for a recording upload that ends without an
error, both after the session's connection was closed and while it is
still open, for each recording failure policy.
Updates tailscale/corp#51365
Change-Id: If021c7fa8fea52301dde058a481d84cad23fd185
---------
Change-Id: I0fda3c9138daf8b2988edb149f5044a491fb1f6f
(cherry picked from commit 6847263baf)
Signed-off-by: chaosinthecrd <[email protected]>
Co-authored-by: Tom Meadows <[email protected]>
This commit is contained in:
1 parent
897add6761
commit
8d72eeef6f
2 files changed
+72
-5
No files matched your search
@@ -19,6 +19,7 @@ import (
|
||||
"net/http/httptrace"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"tailscale.com/client/tailscale/apitype"
|
||||
@@ -221,6 +222,7 @@ func (h *Hijacker) setUpRecording(conn net.Conn) (_ net.Conn, retErr error) {
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown protocol: %s", h.proto)
|
||||
}
|
||||
sc := &sessionConn{Conn: lc, closed: make(chan struct{})}
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
@@ -231,9 +233,14 @@ func (h *Hijacker) setUpRecording(conn net.Conn) (_ net.Conn, retErr error) {
|
||||
case err = <-errChan:
|
||||
}
|
||||
if err == nil {
|
||||
counterSessionRecordingsUploaded.Add(1)
|
||||
h.log.Info("finished uploading the recording")
|
||||
return
|
||||
select {
|
||||
case <-sc.closed:
|
||||
counterSessionRecordingsUploaded.Add(1)
|
||||
h.log.Info("finished uploading the recording")
|
||||
return
|
||||
default:
|
||||
err = errors.New("recording upload ended before the session")
|
||||
}
|
||||
}
|
||||
msg := fmt.Sprintf("connection to the session recorder errored: %v;", err)
|
||||
if h.failOpen {
|
||||
@@ -248,7 +255,21 @@ func (h *Hijacker) setUpRecording(conn net.Conn) (_ net.Conn, retErr error) {
|
||||
h.log.Infof("error closing recorder connections: %v", err)
|
||||
}
|
||||
}()
|
||||
return lc, nil
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// sessionConn wraps a recorded session's connection so that a recording the
|
||||
// recorder ended early can be told apart from one the session ended.
|
||||
type sessionConn struct {
|
||||
net.Conn
|
||||
closeOnce sync.Once
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
// Close marks the session as ended before closing it, which ends the recording.
|
||||
func (c *sessionConn) Close() error {
|
||||
c.closeOnce.Do(func() { close(c.closed) })
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func closeConnWithWarning(conn net.Conn, msg string) error {
|
||||
|
||||
@@ -35,7 +35,10 @@ func Test_Hijacker(t *testing.T) {
|
||||
failOpen bool
|
||||
failRecorderConnect bool // fail initial connect to the recorder
|
||||
failRecorderConnPostConnect bool // send error down the error channel
|
||||
closeConn bool // close the session's conn, as the proxy does when the session ends
|
||||
recorderEndsCleanly bool // close the error channel without an error
|
||||
wantsConnClosed bool
|
||||
wantsUploaded bool
|
||||
wantsSetupErr bool
|
||||
proto Protocol
|
||||
}{
|
||||
@@ -72,6 +75,32 @@ func Test_Hijacker(t *testing.T) {
|
||||
wantsConnClosed: true,
|
||||
proto: SPDYProtocol,
|
||||
},
|
||||
{
|
||||
name: "session_ends_then_recorder_ends_cleanly_recording_is_uploaded",
|
||||
closeConn: true,
|
||||
recorderEndsCleanly: true,
|
||||
wantsConnClosed: true,
|
||||
wantsUploaded: true,
|
||||
proto: SPDYProtocol,
|
||||
},
|
||||
{
|
||||
name: "recorder_ends_cleanly_before_session_policy_is_to_fail_open_conn_stays_open",
|
||||
recorderEndsCleanly: true,
|
||||
failOpen: true,
|
||||
proto: SPDYProtocol,
|
||||
},
|
||||
{
|
||||
name: "recorder_ends_cleanly_before_session_policy_is_to_fail_closed_conn_is_closed",
|
||||
recorderEndsCleanly: true,
|
||||
wantsConnClosed: true,
|
||||
proto: SPDYProtocol,
|
||||
},
|
||||
{
|
||||
name: "recorder_ends_cleanly_before_session_policy_is_to_fail_closed_conn_is_closed_ws",
|
||||
recorderEndsCleanly: true,
|
||||
wantsConnClosed: true,
|
||||
proto: WSProtocol,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -95,11 +124,20 @@ func Test_Hijacker(t *testing.T) {
|
||||
proto: tt.proto,
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, err := h.setUpRecording(tc)
|
||||
uploaded := counterSessionRecordingsUploaded.Value()
|
||||
lc, err := h.setUpRecording(tc)
|
||||
if (err != nil) != tt.wantsSetupErr {
|
||||
t.Errorf("spdyHijacker.setupRecording() error = %v, wantErr %v", err, tt.wantsSetupErr)
|
||||
return
|
||||
}
|
||||
if tt.closeConn {
|
||||
if err := lc.Close(); err != nil {
|
||||
t.Fatalf("closing conn: %v", err)
|
||||
}
|
||||
}
|
||||
if tt.recorderEndsCleanly {
|
||||
close(ch)
|
||||
}
|
||||
if tt.failRecorderConnPostConnect {
|
||||
select {
|
||||
case ch <- errors.New("err"):
|
||||
@@ -120,6 +158,14 @@ func Test_Hijacker(t *testing.T) {
|
||||
}); err != nil {
|
||||
t.Errorf("connection did not reach the desired state within %s", timeout.String())
|
||||
}
|
||||
if err := tstest.WaitFor(timeout, func() error {
|
||||
if got := counterSessionRecordingsUploaded.Value() > uploaded; got != tt.wantsUploaded {
|
||||
return fmt.Errorf("got recording uploaded: %t, wants recording uploaded: %t", got, tt.wantsUploaded)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ctx.Done()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user