Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add coder tunnel command #313

Merged
merged 2 commits into from
Apr 8, 2021
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
Prev Previous commit
Fix auth for p2p
  • Loading branch information
f0ssel committed Apr 8, 2021
commit 54902a7d4aa33b75d885e7f783e8a518ae8a22dc
44 changes: 32 additions & 12 deletions internal/cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"golang.org/x/xerrors"
"nhooyr.io/websocket"

"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/internal/x/xwebrtc"
"cdr.dev/coder-cli/pkg/proto"
)
Expand All @@ -40,31 +39,42 @@ func agentCmd() *cobra.Command {

func startCmd() *cobra.Command {
var (
token string
token string
coderURL string
)
cmd := &cobra.Command{
Use: "start [coderURL] --token=[token]",
Args: xcobra.ExactArgs(1),
Use: "start --coder-url=[coder_url] --token=[token]",
Short: "starts the coder agent",
Long: "starts the coder agent",
Example: `# start the agent and connect with a Coder agent token
Example: `# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars

coder agent start https://my-coder.com --token xxxx-xxxx
coder agent start

# start the agent and use CODER_AGENT_TOKEN env var for auth token
# start the agent and connect with a specified url and agent token

coder agent start https://my-coder.com
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
log := slog.Make(sloghuman.Sink(cmd.OutOrStdout()))

// Pull the URL from the args and do some sanity check.
rawURL := args[0]
if rawURL == "" || !strings.HasPrefix(rawURL, "http") {
if coderURL == "" {
var ok bool
token, ok = os.LookupEnv("CODER_URL")
if !ok {
client, err := newClient(ctx)
if err != nil {
return xerrors.New("must login, pass --coder-url flag, or set the CODER_URL env variable")
}
burl := client.BaseURL()
coderURL = burl.String()
}
}

if !strings.HasPrefix(coderURL, "http") {
return xerrors.Errorf("invalid URL")
}
u, err := url.Parse(rawURL)
u, err := url.Parse(coderURL)
if err != nil {
return xerrors.Errorf("parse url: %w", err)
}
Expand All @@ -79,6 +89,14 @@ coder agent start https://my-coder.com
}
}

if token == "" {
var ok bool
token, ok = os.LookupEnv("CODER_AGENT_TOKEN")
if !ok {
return xerrors.New("must pass --token or set the CODER_AGENT_TOKEN env variable")
}
}

q := u.Query()
q.Set("service_token", token)
u.RawQuery = q.Encode()
Expand Down Expand Up @@ -112,6 +130,8 @@ coder agent start https://my-coder.com
}

cmd.Flags().StringVar(&token, "token", "", "coder agent token")
cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url")

return cmd
}

Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,15 @@ func makeSSHConfig(host, userName, envName, privateKeyFilepath string, p2p bool)
return fmt.Sprintf(
`Host coder.%s
HostName localhost
User %s-%s
ProxyCommand go run cmd/coder/main.go tunnel %s 22 stdio
ProxyCommand coder tunnel %s 22 stdio
StrictHostKeyChecking no
ConnectTimeout=0
IdentitiesOnly yes
IdentityFile="%s"
ServerAliveInterval 60
ServerAliveCountMax 3
`, envName, userName, envName, envName, privateKeyFilepath)
`, envName, envName, privateKeyFilepath)
}

return fmt.Sprintf(
`Host coder.%s
HostName %s
Expand Down