Skip to content

Commit 52e36a3

Browse files
Pass --force-refresh to CLI auth token command (databricks#1628)
## Summary Pass `--force-refresh` to the Databricks CLI `auth token` command when the CLI supports it (>= v0.296.0), bypassing the CLI's internal token cache. ## Why The SDK manages its own token caching via `CachedTokenSource`. When the SDK decides it needs a new token and shells out to `databricks auth token`, the CLI may return a cached token that is about to expire (or has already expired from the SDK's perspective). This creates unnecessary refresh failures and retry loops. The CLI added a `--force-refresh` flag ([databricks/cli#4767](databricks/cli#4767), motivated by [databricks/cli#4564](databricks/cli#4564)) that bypasses its internal cache. By using this flag, the SDK is guaranteed a freshly minted token every time it asks for one. With the version detection infrastructure from the parent PR (databricks#1605), adding `--force-refresh` is a one-constant, one-`if` change. ## What changed ### Interface changes None. `CliTokenSource` is not part of the public API surface. ### Behavioral changes - The SDK now appends `--force-refresh` when invoking `databricks auth token` if the CLI version is >= v0.296.0. - A warning is logged when the CLI is too old for `--force-refresh`: `"Databricks CLI v%s does not support --force-refresh (requires >= v%s). The CLI's token cache may provide stale tokens."` ### Internal changes - **`cliVersionForForceRefresh`**: new version constant `{0, 296, 0}`. - **`buildCliCommand`**: adds a nil guard and appends `--force-refresh` after the base command is resolved. ## How is this tested? Additional test cases in `TestBuildCliCommand`: - host + v0.296.0 → appends `--force-refresh` - host + v0.200.0 → no `--force-refresh` - account host + v0.296.0 → appends `--force-refresh` - profile + v0.296.0 → `--profile` + `--force-refresh` - profile + v0.207.1 → `--profile` only (no `--force-refresh`) - profile-only + v0.296.0 → `--profile` + `--force-refresh` - zero version → `--host` only, no `--force-refresh` Signed-off-by: Mihai Mitrea <[email protected]>
1 parent 1d7243d commit 52e36a3

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

‎NEXT_CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
### Internal Changes
2323

24+
* Pass `--force-refresh` to Databricks CLI `auth token` command to bypass the CLI's internal token cache ([#1628](https://github.com/databricks/databricks-sdk-go/pull/1628)).
25+
2426
### API Changes
2527
* Add [w.TemporaryVolumeCredentials](https://pkg.go.dev/github.com/databricks/databricks-sdk-go/service/catalog#TemporaryVolumeCredentialsAPI) workspace-level service.
2628
* Add `GetPermissionLevels`, `GetPermissions`, `SetPermissions` and `UpdatePermissions` methods for [w.KnowledgeAssistants](https://pkg.go.dev/github.com/databricks/databricks-sdk-go/service/knowledgeassistants#KnowledgeAssistantsAPI) workspace-level service.

‎config/cli_token_source.go‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ type cliTokenResponse struct {
3434

3535
// Minimum CLI versions for flag support. Versions are in the format accepted
3636
// by [golang.org/x/mod/semver] (with leading "v").
37-
const cliVersionForProfile = "v0.207.1" // https://github.com/databricks/cli/pull/855
37+
const (
38+
cliVersionForProfile = "v0.207.1" // https://github.com/databricks/cli/pull/855
39+
cliVersionForForceRefresh = "v0.296.0" // https://github.com/databricks/cli/pull/4767
40+
)
3841

3942
// devBuildVersionPrefix is the sentinel the Databricks CLI uses when
4043
// buildVersion wasn't injected by goreleaser (e.g. a plain `go build`).
@@ -112,11 +115,30 @@ func resolveCliCommand(ctx context.Context, cliPath string, cfg *Config) ([]stri
112115
return cmd, nil
113116
}
114117

115-
// buildCliCommand constructs the CLI command for fetching an auth token.
116-
// The CLI version determines which flags are used. Returns a precise error
117-
// describing which piece of configuration is missing when the command cannot
118-
// be built.
118+
// buildCliCommand constructs the full CLI command for fetching an auth
119+
// token, including capability-gated flags. Returns a precise error
120+
// describing which piece of configuration is missing when the command
121+
// cannot be built.
119122
func buildCliCommand(ctx context.Context, cliPath string, cfg *Config, ver string) ([]string, error) {
123+
cmd, err := buildCoreCliCommand(ctx, cliPath, cfg, ver)
124+
if err != nil {
125+
return nil, err
126+
}
127+
if semver.Compare(ver, cliVersionForForceRefresh) >= 0 {
128+
cmd = append(cmd, "--force-refresh")
129+
} else {
130+
logger.Warnf(ctx, "Databricks CLI %s does not support --force-refresh (requires >= %s). The CLI's token cache may provide stale tokens.",
131+
displayVersion(ver), cliVersionForForceRefresh)
132+
}
133+
return cmd, nil
134+
}
135+
136+
// buildCoreCliCommand constructs the base `auth token` command without any
137+
// capability-gated flags. Falls back to --host when --profile is either not
138+
// configured or not supported by the installed CLI. Returns a precise error
139+
// describing which piece of configuration is missing when the command
140+
// cannot be built.
141+
func buildCoreCliCommand(ctx context.Context, cliPath string, cfg *Config, ver string) ([]string, error) {
120142
if cfg.Profile == "" {
121143
cmd, err := buildHostCommand(cliPath, cfg)
122144
if err != nil {

‎config/cli_token_source_test.go‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,25 @@ func TestBuildCliCommand(t *testing.T) {
210210
wantErr string // substring; empty => expect no error
211211
}{
212212
{
213-
name: "host only",
213+
name: "host only — old CLI, no force-refresh",
214214
cfg: &Config{Host: host},
215215
ver: "v0.200.0",
216216
wantCmd: []string{cliPath, "auth", "token", "--host", host},
217217
},
218218
{
219-
name: "account host",
219+
name: "host only — new CLI, with force-refresh",
220+
cfg: &Config{Host: host},
221+
ver: "v0.296.0",
222+
wantCmd: []string{cliPath, "auth", "token", "--host", host, "--force-refresh"},
223+
},
224+
{
225+
name: "account host — with force-refresh",
226+
cfg: &Config{Host: accountHost, AccountID: accountID},
227+
ver: "v0.296.0",
228+
wantCmd: []string{cliPath, "auth", "token", "--host", accountHost, "--account-id", accountID, "--force-refresh"},
229+
},
230+
{
231+
name: "account host — without force-refresh",
220232
cfg: &Config{Host: accountHost, AccountID: accountID},
221233
ver: "v0.200.0",
222234
wantCmd: []string{cliPath, "auth", "token", "--host", accountHost, "--account-id", accountID},
@@ -232,13 +244,19 @@ func TestBuildCliCommand(t *testing.T) {
232244
wantCmd: []string{cliPath, "auth", "token", "--host", unifiedHost},
233245
},
234246
{
235-
name: "profile with new CLI — uses --profile",
247+
name: "profile with new CLI — uses --profile, no force-refresh",
236248
cfg: &Config{Profile: "my-profile", Host: host},
237249
ver: "v0.207.1",
238250
wantCmd: []string{cliPath, "auth", "token", "--profile", "my-profile"},
239251
},
240252
{
241-
name: "profile with old CLI — falls back to --host",
253+
name: "profile with newest CLI — uses --profile and --force-refresh",
254+
cfg: &Config{Profile: "my-profile", Host: host},
255+
ver: "v0.296.0",
256+
wantCmd: []string{cliPath, "auth", "token", "--profile", "my-profile", "--force-refresh"},
257+
},
258+
{
259+
name: "profile with old CLI — falls back to --host, no force-refresh",
242260
cfg: &Config{Profile: "my-profile", Host: host},
243261
ver: "v0.207.0",
244262
wantCmd: []string{cliPath, "auth", "token", "--host", host},
@@ -256,15 +274,21 @@ func TestBuildCliCommand(t *testing.T) {
256274
wantCmd: []string{cliPath, "auth", "token", "--profile", "my-profile"},
257275
},
258276
{
259-
name: "unknown version (detection failed) — falls back to --host",
277+
name: "profile without host and newest CLI — --profile and --force-refresh",
278+
cfg: &Config{Profile: "my-profile"},
279+
ver: "v0.296.0",
280+
wantCmd: []string{cliPath, "auth", "token", "--profile", "my-profile", "--force-refresh"},
281+
},
282+
{
283+
name: "unknown version (detection failed) — falls back to --host, no force-refresh",
260284
cfg: &Config{Profile: "my-profile", Host: host},
261285
ver: "",
262286
wantCmd: []string{cliPath, "auth", "token", "--host", host},
263287
},
264288
{
265289
name: "neither profile nor host — error",
266290
cfg: &Config{},
267-
ver: "v0.295.0",
291+
ver: "v0.296.0",
268292
wantErr: "neither profile nor host is configured",
269293
},
270294
}

0 commit comments

Comments
 (0)