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

Commit 54902a7

Browse files
committed
Fix auth for p2p
1 parent 7989159 commit 54902a7

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

internal/cmd/agent.go

Lines changed: 32 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,31 +39,42 @@ 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
}
@@ -79,6 +89,14 @@ coder agent start https://my-coder.com
7989
}
8090
}
8191

92+
if token == "" {
93+
var ok bool
94+
token, ok = os.LookupEnv("CODER_AGENT_TOKEN")
95+
if !ok {
96+
return xerrors.New("must pass --token or set the CODER_AGENT_TOKEN env variable")
97+
}
98+
}
99+
82100
q := u.Query()
83101
q.Set("service_token", token)
84102
u.RawQuery = q.Encode()
@@ -112,6 +130,8 @@ coder agent start https://my-coder.com
112130
}
113131

114132
cmd.Flags().StringVar(&token, "token", "", "coder agent token")
133+
cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url")
134+
115135
return cmd
116136
}
117137

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)