Repository navigation
Commit b805cf1
authored
Add force-refresh path for cached U2M tokens (#1552)
## Summary
This PR adds an explicit `PersistentAuth.ForceRefreshToken()` path for
cached U2M OAuth tokens so the CLI can implement `databricks auth token
--force-refresh` on top of the 5-minute proactive refresh buffer
introduced in #1535.
For maintainability reasons it also splits token loading from refresh
decisions and documents a cross-process refresh race for a follow-up
fix.
## Why
#1535 changed the default U2M token path to proactively refresh tokens
that are within 5 minutes of expiry so callers of `databricks auth
token` are less likely to receive a token that expires before it can be
used. That addresses the "give me a useful token" case, which came out
of the `auth token` discussion in [cli issue
#4564](databricks/cli#4564 (comment)).
That proactive refresh is intentionally best-effort, though.
`PersistentAuth.Token()` still returns the existing access token when it
is valid and a proactive refresh fails, because callers did not
explicitly ask to block on a fresh token. That behavior is appropriate
for the default path, but it is not strong enough for integrations that
want to treat the CLI as a token minter or that want to manage their own
cache/TTL policy. In those cases, "return a still-usable token" is
different from "refresh now and give me a newly minted token or fail."
This PR adds that stricter SDK primitive. It gives the CLI a clean way
to implement `--force-refresh` without changing the semantics of the
existing `Token()` flow.
The raw oauth2 error text for forced refresh failures can be misleading.
In particular, when a forced refresh is requested for a cached access
token that is still valid but does not include a refresh token, the
default oauth2 error suggests that the token is expired even though the
real problem is that the cached token is not refresh-capable.
This PR surfaces explicit refresh-token errors instead of relying on the
default oauth2 wording, so callers can distinguish "the current access
token is expired" from "a new token could not be minted because the
refresh token is missing or was rejected by the IdP".
## What changed
### Interface changes
- Added `(*PersistentAuth).ForceRefreshToken() (*oauth2.Token, error)`.
This always attempts to refresh the cached U2M token, regardless of its
current expiry, and redacts the refresh token from the returned value
just like `Token()` does.
### Behavioral changes
- For `Token()`, the only behavioral change is that expired tokens with
no refresh token now return `ErrMissingRefreshToken` instead of a less
specific oauth2 library error. Callers can use `errors.Is(err,
ErrMissingRefreshToken)` to detect this case.
- `PersistentAuth.ForceRefreshToken()` always tries to refresh, even
when the cached access token is still valid.
- If forced refresh fails, `ForceRefreshToken()` returns an error
instead of falling back to the existing token.
- If the cached token does not contain a refresh token,
`ForceRefreshToken()` fails explicitly.
### Internal changes
- Extracted cache lookup into `loadToken()` so token loading and refresh
policy are separated.
- Reused the existing refresh path so forced refresh still goes through
the same endpoint resolution, token exchange, cache update, and
refresh-token redaction logic as the normal path.
- Added dedicated tests for force-refresh success, missing refresh
token, transient refresh failure, invalid refresh token handling, and
the profile-to-host fallback path.
- Documented the remaining cross-process refresh race in `refresh()`
with a follow-up TODO rather than expanding the scope of this PR to add
inter-process coordination.
- Added `ErrMissingRefreshToken` for the "cached token has no refresh
token" case.
- Added missing-refresh-token check into the shared `refresh()` path so
both `Token()` and `ForceRefreshToken()` wrap the same semantic error.
## How is this tested?
- `go test ./credentials/u2m`
- Added unit tests covering:
- refreshing a still-valid cached token via `ForceRefreshToken()`
- failing `ForceRefreshToken()` when the cached token has no refresh
token
- surfacing the exact wrapped error on refresh failure
- preserving `InvalidRefreshTokenError` behavior
- exercising forced refresh through the existing profile-key to host-key
fallback path
---------
Signed-off-by: Mihai Mitrea <[email protected]>1 parent c5ffd91 commit b805cf1
4 files changed
Lines changed: 431 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
6 | 12 | | |
7 | 13 | | |
8 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
| 225 | + | |
229 | 226 | | |
230 | 227 | | |
231 | 228 | | |
232 | 229 | | |
233 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
234 | 234 | | |
235 | | - | |
| 235 | + | |
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
| |||
250 | 250 | | |
251 | 251 | | |
252 | 252 | | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
253 | 265 | | |
254 | 266 | | |
255 | 267 | | |
| |||
259 | 271 | | |
260 | 272 | | |
261 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
262 | 277 | | |
263 | | - | |
264 | | - | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
265 | 303 | | |
266 | 304 | | |
267 | 305 | | |
| |||
278 | 316 | | |
279 | 317 | | |
280 | 318 | | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
281 | 325 | | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
282 | 333 | | |
283 | 334 | | |
284 | 335 | | |
| |||
0 commit comments