Skip to content

Commit 5ee2187

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 c4e12c1 commit 5ee2187

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
@@ -34,6 +34,10 @@ public class DatabricksCliCredentialsProvider implements CredentialsProvider {
3434
// --profile support added in CLI v0.207.1: https://github.com/databricks/cli/pull/855
3535
static final DatabricksCliVersion CLI_VERSION_FOR_PROFILE = new DatabricksCliVersion(0, 207, 1);
3636

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

165169
/**
166-
* Builds the {@code auth token} command for the given CLI version.
170+
* Builds the full {@code auth token} command, including capability-gated flags.
167171
*
168-
* <p>Falls back to {@code --host} when {@code --profile} is either not configured or not
169-
* supported by the installed CLI.
172+
* <p>Delegates the profile/host decision to {@link #buildCoreCliCommand} and appends {@code
173+
* --force-refresh} when the installed CLI supports it.
170174
*/
171175
List<String> buildCliCommand(
172176
String cliPath, DatabricksConfig config, DatabricksCliVersion version) {
177+
List<String> cmd = buildCoreCliCommand(cliPath, config, version);
178+
if (version.atLeast(CLI_VERSION_FOR_FORCE_REFRESH)) {
179+
cmd.add("--force-refresh");
180+
} else if (version.equals(DatabricksCliVersion.UNKNOWN) || version.isDefaultDevBuild()) {
181+
// Detection failed or no version metadata — we can't prove the CLI lacks --force-refresh,
182+
// just failed to confirm it. The version probe already logged the underlying cause.
183+
LOG.warn(
184+
"Could not confirm --force-refresh support for Databricks CLI {} (requires >= {}). "
185+
+ "The CLI's token cache may provide stale tokens.",
186+
version,
187+
CLI_VERSION_FOR_FORCE_REFRESH);
188+
} else {
189+
LOG.warn(
190+
"Databricks CLI {} does not support --force-refresh (requires >= {}). "
191+
+ "The CLI's token cache may provide stale tokens.",
192+
version,
193+
CLI_VERSION_FOR_FORCE_REFRESH);
194+
}
195+
return cmd;
196+
}
197+
198+
/**
199+
* Builds the base {@code auth token} command without capability-gated flags. Falls back to {@code
200+
* --host} when {@code --profile} is either not configured or not supported by the installed CLI.
201+
*/
202+
List<String> buildCoreCliCommand(
203+
String cliPath, DatabricksConfig config, DatabricksCliVersion version) {
173204
if (config.getProfile() == null) {
174205
return buildHostArgs(cliPath, config);
175206
}

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)