diff --git a/.nextchanges/cli/auth-login-workspace-lookup-timeout.md b/.nextchanges/cli/auth-login-workspace-lookup-timeout.md new file mode 100644 index 00000000000..5ed6d0c31ae --- /dev/null +++ b/.nextchanges/cli/auth-login-workspace-lookup-timeout.md @@ -0,0 +1 @@ +* `databricks auth login` now gives up on the optional workspace lookup after about 5 seconds instead of retrying for about 5 minutes, and explains the fallback to the manual workspace ID prompt. ([#7522](https://github.com/databricks/cli/pull/7522)) diff --git a/cmd/auth/export_test.go b/cmd/auth/export_test.go index ad363a491ac..e5476625f4a 100644 --- a/cmd/auth/export_test.go +++ b/cmd/auth/export_test.go @@ -4,3 +4,10 @@ var ( NewWorkspaceSearcher = newWorkspaceSearcher PromptForWorkspaceSelection = promptForWorkspaceSelection ) + +// SetWorkspaceListTimeoutSeconds overrides the workspace lookup budget and returns a restore func. +func SetWorkspaceListTimeoutSeconds(seconds int) func() { + previous := workspaceListTimeoutSeconds + workspaceListTimeoutSeconds = seconds + return func() { workspaceListTimeoutSeconds = previous } +} diff --git a/cmd/auth/login.go b/cmd/auth/login.go index 067d9e6f7be..dc67113db24 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -1054,6 +1054,10 @@ func u2mResourcesFromProfile(p *profile.Profile) []string { return splitScopes(p.Resources) } +// workspaceListTimeoutSeconds bounds the optional workspace lookup during login. +// It is a variable only so tests can shorten it. +var workspaceListTimeoutSeconds = 5 + // promptForWorkspaceSelection lists workspaces for a SPOG account and lets the // user pick one. Returns the selected workspace ID or empty string if skipped. // This is best-effort: errors are returned to the caller for logging, not shown @@ -1068,14 +1072,21 @@ func promptForWorkspaceSelection(ctx context.Context, authArguments *auth.AuthAr Host: authArguments.Host, AccountID: authArguments.AccountID, Credentials: config.NewTokenSourceStrategy("login-token", authconv.AuthTokenSource(tokenSource)), + + // The workspace list is only a convenience with a manual fallback, so + // don't spend the SDK's default 5 minute retry budget on a 503 here. + HTTPTimeoutSeconds: workspaceListTimeoutSeconds, + RetryTimeoutSeconds: workspaceListTimeoutSeconds, }) if err != nil { return "", err } + cmdio.LogString(ctx, "Looking up workspaces for your account...") workspaces, err := a.Workspaces.List(ctx) if err != nil { log.Debugf(ctx, "Failed to load workspaces (this can happen if the user has no account-level access): %v", err) + cmdio.LogString(ctx, "Could not list workspaces for this account. To skip this step, pass a host with the workspace ID, e.g. --host \"?w=\".") return promptForWorkspaceID(ctx) } diff --git a/cmd/auth/login_workspace_test.go b/cmd/auth/login_workspace_test.go index b3b83ce1d19..b5ec553fcac 100644 --- a/cmd/auth/login_workspace_test.go +++ b/cmd/auth/login_workspace_test.go @@ -235,6 +235,25 @@ func TestPromptForWorkspaceSelectionListError(t *testing.T) { } } +func TestPromptForWorkspaceSelectionGivesUpQuicklyOnUnavailable(t *testing.T) { + t.Cleanup(authcmd.SetWorkspaceListTimeoutSeconds(1)) + args, requests := newWorkspaceListServer(t, http.StatusServiceUnavailable, nil) + start := time.Now() + tm := termtest.Run(t, func(ctx context.Context) (string, error) { + return authcmd.PromptForWorkspaceSelection(ctx, args, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: workspaceTestToken})) + }) + tm.WaitFor(workspaceIDPrompt) + // The SDK's default retry budget is 5 minutes; the lookup must fall back well before that. + assert.Less(t, time.Since(start), 30*time.Second) + tm.Type("123456" + termtest.KeyEnter) + id, err := tm.Result() + require.NoError(t, err) + assert.Equal(t, "123456", id) + assert.Positive(t, requests.Load()) + assert.Contains(t, tm.Raw(), "Looking up workspaces") + assert.Contains(t, tm.Raw(), "?w=") +} + // newWorkspaceListServer exercises the SDK's actual list decoding without a live account. func newWorkspaceListServer(t *testing.T, status int, workspaces []provisioning.Workspace) (*auth.AuthArguments, *atomic.Int32) { t.Helper() diff --git a/cmd/auth/testdata/TestPromptForWorkspaceSelectionNoResults/01-initial.golden b/cmd/auth/testdata/TestPromptForWorkspaceSelectionNoResults/01-initial.golden index 66cd29acfdb..30481eb4fb5 100644 --- a/cmd/auth/testdata/TestPromptForWorkspaceSelectionNoResults/01-initial.golden +++ b/cmd/auth/testdata/TestPromptForWorkspaceSelectionNoResults/01-initial.golden @@ -1,3 +1,4 @@ +Looking up workspaces for your account... Search: █ ? Select a workspace (search by name or ID): ▸ Alpha (111)