2021-07-28 06:31:16 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-01-12 14:16:36 -07:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-03-19 15:17:40 -04:00
|
|
|
package worker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-01-12 14:16:36 -07:00
|
|
|
stderrors "errors"
|
2021-12-09 20:41:08 -05:00
|
|
|
"fmt"
|
2021-09-02 12:24:12 -07:00
|
|
|
"io"
|
2020-09-20 23:09:42 -04:00
|
|
|
"net"
|
2020-03-19 15:17:40 -04:00
|
|
|
"net/http"
|
2021-09-01 07:22:45 -07:00
|
|
|
"net/url"
|
2020-09-20 23:09:42 -04:00
|
|
|
"strconv"
|
2022-04-01 16:32:20 -04:00
|
|
|
"strings"
|
2020-03-19 15:17:40 -04:00
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/globals"
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/common"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/daemon/worker/internal/metric"
|
|
|
|
|
proxyHandlers "github.com/hashicorp/boundary/internal/daemon/worker/proxy"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/daemon/worker/session"
|
2023-01-12 14:16:36 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2020-09-20 12:51:08 -04:00
|
|
|
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
|
2021-07-28 06:31:16 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/observability/event"
|
2020-09-10 23:25:40 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/proxy"
|
2023-01-12 14:16:36 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/util"
|
2021-07-14 17:50:15 -07:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/listenerutil"
|
2023-01-12 14:16:36 -07:00
|
|
|
"github.com/hashicorp/nodeenrollment"
|
|
|
|
|
"github.com/hashicorp/nodeenrollment/types"
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
2022-12-06 12:44:00 -07:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2022-07-06 13:37:49 -07:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2020-09-03 13:24:34 -04:00
|
|
|
"nhooyr.io/websocket"
|
2020-09-10 23:25:40 -04:00
|
|
|
"nhooyr.io/websocket/wspb"
|
2020-03-19 15:17:40 -04:00
|
|
|
)
|
|
|
|
|
|
2022-12-06 12:44:00 -07:00
|
|
|
var GetProtocolContext = nilProtocolContext
|
|
|
|
|
|
|
|
|
|
func nilProtocolContext(context.Context, string, session.Session, string) (*anypb.Any, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 15:17:40 -04:00
|
|
|
type HandlerProperties struct {
|
2021-07-14 17:50:15 -07:00
|
|
|
ListenerConfig *listenerutil.ListenerConfig
|
2020-03-19 15:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:30:55 +02:00
|
|
|
// Handler returns a http.Handler for the API. This can be used on
|
2020-09-25 00:55:06 +08:00
|
|
|
// its own to mount the Worker API within another web server.
|
2022-07-06 13:37:49 -07:00
|
|
|
func (w *Worker) handler(props HandlerProperties, sm session.Manager) (http.Handler, error) {
|
2021-12-09 20:41:08 -05:00
|
|
|
const op = "worker.(Worker).handler"
|
2020-03-19 15:17:40 -04:00
|
|
|
// Create the muxer to handle the actual endpoints
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
2023-01-17 09:54:01 -07:00
|
|
|
var h http.Handler
|
2022-07-06 13:37:49 -07:00
|
|
|
h, err := w.handleProxy(props.ListenerConfig, sm)
|
2021-12-09 20:41:08 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
|
|
|
}
|
2023-01-17 09:54:01 -07:00
|
|
|
h = proxyHandlers.ProxyHandlerCounter(h)
|
2022-03-31 19:29:04 +01:00
|
|
|
mux.Handle("/v1/proxy", metric.InstrumentWebsocketWrapper(h))
|
2020-03-19 15:17:40 -04:00
|
|
|
|
2020-09-03 13:24:34 -04:00
|
|
|
genericWrappedHandler := w.wrapGenericHandler(mux, props)
|
2022-04-04 11:38:15 -07:00
|
|
|
metricHandler := metric.InstrumentHttpHandler(genericWrappedHandler)
|
2022-04-01 12:07:40 -07:00
|
|
|
return metricHandler, nil
|
2020-03-19 15:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-06 13:37:49 -07:00
|
|
|
func (w *Worker) handleProxy(listenerCfg *listenerutil.ListenerConfig, sessionManager session.Manager) (http.HandlerFunc, error) {
|
2021-07-28 06:31:16 -04:00
|
|
|
const op = "worker.(Worker).handleProxy"
|
2021-12-09 20:41:08 -05:00
|
|
|
if listenerCfg == nil {
|
|
|
|
|
return nil, fmt.Errorf("%s: missing listener config", op)
|
|
|
|
|
}
|
2021-08-18 15:30:55 +02:00
|
|
|
return func(wr http.ResponseWriter, r *http.Request) {
|
2021-07-28 06:31:16 -04:00
|
|
|
ctx := r.Context()
|
2020-09-03 13:24:34 -04:00
|
|
|
if r.TLS == nil {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("no request tls information found"))
|
2022-04-01 16:32:20 -04:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sessionId string
|
|
|
|
|
outerCertLoop:
|
|
|
|
|
for _, cert := range r.TLS.PeerCertificates {
|
|
|
|
|
for _, name := range cert.DNSNames {
|
2023-03-10 15:06:39 -05:00
|
|
|
if strings.HasPrefix(name, fmt.Sprintf("%s_", globals.SessionPrefix)) {
|
2022-04-01 16:32:20 -04:00
|
|
|
sessionId = name
|
|
|
|
|
break outerCertLoop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if sessionId == "" {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("no session id could be found in peer certificates"))
|
2020-09-03 13:24:34 -04:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 23:09:42 -04:00
|
|
|
clientIp, clientPort, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
|
if err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to understand remote address", "remote_addr", r.RemoteAddr))
|
2020-09-20 23:09:42 -04:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
numPort, err := strconv.Atoi(clientPort)
|
|
|
|
|
if err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to understand remote port"))
|
2020-09-20 23:09:42 -04:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
clientAddr := &net.TCPAddr{
|
|
|
|
|
IP: net.ParseIP(clientIp),
|
|
|
|
|
Port: numPort,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 20:41:08 -05:00
|
|
|
userClientIp, err := common.ClientIpFromRequest(ctx, listenerCfg, r)
|
|
|
|
|
if err != nil {
|
2022-04-01 16:32:20 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to determine user ip"))
|
2021-12-09 20:41:08 -05:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 13:37:49 -07:00
|
|
|
sess := sessionManager.Get(sessionId)
|
|
|
|
|
if sess == nil {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("session not found locally"), event.WithInfo("session_id", sessionId))
|
2020-09-03 13:24:34 -04:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-19 00:18:17 -04:00
|
|
|
|
2020-09-03 13:24:34 -04:00
|
|
|
opts := &websocket.AcceptOptions{
|
|
|
|
|
Subprotocols: []string{globals.TcpProxyV1},
|
|
|
|
|
}
|
|
|
|
|
conn, err := websocket.Accept(wr, r, opts)
|
|
|
|
|
if err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error during websocket upgrade"))
|
2020-09-03 13:24:34 -04:00
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Later calls will cause this to noop if they return a different status
|
|
|
|
|
defer conn.Close(websocket.StatusNormalClosure, "done")
|
|
|
|
|
|
2022-07-06 13:37:49 -07:00
|
|
|
connCtx, connCancel := context.WithDeadline(ctx, sess.GetExpiration())
|
2020-09-20 12:51:08 -04:00
|
|
|
defer connCancel()
|
2020-09-03 13:24:34 -04:00
|
|
|
|
2020-09-19 00:18:17 -04:00
|
|
|
var handshake proxy.ClientHandshake
|
2020-09-10 23:25:40 -04:00
|
|
|
if err := wspb.Read(connCtx, conn, &handshake); err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error reading handshake from client"))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusPolicyViolation, "invalid handshake received"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-09-10 23:25:40 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(handshake.GetTofuToken()) < 20 {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("invalid tofu token"))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusUnsupportedData, "invalid tofu token"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-09-19 00:18:17 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 08:43:14 -04:00
|
|
|
if handshake.Command == proxy.HANDSHAKECOMMAND_HANDSHAKECOMMAND_SESSION_CANCEL {
|
2022-07-06 13:37:49 -07:00
|
|
|
if err := sess.RequestCancel(ctx); err != nil {
|
2022-03-17 08:43:14 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to cancel session"))
|
2023-01-12 14:16:36 -07:00
|
|
|
if err = conn.Close(websocket.StatusInternalError, "unable to cancel session"); err != nil && !stderrors.Is(err, io.EOF) {
|
2022-03-17 08:43:14 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-12 14:16:36 -07:00
|
|
|
if err = conn.Close(websocket.StatusNormalClosure, "session canceled"); err != nil && !stderrors.Is(err, io.EOF) {
|
2022-03-17 08:43:14 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 13:37:49 -07:00
|
|
|
if sess.GetTofuToken() != "" {
|
|
|
|
|
if sess.GetTofuToken() != handshake.GetTofuToken() {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("WARNING: mismatched tofu token"), event.WithInfo("session_id", sessionId))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusPolicyViolation, "tofu token not allowed"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-09-19 00:18:17 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-07-06 13:37:49 -07:00
|
|
|
if sess.GetStatus() != pbs.SESSIONSTATUS_SESSIONSTATUS_PENDING {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("no tofu token but not in correct session state"), event.WithInfo("session_id", sessionId))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusInternalError, "refusing to activate session"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-09-20 12:51:08 -04:00
|
|
|
return
|
|
|
|
|
}
|
2020-12-16 17:14:47 -05:00
|
|
|
if handshake.Command == proxy.HANDSHAKECOMMAND_HANDSHAKECOMMAND_UNSPECIFIED {
|
2022-07-06 13:37:49 -07:00
|
|
|
err = sess.RequestActivate(ctx, handshake.GetTofuToken())
|
2020-12-16 17:14:47 -05:00
|
|
|
if err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to validate session"))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusInternalError, "unable to activate session"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-12-16 17:14:47 -05:00
|
|
|
return
|
|
|
|
|
}
|
2022-06-17 16:20:01 -04:00
|
|
|
event.WriteSysEvent(ctx, op, "session successfully activated", "session_id", sessionId)
|
2020-12-16 17:14:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 09:30:04 -04:00
|
|
|
if w.LastStatusSuccess() == nil || w.LastStatusSuccess().WorkerId == "" {
|
2023-01-12 14:16:36 -07:00
|
|
|
event.WriteError(ctx, op, stderrors.New("worker id is empty"))
|
2022-06-16 09:30:04 -04:00
|
|
|
if err = conn.Close(websocket.StatusInternalError, "worker id is empty"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
workerId := w.LastStatusSuccess().WorkerId
|
|
|
|
|
|
2023-01-10 08:10:54 -05:00
|
|
|
var acResp *pbs.AuthorizeConnectionResponse
|
2020-09-20 16:28:00 -04:00
|
|
|
var connsLeft int32
|
2023-01-10 08:10:54 -05:00
|
|
|
acResp, connsLeft, err = sess.RequestAuthorizeConnection(ctx, workerId, connCancel)
|
2020-09-20 12:51:08 -04:00
|
|
|
if err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to authorize connection"))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusInternalError, "unable to authorize connection"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-09-20 12:51:08 -04:00
|
|
|
return
|
|
|
|
|
}
|
2023-01-10 08:10:54 -05:00
|
|
|
event.WriteSysEvent(ctx, op, "connection successfully authorized", "session_id", sessionId, "connection_id", acResp.GetConnectionId())
|
2022-09-28 15:22:52 +01:00
|
|
|
|
2022-10-05 13:20:35 +01:00
|
|
|
// Wrapping the client websocket with a `net.Conn` implementation that
|
|
|
|
|
// records the bytes that go across Read() and Write().
|
2022-09-28 15:22:52 +01:00
|
|
|
cc := &countingConn{Conn: websocket.NetConn(connCtx, conn, websocket.MessageBinary)}
|
2023-01-10 08:10:54 -05:00
|
|
|
err = sess.ApplyConnectionCounterCallbacks(acResp.GetConnectionId(), cc.BytesRead, cc.BytesWritten)
|
2022-10-05 13:20:35 +01:00
|
|
|
if err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to set counter callbacks for session connection"))
|
|
|
|
|
err = conn.Close(websocket.StatusInternalError, "unable to set counter callbacks for session connection")
|
|
|
|
|
if err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 16:20:01 -04:00
|
|
|
defer func() {
|
2022-10-05 13:20:35 +01:00
|
|
|
ccd := map[string]*session.ConnectionCloseData{
|
2023-01-10 08:10:54 -05:00
|
|
|
acResp.GetConnectionId(): {
|
2022-10-05 13:20:35 +01:00
|
|
|
SessionId: sess.GetId(),
|
|
|
|
|
BytesUp: cc.BytesRead(),
|
|
|
|
|
BytesDown: cc.BytesWritten(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
if sessionManager.RequestCloseConnections(ctx, ccd) {
|
2023-01-10 08:10:54 -05:00
|
|
|
event.WriteSysEvent(ctx, op, "connection closed", "session_id", sessionId, "connection_id", acResp.GetConnectionId())
|
2022-06-17 16:20:01 -04:00
|
|
|
}
|
|
|
|
|
}()
|
2020-09-21 00:09:50 -04:00
|
|
|
|
2020-09-19 13:51:50 -04:00
|
|
|
handshakeResult := &proxy.HandshakeResult{
|
2022-07-06 13:37:49 -07:00
|
|
|
Expiration: timestamppb.New(sess.GetExpiration()),
|
|
|
|
|
ConnectionLimit: sess.GetConnectionLimit(),
|
2020-09-20 16:28:00 -04:00
|
|
|
ConnectionsLeft: connsLeft,
|
2020-09-19 13:51:50 -04:00
|
|
|
}
|
|
|
|
|
if err := wspb.Write(connCtx, conn, handshakeResult); err != nil {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error sending handshake result to client"))
|
2021-08-18 15:30:55 +02:00
|
|
|
if err = conn.Close(websocket.StatusProtocolError, "unable to send handshake result"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
2020-03-19 15:17:40 -04:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 12:44:00 -07:00
|
|
|
endpointUrl, err := url.Parse(sess.GetEndpoint())
|
|
|
|
|
if err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("worker failed to parse target endpoint", "endpoint", sess.GetEndpoint()))
|
|
|
|
|
if err = conn.Close(websocket.StatusProtocolError, "unable to parse endpoint"); err != nil {
|
2021-08-18 15:30:55 +02:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
2020-03-19 15:17:40 -04:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-10 08:10:54 -05:00
|
|
|
protocolCtx := acResp.GetProtocolContext()
|
2022-12-06 12:44:00 -07:00
|
|
|
if protocolCtx == nil {
|
|
|
|
|
// TODO: Remove this if block once pre v0.12.0 controllers are no longer supported.
|
|
|
|
|
if protocolCtx, err = GetProtocolContext(ctx, workerId, sess, endpointUrl.Scheme); err != nil {
|
|
|
|
|
conn.Close(proxyHandlers.WebsocketStatusProtocolSetupError, "unable to get proxy context")
|
|
|
|
|
event.WriteError(ctx, op, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-19 15:17:40 -04:00
|
|
|
|
2023-01-20 11:43:57 -07:00
|
|
|
pDialer, err := proxyHandlers.GetEndpointDialer(ctx, endpointUrl.Host, workerId, acResp, w.downstreamReceiver)
|
2022-12-06 12:44:00 -07:00
|
|
|
if err != nil {
|
|
|
|
|
conn.Close(proxyHandlers.WebsocketStatusProtocolSetupError, "unable to get endpoint dialer")
|
|
|
|
|
event.WriteError(ctx, op, err)
|
|
|
|
|
return
|
2021-12-17 11:26:42 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-06 12:44:00 -07:00
|
|
|
// Verify the protocol has a supported proxy before calling RequestAuthorizeConnection
|
2023-01-10 08:10:54 -05:00
|
|
|
handleProxyFn, err := proxyHandlers.GetHandler(workerId, acResp.GetProtocolContext())
|
2022-12-06 12:44:00 -07:00
|
|
|
if err != nil {
|
|
|
|
|
conn.Close(proxyHandlers.WebsocketStatusProtocolSetupError, "unable to get proxy handler")
|
|
|
|
|
event.WriteError(ctx, op, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-12 14:16:36 -07:00
|
|
|
decryptFn, err := w.credDecryptFn(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
conn.Close(proxyHandlers.WebsocketStatusProtocolSetupError, "error getting decryption function")
|
|
|
|
|
event.WriteError(ctx, op, err)
|
|
|
|
|
}
|
|
|
|
|
runProxy, err := handleProxyFn(ctx, decryptFn, cc, pDialer, acResp.GetConnectionId(), protocolCtx)
|
2022-12-06 12:44:00 -07:00
|
|
|
if err != nil {
|
|
|
|
|
conn.Close(proxyHandlers.WebsocketStatusProtocolSetupError, "unable to setup proxying")
|
|
|
|
|
event.WriteError(ctx, op, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We connect connection only after we have confirmed as much as we can
|
|
|
|
|
// that we can establish the proxy.
|
|
|
|
|
endpointAddr := pDialer.LastConnectionAddr()
|
|
|
|
|
connectionInfo := &pbs.ConnectConnectionRequest{
|
2023-01-10 08:10:54 -05:00
|
|
|
ConnectionId: acResp.GetConnectionId(),
|
2022-12-06 12:44:00 -07:00
|
|
|
ClientTcpAddress: clientAddr.IP.String(),
|
|
|
|
|
ClientTcpPort: uint32(clientAddr.Port),
|
|
|
|
|
EndpointTcpAddress: endpointAddr.Ip(),
|
|
|
|
|
EndpointTcpPort: endpointAddr.Port(),
|
|
|
|
|
Type: endpointUrl.Scheme,
|
|
|
|
|
UserClientIp: userClientIp,
|
|
|
|
|
}
|
|
|
|
|
if err = sess.RequestConnectConnection(ctx, connectionInfo); err != nil {
|
2023-01-10 08:10:54 -05:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error requesting connect connection", "session_id", sess.GetId(), "connection_id", acResp.GetConnectionId()))
|
2021-09-17 11:48:44 -07:00
|
|
|
if err = conn.Close(websocket.StatusInternalError, "unable to establish proxy"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2022-12-06 12:44:00 -07:00
|
|
|
return
|
2021-09-17 11:48:44 -07:00
|
|
|
}
|
2022-12-06 12:44:00 -07:00
|
|
|
|
2023-01-09 13:43:02 -05:00
|
|
|
runProxy(ctx)
|
2021-12-09 20:41:08 -05:00
|
|
|
}, nil
|
2021-08-18 15:30:55 +02:00
|
|
|
}
|
2020-03-19 15:17:40 -04:00
|
|
|
|
2023-01-12 14:16:36 -07:00
|
|
|
// credDecryptFn returns a DecryptFn if the worker is a pki worker with
|
|
|
|
|
// WorkerAuthStorage defined. An error is returned if there is an error
|
|
|
|
|
// loading the node credentials.
|
|
|
|
|
func (w *Worker) credDecryptFn(ctx context.Context) (proxyHandlers.DecryptFn, error) {
|
|
|
|
|
const op = "worker.(*Worker).credDecryptFn"
|
|
|
|
|
if w.WorkerAuthStorage == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
was := w.WorkerAuthStorage
|
|
|
|
|
var opts []nodeenrollment.Option
|
|
|
|
|
if !util.IsNil(w.conf.WorkerAuthStorageKms) {
|
|
|
|
|
opts = append(opts, nodeenrollment.WithWrapper(w.conf.WorkerAuthStorageKms))
|
|
|
|
|
}
|
|
|
|
|
nodeCreds, err := types.LoadNodeCredentials(ctx, was, nodeenrollment.CurrentId, opts...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(ctx, err, op)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return func(ctx context.Context, from []byte, to proto.Message) error {
|
|
|
|
|
if err := nodeenrollment.DecryptMessage(ctx, from, nodeCreds, to); err != nil {
|
|
|
|
|
return errors.Wrap(ctx, err, op)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:30:55 +02:00
|
|
|
func (w *Worker) wrapGenericHandler(h http.Handler, _ HandlerProperties) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(wr http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Set the Cache-Control header for all responses returned
|
|
|
|
|
wr.Header().Set("Cache-Control", "no-store")
|
|
|
|
|
h.ServeHTTP(wr, r)
|
2020-03-19 15:17:40 -04:00
|
|
|
})
|
|
|
|
|
}
|