Skip to content

Commit b68965e

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 228ea4d commit b68965e

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;
@@ -160,13 +164,40 @@ List<String> resolveCliCommand(String cliPath, DatabricksConfig config) {
160164
}
161165

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

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
@@ -106,33 +106,56 @@ void testBuildCliCommand_ProfileWithNullHost_ThrowsClearError() {
106106
private static Stream<Arguments> buildCliCommandCases() {
107107
return Stream.of(
108108
Arguments.of(
109-
"host only — old CLI",
109+
"host only — old CLI, no force-refresh",
110110
new DatabricksConfig().setHost(HOST),
111111
new DatabricksCliVersion(0, 200, 0),
112112
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
113113
Arguments.of(
114-
"account host — old CLI",
114+
"host only — new CLI, with force-refresh",
115+
new DatabricksConfig().setHost(HOST),
116+
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_FORCE_REFRESH,
117+
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST, "--force-refresh")),
118+
Arguments.of(
119+
"account host — old CLI, no force-refresh",
115120
new DatabricksConfig().setHost(ACCOUNT_HOST).setAccountId(ACCOUNT_ID),
116121
new DatabricksCliVersion(0, 200, 0),
117122
Arrays.asList(
118123
CLI_PATH, "auth", "token", "--host", ACCOUNT_HOST, "--account-id", ACCOUNT_ID)),
119124
Arguments.of(
120-
"profile with new CLI — uses --profile",
125+
"account host — new CLI, with force-refresh",
126+
new DatabricksConfig().setHost(ACCOUNT_HOST).setAccountId(ACCOUNT_ID),
127+
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_FORCE_REFRESH,
128+
Arrays.asList(
129+
CLI_PATH,
130+
"auth",
131+
"token",
132+
"--host",
133+
ACCOUNT_HOST,
134+
"--account-id",
135+
ACCOUNT_ID,
136+
"--force-refresh")),
137+
Arguments.of(
138+
"profile with profile-supporting CLI — uses --profile, no force-refresh",
121139
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
122140
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_PROFILE,
123141
Arrays.asList(CLI_PATH, "auth", "token", "--profile", PROFILE)),
124142
Arguments.of(
125-
"profile with old CLI — falls back to --host",
143+
"profile with newest CLI — uses --profile and --force-refresh",
144+
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
145+
DatabricksCliCredentialsProvider.CLI_VERSION_FOR_FORCE_REFRESH,
146+
Arrays.asList(CLI_PATH, "auth", "token", "--profile", PROFILE, "--force-refresh")),
147+
Arguments.of(
148+
"profile with old CLI — falls back to --host, no force-refresh",
126149
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
127150
new DatabricksCliVersion(0, 207, 0),
128151
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
129152
Arguments.of(
130-
"unknown version — falls back to --host",
153+
"unknown version — falls back to --host, no force-refresh",
131154
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
132155
DatabricksCliVersion.UNKNOWN,
133156
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)),
134157
Arguments.of(
135-
"dev build — falls back to --host",
158+
"dev build — falls back to --host, no force-refresh",
136159
new DatabricksConfig().setProfile(PROFILE).setHost(HOST),
137160
new DatabricksCliVersion(0, 0, 0),
138161
Arrays.asList(CLI_PATH, "auth", "token", "--host", HOST)));

0 commit comments

Comments
 (0)