From f113f0f5270c8a0c8d9fca7ee70cdc6037d5f957 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 17 Nov 2026 08:20:47 +0000 Subject: [PATCH 01/12] Support device code login for account and unified hosts Device code login is now enabled on the server for account and unified (SPOG) hosts, so remove the checks that limited it to workspace hosts. Classic account hosts build their OAuth endpoints without fetching the OIDC discovery document, so device code login fetches it to find the device authorization endpoint. Co-authored-by: Isaac --- .../non-interactive/errors/output.txt | 8 +- .../device-code/non-interactive/errors/script | 3 +- .../login/device-code/precondition/output.txt | 3 - .../login/device-code/precondition/script | 1 - .../unified-host/out.databrickscfg | 17 ++++ .../device-code/unified-host/out.test.toml | 1 + .../login/device-code/unified-host/output.txt | 46 +++++++++++ .../login/device-code/unified-host/script | 25 ++++++ .../login/device-code/unified-host/test.toml | 1 + .../unsupported-host/home/.databrickscfg.tmpl | 3 - .../device-code/unsupported-host/output.txt | 25 ++---- .../login/device-code/unsupported-host/script | 14 +--- .../device-code/unsupported-host/test.toml | 14 +++- cmd/auth/README.md | 11 ++- cmd/auth/auth.go | 2 +- cmd/auth/login.go | 49 +++-------- cmd/auth/login_device_code.go | 19 +---- cmd/auth/login_device_code_test.go | 64 ++------------- docs/device-code-login.md | 25 +++--- libs/auth/u2m/device_code.go | 21 +++-- libs/auth/u2m/device_code_test.go | 82 +++++++++++++++++-- libs/auth/u2m/endpoint_supplier.go | 12 ++- libs/testserver/fake_oidc.go | 16 +++- libs/testserver/handlers.go | 16 ++++ 24 files changed, 285 insertions(+), 193 deletions(-) create mode 100644 acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg create mode 100644 acceptance/cmd/auth/login/device-code/unified-host/out.test.toml create mode 100644 acceptance/cmd/auth/login/device-code/unified-host/output.txt create mode 100644 acceptance/cmd/auth/login/device-code/unified-host/script create mode 100644 acceptance/cmd/auth/login/device-code/unified-host/test.toml diff --git a/acceptance/cmd/auth/login/device-code/non-interactive/errors/output.txt b/acceptance/cmd/auth/login/device-code/non-interactive/errors/output.txt index b763d0b0028..fedaf8d96a1 100644 --- a/acceptance/cmd/auth/login/device-code/non-interactive/errors/output.txt +++ b/acceptance/cmd/auth/login/device-code/non-interactive/errors/output.txt @@ -15,10 +15,10 @@ Error: --configure-cluster is not supported by the non-interactive device code l === Missing host >>> musterr [CLI] auth login --use-device-code -Error: --use-device-code requires a workspace host. Pass it as an argument or with --host +Error: --use-device-code requires a host. Pass it as an argument or with --host >>> musterr [CLI] auth login --profile test --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY -Error: --use-device-code requires a workspace host. Pass it as an argument or with --host +Error: --use-device-code requires a host. Pass it as an argument or with --host === Incomplete flags of the second step @@ -36,11 +36,11 @@ Error: --confirmation-code requires --use-device-code >>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test#device-code --use-device-code Error: profile name "test#device-code" must not end with #device-code, which marks pending device code logins -=== Account host +=== Account host without an account ID >>> musterr [CLI] auth login --host https://accounts.cloud.databricks.test --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY Warn: [hostmetadata] failed to fetch host metadata for https://accounts.cloud.databricks.test, will skip for 1m0s -Error: --use-device-code is only supported for workspace hosts +Error: the command is being run in a non-interactive environment, please specify an account ID using --account-id === Profile scoped to resources diff --git a/acceptance/cmd/auth/login/device-code/non-interactive/errors/script b/acceptance/cmd/auth/login/device-code/non-interactive/errors/script index 3817c695ebd..bfbc334230a 100644 --- a/acceptance/cmd/auth/login/device-code/non-interactive/errors/script +++ b/acceptance/cmd/auth/login/device-code/non-interactive/errors/script @@ -23,7 +23,8 @@ trace musterr $CLI auth login --host $DATABRICKS_HOST --profile test --user-code title "Profile name that marks a pending login" trace musterr $CLI auth login --host $DATABRICKS_HOST --profile 'test#device-code' --use-device-code -title "Account host" +# The non-interactive login cannot prompt for the account ID. +title "Account host without an account ID" trace musterr $CLI auth login --host https://accounts.cloud.databricks.test --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY envsubst < "./home/.databrickscfg.tmpl" > "./home/.databrickscfg" diff --git a/acceptance/cmd/auth/login/device-code/precondition/output.txt b/acceptance/cmd/auth/login/device-code/precondition/output.txt index 0b34cd722e0..dd219539142 100644 --- a/acceptance/cmd/auth/login/device-code/precondition/output.txt +++ b/acceptance/cmd/auth/login/device-code/precondition/output.txt @@ -9,8 +9,5 @@ Error: --use-device-code is experimental. Set DATABRICKS_CLI_DEVICE_CODE=true to >>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test --use-device-code --resource https://resource.test Error: --use-device-code cannot be combined with --resource ->>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test --use-device-code --skip-workspace -Error: --use-device-code cannot be combined with --skip-workspace - >>> musterr [CLI] auth login --profile scoped --use-device-code Error: --use-device-code cannot be used with profile scoped because its login is scoped to resources. Log in without --use-device-code diff --git a/acceptance/cmd/auth/login/device-code/precondition/script b/acceptance/cmd/auth/login/device-code/precondition/script index 7df7da78d3f..1a823c73849 100644 --- a/acceptance/cmd/auth/login/device-code/precondition/script +++ b/acceptance/cmd/auth/login/device-code/precondition/script @@ -8,7 +8,6 @@ title "Device code login is disabled by default" title "Inputs device code login cannot honor are rejected" trace musterr $CLI auth login --host $DATABRICKS_HOST --profile test --use-device-code --resource https://resource.test -trace musterr $CLI auth login --host $DATABRICKS_HOST --profile test --use-device-code --skip-workspace # A profile scoped to resources is rejected like --resource. envsubst < "./home/.databrickscfg.tmpl" > "./home/.databrickscfg" diff --git a/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg b/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg new file mode 100644 index 00000000000..f154279afe4 --- /dev/null +++ b/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg @@ -0,0 +1,17 @@ +; The profile defined in the DEFAULT section is to be used as a fallback when no profile is explicitly specified. +[DEFAULT] + +[account] +host = [DATABRICKS_URL] +account_id = spog-acct-123 +workspace_id = [WORKSPACE_ID] +auth_type = databricks-cli + +[__settings__] +default_profile = account + +[workspace] +host = [DATABRICKS_URL] +account_id = spog-acct-123 +workspace_id = 12345 +auth_type = databricks-cli diff --git a/acceptance/cmd/auth/login/device-code/unified-host/out.test.toml b/acceptance/cmd/auth/login/device-code/unified-host/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/auth/login/device-code/unified-host/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/auth/login/device-code/unified-host/output.txt b/acceptance/cmd/auth/login/device-code/unified-host/output.txt new file mode 100644 index 00000000000..80a2104ee25 --- /dev/null +++ b/acceptance/cmd/auth/login/device-code/unified-host/output.txt @@ -0,0 +1,46 @@ + +=== Log in to the account + +>>> [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123 --profile account --use-device-code +Using a browser on another device, visit: +[DATABRICKS_URL]/device/verification + +And enter the code: +WDJB-0001 + +Once finished, complete the login by running the same command with +--user-code WDJB-0001 and --confirmation-code set to the confirmation code +provided in your browser. + +>>> [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123 --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY +Profile account was successfully saved + +>>> [CLI] auth token --profile account +json.access_token = "oauth-token"; + +=== Log in to a workspace of the account + +>>> [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123&o=12345 --profile workspace --use-device-code +Using a browser on another device, visit: +[DATABRICKS_URL]/device/verification + +And enter the code: +WDJB-0002 + +Once finished, complete the login by running the same command with +--user-code WDJB-0002 and --confirmation-code set to the confirmation code +provided in your browser. + +>>> [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123&o=12345 --profile workspace --use-device-code --user-code WDJB-0002 --confirmation-code FTFA-QQKY +Profile workspace was successfully saved + +>>> [CLI] auth token --profile workspace +json.access_token = "oauth-token"; + +=== Device code requests + +>>> print_requests.py --oneline //oidc +{"method": "POST", "path": "/oidc/accounts/spog-acct-123/v1/device/auth", "raw_body": "client_id=databricks-cli&scope=offline_access+all-apis"} +{"method": "POST", "path": "/oidc/accounts/spog-acct-123/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=FTFAQQKY&device_code=device-code-1&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"} +{"method": "POST", "path": "/oidc/accounts/spog-acct-123/v1/device/auth", "raw_body": "client_id=databricks-cli&scope=offline_access+all-apis"} +{"method": "POST", "path": "/oidc/accounts/spog-acct-123/v1/token", "raw_body": "client_id=databricks-cli&confirmation_code=FTFAQQKY&device_code=device-code-2&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"} diff --git a/acceptance/cmd/auth/login/device-code/unified-host/script b/acceptance/cmd/auth/login/device-code/unified-host/script new file mode 100644 index 00000000000..9fcd23a1f2b --- /dev/null +++ b/acceptance/cmd/auth/login/device-code/unified-host/script @@ -0,0 +1,25 @@ +sethome "./home" + +# An account-scoped discovery URL marks the host as a unified host. +export DATABRICKS_DISCOVERY_URL="$DATABRICKS_HOST/oidc/accounts/spog-acct-123/.well-known/oauth-authorization-server" + +# Acceptance tests run the CLI without a terminal on stdin, so device code +# login runs non-interactively. +title "Log in to the account" +trace $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123" --profile account --use-device-code +trace $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123" --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY +trace $CLI auth token --profile account | gron.py | grep access_token + +title "Log in to a workspace of the account" +trace $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123&o=12345" --profile workspace --use-device-code +trace $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123&o=12345" --profile workspace --use-device-code --user-code WDJB-0002 --confirmation-code FTFA-QQKY +trace $CLI auth token --profile workspace | gron.py | grep access_token + +# Both logins use the account's OIDC endpoints. +title "Device code requests" +trace print_requests.py --oneline //oidc + +# The fake server's host metadata returns a workspace ID, so the account +# profile gets one too. +# Track the .databrickscfg file that was created to surface changes. +mv "./home/.databrickscfg" "./out.databrickscfg" diff --git a/acceptance/cmd/auth/login/device-code/unified-host/test.toml b/acceptance/cmd/auth/login/device-code/unified-host/test.toml new file mode 100644 index 00000000000..159efe02696 --- /dev/null +++ b/acceptance/cmd/auth/login/device-code/unified-host/test.toml @@ -0,0 +1 @@ +RecordRequests = true diff --git a/acceptance/cmd/auth/login/device-code/unsupported-host/home/.databrickscfg.tmpl b/acceptance/cmd/auth/login/device-code/unsupported-host/home/.databrickscfg.tmpl index 90baae2ed29..935fa1695bf 100644 --- a/acceptance/cmd/auth/login/device-code/unsupported-host/home/.databrickscfg.tmpl +++ b/acceptance/cmd/auth/login/device-code/unsupported-host/home/.databrickscfg.tmpl @@ -1,6 +1,3 @@ [workspace] host = $DATABRICKS_HOST auth_type = databricks-cli - -[account] -host = https://accounts.cloud.databricks.test diff --git a/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt b/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt index 5d327ce2db2..47f203deb1d 100644 --- a/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt +++ b/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt @@ -2,29 +2,20 @@ === No host >>> musterr [CLI] auth login --profile new-profile --use-device-code -Error: --use-device-code requires a workspace host. Pass it as an argument or with --host - -=== Account host - ->>> musterr [CLI] auth login --host https://accounts.cloud.databricks.test --profile test --use-device-code -Warn: [hostmetadata] failed to fetch host metadata for https://accounts.cloud.databricks.test, will skip for 1m0s -Error: --use-device-code is only supported for workspace hosts - ->>> musterr [CLI] auth login --profile account --use-device-code -Error: --use-device-code is only supported for workspace hosts - -=== Unified host - ->>> DATABRICKS_DISCOVERY_URL=[DATABRICKS_URL]/oidc/accounts/spog-acct-123/.well-known/oauth-authorization-server musterr [CLI] auth login --host [DATABRICKS_URL] --use-device-code -Error: --use-device-code is only supported for workspace hosts +Error: --use-device-code requires a host. Pass it as an argument or with --host === Workspace without device code login >>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test --use-device-code -Error: device code login is not enabled for this workspace +Error: device code login is not enabled for this host >>> musterr [CLI] auth login --profile workspace --use-device-code -Error: device code login is not enabled for this workspace +Error: device code login is not enabled for this host + +=== Account without device code login + +>>> DATABRICKS_DISCOVERY_URL=[DATABRICKS_URL]/oidc/accounts/spog-acct-123/.well-known/oauth-authorization-server musterr [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123 --profile test --use-device-code +Error: device code login is not enabled for this host === Device authorization requests diff --git a/acceptance/cmd/auth/login/device-code/unsupported-host/script b/acceptance/cmd/auth/login/device-code/unsupported-host/script index 46c307b332e..c91d04802cc 100644 --- a/acceptance/cmd/auth/login/device-code/unsupported-host/script +++ b/acceptance/cmd/auth/login/device-code/unsupported-host/script @@ -9,20 +9,14 @@ trace musterr $CLI auth login --profile new-profile --use-device-code envsubst < "./home/.databrickscfg.tmpl" > "./home/.databrickscfg" -# The host is rejected before the CLI would prompt for an account ID, whether -# it is passed directly or comes from a profile without one. -title "Account host" -trace musterr $CLI auth login --host https://accounts.cloud.databricks.test --profile test --use-device-code -trace musterr $CLI auth login --profile account --use-device-code - -# An account-scoped discovery URL marks the host as a unified host. -title "Unified host" -trace DATABRICKS_DISCOVERY_URL="$DATABRICKS_HOST/oidc/accounts/spog-acct-123/.well-known/oauth-authorization-server" musterr $CLI auth login --host $DATABRICKS_HOST --use-device-code - title "Workspace without device code login" trace musterr $CLI auth login --host $DATABRICKS_HOST --profile test --use-device-code trace musterr $CLI auth login --profile workspace --use-device-code +# An account-scoped discovery URL marks the host as a unified host. +title "Account without device code login" +trace DATABRICKS_DISCOVERY_URL="$DATABRICKS_HOST/oidc/accounts/spog-acct-123/.well-known/oauth-authorization-server" musterr $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123" --profile test --use-device-code + # Every host is rejected before the device authorization request. title "Device authorization requests" trace print_requests.py //oidc/v1/device/auth diff --git a/acceptance/cmd/auth/login/device-code/unsupported-host/test.toml b/acceptance/cmd/auth/login/device-code/unsupported-host/test.toml index 9eb77236bab..fcc42850071 100644 --- a/acceptance/cmd/auth/login/device-code/unsupported-host/test.toml +++ b/acceptance/cmd/auth/login/device-code/unsupported-host/test.toml @@ -1,7 +1,8 @@ RecordRequests = true -# Workspaces that are not enrolled in the device code preview do not advertise -# a device_authorization_endpoint in their OIDC discovery document. +# Workspaces and accounts that are not enrolled in the device code preview do +# not advertise a device_authorization_endpoint in their OIDC discovery +# document. [[Server]] Pattern = "GET /oidc/.well-known/oauth-authorization-server" Response.Body = ''' @@ -10,3 +11,12 @@ Response.Body = ''' "token_endpoint": "https://workspace.test/oidc/v1/token" } ''' + +[[Server]] +Pattern = "GET /oidc/accounts/spog-acct-123/.well-known/oauth-authorization-server" +Response.Body = ''' +{ + "authorization_endpoint": "https://spog.test/oidc/accounts/spog-acct-123/v1/authorize", + "token_endpoint": "https://spog.test/oidc/accounts/spog-acct-123/v1/token" +} +''' diff --git a/cmd/auth/README.md b/cmd/auth/README.md index 33ea55d1f00..914c7049f76 100644 --- a/cmd/auth/README.md +++ b/cmd/auth/README.md @@ -54,7 +54,7 @@ sequenceDiagram `databricks auth login --use-device-code` logs in from a machine without a browser. It is experimental: the flag is hidden and requires -`DATABRICKS_CLI_DEVICE_CODE=true`. Only workspace hosts are supported. See +`DATABRICKS_CLI_DEVICE_CODE=true`. See [docs/device-code-login.md](../../docs/device-code-login.md) for the user guide. Databricks implements a variant of [RFC 8628](https://datatracker.ietf.org/doc/html/rfc8628): @@ -88,9 +88,12 @@ sequenceDiagram Notes on the server behavior the CLI relies on: - Workspaces advertise `device_authorization_endpoint` in - `/oidc/.well-known/oauth-authorization-server` only when device code login - is enabled for them. The CLI fails before requesting a device code if it is - missing. + `/oidc/.well-known/oauth-authorization-server`, and accounts in + `/oidc/accounts//.well-known/oauth-authorization-server`, only + when device code login is enabled for them. The CLI fails before requesting + a device code if it is missing. Classic account hosts otherwise build their + OAuth endpoints without this document, so device code login fetches it for + them. - The token request carries the confirmation code in the `confirmation_code` form parameter. A wrong confirmation code does not consume the device code; a successful exchange does. The server answers a wrong confirmation code diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go index 2784e6e164f..f40e6b78efc 100644 --- a/cmd/auth/auth.go +++ b/cmd/auth/auth.go @@ -62,7 +62,7 @@ func promptForHost(ctx context.Context) (string, error) { if strings.TrimSpace(input) == "" { return errors.New("host is required") } - // Reject hosts that setHost would reject after the prompt, + // Reject hosts that setHostAndAccountId would reject after the prompt, // e.g. "/" or "?o=123", while the user can still edit them. _, err := parseLoginHost(input) return err diff --git a/cmd/auth/login.go b/cmd/auth/login.go index dc67113db24..c60e0e047e5 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -247,7 +247,7 @@ a new profile is created. } } - // setHost normalizes the host only after the keyring probe and the + // setHostAndAccountId normalizes the host only after the keyring probe and the // profile name prompt, so reject an invalid host before them. Only trim // it here: an account ID from the query params would make the prompt // suggest ACCOUNT- for a workspace login. @@ -386,7 +386,7 @@ a new profile is created. } } - // The non-interactive login cannot answer the host prompt of setHost. + // The non-interactive login cannot answer the host prompt of setHostAndAccountId. if nonInteractive && shouldUseDiscovery(authArguments.Host, args, existingProfile) { return errDeviceCodeNeedsHost } @@ -394,7 +394,7 @@ a new profile is created. // If no host is available from any source, use the discovery flow // via login.databricks.com. Discovery opens a browser on this machine, // and device code login is meant for machines without one, so it skips - // discovery and setHost resolves the host instead. + // discovery and setHostAndAccountId resolves the host instead. if !useDeviceCode && shouldUseDiscovery(authArguments.Host, args, existingProfile) { if err := validateDiscoveryFlagCompatibility(cmd); err != nil { return err @@ -412,17 +412,7 @@ a new profile is created. }) } - err = setHost(ctx, existingProfile, authArguments, args) - if err != nil { - return err - } - // Check the host once it is resolved, before setAccountID prompts for - // an account ID for account and unified hosts. A workspace without - // device code login enabled is rejected later by deviceCodeLogin. - if useDeviceCode && !isDeviceCodeSupportedForHost(authArguments.Host, authArguments.DiscoveryURL) { - return errDeviceCodeWorkspaceOnly - } - err = setAccountID(ctx, existingProfile, authArguments) + err = setHostAndAccountId(ctx, existingProfile, authArguments, args) if err != nil { return err } @@ -616,14 +606,6 @@ func shouldPromptWorkspace(authArguments *auth.AuthArguments, existingProfile *p return true } -// setHostAndAccountId calls setHost and then setAccountID. -func setHostAndAccountId(ctx context.Context, existingProfile *profile.Profile, authArguments *auth.AuthArguments, args []string) error { - if err := setHost(ctx, existingProfile, authArguments, args); err != nil { - return err - } - return setAccountID(ctx, existingProfile, authArguments) -} - var errInvalidHost = errors.New("invalid host") const exampleHost = "https://.cloud.databricks.com" @@ -685,7 +667,13 @@ func isHTTPScheme(scheme string) bool { // 1. [HOST] (first positional argument) or --host flag. Error if both are specified. // 2. Profile host, if available. // 3. Prompt the user for the host. -func setHost(ctx context.Context, existingProfile *profile.Profile, authArguments *auth.AuthArguments, args []string) error { +// +// Set the account in the persistentAuth object based on the flags. +// Follows the following precedence: +// 1. --account-id flag. +// 2. account-id from the specified profile, if available. +// 3. Prompt the user for the account-id. +func setHostAndAccountId(ctx context.Context, existingProfile *profile.Profile, authArguments *auth.AuthArguments, args []string) error { // If both [HOST] and --host are provided, return an error. host := authArguments.Host if len(args) > 0 && host != "" { @@ -726,15 +714,7 @@ func setHost(ctx context.Context, existingProfile *profile.Profile, authArgument // .well-known/databricks-config endpoint. This is best-effort: failures // are logged as warnings and never block login. runHostDiscovery(ctx, authArguments) - return nil -} -// Set the account in the persistentAuth object based on the flags. -// Follows the following precedence: -// 1. --account-id flag. -// 2. account-id from the specified profile, if available. -// 3. Prompt the user for the account-id. -func setAccountID(ctx context.Context, existingProfile *profile.Profile, authArguments *auth.AuthArguments) error { if needsAccountIDPrompt(authArguments.Host, authArguments.DiscoveryURL) && authArguments.AccountID == "" { if existingProfile != nil && existingProfile.AccountID != "" { authArguments.AccountID = existingProfile.AccountID @@ -754,13 +734,6 @@ func setAccountID(ctx context.Context, existingProfile *profile.Profile, authArg // for OAuth URL construction. True for classic account hosts (accounts.*) and // for unified hosts detected via account-scoped DiscoveryURL. func needsAccountIDPrompt(host, discoveryURL string) bool { - return isAccountOrUnifiedHost(host, discoveryURL) -} - -// isAccountOrUnifiedHost reports whether host is a classic account host -// (accounts.* or accounts-dod.*) or a unified host with an account-scoped -// discoveryURL. -func isAccountOrUnifiedHost(host, discoveryURL string) bool { canonicalHost := (&config.Config{Host: host}).CanonicalHostName() if auth.IsClassicAccountHost(canonicalHost) { return true diff --git a/cmd/auth/login_device_code.go b/cmd/auth/login_device_code.go index 4346bf61404..52fe5082fdf 100644 --- a/cmd/auth/login_device_code.go +++ b/cmd/auth/login_device_code.go @@ -41,29 +41,18 @@ var deviceCodeIncompatibleFlags = []string{ // Resource indicators are sent on the browser authorization request, // which the device code flow does not make. "resource", - // Device code login is only supported for workspace hosts. - "skip-workspace", } var ( - errDeviceCodeNeedsHost = errors.New("--use-device-code requires a workspace host. Pass it as an argument or with --host") - errDeviceCodeWorkspaceOnly = errors.New("--use-device-code is only supported for workspace hosts") + errDeviceCodeNeedsHost = errors.New("--use-device-code requires a host. Pass it as an argument or with --host") // The cluster picker is interactive. errDeviceCodeNonInteractiveCluster = errors.New("--configure-cluster is not supported by the non-interactive device code login. Set cluster_id on the profile after the login") ) -// isDeviceCodeSupportedForHost reports whether host is a workspace host. A -// unified host is only recognized by its account-scoped discoveryURL, so with -// an empty discoveryURL only classic account hosts are rejected. -func isDeviceCodeSupportedForHost(host, discoveryURL string) bool { - return !isAccountOrUnifiedHost(host, discoveryURL) -} - -// matchDeviceCodeProfiles matches the profiles that device code login does not -// always reject. Profiles on unified hosts still match, because the discovery -// URL that identifies them is not saved on the profile. +// matchDeviceCodeProfiles matches the profiles that device code login can +// use: those whose login is not scoped to resources. func matchDeviceCodeProfiles(p profile.Profile) bool { - return isDeviceCodeSupportedForHost(p.Host, "") && len(u2mResourcesFromProfile(&p)) == 0 + return len(u2mResourcesFromProfile(&p)) == 0 } // validateDeviceCodeLogin returns an error if device code login is not enabled diff --git a/cmd/auth/login_device_code_test.go b/cmd/auth/login_device_code_test.go index 01cae5fc2d4..b6a9c607014 100644 --- a/cmd/auth/login_device_code_test.go +++ b/cmd/auth/login_device_code_test.go @@ -59,26 +59,12 @@ func TestValidateDeviceCodeLogin(t *testing.T) { flagVal: "https://workspace.test/ai-gateway/mcp-services/system.ai.github", wantErr: "--use-device-code cannot be combined with --resource", }, - { - name: "skip-workspace is incompatible", - envVal: "1", - setFlag: "skip-workspace", - flagVal: "true", - wantErr: "--use-device-code cannot be combined with --skip-workspace", - }, - { - name: "skip-workspace set to its default", - envVal: "1", - setFlag: "skip-workspace", - flagVal: "false", - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ctx := env.Set(t.Context(), deviceCodeEnvVar, tt.envVal) cmd := &cobra.Command{} cmd.Flags().StringArray("resource", nil, "") - cmd.Flags().Bool("skip-workspace", false, "") if tt.setFlag != "" { require.NoError(t, cmd.Flags().Set(tt.setFlag, tt.flagVal)) } @@ -210,44 +196,6 @@ func TestFormatCode(t *testing.T) { assert.Equal(t, "ABC", formatCode("ABC")) } -func TestIsDeviceCodeSupportedForHost(t *testing.T) { - workspace := newDiscoveryServer(t, map[string]any{ - "account_id": "acct-123", - "workspace_id": "12345", - "oidc_endpoint": "https://workspace.test/oidc", - }) - unified := newDiscoveryServer(t, map[string]any{ - "account_id": "acct-123", - "oidc_endpoint": "https://spog.test/oidc/accounts/acct-123", - }) - // Without an account ID, host discovery cannot fill in the placeholder - // and does not resolve the account-scoped discovery URL. - unifiedWithoutAccountID := newDiscoveryServer(t, map[string]any{ - "oidc_endpoint": "https://spog.test/oidc/accounts/{account_id}", - }) - - tests := []struct { - name string - args auth.AuthArguments - want bool - }{ - {name: "workspace host", args: auth.AuthArguments{Host: workspace.URL}, want: true}, - {name: "workspace host with query params", args: auth.AuthArguments{Host: workspace.URL + "?o=12345"}, want: true}, - {name: "classic account host", args: auth.AuthArguments{Host: "https://accounts.cloud.databricks.test"}}, - {name: "unified host", args: auth.AuthArguments{Host: unified.URL}}, - {name: "unified host with query params", args: auth.AuthArguments{Host: unified.URL + "?o=12345"}}, - {name: "unified host with account ID in query params", args: auth.AuthArguments{Host: unifiedWithoutAccountID.URL + "?a=acct-123"}}, - {name: "unified host with account ID flag", args: auth.AuthArguments{Host: unifiedWithoutAccountID.URL, AccountID: "acct-123"}}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - args := tt.args - require.NoError(t, setHost(t.Context(), nil, &args, nil)) - assert.Equal(t, tt.want, isDeviceCodeSupportedForHost(args.Host, args.DiscoveryURL)) - }) - } -} - // setUpLogin isolates the environment of a login, using config as the contents // of .databrickscfg. func setUpLogin(t *testing.T, config string) { @@ -337,8 +285,9 @@ func TestLoginDeviceCodeCreateNewProfileRejectsHiddenProfileName(t *testing.T) { t.Skip("bubbletea cannot cancel reads on non-console input on Windows") } tm := startDeviceCodeLogin(t, `[DEFAULT] -host = https://accounts.cloud.databricks.test -account_id = acct-123 +host = https://workspace.test +auth_type = databricks-cli +resources = https://workspace.test/ai-gateway/mcp-services/system.ai.github `) tm.WaitFor(profilePickerCreateNewLabel) tm.Type(termtest.KeyEnter) @@ -378,10 +327,9 @@ func TestMatchDeviceCodeProfiles(t *testing.T) { want bool }{ {name: "workspace host", profile: profile.Profile{Host: "https://workspace.test"}, want: true}, - // setHost prompts for the host of a profile without one. + // setHostAndAccountId prompts for the host of a profile without one. {name: "no host", profile: profile.Profile{}, want: true}, - {name: "classic account host", profile: profile.Profile{Host: "https://accounts.cloud.databricks.test", AccountID: "acct-123"}}, - {name: "classic account host for DoD", profile: profile.Profile{Host: "https://accounts-dod.cloud.databricks.test", AccountID: "acct-123"}}, + {name: "classic account host", profile: profile.Profile{Host: "https://accounts.cloud.databricks.test", AccountID: "acct-123"}, want: true}, { name: "scoped to resources", profile: profile.Profile{Host: "https://workspace.test", AuthType: authTypeDatabricksCLI, Resources: "https://workspace.test/ai-gateway/mcp-services/system.ai.github"}, @@ -416,7 +364,7 @@ resources = https://workspace.test/ai-gateway/mcp-services/system.ai.github tm.WaitFor(profilePickerEnterHostLabel) screen := tm.Snapshot() assert.Contains(t, screen, "workspace-prof") - assert.NotContains(t, screen, "account-prof") + assert.Contains(t, screen, "account-prof") assert.NotContains(t, screen, "scoped-prof") tm.Type(termtest.KeyCtrlC) _, err := tm.Result() diff --git a/docs/device-code-login.md b/docs/device-code-login.md index fff366167f4..d9a89d0ed93 100644 --- a/docs/device-code-login.md +++ b/docs/device-code-login.md @@ -6,14 +6,13 @@ approve the login in a browser on another device (for example your laptop or phone), and type the confirmation code it shows you back into the CLI. > [!WARNING] -> Device code login is experimental. It is disabled by default, may change -> without notice, and only supports workspace hosts. -> Account hosts and unified (SPOG) hosts are not supported yet. +> Device code login is experimental. It is disabled by default and may change +> without notice. ## Prerequisites -- The workspace must have device code login enabled. If it is not, the CLI - fails with `device code login is not enabled for this workspace`. +- The workspace or account must have device code login enabled. If it is + not, the CLI fails with `device code login is not enabled for this host`. - Enable the feature in the CLI by setting `DATABRICKS_CLI_DEVICE_CODE=true`. ## Log in from a headless machine @@ -34,12 +33,12 @@ Profile my-profile was successfully saved 1. Run `databricks auth login` with `--use-device-code`. The host can be passed as an argument, with `--host`, or come from an existing profile. Otherwise the - CLI prompts for it. The profile picker leaves out profiles that device code - login cannot use: profiles for an account host and profiles saved with - `--resource`. Profiles for a unified host are still listed and are rejected - after you pick one. -2. On another device, open the URL that the CLI shows, sign in to the - workspace if needed, and enter the code. The code expires after about 10 + CLI prompts for it. The host can be a workspace host, an account host, or a + unified host. For an account or unified host, the CLI also needs the account + ID, like the browser-based login. The profile picker leaves out profiles + saved with `--resource`, which device code login cannot use. +2. On another device, open the URL that the CLI shows, sign in if needed, + and enter the code. The code expires after about 10 minutes. 3. Confirm that you started this login yourself. Only approve a login that you initiated: approving it grants the CLI access to the Databricks API as you. @@ -126,8 +125,8 @@ fail with a token refresh error and you need to run the login again. | Error or symptom | Likely cause | What to do | | --- | --- | --- | | `--use-device-code is experimental. Set DATABRICKS_CLI_DEVICE_CODE=true to enable it` | The feature is not enabled in the CLI. | Set `DATABRICKS_CLI_DEVICE_CODE=true`. | -| `device code login is not enabled for this workspace` | The workspace does not advertise device code login. | Use a workspace where the feature is enabled. | -| `--use-device-code is only supported for workspace hosts` | The host is an account or unified host. | Log in to a workspace host instead. | +| `device code login is not enabled for this host` | The workspace or account does not advertise device code login. | Use a workspace or account where the feature is enabled. | +| `the command is being run in a non-interactive environment, please specify an account ID using --account-id` | The non-interactive login to an account or unified host cannot prompt for the account ID. | Pass `--account-id`, or log in with a profile that has `account_id`. | | The browser says the code is invalid or expired | The code was mistyped, or more than ~10 minutes passed. | Re-enter the code carefully, or run the login again for a new code. Codes never contain `0`, `1`, `2`, `O`, `Z`, or `I`. | | `Invalid confirmation code` | The confirmation code was mistyped or left blank. | Re-enter the code. After 3 rejected codes, the login fails with `Invalid confirmation code (error code: invalid_request). Start login from scratch` and you need to run it again. | | `token exchange: Invalid device code` | The login expired. A confirmation code entered after the login expired is first rejected as `Invalid confirmation code`, and the workspace then discards the login. | Run the login again to get a new code. | diff --git a/libs/auth/u2m/device_code.go b/libs/auth/u2m/device_code.go index 08966cd14af..0c0bb0c66a9 100644 --- a/libs/auth/u2m/device_code.go +++ b/libs/auth/u2m/device_code.go @@ -28,9 +28,9 @@ const deviceCodeGrantType = "urn:ietf:params:oauth:grant-type:device_code" // it discarded after the login expired. const invalidDeviceCodeDescription = "Invalid device code" -// ErrDeviceCodeNotSupported is returned when the workspace does not advertise -// a device authorization endpoint in its OIDC discovery document. -var ErrDeviceCodeNotSupported = errors.New("device code login is not enabled for this workspace") +// ErrDeviceCodeNotSupported is returned when the workspace or account does not +// advertise a device authorization endpoint in its OIDC discovery document. +var ErrDeviceCodeNotSupported = errors.New("device code login is not enabled for this host") // ErrInvalidConfirmationCode matches the token exchange error for a // confirmation code the server rejects. The device code stays valid, so the @@ -62,9 +62,6 @@ type DeviceAuthorization struct { // deviceCodeConfig returns the OAuth config for device code login, or an // error if the host does not support it. func (a *PersistentAuth) deviceCodeConfig(ctx context.Context) (*oauth2.Config, error) { - if _, ok := a.oAuthArgument.(WorkspaceOAuthArgument); !ok { - return nil, errors.New("device code login is only supported for workspace hosts") - } // Resource indicators are only sent on the browser authorization request, // so the token would not be scoped to them. if len(a.resources) > 0 { @@ -74,6 +71,15 @@ func (a *PersistentAuth) deviceCodeConfig(ctx context.Context) (*oauth2.Config, if err != nil { return nil, fmt.Errorf("fetching oauth config: %w", err) } + // The endpoints of classic account hosts are built without fetching the + // discovery document, so they lack the device authorization endpoint. + if arg, ok := a.oAuthArgument.(AccountOAuthArgument); ok { + endpoints, err := a.endpointSupplier.GetEndpointsFromURL(ctx, accountDiscoveryURL(arg.GetAccountHost(), arg.GetAccountId())) + if err != nil { + return nil, fmt.Errorf("fetching OAuth endpoints: %w", err) + } + cfg.Endpoint.DeviceAuthURL = endpoints.DeviceAuthorizationEndpoint + } if cfg.Endpoint.DeviceAuthURL == "" { return nil, ErrDeviceCodeNotSupported } @@ -89,8 +95,7 @@ type DeviceCodeLogin struct { } // NewDeviceCodeLogin fetches the OAuth endpoints for a device code login, or -// returns an error if the host does not support it. Only workspace hosts are -// supported. +// returns an error if the host does not support it. func (a *PersistentAuth) NewDeviceCodeLogin(ctx context.Context) (*DeviceCodeLogin, error) { cfg, err := a.deviceCodeConfig(ctx) if err != nil { diff --git a/libs/auth/u2m/device_code_test.go b/libs/auth/u2m/device_code_test.go index 39c369385ce..03834839013 100644 --- a/libs/auth/u2m/device_code_test.go +++ b/libs/auth/u2m/device_code_test.go @@ -6,6 +6,7 @@ import ( "errors" "net/http" "net/http/httptest" + "strings" "sync/atomic" "testing" "time" @@ -342,18 +343,81 @@ func TestNewDeviceCodeLogin_Resources(t *testing.T) { assert.EqualError(t, err, "device code login does not support resource indicators") } -func TestNewDeviceCodeLogin_AccountHost(t *testing.T) { - arg, err := NewBasicAccountOAuthArgument("https://accounts.cloud.databricks.test", "xyz") +// accountDeviceCodeEndpointSupplier serves the endpoints of account xyz, which +// only its discovery document advertises the device authorization endpoint of. +type accountDeviceCodeEndpointSupplier struct { + MockOAuthEndpointSupplier + deviceAuthAdvertise bool +} + +func (s accountDeviceCodeEndpointSupplier) GetEndpointsFromURL(_ context.Context, rawURL string) (*OAuthAuthorizationServer, error) { + host, ok := strings.CutSuffix(rawURL, "/oidc/accounts/xyz/.well-known/oauth-authorization-server") + if !ok { + return nil, ErrOAuthNotSupported + } + endpoints := &OAuthAuthorizationServer{ + AuthorizationEndpoint: host + "/oidc/accounts/xyz/v1/authorize", + TokenEndpoint: host + "/oidc/accounts/xyz/v1/token", + } + if s.deviceAuthAdvertise { + endpoints.DeviceAuthorizationEndpoint = host + "/oidc/accounts/xyz/v1/device/auth" + } + return endpoints, nil +} + +func (s accountDeviceCodeEndpointSupplier) GetUnifiedOAuthEndpoints(ctx context.Context, host, accountId string) (*OAuthAuthorizationServer, error) { + return s.GetEndpointsFromURL(ctx, accountDiscoveryURL(host, accountId)) +} + +func TestNewDeviceCodeLogin_AccountLevel(t *testing.T) { + account, err := NewBasicAccountOAuthArgument("https://accounts.cloud.databricks.test", "xyz") require.NoError(t, err) - p, err := NewPersistentAuth(t.Context(), - WithOAuthArgument(arg), - WithOAuthEndpointSupplier(MockOAuthEndpointSupplier{}), - ) + unified, err := NewBasicUnifiedOAuthArgument("https://spog.test", "xyz") require.NoError(t, err) - defer p.Close() - _, err = p.NewDeviceCodeLogin(t.Context()) - assert.EqualError(t, err, "device code login is only supported for workspace hosts") + tests := []struct { + name string + arg OAuthArgument + advertise bool + wantDeviceURL string + wantTokenURL string + }{ + { + name: "account host", + arg: account, + advertise: true, + wantDeviceURL: "https://accounts.cloud.databricks.test/oidc/accounts/xyz/v1/device/auth", + wantTokenURL: "https://accounts.cloud.databricks.test/oidc/accounts/xyz/v1/token", + }, + {name: "account host not advertised", arg: account}, + { + name: "unified host", + arg: unified, + advertise: true, + wantDeviceURL: "https://spog.test/oidc/accounts/xyz/v1/device/auth", + wantTokenURL: "https://spog.test/oidc/accounts/xyz/v1/token", + }, + {name: "unified host not advertised", arg: unified}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p, err := NewPersistentAuth(t.Context(), + WithOAuthArgument(tt.arg), + WithOAuthEndpointSupplier(accountDeviceCodeEndpointSupplier{deviceAuthAdvertise: tt.advertise}), + ) + require.NoError(t, err) + defer p.Close() + + l, err := p.NewDeviceCodeLogin(t.Context()) + if tt.wantDeviceURL == "" { + assert.ErrorIs(t, err, ErrDeviceCodeNotSupported) + return + } + require.NoError(t, err) + assert.Equal(t, tt.wantDeviceURL, l.cfg.Endpoint.DeviceAuthURL) + assert.Equal(t, tt.wantTokenURL, l.cfg.Endpoint.TokenURL) + }) + } } func TestNormalizeCode(t *testing.T) { diff --git a/libs/auth/u2m/endpoint_supplier.go b/libs/auth/u2m/endpoint_supplier.go index 987f75ead24..f3f198bf9a4 100644 --- a/libs/auth/u2m/endpoint_supplier.go +++ b/libs/auth/u2m/endpoint_supplier.go @@ -64,8 +64,13 @@ func (c *BasicOAuthEndpointSupplier) GetAccountOAuthEndpoints(ctx context.Contex // GetUnifiedOAuthEndpoints returns the OAuth2 endpoints for the unified host func (c *BasicOAuthEndpointSupplier) GetUnifiedOAuthEndpoints(ctx context.Context, host, accountId string) (*OAuthAuthorizationServer, error) { - oidc := fmt.Sprintf("%s/oidc/accounts/%s/.well-known/oauth-authorization-server", host, accountId) - return c.GetEndpointsFromURL(ctx, oidc) + return c.GetEndpointsFromURL(ctx, accountDiscoveryURL(host, accountId)) +} + +// accountDiscoveryURL returns the URL of the OIDC discovery document of the +// account on host, which is either a classic account host or a unified host. +func accountDiscoveryURL(host, accountId string) string { + return fmt.Sprintf("%s/oidc/accounts/%s/.well-known/oauth-authorization-server", host, accountId) } // OAuthAuthorizationServer contains the OAuth endpoints for a Databricks account @@ -80,6 +85,7 @@ type OAuthAuthorizationServer struct { TokenEndpoint string `json:"token_endpoint"` // DeviceAuthorizationEndpoint is the RFC 8628 device authorization endpoint. - // Workspaces only advertise it when device code login is enabled for them. + // Workspaces and accounts only advertise it when device code login is + // enabled for them. DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint"` } diff --git a/libs/testserver/fake_oidc.go b/libs/testserver/fake_oidc.go index cf1593960f0..8e9e73ba2a1 100644 --- a/libs/testserver/fake_oidc.go +++ b/libs/testserver/fake_oidc.go @@ -36,11 +36,21 @@ type FakeOidc struct { } func (s *FakeOidc) OidcEndpoints() Response { + return oidcEndpoints(s.url + "/oidc") +} + +// AccountOidcEndpoints returns the OIDC discovery document of an account, +// which classic account hosts and unified hosts serve. +func (s *FakeOidc) AccountOidcEndpoints(accountID string) Response { + return oidcEndpoints(s.url + "/oidc/accounts/" + accountID) +} + +func oidcEndpoints(issuer string) Response { return Response{ Body: map[string]string{ - "authorization_endpoint": s.url + "/oidc/v1/authorize", - "token_endpoint": s.url + "/oidc/v1/token", - "device_authorization_endpoint": s.url + "/oidc/v1/device/auth", + "authorization_endpoint": issuer + "/v1/authorize", + "token_endpoint": issuer + "/v1/token", + "device_authorization_endpoint": issuer + "/v1/device/auth", }, } } diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 20aac63e2b7..4a2c7a9bb0e 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -610,6 +610,22 @@ func AddDefaultHandlers(server *Server) { return server.fakeOidc.OidcDeviceAuth(req) }) + server.Handle("GET", "/oidc/accounts/{account_id}/.well-known/oauth-authorization-server", func(req Request) any { + return server.fakeOidc.AccountOidcEndpoints(req.Vars["account_id"]) + }) + + server.Handle("GET", "/oidc/accounts/{account_id}/v1/authorize", func(req Request) any { + return server.fakeOidc.OidcAuthorize(req) + }) + + server.Handle("POST", "/oidc/accounts/{account_id}/v1/token", func(req Request) any { + return server.fakeOidc.OidcToken(req) + }) + + server.Handle("POST", "/oidc/accounts/{account_id}/v1/device/auth", func(req Request) any { + return server.fakeOidc.OidcDeviceAuth(req) + }) + server.Handle("POST", "/telemetry-ext", func(req Request) any { var request struct { ProtoLogs []string `json:"protoLogs"` From 2a9d617d942c9051f99b7372c3c4354f0debf777 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 17 Nov 2026 08:40:14 +0000 Subject: [PATCH 02/12] Check only whether device code login's incompatible flags are set No remaining flag needs the comparison with the default, which was added for --skip-workspace. Co-authored-by: Isaac --- cmd/auth/login_device_code.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/auth/login_device_code.go b/cmd/auth/login_device_code.go index 52fe5082fdf..28834707e73 100644 --- a/cmd/auth/login_device_code.go +++ b/cmd/auth/login_device_code.go @@ -62,9 +62,7 @@ func validateDeviceCodeLogin(ctx context.Context, cmd *cobra.Command) error { return fmt.Errorf("--use-device-code is experimental. Set %s=true to enable it", deviceCodeEnvVar) } for _, name := range deviceCodeIncompatibleFlags { - // Compare with the default so that, e.g., --skip-workspace=false is - // accepted. - if f := cmd.Flag(name); f.Changed && f.Value.String() != f.DefValue { + if cmd.Flag(name).Changed { return fmt.Errorf("--use-device-code cannot be combined with --%s", name) } } From d4ba77d5bcc7a9d54cc43aa8dcdcb1e219db6447 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 17 Nov 2026 08:40:15 +0000 Subject: [PATCH 03/12] Check that no host requests a device code in unsupported-host The filter missed the account-scoped device authorization path. Co-authored-by: Isaac --- .../cmd/auth/login/device-code/unsupported-host/output.txt | 2 +- acceptance/cmd/auth/login/device-code/unsupported-host/script | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt b/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt index 47f203deb1d..8131adc5b2d 100644 --- a/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt +++ b/acceptance/cmd/auth/login/device-code/unsupported-host/output.txt @@ -19,4 +19,4 @@ Error: device code login is not enabled for this host === Device authorization requests ->>> print_requests.py //oidc/v1/device/auth +>>> print_requests.py //device/auth diff --git a/acceptance/cmd/auth/login/device-code/unsupported-host/script b/acceptance/cmd/auth/login/device-code/unsupported-host/script index c91d04802cc..0c26ab8fd66 100644 --- a/acceptance/cmd/auth/login/device-code/unsupported-host/script +++ b/acceptance/cmd/auth/login/device-code/unsupported-host/script @@ -19,4 +19,4 @@ trace DATABRICKS_DISCOVERY_URL="$DATABRICKS_HOST/oidc/accounts/spog-acct-123/.we # Every host is rejected before the device authorization request. title "Device authorization requests" -trace print_requests.py //oidc/v1/device/auth +trace print_requests.py //device/auth From 503249578e115fdc81b88cf08c0b4f51a519542d Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 17 Nov 2026 08:52:44 +0000 Subject: [PATCH 04/12] Let test stubs override the fake server's host metadata testserver.New registered the default /.well-known/databricks-config handler before any test stub, and the router keeps the first registration, so test.toml overrides of that path were ignored. Register it as a fallback that serves only requests no other handler matches. Co-authored-by: Isaac --- libs/testserver/router.go | 21 ++++++++++++++++++++ libs/testserver/router_test.go | 36 ++++++++++++++++++++++++++++++++++ libs/testserver/server.go | 5 +++-- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/libs/testserver/router.go b/libs/testserver/router.go index bcf5493b347..ed806a3e3af 100644 --- a/libs/testserver/router.go +++ b/libs/testserver/router.go @@ -42,6 +42,7 @@ type Router struct { mux *http.ServeMux exact map[string]map[string]HandlerFunc wildcard map[string]bool + fallback map[string]map[string]HandlerFunc // Dispatch is invoked when a route matches. vars holds the path values for // wildcard routes and is nil for exact routes. @@ -55,8 +56,15 @@ func NewRouter() *Router { mux: http.NewServeMux(), exact: map[string]map[string]HandlerFunc{}, wildcard: map[string]bool{}, + fallback: map[string]map[string]HandlerFunc{}, } r.mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + if h, ok := r.fallback[req.URL.Path][req.Method]; ok { + if r.Dispatch != nil { + r.Dispatch(w, req, h, nil) + } + return + } if r.NotFound != nil { r.NotFound(w, req) } @@ -93,6 +101,19 @@ func (r *Router) Handle(method, path string, handler HandlerFunc) { }) } +// HandleFallback registers a handler for an exact method+path that serves +// only requests no other registration matches, whenever that one is made. +// Unlike the first-wins rule of Handle, this lets a handler installed when +// the server is created be overridden by a test stub registered later. +func (r *Router) HandleFallback(method, path string, handler HandlerFunc) { + if r.fallback[path] == nil { + r.fallback[path] = map[string]HandlerFunc{} + } + if _, ok := r.fallback[path][method]; !ok { + r.fallback[path][method] = handler + } +} + // HandleRaw registers a handler with the raw ResponseWriter and Request, bypassing // the JSON serve() pipeline, for endpoints that take over the connection (e.g. a // websocket upgrade). First registration wins; only wildcard patterns are supported. diff --git a/libs/testserver/router_test.go b/libs/testserver/router_test.go index 9d2c8c603e4..7a30fa7057c 100644 --- a/libs/testserver/router_test.go +++ b/libs/testserver/router_test.go @@ -102,6 +102,42 @@ func TestRouterFirstRegistrationWins(t *testing.T) { }) } +func TestRouterFallback(t *testing.T) { + t.Run("served when nothing else matches", func(t *testing.T) { + r, c := newRouter(t) + r.HandleFallback("GET", "/foo", handlerNamed("fallback")) + + r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/foo", nil)) + assert.Equal(t, "fallback", c.handler) + }) + + t.Run("exact registered later wins", func(t *testing.T) { + r, c := newRouter(t) + r.HandleFallback("GET", "/foo", handlerNamed("fallback")) + r.Handle("GET", "/foo", handlerNamed("exact")) + + r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/foo", nil)) + assert.Equal(t, "exact", c.handler) + }) + + t.Run("wildcard registered later wins", func(t *testing.T) { + r, c := newRouter(t) + r.HandleFallback("GET", "/items/42", handlerNamed("fallback")) + r.Handle("GET", "/items/{id}", handlerNamed("wildcard")) + + r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/items/42", nil)) + assert.Equal(t, "wildcard", c.handler) + }) + + t.Run("other methods are not found", func(t *testing.T) { + r, c := newRouter(t) + r.HandleFallback("GET", "/foo", handlerNamed("fallback")) + + r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/foo", nil)) + assert.True(t, c.notFound) + }) +} + func TestRouterNotFound(t *testing.T) { r, c := newRouter(t) r.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/missing", nil)) diff --git a/libs/testserver/server.go b/libs/testserver/server.go index 5170b318818..a1aeeac4f0f 100644 --- a/libs/testserver/server.go +++ b/libs/testserver/server.go @@ -315,8 +315,9 @@ Response.Body = '' // The SDK resolves this during config initialization (as of v0.126.0) to // determine workspace/account IDs, cloud, and OIDC endpoints. Without this // handler, any test that creates an SDK client against this server would fail - // with "No handler for URL: /.well-known/databricks-config". - s.Handle("GET", "/.well-known/databricks-config", func(_ Request) any { + // with "No handler for URL: /.well-known/databricks-config". It is a + // fallback so that test stubs can serve other host metadata. + s.HandleFallback("GET", "/.well-known/databricks-config", func(_ Request) any { return map[string]any{ "oidc_endpoint": server.URL + "/oidc", "workspace_id": strconv.Itoa(TestWorkspaceID), From 059d02272a56cc08c8b0f508995640320570bb33 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 17 Nov 2026 08:52:45 +0000 Subject: [PATCH 05/12] Log in at the account level in the unified-host device code test The fake server's default host metadata has a workspace ID, so the account login saved one and never reached the account-level path. Serve unified host metadata without it. Co-authored-by: Isaac --- .../login/device-code/unified-host/out.databrickscfg | 7 +++---- .../cmd/auth/login/device-code/unified-host/output.txt | 1 + .../cmd/auth/login/device-code/unified-host/script | 2 -- .../cmd/auth/login/device-code/unified-host/test.toml | 10 ++++++++++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg b/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg index f154279afe4..6ac78f6d828 100644 --- a/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg +++ b/acceptance/cmd/auth/login/device-code/unified-host/out.databrickscfg @@ -2,10 +2,9 @@ [DEFAULT] [account] -host = [DATABRICKS_URL] -account_id = spog-acct-123 -workspace_id = [WORKSPACE_ID] -auth_type = databricks-cli +host = [DATABRICKS_URL] +account_id = spog-acct-123 +auth_type = databricks-cli [__settings__] default_profile = account diff --git a/acceptance/cmd/auth/login/device-code/unified-host/output.txt b/acceptance/cmd/auth/login/device-code/unified-host/output.txt index 80a2104ee25..3deef52510a 100644 --- a/acceptance/cmd/auth/login/device-code/unified-host/output.txt +++ b/acceptance/cmd/auth/login/device-code/unified-host/output.txt @@ -13,6 +13,7 @@ Once finished, complete the login by running the same command with provided in your browser. >>> [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123 --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY +To use workspace commands, set workspace_id in your profile or pass --workspace-id. Profile account was successfully saved >>> [CLI] auth token --profile account diff --git a/acceptance/cmd/auth/login/device-code/unified-host/script b/acceptance/cmd/auth/login/device-code/unified-host/script index 9fcd23a1f2b..3252f8b4329 100644 --- a/acceptance/cmd/auth/login/device-code/unified-host/script +++ b/acceptance/cmd/auth/login/device-code/unified-host/script @@ -19,7 +19,5 @@ trace $CLI auth token --profile workspace | gron.py | grep access_token title "Device code requests" trace print_requests.py --oneline //oidc -# The fake server's host metadata returns a workspace ID, so the account -# profile gets one too. # Track the .databrickscfg file that was created to surface changes. mv "./home/.databrickscfg" "./out.databrickscfg" diff --git a/acceptance/cmd/auth/login/device-code/unified-host/test.toml b/acceptance/cmd/auth/login/device-code/unified-host/test.toml index 159efe02696..c73baffa48c 100644 --- a/acceptance/cmd/auth/login/device-code/unified-host/test.toml +++ b/acceptance/cmd/auth/login/device-code/unified-host/test.toml @@ -1 +1,11 @@ RecordRequests = true + +# Host metadata of a unified host: unlike the fake server's default, it has no +# workspace ID, so the account login stays account-level. +[[Server]] +Pattern = "GET /.well-known/databricks-config" +Response.Body = ''' +{ + "account_id": "spog-acct-123" +} +''' From 3a2bb24097e997e71993dd1171ae2c6c39b0bc34 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 17 Nov 2026 17:50:31 +0000 Subject: [PATCH 06/12] Reject completing a device code login for another account The token endpoint of account and unified hosts depends on the account ID, so a pending login started for one account would send its device code to another account's token endpoint. Store the account ID with the pending login and reject a completion with a different one, like a different host. Co-authored-by: Isaac --- .../login/device-code/unified-host/output.txt | 3 +++ .../auth/login/device-code/unified-host/script | 3 +++ cmd/auth/login.go | 1 + cmd/auth/login_device_code.go | 16 ++++++++++++++++ libs/auth/storage/storage.go | 16 +++++++++------- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/acceptance/cmd/auth/login/device-code/unified-host/output.txt b/acceptance/cmd/auth/login/device-code/unified-host/output.txt index 3deef52510a..e55e853335a 100644 --- a/acceptance/cmd/auth/login/device-code/unified-host/output.txt +++ b/acceptance/cmd/auth/login/device-code/unified-host/output.txt @@ -12,6 +12,9 @@ Once finished, complete the login by running the same command with --user-code WDJB-0001 and --confirmation-code set to the confirmation code provided in your browser. +>>> musterr [CLI] auth login --host [DATABRICKS_URL]?a=other-acct --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY +Error: the login for profile account was started with a different account ID. Complete it with the same command that started it. Run the same command without --user-code and --confirmation-code to start a new login + >>> [CLI] auth login --host [DATABRICKS_URL]?a=spog-acct-123 --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY To use workspace commands, set workspace_id in your profile or pass --workspace-id. Profile account was successfully saved diff --git a/acceptance/cmd/auth/login/device-code/unified-host/script b/acceptance/cmd/auth/login/device-code/unified-host/script index 3252f8b4329..6ed3a6dfd16 100644 --- a/acceptance/cmd/auth/login/device-code/unified-host/script +++ b/acceptance/cmd/auth/login/device-code/unified-host/script @@ -7,6 +7,9 @@ export DATABRICKS_DISCOVERY_URL="$DATABRICKS_HOST/oidc/accounts/spog-acct-123/.w # login runs non-interactively. title "Log in to the account" trace $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123" --profile account --use-device-code +# The device code only works with the token endpoint of the account that +# issued it, so the login cannot be completed for another account. +trace musterr $CLI auth login --host "$DATABRICKS_HOST?a=other-acct" --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY trace $CLI auth login --host "$DATABRICKS_HOST?a=spog-acct-123" --profile account --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY trace $CLI auth token --profile account | gron.py | grep access_token diff --git a/cmd/auth/login.go b/cmd/auth/login.go index c60e0e047e5..64fc3bd1328 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -463,6 +463,7 @@ a new profile is created. key: pendingLoginKey(oauthArgument.GetCacheKey()), profile: profileName, host: (&config.Config{Host: authArguments.Host}).CanonicalHostName(), + accountID: oauthAccountID(oauthArgument), clientID: clientID, scopes: slices.Sorted(slices.Values(scopesList)), } diff --git a/cmd/auth/login_device_code.go b/cmd/auth/login_device_code.go index 28834707e73..59f5599fc43 100644 --- a/cmd/auth/login_device_code.go +++ b/cmd/auth/login_device_code.go @@ -142,10 +142,23 @@ type nonInteractiveDeviceCodeLogin struct { key string profile string host string + accountID string clientID string scopes []string } +// oauthAccountID returns the ID of the account whose OIDC endpoints arg logs +// in with, or "" for a workspace host. +func oauthAccountID(arg u2m.OAuthArgument) string { + switch a := arg.(type) { + case u2m.AccountOAuthArgument: + return a.GetAccountId() + case u2m.UnifiedOAuthArgument: + return a.GetAccountId() + } + return "" +} + // pendingLoginKey returns the token store key of the pending login for the // login with cacheKey. func pendingLoginKey(cacheKey string) string { @@ -185,6 +198,7 @@ func (l nonInteractiveDeviceCodeLogin) start(ctx context.Context) error { DeviceCode: da.DeviceCode, UserCode: da.UserCode, Host: l.host, + AccountID: l.accountID, ClientID: l.clientID, Scopes: l.scopes, }}) @@ -272,6 +286,8 @@ func (l nonInteractiveDeviceCodeLogin) changedSetting(pending storage.PendingLog switch { case pending.Host != l.host: return "a different host" + case pending.AccountID != l.accountID: + return "a different account ID" case pending.ClientID != l.clientID: return "a different client ID" case !slices.Equal(pending.Scopes, l.scopes): diff --git a/libs/auth/storage/storage.go b/libs/auth/storage/storage.go index 8f15f465e4e..c820d732134 100644 --- a/libs/auth/storage/storage.go +++ b/libs/auth/storage/storage.go @@ -44,13 +44,15 @@ type PendingLogin struct { // login when a newer one replaced it under the same key. UserCode string `json:"user_code"` - // Host, ClientID and Scopes are the settings the login was started with. - // The device code only works with the same host and client, and the - // token gets the scopes of the device authorization, so the login can - // only be completed with the same settings. - Host string `json:"host"` - ClientID string `json:"client_id,omitempty"` - Scopes []string `json:"scopes,omitempty"` + // Host, AccountID, ClientID and Scopes are the settings the login was + // started with. The device code only works with the same host, account + // and client, and the token gets the scopes of the device authorization, + // so the login can only be completed with the same settings. AccountID is + // empty for workspace hosts, which do not log in with account endpoints. + Host string `json:"host"` + AccountID string `json:"account_id,omitempty"` + ClientID string `json:"client_id,omitempty"` + Scopes []string `json:"scopes,omitempty"` } // Store is the CLI's token-storage abstraction: a key/value store with no From cf90532a5a2e2a4dffabbc453d2cc9685d999eca Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Wed, 18 Nov 2026 11:54:19 +0000 Subject: [PATCH 07/12] Test the account ID check of device code login completion Co-authored-by: Isaac --- cmd/auth/login_device_code_test.go | 52 +++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/cmd/auth/login_device_code_test.go b/cmd/auth/login_device_code_test.go index b6a9c607014..6979d425312 100644 --- a/cmd/auth/login_device_code_test.go +++ b/cmd/auth/login_device_code_test.go @@ -543,13 +543,25 @@ func TestLoginDeviceCodeCompletionChecksSettings(t *testing.T) { completeArgs []string // completeOnOtherHost completes the login on a second server. completeOnOtherHost bool - wantChanged string + // unifiedHost makes the server a unified host of an account. + unifiedHost bool + // startQuery and completeQuery are added to the host of each step. + startQuery string + completeQuery string + wantChanged string }{ { name: "host", completeOnOtherHost: true, wantChanged: "a different host", }, + { + name: "account ID", + unifiedHost: true, + startQuery: "?a=spog-acct-123", + completeQuery: "?a=other-acct", + wantChanged: "a different account ID", + }, { name: "client ID", completeArgs: []string{"--client-id", "other-client"}, @@ -569,15 +581,23 @@ func TestLoginDeviceCodeCompletionChecksSettings(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { server := newDeviceCodeServer(t) + if tt.unifiedHost { + server.Handle("GET", "/.well-known/databricks-config", func(_ testserver.Request) any { + return map[string]any{ + "account_id": "spog-acct-123", + "oidc_endpoint": server.URL + "/oidc/accounts/spog-acct-123", + } + }) + } var tokenRequests atomic.Int32 server.RequestCallback = func(req *testserver.Request) { - if req.URL.Path == "/oidc/v1/token" { + if strings.HasSuffix(req.URL.Path, "/v1/token") { tokenRequests.Add(1) } } setUpDeviceCodeLogin(t, "") ctx := cmdio.MockDiscard(t.Context()) - require.NoError(t, runDeviceCodeLogin(ctx, append([]string{"--host", server.URL, "--profile", "test"}, tt.startArgs...)...)) + require.NoError(t, runDeviceCodeLogin(ctx, append([]string{"--host", server.URL + tt.startQuery, "--profile", "test"}, tt.startArgs...)...)) host := server.URL if tt.completeOnOtherHost { @@ -585,7 +605,7 @@ func TestLoginDeviceCodeCompletionChecksSettings(t *testing.T) { other.RequestCallback = server.RequestCallback host = other.URL } - args := append([]string{"--host", host, "--profile", "test", "--user-code", "WDJB-0001", "--confirmation-code", "FTFA-QQKY"}, tt.completeArgs...) + args := append([]string{"--host", host + tt.completeQuery, "--profile", "test", "--user-code", "WDJB-0001", "--confirmation-code", "FTFA-QQKY"}, tt.completeArgs...) err := runDeviceCodeLogin(ctx, args...) assert.EqualError(t, err, "the login for profile test was started with "+tt.wantChanged+". Complete it with the same command that started it. "+startNewLoginHint) assert.Zero(t, tokenRequests.Load()) @@ -593,6 +613,30 @@ func TestLoginDeviceCodeCompletionChecksSettings(t *testing.T) { } } +func TestOAuthAccountID(t *testing.T) { + workspace, err := u2m.NewBasicWorkspaceOAuthArgument("https://workspace.cloud.databricks.test") + require.NoError(t, err) + account, err := u2m.NewBasicAccountOAuthArgument("https://accounts.cloud.databricks.test", "acct-1") + require.NoError(t, err) + unified, err := u2m.NewBasicUnifiedOAuthArgument("https://unified.cloud.databricks.test", "acct-2") + require.NoError(t, err) + + tests := []struct { + name string + arg u2m.OAuthArgument + want string + }{ + {name: "workspace", arg: workspace, want: ""}, + {name: "account", arg: account, want: "acct-1"}, + {name: "unified", arg: unified, want: "acct-2"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, oauthAccountID(tt.arg)) + }) + } +} + // The host of the second step is compared in its canonical form, like the // host in the cache key. func TestLoginDeviceCodeCompletionWithSameHostInAnotherForm(t *testing.T) { From 3c84a433ae8ca358c94b1d5408b1ad7839dfdd73 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Wed, 18 Nov 2026 11:54:37 +0000 Subject: [PATCH 08/12] Clarify comments on setHostAndAccountId and the account endpoint supplier Co-authored-by: Isaac --- cmd/auth/login.go | 9 +++++---- libs/auth/u2m/device_code_test.go | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/auth/login.go b/cmd/auth/login.go index 64fc3bd1328..da65aef72e2 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -663,14 +663,15 @@ func isHTTPScheme(scheme string) bool { return scheme == "http" || scheme == "https" } -// Sets the host in the persistentAuth object based on the provided arguments and flags. -// Follows the following precedence: +// setHostAndAccountId sets the host and, for account and unified hosts, the +// account ID of authArguments. +// +// The host is taken in this precedence: // 1. [HOST] (first positional argument) or --host flag. Error if both are specified. // 2. Profile host, if available. // 3. Prompt the user for the host. // -// Set the account in the persistentAuth object based on the flags. -// Follows the following precedence: +// The account ID is taken in this precedence: // 1. --account-id flag. // 2. account-id from the specified profile, if available. // 3. Prompt the user for the account-id. diff --git a/libs/auth/u2m/device_code_test.go b/libs/auth/u2m/device_code_test.go index 03834839013..ba5852e44d5 100644 --- a/libs/auth/u2m/device_code_test.go +++ b/libs/auth/u2m/device_code_test.go @@ -343,8 +343,8 @@ func TestNewDeviceCodeLogin_Resources(t *testing.T) { assert.EqualError(t, err, "device code login does not support resource indicators") } -// accountDeviceCodeEndpointSupplier serves the endpoints of account xyz, which -// only its discovery document advertises the device authorization endpoint of. +// accountDeviceCodeEndpointSupplier serves the endpoints of account xyz. Only +// its discovery document advertises the device authorization endpoint. type accountDeviceCodeEndpointSupplier struct { MockOAuthEndpointSupplier deviceAuthAdvertise bool From c219401807c03f928c2fcdb1b7dd2d3a6e0e7af3 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Wed, 18 Nov 2026 11:55:06 +0000 Subject: [PATCH 09/12] Name the account when its OIDC configuration cannot be fetched Co-authored-by: Isaac --- libs/auth/u2m/device_code.go | 2 +- libs/auth/u2m/device_code_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libs/auth/u2m/device_code.go b/libs/auth/u2m/device_code.go index 0c0bb0c66a9..0cd7afbda14 100644 --- a/libs/auth/u2m/device_code.go +++ b/libs/auth/u2m/device_code.go @@ -76,7 +76,7 @@ func (a *PersistentAuth) deviceCodeConfig(ctx context.Context) (*oauth2.Config, if arg, ok := a.oAuthArgument.(AccountOAuthArgument); ok { endpoints, err := a.endpointSupplier.GetEndpointsFromURL(ctx, accountDiscoveryURL(arg.GetAccountHost(), arg.GetAccountId())) if err != nil { - return nil, fmt.Errorf("fetching OAuth endpoints: %w", err) + return nil, fmt.Errorf("fetching the OIDC configuration of account %s: %w", arg.GetAccountId(), err) } cfg.Endpoint.DeviceAuthURL = endpoints.DeviceAuthorizationEndpoint } diff --git a/libs/auth/u2m/device_code_test.go b/libs/auth/u2m/device_code_test.go index ba5852e44d5..c7c9a88e7c2 100644 --- a/libs/auth/u2m/device_code_test.go +++ b/libs/auth/u2m/device_code_test.go @@ -369,6 +369,23 @@ func (s accountDeviceCodeEndpointSupplier) GetUnifiedOAuthEndpoints(ctx context. return s.GetEndpointsFromURL(ctx, accountDiscoveryURL(host, accountId)) } +// A mistyped account ID fails the discovery request, so the error names the +// account rather than saying that the host does not support OAuth. +func TestNewDeviceCodeLogin_UnknownAccount(t *testing.T) { + arg, err := NewBasicAccountOAuthArgument("https://accounts.cloud.databricks.test", "typo") + require.NoError(t, err) + p, err := NewPersistentAuth(t.Context(), + WithOAuthArgument(arg), + WithOAuthEndpointSupplier(accountDeviceCodeEndpointSupplier{deviceAuthAdvertise: true}), + ) + require.NoError(t, err) + defer p.Close() + + _, err = p.NewDeviceCodeLogin(t.Context()) + assert.ErrorIs(t, err, ErrOAuthNotSupported) + assert.ErrorContains(t, err, "fetching the OIDC configuration of account typo: ") +} + func TestNewDeviceCodeLogin_AccountLevel(t *testing.T) { account, err := NewBasicAccountOAuthArgument("https://accounts.cloud.databricks.test", "xyz") require.NoError(t, err) From 9939112611c3eaeea52a1e894ebc01d22c59b06d Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Wed, 18 Nov 2026 11:55:28 +0000 Subject: [PATCH 10/12] Mention accounts in the device code login docs Co-authored-by: Isaac --- cmd/auth/README.md | 8 ++++---- docs/device-code-login.md | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/auth/README.md b/cmd/auth/README.md index 914c7049f76..390d2d602f8 100644 --- a/cmd/auth/README.md +++ b/cmd/auth/README.md @@ -151,7 +151,7 @@ of the profile, and the one of its host when it deletes the host's token. A pending login for a profile that was never saved, or for a host that no profile uses, stays until a new login for it replaces it. -The second step also checks the host, client ID and scopes that the pending -login stores: the device code only works with the same host and client, and the -token gets the scopes of the device authorization, so a profile saved with -other settings would not match its token. +The second step also checks the host, account ID, client ID and scopes that the +pending login stores: the device code only works with the same host, account +and client, and the token gets the scopes of the device authorization, so a +profile saved with other settings would not match its token. diff --git a/docs/device-code-login.md b/docs/device-code-login.md index d9a89d0ed93..bce0bd6745c 100644 --- a/docs/device-code-login.md +++ b/docs/device-code-login.md @@ -105,9 +105,9 @@ another machine. If the confirmation code is rejected, check it and run the command again. If the login expired, start a new one. -Each profile, or host without a profile, has one pending login. Starting a -second login for it replaces the first, so complete one login before starting -another. +Each profile has one pending login. Without a profile, each host has one, or +each host and account for an account or unified host. Starting a second login +for it replaces the first, so complete one login before starting another. Completing the login and `databricks auth logout` remove its pending login. Other logins for the profile, e.g. in a browser, keep it. @@ -129,15 +129,15 @@ fail with a token refresh error and you need to run the login again. | `the command is being run in a non-interactive environment, please specify an account ID using --account-id` | The non-interactive login to an account or unified host cannot prompt for the account ID. | Pass `--account-id`, or log in with a profile that has `account_id`. | | The browser says the code is invalid or expired | The code was mistyped, or more than ~10 minutes passed. | Re-enter the code carefully, or run the login again for a new code. Codes never contain `0`, `1`, `2`, `O`, `Z`, or `I`. | | `Invalid confirmation code` | The confirmation code was mistyped or left blank. | Re-enter the code. After 3 rejected codes, the login fails with `Invalid confirmation code (error code: invalid_request). Start login from scratch` and you need to run it again. | -| `token exchange: Invalid device code` | The login expired. A confirmation code entered after the login expired is first rejected as `Invalid confirmation code`, and the workspace then discards the login. | Run the login again to get a new code. | +| `token exchange: Invalid device code` | The login expired. A confirmation code entered after the login expired is first rejected as `Invalid confirmation code`, and the server then discards the login. | Run the login again to get a new code. | | `token exchange: Device grant rate limit exceeded` | Too many confirmation codes were sent for this login. | Run the login again to get a new code. | -| `unauthorized_client` or `invalid_client` | The OAuth client is not allowed to use device code login in this workspace. | Use the default `databricks-cli` client, or ask your admin to enable the custom client. | +| `unauthorized_client` or `invalid_client` | The OAuth client is not allowed to use device code login on this host. | Use the default `databricks-cli` client, or ask your admin to enable the custom client. | | `--configure-cluster is not supported by the non-interactive device code login` | The cluster picker needs a terminal. | Log in first, then set `cluster_id` on the profile. | | `token exchange: Invalid confirmation code (error code: invalid_request). Check the confirmation code ...` | The confirmation code was mistyped, or the login was not approved yet. | Check the code and approve the login, then run the command again. If it still fails, start a new login. | | `token exchange: Invalid device code (error code: invalid_request). Run the same command without --user-code and --confirmation-code ...` | The login expired. | Start a new login. | | `token exchange: Device grant rate limit exceeded (error code: invalid_request). Rerun this command later ...` | Too many confirmation codes were sent for this login. | Run the command again later. If it still fails, start a new login. | | `--user-code does not match the pending login for profile ...` | The user code was mistyped or swapped with the confirmation code, or a second login was started for the same profile or host. | Check the user code. After a second login, approve the newest code, and complete the newest login with its user code. | -| `the login for profile ... was started with a different host` (or client ID, or scopes) | The second command does not use the host, `--client-id` or `--scopes` of the first one. | Complete the login with the same command that started it, plus `--user-code` and `--confirmation-code`. | +| `the login for profile ... was started with a different host` (or account ID, client ID, or scopes) | The second command does not use the host, account ID, `--client-id` or `--scopes` of the first one. | Complete the login with the same command that started it, plus `--user-code` and `--confirmation-code`. | | `no pending device code login for profile ...` | The login was already completed, or no login was started for this profile or host. | Start a new login. | | `--confirmation-code requires --user-code` | `--user-code` is missing from the command. | Add `--user-code` with the code from the first step. | | `--confirmation-code requires --use-device-code` | `--use-device-code` is missing from the command. | Run the same command as the first step, with `--user-code` and `--confirmation-code`. | From fc9bdbc08b1e43e6dc0f68f143e3a7a138423059 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Wed, 18 Nov 2026 11:58:04 +0000 Subject: [PATCH 11/12] Skip the workspace picker in the non-interactive device code login The second step of a device code login can run in a terminal. For an account-level login on a unified host it then listed the workspaces and waited on a picker, although the non-interactive login never prompts. Co-authored-by: Isaac --- cmd/auth/login.go | 30 +++++++++++++------- cmd/auth/login_device_code_test.go | 44 ++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/cmd/auth/login.go b/cmd/auth/login.go index da65aef72e2..1583730ce4f 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -490,16 +490,22 @@ a new profile is created. // 3. Saving the profile. if shouldPromptWorkspace(authArguments, existingProfile, skipWorkspace) { - wsID, wsErr := promptForWorkspaceSelection(ctx, authArguments, tokenSource) - if wsErr != nil { - log.Warnf(ctx, "Workspace selection failed: %v", wsErr) - } else if wsID != "" { - authArguments.WorkspaceID = wsID + // The non-interactive login never prompts, even when its second + // step runs in a terminal. + if nonInteractive { + cmdio.LogString(ctx, workspaceIDHint) + } else { + wsID, wsErr := promptForWorkspaceSelection(ctx, authArguments, tokenSource) + if wsErr != nil { + log.Warnf(ctx, "Workspace selection failed: %v", wsErr) + } else if wsID != "" { + authArguments.WorkspaceID = wsID + } + // If wsID is empty, the user picked "Skip" — leave WorkspaceID empty. + // SaveToProfile omits the workspace_id key entirely for account-level + // profiles; MatchAccountProfiles treats absent workspace_id the same + // as the legacy "none" sentinel. } - // If wsID is empty, the user picked "Skip" — leave WorkspaceID empty. - // SaveToProfile omits the workspace_id key entirely for account-level - // profiles; MatchAccountProfiles treats absent workspace_id the same - // as the legacy "none" sentinel. } var clusterID, serverlessComputeID string @@ -1029,6 +1035,10 @@ func u2mResourcesFromProfile(p *profile.Profile) []string { return splitScopes(p.Resources) } +// workspaceIDHint tells the user how to use workspace commands after an +// account-level login on a unified host. +const workspaceIDHint = "To use workspace commands, set workspace_id in your profile or pass --workspace-id." + // workspaceListTimeoutSeconds bounds the optional workspace lookup during login. // It is a variable only so tests can shorten it. var workspaceListTimeoutSeconds = 5 @@ -1039,7 +1049,7 @@ var workspaceListTimeoutSeconds = 5 // to the user. func promptForWorkspaceSelection(ctx context.Context, authArguments *auth.AuthArguments, tokenSource oauth2.TokenSource) (string, error) { if !cmdio.IsPromptSupported(ctx) { - cmdio.LogString(ctx, "To use workspace commands, set workspace_id in your profile or pass --workspace-id.") + cmdio.LogString(ctx, workspaceIDHint) return "", nil } diff --git a/cmd/auth/login_device_code_test.go b/cmd/auth/login_device_code_test.go index 6979d425312..0e6fa1eb40c 100644 --- a/cmd/auth/login_device_code_test.go +++ b/cmd/auth/login_device_code_test.go @@ -423,6 +423,19 @@ func newDeviceCodeServer(t *testing.T) *testserver.Server { return server } +// newUnifiedDeviceCodeServer returns a server for a unified host of account +// spog-acct-123. +func newUnifiedDeviceCodeServer(t *testing.T) *testserver.Server { + server := newDeviceCodeServer(t) + server.Handle("GET", "/.well-known/databricks-config", func(_ testserver.Request) any { + return map[string]any{ + "account_id": "spog-acct-123", + "oidc_endpoint": server.URL + "/oidc/accounts/spog-acct-123", + } + }) + return server +} + // A wrong or blank confirmation code leaves the device code valid, so the user // can enter it again. The code is accepted the way the browser displays it, in // any case. @@ -510,6 +523,30 @@ func TestLoginDeviceCodeCompletesInTerminal(t *testing.T) { assert.Empty(t, profiles) } +// The second step of an account-level login on a unified host does not open +// the workspace picker, even in a terminal. +func TestLoginDeviceCodeCompletionInTerminalSkipsWorkspacePicker(t *testing.T) { + server := newUnifiedDeviceCodeServer(t) + var workspaceRequests atomic.Int32 + server.RequestCallback = func(req *testserver.Request) { + if strings.HasSuffix(req.URL.Path, "/workspaces") { + workspaceRequests.Add(1) + } + } + setUpDeviceCodeLogin(t, "") + host := server.URL + "?a=spog-acct-123" + ctx := cmdio.MockDiscard(t.Context()) + require.NoError(t, runDeviceCodeLogin(ctx, "--host", host, "--profile", "account")) + + tm := termtest.Run(t, func(ctx context.Context) (struct{}, error) { + return struct{}{}, runDeviceCodeLogin(ctx, "--host", host, "--profile", "account", "--user-code", "WDJB-0001", "--confirmation-code", "FTFA-QQKY") + }) + tm.WaitFor("set workspace_id in your profile") + _, err := tm.Result() + require.NoError(t, err) + assert.Zero(t, workspaceRequests.Load()) +} + // A user code that does not match the pending login fails before the token // exchange, so the confirmation code is not sent with the device code of // another login. @@ -582,12 +619,7 @@ func TestLoginDeviceCodeCompletionChecksSettings(t *testing.T) { t.Run(tt.name, func(t *testing.T) { server := newDeviceCodeServer(t) if tt.unifiedHost { - server.Handle("GET", "/.well-known/databricks-config", func(_ testserver.Request) any { - return map[string]any{ - "account_id": "spog-acct-123", - "oidc_endpoint": server.URL + "/oidc/accounts/spog-acct-123", - } - }) + server = newUnifiedDeviceCodeServer(t) } var tokenRequests atomic.Int32 server.RequestCallback = func(req *testserver.Request) { From 7bde7ae2f068a2f65981175f0f8a75cc05ff0a35 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Wed, 18 Nov 2026 11:58:18 +0000 Subject: [PATCH 12/12] Document when the test server router uses HandleFallback Co-authored-by: Isaac --- libs/testserver/router.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libs/testserver/router.go b/libs/testserver/router.go index ed806a3e3af..770180be6aa 100644 --- a/libs/testserver/router.go +++ b/libs/testserver/router.go @@ -35,6 +35,11 @@ type HandlerFunc func(req Request) any // test.toml stubs in reverse so leaf-directory configs register first // and win over inherited parent stubs. // +// - First wins only works when the default is registered after the +// stubs, as AddDefaultHandlers is. A default registered when the server +// is created, before any stub, uses HandleFallback instead: it loses to +// any exact or wildcard route, whatever the registration order. +// // Router also clears req.URL.RawPath before dispatch so percent-encoded // slashes (%2F) match literal slashes in patterns; workspace file paths // in tests routinely contain encoded slashes.