Files
threshold/api/proxy/websocket.go
T
Elim Tsiagbey 1b1c8b3bf9 fix: Proxy closing listener with 0 connections left (#4389)
* fix: Proxy closing listener with 0 connections left

A bug was introduced in `0.15.0` where sessions with max connection counts were closed when the connections left hit 0. This issue was highlighted by a user: https://github.com/hashicorp/boundary/issues/4364. What was happening was when we have zero connections left the proxy code was closing the listener which closed its connections

To fix this:
- `boundary connect  -target-id=xx -host-id=xx` command with no connections left will now wait for all connections to be closed and before exiting the session just like it did in `0.14.3`
- As soon as there are no more connections left, the listener will close to stop accepting new connections
- Fix issue where the websocket connection was throwing closing errors during the session teardown.
2024-02-09 18:15:00 -05:00

127 lines
3.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package proxy
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"github.com/hashicorp/boundary/api/consts"
pb "github.com/hashicorp/boundary/sdk/pbs/proxy"
"github.com/hashicorp/boundary/sdk/wspb"
"nhooyr.io/websocket"
)
func (p *ClientProxy) getWsConn(ctx context.Context) (*websocket.Conn, error) {
conn, resp, err := websocket.Dial(
ctx,
fmt.Sprintf("ws://%s/v1/proxy", p.workerAddr),
&websocket.DialOptions{
HTTPClient: &http.Client{
Transport: p.transport,
},
Subprotocols: []string{consts.WebsocketProtocolTcpProxyV1},
},
)
if err != nil {
switch {
case strings.Contains(err.Error(), "tls: internal error"):
return nil, errors.New("session credentials were not accepted, or session is unauthorized")
case strings.Contains(err.Error(), "connect: connection refused"):
return nil, fmt.Errorf("unable to connect to worker at %s", p.workerAddr)
default:
return nil, fmt.Errorf("error dialing the worker: %w", err)
}
}
if resp == nil {
return nil, errors.New("response from worker is nil")
}
if resp.Header == nil {
return nil, errors.New("response header is nil")
}
negProto := resp.Header.Get("Sec-WebSocket-Protocol")
if negProto != consts.WebsocketProtocolTcpProxyV1 {
return nil, fmt.Errorf("unexpected negotiated protocol: %s", negProto)
}
return conn, nil
}
func (p *ClientProxy) sendSessionTeardown(ctx context.Context) error {
handshake := pb.ClientHandshake{
TofuToken: p.tofuToken,
Command: pb.HANDSHAKECOMMAND_HANDSHAKECOMMAND_SESSION_CANCEL,
}
wsConn, err := p.getWsConn(ctx)
if err != nil {
return fmt.Errorf("error fetching connection to send session teardown request to worker: %w", err)
}
if err := wspb.Write(ctx, wsConn, &handshake); err != nil {
return fmt.Errorf("error sending teardown handshake to worker: %w", err)
}
wsConn.Close(websocket.StatusNormalClosure, "session teardown finished")
return nil
}
func (p *ClientProxy) runTcpProxyV1(wsConn *websocket.Conn, listeningConn net.Conn) error {
handshake := pb.ClientHandshake{TofuToken: p.tofuToken}
if err := wspb.Write(p.ctx, wsConn, &handshake); err != nil {
return fmt.Errorf("error sending handshake to worker: %w", err)
}
var handshakeResult pb.HandshakeResult
if err := wspb.Read(p.ctx, wsConn, &handshakeResult); err != nil {
switch {
case strings.Contains(err.Error(), "unable to authorize connection"):
// There's no reason to think we'd be able to authorize any more
// connections after the first has failed. We don't cancel the
// context here as existing connections may be fine.
p.connsLeftCh <- 0
return errors.New("unable to authorize connection")
}
switch {
case strings.Contains(err.Error(), "tofu token not allowed"):
// If our tofu token is not allowed something is wrong, and we
// should cancel anything we have going.
p.cancel()
return errors.New("session is already in use")
default:
// If we can't handshake we can't do anything, so quit out
p.cancel()
return fmt.Errorf("error reading handshake result: %w", err)
}
}
if handshakeResult.GetConnectionsLeft() != -1 {
p.connsLeftCh <- handshakeResult.GetConnectionsLeft()
}
// Get a wrapped net.Conn so we can use io.Copy
netConn := websocket.NetConn(p.ctx, wsConn, websocket.MessageBinary)
localWg := new(sync.WaitGroup)
localWg.Add(2)
go func() {
defer localWg.Done()
io.Copy(netConn, listeningConn)
netConn.Close()
listeningConn.Close()
}()
go func() {
defer localWg.Done()
io.Copy(listeningConn, netConn)
listeningConn.Close()
netConn.Close()
}()
localWg.Wait()
return nil
}