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

Commit 3ccb7dd

Browse files
committed
Fix auth for p2p
1 parent 7989159 commit 3ccb7dd

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

internal/cmd/agent.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"golang.org/x/xerrors"
2020
"nhooyr.io/websocket"
2121

22-
"cdr.dev/coder-cli/internal/x/xcobra"
2322
"cdr.dev/coder-cli/internal/x/xwebrtc"
2423
"cdr.dev/coder-cli/pkg/proto"
2524
)
@@ -40,37 +39,57 @@ func agentCmd() *cobra.Command {
4039

4140
func startCmd() *cobra.Command {
4241
var (
43-
token string
42+
token string
43+
coderURL string
4444
)
4545
cmd := &cobra.Command{
46-
Use: "start [coderURL] --token=[token]",
47-
Args: xcobra.ExactArgs(1),
46+
Use: "start --coder-url=[coder_url] --token=[token]",
4847
Short: "starts the coder agent",
4948
Long: "starts the coder agent",
50-
Example: `# start the agent and connect with a Coder agent token
49+
Example: `# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars
5150
52-
coder agent start https://my-coder.com --token xxxx-xxxx
51+
coder agent start
5352
54-
# start the agent and use CODER_AGENT_TOKEN env var for auth token
53+
# start the agent and connect with a specified url and agent token
5554
56-
coder agent start https://my-coder.com
55+
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
5756
`,
5857
RunE: func(cmd *cobra.Command, args []string) error {
5958
ctx := cmd.Context()
6059
log := slog.Make(sloghuman.Sink(cmd.OutOrStdout()))
6160

62-
// Pull the URL from the args and do some sanity check.
63-
rawURL := args[0]
64-
if rawURL == "" || !strings.HasPrefix(rawURL, "http") {
61+
if coderURL == "" {
62+
var ok bool
63+
token, ok = os.LookupEnv("CODER_URL")
64+
if !ok {
65+
client, err := newClient(ctx)
66+
if err != nil {
67+
return xerrors.New("must login, pass --coder-url flag, or set the CODER_URL env variable")
68+
}
69+
burl := client.BaseURL()
70+
coderURL = burl.String()
71+
}
72+
}
73+
74+
if !strings.HasPrefix(coderURL, "http") {
6575
return xerrors.Errorf("invalid URL")
6676
}
67-
u, err := url.Parse(rawURL)
77+
u, err := url.Parse(coderURL)
6878
if err != nil {
6979
return xerrors.Errorf("parse url: %w", err)
7080
}
7181
// Remove the trailing '/' if any.
7282
u.Path = "/api/private/envagent/listen"
7383

84+
85+
if token == "" {
86+
var ok bool
87+
token, ok = os.LookupEnv("CODER_AGENT_TOKEN")
88+
if !ok {
89+
return xerrors.New("must pass --token or set the CODER_AGENT_TOKEN env variable")
90+
}
91+
}
92+
7493
if token == "" {
7594
var ok bool
7695
token, ok = os.LookupEnv("CODER_AGENT_TOKEN")
@@ -112,6 +131,8 @@ coder agent start https://my-coder.com
112131
}
113132

114133
cmd.Flags().StringVar(&token, "token", "", "coder agent token")
134+
cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url")
135+
115136
return cmd
116137
}
117138

internal/cmd/configssh.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,15 @@ func makeSSHConfig(host, userName, envName, privateKeyFilepath string, p2p bool)
206206
return fmt.Sprintf(
207207
`Host coder.%s
208208
HostName localhost
209-
User %s-%s
210-
ProxyCommand go run cmd/coder/main.go tunnel %s 22 stdio
209+
ProxyCommand coder tunnel %s 22 stdio
211210
StrictHostKeyChecking no
212211
ConnectTimeout=0
213-
IdentitiesOnly yes
214212
IdentityFile="%s"
215213
ServerAliveInterval 60
216214
ServerAliveCountMax 3
217-
`, envName, userName, envName, envName, privateKeyFilepath)
215+
`, envName, envName, privateKeyFilepath)
218216
}
217+
219218
return fmt.Sprintf(
220219
`Host coder.%s
221220
HostName %s

0 commit comments

Comments
 (0)