Skip to content

Commit d151b00

Browse files
Add --force-refresh flag to auth token command (#4767)
Wire a new `--force-refresh` CLI flag that delegates to the SDK's `ForceRefreshToken()` method, bypassing the cached token validity check. The default path through `Token()` is unchanged. > **Note:** this will not compile until the SDK ships `ForceRefreshToken()` ([databricks/databricks-sdk-go#1552](databricks/databricks-sdk-go#1552)) and the CLI''s Go SDK dependency is bumped to v0.126. ## Changes - Add a `--force-refresh` boolean flag to `databricks auth token`. When set, the command calls `PersistentAuth.ForceRefreshToken()` instead of `PersistentAuth.Token()`, which always performs a token refresh against the IdP regardless of the cached token's remaining TTL. - The error handling is shared between both paths: invalid refresh tokens produce the same actionable "To reauthenticate, run..." message, and the backward-compat `cache.ErrNotFound` rewrite is preserved. - Add three acceptance tests covering the force-refresh flag end-to-end: - **`force-refresh-success`**: pre-populated cache with a valid token, `--force-refresh` returns the server's new token (not the cached one). - **`force-refresh-invalid-refresh-token`**: server returns 401 with invalid refresh token; asserts the actionable re-login error message. - **`force-refresh-no-cache`**: no cached token exists; asserts the backward-compat error message is preserved for `--force-refresh`. - Add unit tests verifying the default path still returns a cached valid token without refreshing, and that `--force-refresh` correctly delegates to `ForceRefreshToken()` for both success and failure cases. ## Why [#4564](#4564) reports that external consumers using `databricks auth token` as a credential helper (e.g. Claude Code via `apiKeyHelper`) can receive near-expired tokens that expire before they can be used. [databricks/databricks-sdk-go#1535](databricks/databricks-sdk-go#1535) addressed the common case by adding a 5-minute proactive refresh buffer to `Token()`. However, that proactive refresh is intentionally best-effort: `Token()` still returns the existing access token when it is valid and a proactive refresh fails, because callers did not explicitly ask for a fresh token. For integrations that treat the CLI as a token minter or want to manage their own cache/TTL policy, "return a still-usable token" is different from "refresh now and give me a newly minted token or fail." The `--force-refresh` flag gives those integrations an explicit way to guarantee a fresh token. ## Tests - Unit tests in `cmd/auth/token_test.go`: table-driven cases covering default-path cache reuse, force-refresh success, and force-refresh error preservation. - Acceptance tests in `acceptance/cmd/auth/token/force-refresh-{success,invalid-refresh-token,no-cache}/`: end-to-end tests against the mock OIDC server, run for both `terraform` and `direct` engine variants. --------- Co-authored-by: simon <simon.faltum@databricks.com>
1 parent df16914 commit d151b00

File tree

15 files changed

+174
-9
lines changed

15 files changed

+174
-9
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Notable Changes
66

77
### CLI
8+
* Add `--force-refresh` flag to `databricks auth token` to force a token refresh even when the cached token is still valid ([#4767](https://github.com/databricks/cli/pull/4767)).
89

910
### Bundles
1011
* engine/direct: Fix drift in grants resource due to privilege reordering ([#4794](https://github.com/databricks/cli/pull/4794))

acceptance/cmd/auth/token/force-refresh-invalid-refresh-token/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Error: A new access token could not be retrieved because the refresh token is invalid. To reauthenticate, run the following command:
2+
$ databricks auth login --profile test-profile
3+
4+
Exit code: 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
setup_test_profile
2+
setup_test_token_cache
3+
4+
errcode $CLI auth token --profile test-profile --force-refresh
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[[Server]]
2+
Pattern = "POST /oidc/v1/token"
3+
Response.StatusCode = 401
4+
Response.Body = '{"error": "invalid_request", "error_description": "Refresh token is invalid"}'

acceptance/cmd/auth/token/force-refresh-no-cache/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Error: cache: databricks OAuth is not configured for this host. Try logging in again with `databricks auth login --profile test-profile` before retrying. If this fails, please report this issue to the Databricks CLI maintainers at https://github.com/databricks/cli/issues/new
2+
3+
Exit code: 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setup_test_profile
2+
3+
errcode $CLI auth token --profile test-profile --force-refresh

acceptance/cmd/auth/token/force-refresh-success/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
>>> [CLI] auth token --profile test-profile --force-refresh
3+
"oauth-token"

0 commit comments

Comments
 (0)