Files
teleport/lib/web/desktop_playback.go
T
rosstimothy 07e2ca13e4 Prevent race from causing remote clients from being closed (#19010)
* Prevent race from causing remote clients from being closed

`web.SessionContext` was susceptible to races retrieving `auth.ClientI`
for remote sites. The mutex on the remote client cache was not held for
the duration of the `GetUserClient` which meant two simultaneous requests
could find no client existed in the cache, create one and then add it to
the cache. When the second client is added to the cache it would close the
first client created, which results in the `use of closed network connection`
error displaying on the web UI as described in #17173.

To remedy this the portion of `GetUserClient` that retrieves the remote client
is now protected by a `singleflight.Group`. This ensures that races on creating
a client no longer happen.

`web.SessionContext` was slightly refactored to make it easier to test, to
remove some unnecessary operations, and to provide `context.Context` to `GetUserClient`.
Additionally, all `web.SessionContext` variables which were named `ctx` in `lib/web`
have been renamed to avoid confusion as that often refers to a `context.Context`.

Fixes #17173
2022-12-05 20:10:01 +00:00

53 lines
1.4 KiB
Go

/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package web
import (
"net/http"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/websocket"
"github.com/gravitational/teleport/lib/reversetunnel"
"github.com/gravitational/teleport/lib/web/desktop"
)
func (h *Handler) desktopPlaybackHandle(
w http.ResponseWriter,
r *http.Request,
p httprouter.Params,
ctx *SessionContext,
site reversetunnel.RemoteSite,
) (interface{}, error) {
sID := p.ByName("sid")
if sID == "" {
return nil, trace.BadParameter("missing sid in request URL")
}
clt, err := ctx.GetUserClient(r.Context(), site)
if err != nil {
return nil, trace.Wrap(err)
}
websocket.Handler(func(ws *websocket.Conn) {
defer h.log.Debug("desktopPlaybackHandle websocket handler returned")
desktop.NewPlayer(sID, ws, clt, h.log).Play(r.Context())
}).ServeHTTP(w, r)
return nil, nil
}