Skip to content
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: fail fast when keyring is not available
  • Loading branch information
zedkipp committed Oct 30, 2025
commit ca18b6f60da5ef796aa2da70ae766fcde42c5c51
19 changes: 2 additions & 17 deletions cli/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,31 +294,16 @@ func TestUseKeyringUnsupportedOS(t *testing.T) {

client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
pty := ptytest.New(t)

// Try to login with --use-keyring on an unsupported OS
inv, _ := clitest.New(t,
"login",
"--force-tty",
"--use-keyring",
"--no-open",
client.URL.String(),
)
inv.Stdin = pty.Input()
inv.Stdout = pty.Output()

doneChan := make(chan struct{})
var loginErr error
go func() {
defer close(doneChan)
loginErr = inv.Run()
}()

// Provide the token when prompted
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())

<-doneChan
// The error should occur immediately, before any prompts
loginErr := inv.Run()

// Verify we got an error about unsupported OS
require.Error(t, loginErr)
Expand Down
10 changes: 10 additions & 0 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (r *RootCmd) login() *serpent.Command {
Middleware: serpent.RequireRangeArgs(0, 1),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()

rawURL := ""
var urlSource string

Expand Down Expand Up @@ -204,6 +205,15 @@ func (r *RootCmd) login() *serpent.Command {
return err
}

// Check keyring availability before prompting the user for a token to fail fast.
if r.useKeyring {
backend := r.ensureTokenBackend()
_, err := backend.Read(client.URL)
if err != nil && xerrors.Is(err, sessionstore.ErrNotImplemented) {
return errKeyringNotSupported
}
}

hasFirstUser, err := client.HasFirstUser(ctx)
if err != nil {
return xerrors.Errorf("Failed to check server %q for first user, is the URL correct and is coder accessible from your browser? Error - has initial user: %w", serverURL.String(), err)
Expand Down
Loading