Skip to content

Commit 54f6878

Browse files
committed
refactor agent auth options into it's own type
1 parent 80aa609 commit 54f6878

30 files changed

+176
-141
lines changed

agent/agenttest/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func New(t testing.TB, coderURL *url.URL, agentToken string, opts ...func(*agent
3030
}
3131

3232
if o.Client == nil {
33-
agentClient := agentsdk.New(coderURL, agentsdk.UsingFixedToken(agentToken))
33+
agentClient := agentsdk.New(coderURL, agentsdk.WithFixedToken(agentToken))
3434
agentClient.SDK.SetLogger(log)
3535
o.Client = agentClient
3636
}

cli/agent.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"github.com/coder/coder/v2/codersdk/agentsdk"
3838
)
3939

40-
func (r *RootCmd) workspaceAgent() *serpent.Command {
40+
func workspaceAgent() *serpent.Command {
4141
var (
4242
logDir string
4343
scriptDataDir string
@@ -57,6 +57,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
5757
devcontainerProjectDiscovery bool
5858
devcontainerDiscoveryAutostart bool
5959
)
60+
agentAuth := NewAgentAuth()
6061
cmd := &serpent.Command{
6162
Use: "agent",
6263
Short: `Starts the Coder workspace agent.`,
@@ -174,11 +175,11 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
174175

175176
version := buildinfo.Version()
176177
logger.Info(ctx, "agent is starting now",
177-
slog.F("url", r.agentURL),
178-
slog.F("auth", r.agentAuth),
178+
slog.F("url", agentAuth.agentURL),
179+
slog.F("auth", agentAuth.agentAuth),
179180
slog.F("version", version),
180181
)
181-
client, err := r.createAgentClient(ctx)
182+
client, err := agentAuth.CreateClient(ctx)
182183
if err != nil {
183184
return xerrors.Errorf("create agent client: %w", err)
184185
}
@@ -190,7 +191,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
190191
client.SDK.HTTPClient.Timeout = 30 * time.Second
191192
// Attach header transport so we process --agent-header and
192193
// --agent-header-command flags
193-
headerTransport, err := headerTransport(ctx, r.agentURL, agentHeader, agentHeaderCommand)
194+
headerTransport, err := headerTransport(ctx, agentAuth.agentURL, agentHeader, agentHeaderCommand)
194195
if err != nil {
195196
return xerrors.Errorf("configure header transport: %w", err)
196197
}
@@ -292,7 +293,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
292293
Execer: execer,
293294
Devcontainers: devcontainers,
294295
DevcontainerAPIOptions: []agentcontainers.Option{
295-
agentcontainers.WithSubAgentURL(r.agentURL.String()),
296+
agentcontainers.WithSubAgentURL(agentAuth.agentURL.String()),
296297
agentcontainers.WithProjectDiscovery(devcontainerProjectDiscovery),
297298
agentcontainers.WithDiscoveryAutostart(devcontainerDiscoveryAutostart),
298299
},
@@ -449,7 +450,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
449450
Value: serpent.BoolOf(&devcontainerDiscoveryAutostart),
450451
},
451452
}
452-
453+
agentAuth.AttachOptions(cmd, false)
453454
return cmd
454455
}
455456

cli/exp_mcp.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (r *RootCmd) mcpConfigure() *serpent.Command {
5656
},
5757
Children: []*serpent.Command{
5858
r.mcpConfigureClaudeDesktop(),
59-
r.mcpConfigureClaudeCode(),
59+
mcpConfigureClaudeCode(),
6060
r.mcpConfigureCursor(),
6161
},
6262
}
@@ -117,7 +117,7 @@ func (*RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
117117
return cmd
118118
}
119119

120-
func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
120+
func mcpConfigureClaudeCode() *serpent.Command {
121121
var (
122122
claudeAPIKey string
123123
claudeConfigPath string
@@ -131,6 +131,7 @@ func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
131131

132132
deprecatedCoderMCPClaudeAPIKey string
133133
)
134+
agentAuth := NewAgentAuth()
134135
cmd := &serpent.Command{
135136
Use: "claude-code <project-directory>",
136137
Short: "Configure the Claude Code server. You will need to run this command for each project you want to use. Specify the project directory as the first argument.",
@@ -148,7 +149,7 @@ func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
148149
binPath = testBinaryName
149150
}
150151
configureClaudeEnv := map[string]string{}
151-
agentClient, err := r.createAgentClient(inv.Context())
152+
agentClient, err := agentAuth.CreateClient(inv.Context())
152153
if err != nil {
153154
cliui.Warnf(inv.Stderr, "failed to create agent client: %s", err)
154155
} else {
@@ -292,6 +293,7 @@ func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
292293
},
293294
},
294295
}
296+
agentAuth.AttachOptions(cmd, false)
295297
return cmd
296298
}
297299

@@ -403,7 +405,8 @@ func (r *RootCmd) mcpServer() *serpent.Command {
403405
appStatusSlug string
404406
aiAgentAPIURL url.URL
405407
)
406-
return &serpent.Command{
408+
agentAuth := NewAgentAuth()
409+
cmd := &serpent.Command{
407410
Use: "server",
408411
Handler: func(inv *serpent.Invocation) error {
409412
var lastReport taskReport
@@ -494,7 +497,7 @@ func (r *RootCmd) mcpServer() *serpent.Command {
494497
}
495498

496499
// Try to create an agent client for status reporting. Not validated.
497-
agentClient, err := r.createAgentClient(inv.Context())
500+
agentClient, err := agentAuth.CreateClient(inv.Context())
498501
if err == nil {
499502
cliui.Infof(inv.Stderr, "Agent URL : %s", agentClient.SDK.URL.String())
500503
srv.agentClient = agentClient
@@ -579,6 +582,8 @@ func (r *RootCmd) mcpServer() *serpent.Command {
579582
},
580583
},
581584
}
585+
agentAuth.AttachOptions(cmd, false)
586+
return cmd
582587
}
583588

584589
func (s *mcpServer) startReporter(ctx context.Context, inv *serpent.Invocation) {

cli/externalauth.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@ package cli
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
7-
"golang.org/x/xerrors"
85

96
"github.com/tidwall/gjson"
7+
"golang.org/x/xerrors"
108

119
"github.com/coder/coder/v2/cli/cliui"
1210
"github.com/coder/coder/v2/codersdk/agentsdk"
13-
"github.com/coder/pretty"
1411
"github.com/coder/serpent"
1512
)
1613

17-
func (r *RootCmd) externalAuth() *serpent.Command {
14+
func externalAuth() *serpent.Command {
1815
return &serpent.Command{
1916
Use: "external-auth",
2017
Short: "Manage external authentication",
@@ -23,14 +20,15 @@ func (r *RootCmd) externalAuth() *serpent.Command {
2320
return i.Command.HelpHandler(i)
2421
},
2522
Children: []*serpent.Command{
26-
r.externalAuthAccessToken(),
23+
externalAuthAccessToken(),
2724
},
2825
}
2926
}
3027

31-
func (r *RootCmd) externalAuthAccessToken() *serpent.Command {
28+
func externalAuthAccessToken() *serpent.Command {
3229
var extra string
33-
return &serpent.Command{
30+
agentAuth := NewAgentAuth()
31+
cmd := &serpent.Command{
3432
Use: "access-token <provider>",
3533
Short: "Print auth for an external provider",
3634
Long: "Print an access-token for an external auth provider. " +
@@ -70,12 +68,7 @@ fi
7068
ctx, stop := inv.SignalNotifyContext(ctx, StopSignals...)
7169
defer stop()
7270

73-
if r.agentToken == "" {
74-
_, _ = fmt.Fprint(inv.Stderr, pretty.Sprintf(headLineStyle(), "No agent token found, this command must be run from inside a running workspace.\n"))
75-
return xerrors.Errorf("agent token not found")
76-
}
77-
78-
client, err := r.createAgentClient(ctx)
71+
client, err := agentAuth.CreateClient(ctx)
7972
if err != nil {
8073
return xerrors.Errorf("create agent client: %w", err)
8174
}
@@ -115,4 +108,6 @@ fi
115108
return nil
116109
},
117110
}
111+
agentAuth.AttachOptions(cmd, false)
112+
return cmd
118113
}

cli/gitaskpass.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818

1919
// gitAskpass is used by the Coder agent to automatically authenticate
2020
// with Git providers based on a hostname.
21-
func (r *RootCmd) gitAskpass() *serpent.Command {
22-
return &serpent.Command{
21+
func gitAskpass(agentAuth *AgentAuth) *serpent.Command {
22+
cmd := &serpent.Command{
2323
Use: "gitaskpass",
2424
Hidden: true,
2525
Handler: func(inv *serpent.Invocation) error {
@@ -33,7 +33,7 @@ func (r *RootCmd) gitAskpass() *serpent.Command {
3333
return xerrors.Errorf("parse host: %w", err)
3434
}
3535

36-
client, err := r.createAgentClient(ctx)
36+
client, err := agentAuth.CreateClient(ctx)
3737
if err != nil {
3838
return xerrors.Errorf("create agent client: %w", err)
3939
}
@@ -90,4 +90,6 @@ func (r *RootCmd) gitAskpass() *serpent.Command {
9090
return nil
9191
},
9292
}
93+
agentAuth.AttachOptions(cmd, false)
94+
return cmd
9395
}

cli/gitssh.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
"github.com/coder/serpent"
1919
)
2020

21-
func (r *RootCmd) gitssh() *serpent.Command {
21+
func gitssh() *serpent.Command {
22+
agentAuth := NewAgentAuth()
2223
cmd := &serpent.Command{
2324
Use: "gitssh",
2425
Hidden: true,
@@ -38,7 +39,7 @@ func (r *RootCmd) gitssh() *serpent.Command {
3839
return err
3940
}
4041

41-
client, err := r.createAgentClient(ctx)
42+
client, err := agentAuth.CreateClient(ctx)
4243
if err != nil {
4344
return xerrors.Errorf("create agent client: %w", err)
4445
}
@@ -108,7 +109,7 @@ func (r *RootCmd) gitssh() *serpent.Command {
108109
return nil
109110
},
110111
}
111-
112+
agentAuth.AttachOptions(cmd, false)
112113
return cmd
113114
}
114115

cli/gitssh_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func prepareTestGitSSH(ctx context.Context, t *testing.T) (*agentsdk.Client, str
5454
}).WithAgent().Do()
5555

5656
// start workspace agent
57-
agentClient := agentsdk.New(client.URL, agentsdk.UsingFixedToken(r.AgentToken))
57+
agentClient := agentsdk.New(client.URL, agentsdk.WithFixedToken(r.AgentToken))
5858
_ = agenttest.New(t, client.URL, r.AgentToken, func(o *agent.Options) {
5959
o.Client = agentClient
6060
})

0 commit comments

Comments
 (0)