Files
threshold/internal/cmd/commands/connect/connect.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

951 lines
26 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
package connect
2020-09-03 13:24:34 -04:00
import (
"context"
2020-09-03 13:24:34 -04:00
"crypto/tls"
"encoding/json"
2020-09-20 12:51:08 -04:00
"errors"
2020-09-03 13:24:34 -04:00
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
2020-09-03 13:24:34 -04:00
"strings"
"sync"
"syscall"
"time"
2020-09-03 13:24:34 -04:00
2020-09-24 14:34:58 -07:00
"github.com/hashicorp/boundary/api"
apiproxy "github.com/hashicorp/boundary/api/proxy"
"github.com/hashicorp/boundary/api/targets"
2020-09-03 13:24:34 -04:00
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/cmd/base"
targetspb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/targets"
"github.com/hashicorp/boundary/sdk/pbs/proxy"
"github.com/hashicorp/boundary/sdk/wspb"
2020-09-03 13:24:34 -04:00
"github.com/hashicorp/go-cleanhttp"
2021-07-14 17:31:58 -07:00
"github.com/hashicorp/go-secure-stdlib/base62"
2020-09-03 13:24:34 -04:00
"github.com/mitchellh/cli"
2020-09-26 14:01:49 -04:00
"github.com/mr-tron/base58"
2020-09-03 13:24:34 -04:00
"github.com/posener/complete"
"go.uber.org/atomic"
2021-01-21 17:04:01 -05:00
exec "golang.org/x/sys/execabs"
2020-09-20 12:51:08 -04:00
"google.golang.org/protobuf/proto"
2020-09-03 13:24:34 -04:00
"nhooyr.io/websocket"
)
const sessionCancelTimeout = 10 * time.Second
type SessionInfo struct {
Address string `json:"address"`
Port int `json:"port"`
Protocol string `json:"protocol"`
Expiration time.Time `json:"expiration"`
ConnectionLimit int32 `json:"connection_limit"`
SessionId string `json:"session_id"`
Credentials []*targets.SessionCredential `json:"credentials,omitempty"`
}
type ConnectionInfo struct {
ConnectionsLeft int32 `json:"connections_left"`
}
type TerminationInfo struct {
Reason string `json:"termination_reason"`
}
var (
_ cli.Command = (*Command)(nil)
_ cli.CommandAutocomplete = (*Command)(nil)
)
2020-09-03 13:24:34 -04:00
type Command struct {
*base.Command
2020-10-07 21:30:44 -04:00
flagAuthzToken string
2020-09-03 13:24:34 -04:00
flagListenAddr string
flagListenPort int
flagTargetId string
flagTargetName string
flagHostId string
flagExec string
2020-09-25 12:26:16 -04:00
flagUsername string
flagDbname string
2020-10-01 00:27:13 -04:00
// HTTP
httpFlags
2020-12-14 16:52:11 -05:00
// Kube
kubeFlags
2020-09-24 21:16:12 -04:00
// Postgres
postgresFlags
2020-09-24 21:16:12 -04:00
// RDP
rdpFlags
// SSH
sshFlags
2020-09-24 21:16:12 -04:00
Func string
2020-09-20 12:51:08 -04:00
sessionAuthz *targets.SessionAuthorization
2020-10-07 21:30:44 -04:00
sessionAuthzData *targetspb.SessionAuthorizationData
connWg *sync.WaitGroup
listenerCloseOnce sync.Once
listener *net.TCPListener
listenerAddr *net.TCPAddr
connsLeftCh chan int32
connectionsLeft *atomic.Int32
expiration time.Time
execCmdReturnValue *atomic.Int32
proxyCtx context.Context
proxyCancel context.CancelFunc
outputJsonErrors bool
cleanupFuncs []func() error
2020-09-03 13:24:34 -04:00
}
func (c *Command) Synopsis() string {
switch c.Func {
case "connect":
return "Connect to a target through a Boundary worker"
2020-10-01 00:27:13 -04:00
case "http":
return httpSynopsis
2020-09-24 21:16:12 -04:00
case "postgres":
return postgresSynopsis
2020-09-24 21:16:12 -04:00
case "rdp":
return rdpSynopsis
case "ssh":
return sshSynopsis
2020-12-14 16:52:11 -05:00
case "kube":
return kubeSynopsis
default:
return ""
}
2020-09-03 13:24:34 -04:00
}
func (c *Command) Help() string {
switch c.Func {
case "connect":
return base.WrapForHelpText([]string{
"Usage: boundary connect [options] [args]",
"",
2020-10-07 21:30:44 -04:00
` This command performs a target authorization (or consumes an existing authorization token) and launches a proxied connection.`,
"",
" Example:",
"",
2023-05-09 19:00:05 +02:00
` $ boundary connect -target-id ttcp_1234567890`,
"",
"",
}) + c.Flags().Help()
default:
return base.WrapForHelpText([]string{
fmt.Sprintf("Usage: boundary connect %s [options] [args]", c.Func),
"",
fmt.Sprintf(` This command performs a target authorization (or consumes an existing authorization token) and launches a proxied %s connection.`, c.Func),
"",
" Example:",
"",
2023-05-09 19:00:05 +02:00
fmt.Sprintf(` $ boundary connect %s -target-id ttcp_1234567890`, c.Func),
"",
"",
}) + c.Flags().Help()
}
2020-09-03 13:24:34 -04:00
}
func (c *Command) Flags() *base.FlagSets {
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
f := set.NewFlagSet("Connect Options")
f.StringVar(&base.StringVar{
Name: "authz-token",
Target: &c.flagAuthzToken,
EnvVar: "BOUNDARY_CONNECT_AUTHZ_TOKEN",
Completion: complete.PredictNothing,
Usage: `Only needed if -target-id is not set. The authorization string returned from the Boundary controller via an "authorize-session" action against a target. If set to "-", the command will attempt to read in the authorization string from standard input.`,
})
f.StringVar(&base.StringVar{
Name: "target-id",
Target: &c.flagTargetId,
Usage: "The ID of the target to authorize against. Cannot be used with -authz-token.",
})
f.StringVar(&base.StringVar{
Name: "host-id",
Target: &c.flagHostId,
Usage: "The ID of a specific host to connect to out of the hosts from the target's host sets. If not specified, one is chosen at random.",
})
2020-10-07 21:30:44 -04:00
f.StringVar(&base.StringVar{
Name: "exec",
Target: &c.flagExec,
EnvVar: "BOUNDARY_CONNECT_EXEC",
Completion: complete.PredictAnything,
Usage: `If set, after connecting to the worker, the given binary will be executed. This should be a binary on your path, or an absolute path. If all command flags are followed by " -- " (space, two hyphens, space), then any arguments after that will be sent directly to the binary.`,
})
f.StringVar(&base.StringVar{
Name: "target-name",
Target: &c.flagTargetName,
Usage: "Target name, if authorizing the session via scope parameters and target name.",
})
f.StringVar(&base.StringVar{
Name: "target-scope-id",
Target: &c.FlagScopeId,
EnvVar: "BOUNDARY_CONNECT_TARGET_SCOPE_ID",
Completion: complete.PredictAnything,
Usage: "Target scope ID, if authorizing the session via scope parameters and target name. Mutually exclusive with -scope-name.",
})
f.StringVar(&base.StringVar{
Name: "target-scope-name",
Target: &c.FlagScopeName,
EnvVar: "BOUNDARY_CONNECT_TARGET_SCOPE_NAME",
Completion: complete.PredictAnything,
Usage: "Target scope name, if authorizing the session via scope parameters and target name. Mutually exclusive with -scope-id.",
})
switch c.Func {
case "connect":
f.StringVar(&base.StringVar{
Name: "listen-addr",
Target: &c.flagListenAddr,
EnvVar: "BOUNDARY_CONNECT_LISTEN_ADDR",
Completion: complete.PredictAnything,
Usage: `If set, the CLI will attempt to bind its listening address to the given value, which must be an IP address. If it cannot, the command will error. If not set, defaults to the most common IPv4 loopback address (127.0.0.1).`,
})
f.IntVar(&base.IntVar{
Name: "listen-port",
Target: &c.flagListenPort,
EnvVar: "BOUNDARY_CONNECT_LISTEN_PORT",
Completion: complete.PredictAnything,
Usage: `If set, the CLI will attempt to bind its listening port to the given value. If it cannot, the command will error.`,
})
2020-10-01 00:27:13 -04:00
case "http":
httpOptions(c, set)
2020-09-25 12:26:16 -04:00
2020-09-24 21:16:12 -04:00
case "postgres":
postgresOptions(c, set)
2020-09-24 21:16:12 -04:00
case "rdp":
rdpOptions(c, set)
2020-09-24 21:16:12 -04:00
case "ssh":
sshOptions(c, set)
2020-12-14 16:52:11 -05:00
case "kube":
kubeOptions(c, set)
}
2020-09-03 13:24:34 -04:00
return set
}
func (c *Command) AutocompleteArgs() complete.Predictor {
return complete.PredictAnything
}
func (c *Command) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
2020-09-20 12:51:08 -04:00
func (c *Command) Run(args []string) (retCode int) {
var passthroughArgs []string
for i, v := range args {
if v == "--" {
passthroughArgs = args[i+1:]
args = args[:i]
break // only consider the first instance of '--' in the args list
}
}
2020-09-03 13:24:34 -04:00
f := c.Flags()
if err := f.Parse(args); err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(err)
return base.CommandUserError
2020-09-03 13:24:34 -04:00
}
2020-10-07 21:30:44 -04:00
switch {
2020-11-13 07:06:37 -08:00
case c.flagAuthzToken != "":
switch {
case c.flagTargetId != "":
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New(`-target-id and -authz-token cannot both be specified`))
return base.CommandUserError
2020-11-13 07:06:37 -08:00
case c.flagTargetName != "":
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New(`-target-name and -authz-token cannot both be specified`))
return base.CommandUserError
2020-11-13 07:06:37 -08:00
}
default:
if c.flagTargetId == "" &&
(c.flagTargetName == "" ||
(c.FlagScopeId == "" && c.FlagScopeName == "")) {
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New("Target ID was not passed in, but no combination of target name and scope ID/name was passed in either"))
return base.CommandUserError
}
if c.flagTargetId != "" &&
(c.flagTargetName != "" || c.FlagScopeId != "" || c.FlagScopeName != "") {
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New("Cannot specify a target ID and also other lookup parameters"))
return base.CommandUserError
}
2020-10-07 21:30:44 -04:00
}
if c.flagExec == "" {
switch c.Func {
case "http":
c.flagExec = c.httpFlags.defaultExec()
case "ssh":
c.flagExec = c.sshFlags.defaultExec()
case "postgres":
c.flagExec = c.postgresFlags.defaultExec()
case "rdp":
c.flagExec = c.rdpFlags.defaultExec()
2020-12-14 16:52:11 -05:00
case "kube":
c.flagExec = c.kubeFlags.defaultExec()
2020-09-24 21:16:12 -04:00
}
}
2020-09-20 12:51:08 -04:00
tofuToken, err := base62.Random(20)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Could not derive random bytes for tofu token: %w", err))
return base.CommandCliError
}
c.connectionsLeft = atomic.NewInt32(0)
c.connsLeftCh = make(chan int32)
2020-09-03 13:24:34 -04:00
if c.flagListenAddr == "" {
c.flagListenAddr = "127.0.0.1"
}
listenAddr := net.ParseIP(c.flagListenAddr)
if listenAddr == nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Could not successfully parse listen address of %s", c.flagListenAddr))
return base.CommandUserError
2020-09-03 13:24:34 -04:00
}
2020-10-07 21:30:44 -04:00
authzString := c.flagAuthzToken
switch {
case authzString != "":
if authzString == "-" {
authBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("No authorization string was provided and encountered the following error attempting to read it from stdin: %w", err))
return base.CommandUserError
}
if len(authBytes) == 0 {
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New("No authorization data read from stdin"))
return base.CommandUserError
}
authzString = string(authBytes)
}
if authzString == "" {
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New("Authorization data was empty"))
return base.CommandUserError
2020-09-03 13:24:34 -04:00
}
if authzString[0] == '{' {
// Attempt to decode the JSON output of an authorize-session call
// and pull the token out of there
c.sessionAuthz = new(targets.SessionAuthorization)
if err := json.Unmarshal([]byte(authzString), c.sessionAuthz); err == nil {
authzString = c.sessionAuthz.AuthorizationToken
}
}
2020-10-07 21:30:44 -04:00
default:
client, err := c.Client()
if c.WrapperCleanupFunc != nil {
defer func() {
if err := c.WrapperCleanupFunc(); err != nil {
c.PrintCliError(fmt.Errorf("Error cleaning kms wrapper: %w", err))
}
}()
}
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err))
return base.CommandCliError
}
targetClient := targets.NewClient(client)
var opts []targets.Option
if len(c.flagHostId) != 0 {
opts = append(opts, targets.WithHostId(c.flagHostId))
}
if len(c.flagTargetName) > 0 {
opts = append(opts, targets.WithName(c.flagTargetName))
}
if len(c.FlagScopeId) > 0 {
opts = append(opts, targets.WithScopeId(c.FlagScopeId))
}
if len(c.FlagScopeName) > 0 {
opts = append(opts, targets.WithScopeName(c.FlagScopeName))
}
sar, err := targetClient.AuthorizeSession(c.Context, c.flagTargetId, opts...)
if err != nil {
2020-10-06 14:08:49 -07:00
if apiErr := api.AsServerError(err); apiErr != nil {
2021-02-25 12:43:17 -05:00
c.PrintApiError(apiErr, "Error from controller when performing authorize-session action against given target")
return base.CommandApiError
2020-09-24 14:34:58 -07:00
}
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error trying to authorize a session against target: %w", err))
return base.CommandCliError
}
c.sessionAuthz = sar.GetItem().(*targets.SessionAuthorization)
authzString = c.sessionAuthz.AuthorizationToken
}
2020-09-26 14:01:49 -04:00
marshaled, err := base58.FastBase58Decoding(authzString)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Unable to base58-decode authorization data: %w", err))
return base.CommandUserError
2020-09-26 14:01:49 -04:00
}
if len(marshaled) == 0 {
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New("Zero length authorization information after decoding"))
return base.CommandUserError
2020-09-03 13:24:34 -04:00
}
2020-10-07 21:30:44 -04:00
c.sessionAuthzData = new(targetspb.SessionAuthorizationData)
if err := proto.Unmarshal(marshaled, c.sessionAuthzData); err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Unable to proto-decode authorization data: %w", err))
return base.CommandUserError
2020-09-03 13:24:34 -04:00
}
2020-10-07 21:30:44 -04:00
if len(c.sessionAuthzData.GetWorkerInfo()) == 0 {
2021-02-25 12:43:17 -05:00
c.PrintCliError(errors.New("No workers found in authorization string"))
return base.CommandUserError
2020-09-03 13:24:34 -04:00
}
if c.flagListenPort == 0 {
c.flagListenPort = int(c.sessionAuthzData.DefaultClientPort)
}
2020-10-07 21:30:44 -04:00
c.connectionsLeft.Store(c.sessionAuthzData.ConnectionLimit)
workerAddr := c.sessionAuthzData.GetWorkerInfo()[0].GetAddress()
2022-04-01 16:32:20 -04:00
workerHost, _, err := net.SplitHostPort(workerAddr)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
workerHost = workerAddr
} else {
c.PrintCliError(fmt.Errorf("Error splitting worker adddress host/port: %w", err))
return base.CommandUserError
}
}
2020-09-20 12:51:08 -04:00
tlsConf, err := ClientTlsConfig(c.sessionAuthzData, workerHost)
2020-09-03 13:24:34 -04:00
if err != nil {
c.PrintCliError(fmt.Errorf("Error creating TLS configuration: %w", err))
return base.CommandCliError
2020-09-03 13:24:34 -04:00
}
c.expiration = tlsConf.Certificates[0].Leaf.NotAfter
// We don't _rely_ on client-side timeout verification but this prevents us
// seeming to be ready for a connection that will immediately fail when we
// try to actually make it
c.proxyCtx, c.proxyCancel = context.WithDeadline(c.Context, c.expiration)
defer c.proxyCancel()
2020-09-03 13:24:34 -04:00
transport := cleanhttp.DefaultTransport()
transport.DisableKeepAlives = false
// This isn't/shouldn't used anyways really because the connection is
// hijacked, just setting for completeness
transport.IdleConnTimeout = 0
2022-04-01 16:32:20 -04:00
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &tls.Dialer{Config: tlsConf}
return dialer.DialContext(ctx, network, addr)
}
2020-09-03 13:24:34 -04:00
c.listener, err = net.ListenTCP("tcp", &net.TCPAddr{
2020-09-03 13:24:34 -04:00
IP: listenAddr,
Port: c.flagListenPort,
})
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error starting listening port: %w", err))
return base.CommandCliError
2020-09-03 13:24:34 -04:00
}
2020-09-20 12:51:08 -04:00
listenerCloseFunc := func() {
// Forces the for loop to exist instead of spinning on errors
c.connectionsLeft.Store(0)
if err := c.listener.Close(); err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error closing listener on shutdown: %w", err))
retCode = 2
2020-09-20 12:51:08 -04:00
}
}
// Ensure it runs on any other return condition
defer func() {
c.listenerCloseOnce.Do(listenerCloseFunc)
2020-09-20 12:51:08 -04:00
}()
c.listenerAddr = c.listener.Addr().(*net.TCPAddr)
2020-09-20 12:51:08 -04:00
if c.Func == "connect" {
// "connect" indicates there is no subcommand to the connect function.
// The only way a user will be able to connect to the session is by
// connecting directly to the port and address we report to them here.
var creds []*targets.SessionCredential
if c.sessionAuthz != nil && len(c.sessionAuthz.Credentials) > 0 {
creds = c.sessionAuthz.Credentials
}
sessInfo := SessionInfo{
Protocol: c.sessionAuthzData.GetType(),
Address: c.listenerAddr.IP.String(),
Port: c.listenerAddr.Port,
Expiration: c.expiration,
2020-10-07 21:30:44 -04:00
ConnectionLimit: c.sessionAuthzData.GetConnectionLimit(),
SessionId: c.sessionAuthzData.GetSessionId(),
2021-06-29 06:59:31 -04:00
Credentials: creds,
}
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateSessionInfoTableOutput(sessInfo))
case "json":
out, err := json.Marshal(&sessInfo)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("error marshaling session information: %w", err))
return base.CommandCliError
}
c.UI.Output(string(out))
2020-09-20 12:51:08 -04:00
}
}
c.connWg = new(sync.WaitGroup)
2020-09-20 12:51:08 -04:00
c.connWg.Add(1)
go func() {
defer c.connWg.Done()
for {
listeningConn, err := c.listener.AcceptTCP()
if err != nil {
select {
case <-c.proxyCtx.Done():
return
case <-c.Context.Done():
return
default:
2020-10-07 11:32:10 -04:00
// When this hits zero we trigger listener close so this
// isn't actually an error condition
if c.connectionsLeft.Load() == 0 {
return
}
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error accepting connection: %w", err))
continue
}
2020-09-20 12:51:08 -04:00
}
c.connWg.Add(1)
go func() {
defer listeningConn.Close()
defer c.connWg.Done()
wsConn, err := c.getWsConn(
c.proxyCtx,
workerAddr,
transport)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(err)
} else {
if err := c.runTcpProxyV1(wsConn, listeningConn, tofuToken); err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(err)
}
}
}()
2020-09-20 12:51:08 -04:00
}
}()
timer := time.NewTimer(time.Until(c.expiration))
c.connWg.Add(1)
go func() {
defer c.connWg.Done()
defer c.listenerCloseOnce.Do(listenerCloseFunc)
for {
select {
case <-c.proxyCtx.Done():
timer.Stop()
return
case <-c.Context.Done():
timer.Stop()
return
case <-timer.C:
return
case connsLeft := <-c.connsLeftCh:
c.updateConnsLeft(connsLeft)
if connsLeft == 0 {
return
}
2020-09-20 12:51:08 -04:00
}
}
}()
if c.flagExec != "" {
c.connWg.Add(1)
c.execCmdReturnValue = atomic.NewInt32(0)
go c.handleExec(passthroughArgs)
}
c.connWg.Wait()
if c.execCmdReturnValue != nil {
retCode = int(c.execCmdReturnValue.Load())
}
termInfo := TerminationInfo{Reason: "Unknown"}
sendSessionCancel := false
select {
case <-c.Context.Done():
termInfo.Reason = "Received shutdown signal"
sendSessionCancel = true
default:
if c.execCmdReturnValue != nil {
// Don't print out in this case, so ensure we clear it
termInfo.Reason = ""
sendSessionCancel = true
} else if !timer.Stop() {
termInfo.Reason = "Session has expired"
} else {
if c.connectionsLeft.Load() == 0 {
termInfo.Reason = "No connections left in session"
}
}
}
// Only send it if we should, and also if we're not after expiration, with a
// bit of buffer in case clocks are not quite the same between worker and
// this machine.
if sendSessionCancel && time.Now().Before(c.expiration.Add(-5*time.Minute)) {
ctx, cancel := context.WithTimeout(context.Background(), sessionCancelTimeout)
wsConn, err := c.getWsConn(ctx, workerAddr, transport)
if err != nil {
2023-11-08 12:30:54 -05:00
c.PrintCliError(fmt.Errorf("Error fetching connection to send session teardown request to worker: %w", err))
} else {
if err := c.sendSessionTeardown(ctx, wsConn, tofuToken); err != nil {
2023-11-08 12:30:54 -05:00
c.PrintCliError(fmt.Errorf("Error sending session teardown request to worker: %w", err))
}
}
cancel()
}
for _, f := range c.cleanupFuncs {
if err := f(); err != nil {
c.PrintCliError(err)
}
}
2020-10-01 00:27:13 -04:00
if termInfo.Reason != "" {
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateTerminationInfoTableOutput(termInfo))
case "json":
out, err := json.Marshal(&termInfo)
if err != nil {
2023-11-08 12:30:54 -05:00
c.PrintCliError(fmt.Errorf("Error marshaling termination information: %w", err))
return base.CommandCliError
2020-10-01 00:27:13 -04:00
}
c.UI.Output(string(out))
}
2020-09-20 12:51:08 -04:00
}
return
2020-09-20 12:51:08 -04:00
}
func (c *Command) printCredentials(creds []*targets.SessionCredential) error {
if len(creds) == 0 {
return nil
}
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateCredentialTableOutput(creds))
case "json":
out, err := json.Marshal(&struct {
Credentials []*targets.SessionCredential `json:"credentials"`
}{
Credentials: creds,
})
if err != nil {
2023-11-08 12:30:54 -05:00
return fmt.Errorf("Error marshaling credential information: %w", err)
}
c.UI.Output(string(out))
}
return nil
}
func (c *Command) getWsConn(
ctx context.Context,
2020-09-20 12:51:08 -04:00
workerAddr string,
transport *http.Transport,
) (*websocket.Conn, error) {
2020-09-03 13:24:34 -04:00
conn, resp, err := websocket.Dial(
ctx,
2022-04-01 16:32:20 -04:00
fmt.Sprintf("ws://%s/v1/proxy", workerAddr),
2020-09-03 13:24:34 -04:00
&websocket.DialOptions{
HTTPClient: &http.Client{
Transport: transport,
},
Subprotocols: []string{globals.TcpProxyV1},
},
)
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")
2020-09-03 13:24:34 -04:00
case strings.Contains(err.Error(), "connect: connection refused"):
return nil, fmt.Errorf("Unable to connect to worker at %s", workerAddr)
2020-09-03 13:24:34 -04:00
default:
return nil, fmt.Errorf("Error dialing the worker: %w", err)
2020-09-03 13:24:34 -04:00
}
}
if resp == nil {
return nil, errors.New("Response from worker is nil")
2020-09-03 13:24:34 -04:00
}
if resp.Header == nil {
return nil, errors.New("Response header is nil")
2020-09-03 13:24:34 -04:00
}
negProto := resp.Header.Get("Sec-WebSocket-Protocol")
if negProto != globals.TcpProxyV1 {
return nil, fmt.Errorf("Unexpected negotiated protocol: %s", negProto)
}
return conn, nil
}
func (c *Command) sendSessionTeardown(
ctx context.Context,
wsConn *websocket.Conn,
tofuToken string,
) error {
handshake := proxy.ClientHandshake{
TofuToken: tofuToken,
Command: proxy.HANDSHAKECOMMAND_HANDSHAKECOMMAND_SESSION_CANCEL,
}
if err := wspb.Write(ctx, wsConn, &handshake); err != nil {
return fmt.Errorf("error sending teardown handshake to worker: %w", err)
2020-09-03 13:24:34 -04:00
}
return nil
}
func (c *Command) runTcpProxyV1(
wsConn *websocket.Conn,
listeningConn *net.TCPConn,
tofuToken string,
) error {
2020-09-20 12:51:08 -04:00
handshake := proxy.ClientHandshake{TofuToken: tofuToken}
if err := wspb.Write(c.proxyCtx, wsConn, &handshake); err != nil {
2023-11-08 12:30:54 -05:00
return fmt.Errorf("Error sending handshake to worker: %w", err)
}
var handshakeResult proxy.HandshakeResult
if err := wspb.Read(c.proxyCtx, 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
c.connsLeftCh <- 0
return errors.New("Unable to authorize connection")
}
2020-10-07 21:30:44 -04:00
switch {
case strings.Contains(err.Error(), "tofu token not allowed"):
// Nothing will be able to be done here, so cancel the context too
c.proxyCancel()
return errors.New("Session token has already been used")
2020-10-07 21:30:44 -04:00
default:
2023-11-08 12:30:54 -05:00
// If we can't handshake we can't do anything, so quit out
c.proxyCancel()
return fmt.Errorf("Error reading handshake result: %w", err)
2020-10-07 21:30:44 -04:00
}
}
if handshakeResult.GetConnectionsLeft() != -1 {
c.connsLeftCh <- handshakeResult.GetConnectionsLeft()
}
2020-09-03 13:24:34 -04:00
// Get a wrapped net.Conn so we can use io.Copy
netConn := websocket.NetConn(c.proxyCtx, wsConn, websocket.MessageBinary)
2020-09-03 13:24:34 -04:00
2020-09-20 12:51:08 -04:00
localWg := new(sync.WaitGroup)
localWg.Add(2)
2020-09-03 13:24:34 -04:00
go func() {
2020-09-20 12:51:08 -04:00
defer localWg.Done()
io.Copy(netConn, listeningConn)
2020-09-03 13:24:34 -04:00
netConn.Close()
listeningConn.Close()
}()
go func() {
2020-09-20 12:51:08 -04:00
defer localWg.Done()
io.Copy(listeningConn, netConn)
2020-09-03 13:24:34 -04:00
listeningConn.Close()
netConn.Close()
}()
2020-09-20 12:51:08 -04:00
localWg.Wait()
return nil
2020-09-03 13:24:34 -04:00
}
func (c *Command) updateConnsLeft(connsLeft int32) {
c.connectionsLeft.Store(connsLeft)
connInfo := ConnectionInfo{
ConnectionsLeft: connsLeft,
}
if c.flagExec == "" {
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateConnectionInfoTableOutput(connInfo))
case "json":
out, err := json.Marshal(&connInfo)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("error marshaling connection information: %w", err))
}
c.UI.Output(string(out))
}
}
}
func (c *Command) handleExec(passthroughArgs []string) {
defer c.connWg.Done()
defer c.proxyCancel()
port := strconv.Itoa(c.listenerAddr.Port)
ip := c.listenerAddr.IP.String()
addr := c.listenerAddr.String()
var args []string
var envs []string
var argsErr error
var creds apiproxy.Credentials
if c.sessionAuthz != nil {
var err error
creds, err = apiproxy.ParseCredentials(c.sessionAuthz.Credentials)
if err != nil {
c.PrintCliError(fmt.Errorf("Error interpreting secret: %w", err))
c.execCmdReturnValue.Store(int32(3))
return
}
}
switch c.Func {
2020-10-01 00:27:13 -04:00
case "http":
2020-12-09 14:45:53 -05:00
httpArgs, err := c.httpFlags.buildArgs(c, port, ip, addr)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error parsing session args: %w", err))
2020-12-09 14:45:53 -05:00
c.execCmdReturnValue.Store(int32(3))
return
}
args = append(args, httpArgs...)
2020-09-24 21:16:12 -04:00
case "postgres":
pgArgs, pgEnvs, pgCreds, pgErr := c.postgresFlags.buildArgs(c, port, ip, addr, creds)
if pgErr != nil {
argsErr = pgErr
break
}
args = append(args, pgArgs...)
envs = append(envs, pgEnvs...)
creds = pgCreds
2020-09-24 21:16:12 -04:00
case "rdp":
args = append(args, c.rdpFlags.buildArgs(c, port, ip, addr)...)
case "ssh":
sshArgs, sshEnvs, sshCreds, sshErr := c.sshFlags.buildArgs(c, port, ip, addr, creds)
if sshErr != nil {
argsErr = sshErr
break
}
args = append(args, sshArgs...)
envs = append(envs, sshEnvs...)
creds = sshCreds
2020-12-14 16:52:11 -05:00
case "kube":
kubeArgs, err := c.kubeFlags.buildArgs(c, port, ip, addr)
if err != nil {
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Error parsing session args: %w", err))
2020-12-14 16:52:11 -05:00
c.execCmdReturnValue.Store(int32(3))
return
}
args = append(args, kubeArgs...)
}
if argsErr != nil {
c.PrintCliError(fmt.Errorf("Failed to collect args: %w", argsErr))
c.execCmdReturnValue.Store(int32(2))
return
}
if err := c.printCredentials(creds.UnconsumedSessionCredentials()); err != nil {
c.PrintCliError(fmt.Errorf("Failed to print credentials: %w", err))
c.execCmdReturnValue.Store(int32(2))
return
}
args = append(passthroughArgs, args...)
stringReplacer := func(in, typ, replacer string) string {
for _, style := range []string{
fmt.Sprintf("{{boundary.%s}}", typ),
fmt.Sprintf("{{ boundary.%s}}", typ),
fmt.Sprintf("{{boundary.%s }}", typ),
fmt.Sprintf("{{ boundary.%s }}", typ),
} {
in = strings.Replace(in, style, replacer, -1)
}
return in
}
for i := range args {
args[i] = stringReplacer(args[i], "port", port)
args[i] = stringReplacer(args[i], "ip", ip)
args[i] = stringReplacer(args[i], "addr", addr)
}
// NOTE: exec.CommandContext is a hard kill, so if used it leaves the
// terminal in a weird state. It suffices to simply close the connection,
// which already happens, so we don't need/want CommandContext here.
cmd := exec.Command(c.flagExec, args...)
// Add original and network related envs here
cmd.Env = append(os.Environ(),
fmt.Sprintf("BOUNDARY_PROXIED_PORT=%s", port),
fmt.Sprintf("BOUNDARY_PROXIED_IP=%s", ip),
fmt.Sprintf("BOUNDARY_PROXIED_ADDR=%s", addr),
)
// Envs that came from subcommand handling
cmd.Env = append(cmd.Env, envs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
exitCode := 2
if exitError, ok := err.(*exec.ExitError); ok {
if exitError.Success() {
c.execCmdReturnValue.Store(0)
return
}
if ws, ok := exitError.Sys().(syscall.WaitStatus); ok {
c.execCmdReturnValue.Store(int32(ws.ExitStatus()))
return
}
}
2021-02-25 12:43:17 -05:00
c.PrintCliError(fmt.Errorf("Failed to run command: %w", err))
c.execCmdReturnValue.Store(int32(exitCode))
return
}
c.execCmdReturnValue.Store(0)
}