Skip to content

Commit 1b4a9b0

Browse files
mihaimitrea-dbIsaac
andcommitted
auth: reject cached tokens when login settings change
Store a checksum of the login settings that decide what an OAuth token may do (client ID, scopes and resources) with each profile's cached token. Reject the cached token when the current settings differ, so a token can no longer be used with permissions the profile does not ask for. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 4aa3a4c commit 1b4a9b0

46 files changed

Lines changed: 1303 additions & 163 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Reject a profile's cached OAuth token when the profile's client ID, scopes, or resources change after login, so a token can no longer be used with permissions the profile does not ask for. Run `databricks auth login` again for affected profiles. Profiles logged in with older CLI versions also need one new login. ([#7028](https://github.com/databricks/cli/pull/7028))

‎acceptance/air/image-push/token-cache.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"access_token": "cached-access-token",
66
"token_type": "Bearer",
77
"refresh_token": "test-refresh-token",
8-
"expiry": "2099-01-01T00:00:00Z"
8+
"expiry": "2099-01-01T00:00:00Z",
9+
"profile_checksum": "46123320576e828068e9582e08b5bc7fef4b61796070362caa45c4e488d0b668"
910
}
1011
}
1112
}

‎acceptance/cmd/auth/docker/invalid-refresh-token/token-cache.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"access_token": "expired-access-token",
66
"token_type": "Bearer",
77
"refresh_token": "test-refresh-token",
8-
"expiry": "2000-01-01T00:00:00Z"
8+
"expiry": "2000-01-01T00:00:00Z",
9+
"profile_checksum": "46123320576e828068e9582e08b5bc7fef4b61796070362caa45c4e488d0b668"
910
}
1011
}
1112
}

‎acceptance/cmd/auth/docker/token-cache.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"access_token": "cached-access-token",
66
"token_type": "Bearer",
77
"refresh_token": "test-refresh-token",
8-
"expiry": "2099-01-01T00:00:00Z"
8+
"expiry": "2099-01-01T00:00:00Z",
9+
"profile_checksum": "46123320576e828068e9582e08b5bc7fef4b61796070362caa45c4e488d0b668"
910
}
1011
}
1112
}

‎acceptance/cmd/auth/logout/stale-account-id-workspace-host/script‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ cat > "./home/.databricks/token-cache.json" <<EOF
2020
"tokens": {
2121
"logfood": {
2222
"access_token": "logfood-cached-token",
23-
"token_type": "Bearer"
23+
"token_type": "Bearer",
24+
"profile_checksum": "46123320576e828068e9582e08b5bc7fef4b61796070362caa45c4e488d0b668"
2425
},
2526
"${DATABRICKS_HOST}": {
2627
"access_token": "logfood-host-token",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cloud = false
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
>>> [CLI] auth login --host [DATABRICKS_URL] --profile checksum-test
3+
Profile checksum-test was successfully saved
4+
5+
=== The error for settings that differ from the login
6+
>>> DATABRICKS_CONFIG_PROFILE=checksum-test DATABRICKS_CLIENT_ID=other-client musterr [CLI] clusters list
7+
Error: error getting token: the cached login for profile "checksum-test" does not match this command's settings. Run `databricks auth describe` with the same flags and environment variables to see where each setting comes from, or run `databricks auth login --profile checksum-test` if you changed the profile on purpose
8+
9+
=== auth describe shows where the conflicting client ID comes from
10+
>>> DATABRICKS_CONFIG_PROFILE=checksum-test DATABRICKS_CLIENT_ID=other-client [CLI] auth describe
11+
~ client_id: other-client (from DATABRICKS_CLIENT_ID environment variable, not used for auth type databricks-cli)
12+
13+
=== auth token reports the error as JSON
14+
>>> musterr [CLI] auth token --profile checksum-test --output json
15+
{
16+
"error_code": "UNAUTHENTICATED",
17+
"message": "the cached login for profile \"checksum-test\" does not match this command's settings. Run `databricks auth describe` with the same flags and environment variables to see where each setting comes from, or run `databricks auth login --profile checksum-test` if you changed the profile on purpose"
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trace $CLI auth login --host "$DATABRICKS_HOST" --profile checksum-test
2+
3+
title "The error for settings that differ from the login"
4+
trace DATABRICKS_CONFIG_PROFILE=checksum-test DATABRICKS_CLIENT_ID=other-client musterr $CLI clusters list
5+
6+
title "auth describe shows where the conflicting client ID comes from"
7+
trace DATABRICKS_CONFIG_PROFILE=checksum-test DATABRICKS_CLIENT_ID=other-client $CLI auth describe | grep "client_id:"
8+
9+
title "auth token reports the error as JSON"
10+
update_file.py "./home/.databrickscfg" "[checksum-test]" "[checksum-test]
11+
scopes = sql"
12+
trace musterr $CLI auth token --profile checksum-test --output json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cloud = false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
=== Default settings
3+
ok [CLI] auth login --host [DATABRICKS_URL] --profile plain
4+
ok [CLI] auth token --profile plain
5+
ok [CLI] clusters list --profile plain
6+
7+
=== Custom scopes
8+
ok [CLI] auth login --host [DATABRICKS_URL] --profile scopes --scopes sql,all-apis
9+
ok [CLI] auth token --profile scopes
10+
ok [CLI] clusters list --profile scopes
11+
12+
=== Logging in again with the saved settings
13+
ok [CLI] auth login --profile scopes
14+
ok [CLI] auth token --profile scopes
15+
ok [CLI] clusters list --profile scopes

0 commit comments

Comments
 (0)