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 1 commit
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
29 changes: 19 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"
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",
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
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
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