Skip to content

Commit 20b60a3

Browse files
committed
Make createAgentClient use token file and errors
For now, the old function has been renamed to tryCreateAgentClient because it is not clear to me if it is safe to error in these cases or if it was intentional to create a client even if there was no valid URL or token.
1 parent f696ff4 commit 20b60a3

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

cli/externalauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fi
7575
return xerrors.Errorf("agent token not found")
7676
}
7777

78-
client, err := r.createAgentClient()
78+
client, err := r.tryCreateAgentClient()
7979
if err != nil {
8080
return xerrors.Errorf("create agent client: %w", err)
8181
}

cli/gitaskpass.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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()
36+
client, err := r.tryCreateAgentClient()
3737
if err != nil {
3838
return xerrors.Errorf("create agent client: %w", err)
3939
}

cli/gitssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (r *RootCmd) gitssh() *serpent.Command {
3838
return err
3939
}
4040

41-
client, err := r.createAgentClient()
41+
client, err := r.tryCreateAgentClient()
4242
if err != nil {
4343
return xerrors.Errorf("create agent client: %w", err)
4444
}

cli/root.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const (
8181
envAgentToken = "CODER_AGENT_TOKEN"
8282
//nolint:gosec
8383
envAgentTokenFile = "CODER_AGENT_TOKEN_FILE"
84+
envAgentURL = "CODER_AGENT_URL"
8485
envURL = "CODER_URL"
8586
)
8687

@@ -398,7 +399,7 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
398399
},
399400
{
400401
Flag: varAgentURL,
401-
Env: "CODER_AGENT_URL",
402+
Env: envAgentURL,
402403
Description: "URL for an agent to access your deployment.",
403404
Value: serpent.URLOf(r.agentURL),
404405
Hidden: true,
@@ -668,9 +669,35 @@ func (r *RootCmd) createUnauthenticatedClient(ctx context.Context, serverURL *ur
668669
return &client, err
669670
}
670671

671-
// createAgentClient returns a new client from the command context.
672-
// It works just like CreateClient, but uses the agent token and URL instead.
672+
// createAgentClient returns a new client from the command context. It works
673+
// just like InitClient, but uses the agent token and URL instead.
673674
func (r *RootCmd) createAgentClient() (*agentsdk.Client, error) {
675+
agentURL := r.agentURL
676+
if agentURL == nil || agentURL.String() == "" {
677+
return nil, xerrors.Errorf("%s must be set", envAgentURL)
678+
}
679+
token := r.agentToken
680+
if token == "" {
681+
if r.agentTokenFile == "" {
682+
return nil, xerrors.Errorf("Either %s or %s must be set", envAgentToken, envAgentTokenFile)
683+
}
684+
tokenBytes, err := os.ReadFile(r.agentTokenFile)
685+
if err != nil {
686+
return nil, xerrors.Errorf("read token file %q: %w", r.agentTokenFile, err)
687+
}
688+
token = strings.TrimSpace(string(tokenBytes))
689+
}
690+
client := agentsdk.New(agentURL)
691+
client.SetSessionToken(token)
692+
return client, nil
693+
}
694+
695+
// tryCreateAgentClient returns a new client from the command context. It works
696+
// just like tryCreateAgentClient, but does not error.
697+
func (r *RootCmd) tryCreateAgentClient() (*agentsdk.Client, error) {
698+
// TODO: Why does this not actually return any errors despite the function
699+
// signature? Could we just use createAgentClient instead, or is it expected
700+
// that we return a client in some cases even without a valid URL or token?
674701
client := agentsdk.New(r.agentURL)
675702
client.SetSessionToken(r.agentToken)
676703
return client, nil

0 commit comments

Comments
 (0)