Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; 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
auth_type = databricks-cli

[__settings__]
default_profile = account

[workspace]
host = [DATABRICKS_URL]
account_id = spog-acct-123
workspace_id = 12345
auth_type = databricks-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
50 changes: 50 additions & 0 deletions acceptance/cmd/auth/login/device-code/unified-host/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

=== 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.

>>> 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

>>> [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"}
26 changes: 26 additions & 0 deletions acceptance/cmd/auth/login/device-code/unified-host/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
# 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

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

# Track the .databrickscfg file that was created to surface changes.
mv "./home/.databrickscfg" "./out.databrickscfg"
11 changes: 11 additions & 0 deletions acceptance/cmd/auth/login/device-code/unified-host/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +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"
}
'''
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[workspace]
host = $DATABRICKS_HOST
auth_type = databricks-cli

[account]
host = https://accounts.cloud.databricks.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@
=== 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

>>> print_requests.py //oidc/v1/device/auth
>>> print_requests.py //device/auth
16 changes: 5 additions & 11 deletions acceptance/cmd/auth/login/device-code/unsupported-host/script
Original file line number Diff line number Diff line change
Expand Up @@ -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
trace print_requests.py //device/auth
Original file line number Diff line number Diff line change
@@ -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 = '''
Expand All @@ -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"
}
'''
19 changes: 11 additions & 8 deletions cmd/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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/<account-id>/.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
Expand Down Expand Up @@ -148,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.
2 changes: 1 addition & 1 deletion cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading