Skip to content

Commit 68844a4

Browse files
Pass --force-refresh to Databricks CLI auth token command
The SDK manages its own token caching via `CachedTokenSource`. When the SDK shells out to `databricks auth token`, the CLI may return a token from *its* own cache that is about to expire (or has already expired from the SDK's perspective), producing unnecessary refresh failures and retry loops. The CLI added `--force-refresh` in v0.296.0 (databricks/cli#4767) to let callers bypass its cache. With the version-detection infrastructure from the parent PR already in place, opting in is a one-constant, one-branch change: * Introduce `CLI_VERSION_FOR_FORCE_REFRESH = v0.296.0`. * Split `buildCliCommand` into the existing profile/host decision (now `buildCoreCliCommand`) and a thin wrapper that appends `--force-refresh` when supported and otherwise logs a precise warning. Future capability-gated flags slot into the same wrapper. Mirrors: * databricks/databricks-sdk-go#1628 * databricks/databricks-sdk-py#1378 Co-authored-by: Isaac
1 parent 6d775fc commit 68844a4

3 files changed

Lines changed: 64 additions & 9 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515

1616
### Internal Changes
1717
* Detect Databricks CLI version at init time via `databricks version --output json`, enabling version-gated flag support. Successful detections are cached per CLI path; subprocess failures fall back to the most conservative command and are retried on the next call.
18+
* Pass `--force-refresh` to Databricks CLI `auth token` command (when the installed CLI is >= v0.296.0) so the SDK always receives a freshly minted token instead of a potentially stale one from the CLI's internal cache.
1819

1920
### API Changes

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksCliCredentialsProvider.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class DatabricksCliCredentialsProvider implements CredentialsProvider {
3232
// --profile support added in CLI v0.207.1: https://github.com/databricks/cli/pull/855
3333
static final DatabricksCliVersion CLI_VERSION_FOR_PROFILE = new DatabricksCliVersion(0, 207, 1);
3434

35+
// --force-refresh support added in CLI v0.296.0: https://github.com/databricks/cli/pull/4767
36+
static final DatabricksCliVersion CLI_VERSION_FOR_FORCE_REFRESH =
37+
new DatabricksCliVersion(0, 296, 0);
38+
3539
// 5-second cap on `databricks version` so a hung CLI (slow first-run scan, antivirus, blocked
3640
// stdin) does not wedge SDK init indefinitely.
3741
private static final long VERSION_PROBE_TIMEOUT_SECONDS = 5;
@@ -78,13 +82,40 @@ List<String> buildHostArgs(String cliPath, DatabricksConfig config) {
7882
}
7983

8084
/**
81-
* Builds the {@code auth token} command for the given CLI version.
85+
* Builds the full {@code auth token} command, including capability-gated flags.
8286
*
83-
* <p>Falls back to {@code --host} when {@code --profile} is either not configured or not
84-
* supported by the installed CLI.
87+
* <p>Delegates the profile/host decision to {@link #buildCoreCliCommand} and appends {@code
88+
* --force-refresh} when the installed CLI supports it.
8589
*/
8690
List<String> buildCliCommand(
8791
String cliPath, DatabricksConfig config, DatabricksCliVersion version) {
92+
List<String> cmd = buildCoreCliCommand(cliPath, config, version);
93+
if (version.atLeast(CLI_VERSION_FOR_FORCE_REFRESH)) {
94+
cmd.add("--force-refresh");
95+
} else if (version.equals(DatabricksCliVersion.UNKNOWN) || version.isDefaultDevBuild()) {
96+
// Detection failed or no version metadata — we can't prove the CLI lacks --force-refresh,
97+
// just failed to confirm it. The version probe already logged the underlying cause.
98+
LOG.warn(
99+
"Could not confirm --force-refresh support for Databricks CLI {} (requires >= {}). "
100+
+ "The CLI's token cache may provide stale tokens.",
101+
version,
102+
CLI_VERSION_FOR_FORCE_REFRESH);
103+
} else {
104+
LOG.warn(
105+
"Databricks CLI {} does not support --force-refresh (requires >= {}). "
106+
+ "The CLI's token cache may provide stale tokens.",
107+
version,
108+
CLI_VERSION_FOR_FORCE_REFRESH);
109+
}
110+
return cmd;
111+
}
112+
113+
/**
114+
* Builds the base {@code auth token} command without capability-gated flags. Falls back to {@code
115+
* --host} when {@code --profile} is either not configured or not supported by the installed CLI.
116+
*/
117+
List<String> buildCoreCliCommand(
118+
String cliPath, DatabricksConfig config, DatabricksCliVersion version) {
88119
if (config.getProfile() == null) {
89120
return buildHostArgs(cliPath, config);
90121
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksCliCredentialsProviderTest.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,56 @@ void testBuildHostCommand_NonAccountsHostWithAccountId() {
5858
private static Stream<Arguments> buildCliCommandCases() {
5959
return Stream.of(
6060
Arguments.of(
61-
"host only — old CLI",
61+
"host only — old CLI, no force-refresh",
6262
new DatabricksConfig().setHost(HOST),
6363
new DatabricksCliVersion(0, 200, 0),
6464
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
6565
Arguments.of(
66-
"account host — old CLI",
66+
"host only — new CLI, with force-refresh",
67+
new DatabricksConfig().setHost(HOST),
68+
new DatabricksCliVersion(0, 296, 0),
69+
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST, "--force-refresh")),
70+
Arguments.of(
71+
"account host — old CLI, no force-refresh",
6772
new DatabricksConfig().setHost(ACCOUNT_HOST).setAccountId(ACCOUNT_ID),
6873
new DatabricksCliVersion(0, 200, 0),
6974
Arrays.asList(
7075
CLI_PATH, "auth", "token", "--host", ACCOUNT_HOST, "--account-id", ACCOUNT_ID)),
7176
Arguments.of(
72-
"profile with new CLI — uses --profile",
77+
"account host — new CLI, with force-refresh",
78+
new DatabricksConfig().setHost(ACCOUNT_HOST).setAccountId(ACCOUNT_ID),
79+
new DatabricksCliVersion(0, 296, 0),
80+
Arrays.asList(
81+
CLI_PATH,
82+
"auth",
83+
"token",
84+
"--host",
85+
ACCOUNT_HOST,
86+
"--account-id",
87+
ACCOUNT_ID,
88+
"--force-refresh")),
89+
Arguments.of(
90+
"profile with profile-supporting CLI — uses --profile, no force-refresh",
7391
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
7492
new DatabricksCliVersion(0, 207, 1),
7593
Arrays.asList(CLI_PATH, "auth", "token", "--profile", PROFILE)),
7694
Arguments.of(
77-
"profile with old CLI — falls back to --host",
95+
"profile with newest CLI — uses --profile and --force-refresh",
96+
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
97+
new DatabricksCliVersion(0, 296, 0),
98+
Arrays.asList(CLI_PATH, "auth", "token", "--profile", PROFILE, "--force-refresh")),
99+
Arguments.of(
100+
"profile with old CLI — falls back to --host, no force-refresh",
78101
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
79102
new DatabricksCliVersion(0, 207, 0),
80103
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
81104
Arguments.of(
82-
"unknown version — falls back to --host",
105+
"unknown version — falls back to --host, no force-refresh",
83106
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
84107
DatabricksCliVersion.UNKNOWN,
85108
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
86109
Arguments.of(
87-
"dev build — falls back to --host",
110+
"dev build — falls back to --host, no force-refresh",
88111
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
89112
new DatabricksCliVersion(0, 0, 0),
90113
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)));

0 commit comments

Comments
 (0)