2020-03-19 15:17:40 -04:00
|
|
|
package worker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-07-28 06:31:16 -04:00
|
|
|
"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"
|
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"
|
2021-07-14 17:50:15 -07:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/listenerutil"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
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 {
|
2022-04-01 16:32:20 -04:00
|
|
|
event.WriteError(ctx, op, errors.New("no request tls information found"))
|
|
|
|
|
wr.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sessionId string
|
|
|
|
|
outerCertLoop:
|
|
|
|
|
for _, cert := range r.TLS.PeerCertificates {
|
|
|
|
|
for _, name := range cert.DNSNames {
|
|
|
|
|
if strings.HasPrefix(name, globals.SessionPrefix) {
|
|
|
|
|
sessionId = name
|
|
|
|
|
break outerCertLoop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if sessionId == "" {
|
|
|
|
|
event.WriteError(ctx, op, errors.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 {
|
|
|
|
|
event.WriteError(ctx, op, errors.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 {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, errors.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"))
|
|
|
|
|
if err = conn.Close(websocket.StatusInternalError, "unable to cancel session"); err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err = conn.Close(websocket.StatusNormalClosure, "session canceled"); err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
|
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() {
|
2021-07-28 06:31:16 -04:00
|
|
|
event.WriteError(ctx, op, errors.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 {
|
2022-03-17 08:43:14 -04:00
|
|
|
event.WriteError(ctx, op, errors.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-07-06 13:37:49 -07:00
|
|
|
// Verify the protocol has a supported proxy before calling RequestAuthorizeConnection
|
|
|
|
|
endpointUrl, err := url.Parse(sess.GetEndpoint())
|
2021-08-18 15:30:55 +02:00
|
|
|
if err != nil {
|
2022-07-06 13:37:49 -07:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("worker failed to parse target endpoint", "endpoint", sess.GetEndpoint()))
|
2022-09-13 13:05:36 -07:00
|
|
|
if err = conn.Close(websocket.StatusProtocolError, "unable to parse endpoint"); err != nil {
|
2021-09-01 07:22:45 -07:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
handleProxyFn, err := proxyHandlers.GetHandler(endpointUrl.Scheme)
|
|
|
|
|
if err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("worker received request for unsupported protocol", "protocol", endpointUrl.Scheme))
|
2022-09-13 13:05:36 -07:00
|
|
|
if err = conn.Close(websocket.StatusProtocolError, fmt.Sprintf("worker does not support %q type", endpointUrl.Scheme)); err != nil {
|
2021-08-18 15:30:55 +02:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
|
|
|
|
}
|
2020-12-16 17:14:47 -05:00
|
|
|
return
|
2020-09-19 00:18:17 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-16 09:30:04 -04:00
|
|
|
if w.LastStatusSuccess() == nil || w.LastStatusSuccess().WorkerId == "" {
|
|
|
|
|
event.WriteError(ctx, op, errors.New("worker id is empty"))
|
|
|
|
|
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
|
|
|
|
|
|
2022-07-06 13:37:49 -07:00
|
|
|
var ci session.ConnInfo
|
2020-09-20 16:28:00 -04:00
|
|
|
var connsLeft int32
|
2022-07-06 13:37:49 -07:00
|
|
|
ci, 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
|
|
|
|
|
}
|
2022-06-17 16:20:01 -04:00
|
|
|
event.WriteSysEvent(ctx, op, "connection successfully authorized", "session_id", sessionId, "connection_id", ci.Id)
|
|
|
|
|
defer func() {
|
2022-07-06 13:37:49 -07:00
|
|
|
if sessionManager.RequestCloseConnections(ctx, map[string]string{ci.Id: sess.GetId()}) {
|
2022-06-17 16:20:01 -04:00
|
|
|
event.WriteSysEvent(ctx, op, "connection closed", "session_id", sessionId, "connection_id", ci.Id)
|
|
|
|
|
}
|
|
|
|
|
}()
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:30:55 +02:00
|
|
|
conf := proxyHandlers.Config{
|
2021-12-09 20:41:08 -05:00
|
|
|
UserClientIp: net.ParseIP(userClientIp),
|
2021-08-18 15:30:55 +02:00
|
|
|
ClientAddress: clientAddr,
|
|
|
|
|
ClientConn: conn,
|
2022-07-06 13:37:49 -07:00
|
|
|
RemoteEndpoint: sess.GetEndpoint(),
|
|
|
|
|
Session: sess,
|
2021-08-18 15:30:55 +02:00
|
|
|
ConnectionId: ci.Id,
|
2020-03-19 15:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:30:55 +02:00
|
|
|
if err := conf.Validate(); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error validating proxy config"))
|
|
|
|
|
if err = conn.Close(websocket.StatusInternalError, "unable to validate proxy parameters"); err != nil {
|
|
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error closing client connection"))
|
2020-03-19 15:17:40 -04:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 13:37:49 -07:00
|
|
|
credentials := sess.GetCredentials()
|
2021-12-17 11:26:42 -08:00
|
|
|
var proxyOpts []proxyHandlers.Option
|
|
|
|
|
if len(credentials) > 0 {
|
2022-07-19 15:16:24 -07:00
|
|
|
proxyOpts = append(proxyOpts, proxyHandlers.WithInjectedApplicationCredentials(credentials))
|
2021-12-17 11:26:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = handleProxyFn(connCtx, conf, proxyOpts...); err != nil {
|
2022-07-06 13:37:49 -07:00
|
|
|
event.WriteError(ctx, op, err, event.WithInfoMsg("error handling proxy", "session_id", sessionId, "endpoint", sess.GetEndpoint()))
|
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"))
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
})
|
|
|
|
|
}
|