Skip to content

feat: add --disable-direct-connections flag to CLI #8131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cli/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ func (r *RootCmd) ping() *clibase.Cmd {
logger = slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelDebug)
}

conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{Logger: logger})
if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
}
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger,
BlockEndpoints: r.disableDirect,
})
if err != nil {
return err
}
Expand Down
16 changes: 15 additions & 1 deletion cli/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"github.com/pion/udp"
"golang.org/x/xerrors"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"

"github.com/coder/coder/agent/agentssh"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/cliui"
Expand Down Expand Up @@ -98,7 +101,18 @@ func (r *RootCmd) portForward() *clibase.Cmd {
return xerrors.Errorf("await agent: %w", err)
}

conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, nil)
var logger slog.Logger
if r.verbose {
logger = slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelDebug)
}

if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
}
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger,
BlockEndpoints: r.disableDirect,
})
if err != nil {
return err
}
Expand Down
30 changes: 20 additions & 10 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
varNoFeatureWarning = "no-feature-warning"
varForceTty = "force-tty"
varVerbose = "verbose"
varDisableDirect = "disable-direct-connections"
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."

envNoVersionCheck = "CODER_NO_VERSION_WARNING"
Expand Down Expand Up @@ -366,6 +367,13 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
Value: clibase.BoolOf(&r.verbose),
Group: globalGroup,
},
{
Flag: varDisableDirect,
Env: "CODER_DISABLE_DIRECT_CONNECTIONS",
Description: "Disable direct (P2P) connections to workspaces.",
Value: clibase.BoolOf(&r.disableDirect),
Group: globalGroup,
},
{
Flag: "debug-http",
Description: "Debug codersdk HTTP requests.",
Expand Down Expand Up @@ -412,16 +420,17 @@ func isTest() bool {

// RootCmd contains parameters and helpers useful to all commands.
type RootCmd struct {
clientURL *url.URL
token string
globalConfig string
header []string
agentToken string
agentURL *url.URL
forceTTY bool
noOpen bool
verbose bool
debugHTTP bool
clientURL *url.URL
token string
globalConfig string
header []string
agentToken string
agentURL *url.URL
forceTTY bool
noOpen bool
verbose bool
disableDirect bool
debugHTTP bool

noVersionCheck bool
noFeatureWarning bool
Expand Down Expand Up @@ -523,6 +532,7 @@ func (r *RootCmd) InitClient(client *codersdk.Client) clibase.MiddlewareFunc {
client.PlainLogger = os.Stderr
client.LogBodies = true
}
client.DisableDirectConnections = r.disableDirect

// We send these requests in parallel to minimize latency.
var (
Expand Down
5 changes: 5 additions & 0 deletions cli/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ func (r *RootCmd) speedtest() *clibase.Cmd {
if err != nil && !xerrors.Is(err, cliui.AgentStartError) {
return xerrors.Errorf("await agent: %w", err)
}

logger, ok := LoggerFromContext(ctx)
if !ok {
logger = slog.Make(sloghuman.Sink(inv.Stderr))
}
if r.verbose {
logger = logger.Leveled(slog.LevelDebug)
}

if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
}
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger,
})
Expand Down
6 changes: 5 additions & 1 deletion cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ func (r *RootCmd) ssh() *clibase.Cmd {
// We don't print the error because cliui.Agent does that for us.
}

if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
}
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger,
Logger: logger,
BlockEndpoints: r.disableDirect,
})
if err != nil {
return xerrors.Errorf("dial agent: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ variables or flags.
--debug-options bool
Print all options, how they're set, then exit.

--disable-direct-connections bool, $CODER_DISABLE_DIRECT_CONNECTIONS
Disable direct (P2P) connections to workspaces.

--global-config string, $CODER_CONFIG_DIR (default: ~/.config/coderv2)
Path to the global `coder` config directory.

Expand Down
16 changes: 15 additions & 1 deletion cli/vscodessh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"tailscale.com/tailcfg"
"tailscale.com/types/netlogtype"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"

"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/codersdk"
)
Expand Down Expand Up @@ -126,7 +129,18 @@ func (r *RootCmd) vscodeSSH() *clibase.Cmd {
}
}

agentConn, err := client.DialWorkspaceAgent(ctx, agent.ID, &codersdk.DialWorkspaceAgentOptions{})
var logger slog.Logger
if r.verbose {
logger = slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelDebug)
}

if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
}
agentConn, err := client.DialWorkspaceAgent(ctx, agent.ID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger,
BlockEndpoints: r.disableDirect,
})
if err != nil {
return xerrors.Errorf("dial workspace agent: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ type Client struct {
// Trace can be enabled to propagate tracing spans to the Coder API.
// This is useful for tracking a request end-to-end.
Trace bool

// DisableDirectConnections forces any connections to workspaces to go
// through DERP, regardless of the BlockEndpoints setting on each
// connection.
DisableDirectConnections bool
}

// SessionToken returns the currently set token for the client.
Expand Down
5 changes: 3 additions & 2 deletions codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ type WorkspaceAgentConnectionInfo struct {
// @typescript-ignore DialWorkspaceAgentOptions
type DialWorkspaceAgentOptions struct {
Logger slog.Logger
// BlockEndpoints forced a direct connection through DERP.
// BlockEndpoints forced a direct connection through DERP. The Client may
// have DisableDirect set which will override this value.
BlockEndpoints bool
}

Expand Down Expand Up @@ -208,7 +209,7 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
DERPMap: connInfo.DERPMap,
DERPHeader: &header,
Logger: options.Logger,
BlockEndpoints: options.BlockEndpoints,
BlockEndpoints: c.DisableDirectConnections || options.BlockEndpoints,
})
if err != nil {
return nil, xerrors.Errorf("create tailnet: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ Coder — A tool for provisioning self-hosted development environments with Terr

Print all options, how they're set, then exit.

### --disable-direct-connections

| | |
| ----------- | ---------------------------------------------- |
| Type | <code>bool</code> |
| Environment | <code>$CODER_DISABLE_DIRECT_CONNECTIONS</code> |

Disable direct (P2P) connections to workspaces.

### --global-config

| | |
Expand Down
3 changes: 3 additions & 0 deletions enterprise/cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ variables or flags.
--debug-options bool
Print all options, how they're set, then exit.

--disable-direct-connections bool, $CODER_DISABLE_DIRECT_CONNECTIONS
Disable direct (P2P) connections to workspaces.

--global-config string, $CODER_CONFIG_DIR (default: ~/.config/coderv2)
Path to the global `coder` config directory.

Expand Down