Skip to content

Commit 9b49187

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 5525326 commit 9b49187

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

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

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

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
@@ -111,33 +111,56 @@ void testBuildCliCommand_ProfileWithNullHost_ThrowsClearError() {
111111
private static Stream<Arguments> buildCliCommandCases() {
112112
return Stream.of(
113113
Arguments.of(
114-
"host only — old CLI",
114+
"host only — old CLI, no force-refresh",
115115
new DatabricksConfig().setHost(HOST),
116116
new DatabricksCliVersion(0, 200, 0),
117117
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
118118
Arguments.of(
119-
"account host — old CLI",
119+
"host only — new CLI, with force-refresh",
120+
new DatabricksConfig().setHost(HOST),
121+
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_FORCE_REFRESH,
122+
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST, "--force-refresh")),
123+
Arguments.of(
124+
"account host — old CLI, no force-refresh",
120125
new DatabricksConfig().setHost(ACCOUNT_HOST).setAccountId(ACCOUNT_ID),
121126
new DatabricksCliVersion(0, 200, 0),
122127
Arrays.asList(
123128
CLI_PATH, "auth", "token", "--host", ACCOUNT_HOST, "--account-id", ACCOUNT_ID)),
124129
Arguments.of(
125-
"profile with new CLI — uses --profile",
130+
"account host — new CLI, with force-refresh",
131+
new DatabricksConfig().setHost(ACCOUNT_HOST).setAccountId(ACCOUNT_ID),
132+
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_FORCE_REFRESH,
133+
Arrays.asList(
134+
CLI_PATH,
135+
"auth",
136+
"token",
137+
"--host",
138+
ACCOUNT_HOST,
139+
"--account-id",
140+
ACCOUNT_ID,
141+
"--force-refresh")),
142+
Arguments.of(
143+
"profile with profile-supporting CLI — uses --profile, no force-refresh",
126144
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
127145
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_PROFILE,
128146
Arrays.asList(CLI_PATH, "auth", "token", "--profile", PROFILE)),
129147
Arguments.of(
130-
"profile with old CLI — falls back to --host",
148+
"profile with newest CLI — uses --profile and --force-refresh",
149+
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
150+
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_FORCE_REFRESH,
151+
Arrays.asList(CLI_PATH, "auth", "token", "--profile", PROFILE, "--force-refresh")),
152+
Arguments.of(
153+
"profile with old CLI — falls back to --host, no force-refresh",
131154
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
132155
new DatabricksCliVersion(0, 207, 0),
133156
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
134157
Arguments.of(
135-
"unknown version — falls back to --host",
158+
"unknown version — falls back to --host, no force-refresh",
136159
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
137160
DatabricksCliVersion.UNKNOWN,
138161
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
139162
Arguments.of(
140-
"dev build — falls back to --host",
163+
"dev build — falls back to --host, no force-refresh",
141164
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
142165
new DatabricksCliVersion(0, 0, 0),
143166
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)));

0 commit comments

Comments
 (0)