From ab637faa4f3c2bdce98bcab6923e358a85c8466e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 21:25:29 +0000 Subject: [PATCH 1/2] auth: read auth_storage from the config file the profile was loaded from CLICredentials.Configure resolved the token storage mode through storage.ResolveStore, which reads [__settings__] auth_storage from DATABRICKS_CONFIG_FILE. A config built with its own ConfigFile, such as one loaded from a pinned file, then took its storage mode from a different file whenever the environment named another one. The SDK calls Configure with a background context, so env overrides from the caller did not apply. Configure now passes the config's ConfigFile to the storage lookup when it is set. An empty ConfigFile keeps the existing lookup. Co-authored-by: Isaac --- .nextchanges/cli/auth-store-config-file.md | 1 + libs/auth/credentials.go | 12 ++++- libs/auth/credentials_test.go | 63 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .nextchanges/cli/auth-store-config-file.md diff --git a/.nextchanges/cli/auth-store-config-file.md b/.nextchanges/cli/auth-store-config-file.md new file mode 100644 index 00000000000..e421e950c92 --- /dev/null +++ b/.nextchanges/cli/auth-store-config-file.md @@ -0,0 +1 @@ +* Fix the token storage mode (`auth_storage`) being read from the file named by `DATABRICKS_CONFIG_FILE` instead of the config file the profile was loaded from. ([#0](https://github.com/databricks/cli/pull/0)) diff --git a/libs/auth/credentials.go b/libs/auth/credentials.go index 26d73ea4eec..db74be07b56 100644 --- a/libs/auth/credentials.go +++ b/libs/auth/credentials.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/libs/auth/storage" "github.com/databricks/cli/libs/auth/u2m" + "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/config/credentials" "github.com/databricks/databricks-sdk-go/config/experimental/auth" @@ -133,7 +134,16 @@ func (c CLICredentials) Configure(ctx context.Context, cfg *config.Config) (cred // Without WithTokenStore, u2m.NewPersistentAuth falls back to an in-memory // store. Every workspace client built through this strategy would then miss // the token persisted by login and fail with "cache: token not found". - tokenStore, mode, err := storage.ResolveStore(ctx, "") + // + // The storage mode is read from [__settings__] in the file the profile was + // loaded from. ResolveStore would otherwise read DATABRICKS_CONFIG_FILE, + // which can name a different file. An empty cfg.ConfigFile keeps that + // lookup unchanged. + storeCtx := ctx + if cfg.ConfigFile != "" { + storeCtx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", cfg.ConfigFile) + } + tokenStore, mode, err := storage.ResolveStore(storeCtx, "") if err != nil { return nil, err } diff --git a/libs/auth/credentials_test.go b/libs/auth/credentials_test.go index ac54c933798..bc9a3976dff 100644 --- a/libs/auth/credentials_test.go +++ b/libs/auth/credentials_test.go @@ -12,6 +12,7 @@ import ( "github.com/databricks/cli/libs/auth/storage" "github.com/databricks/cli/libs/auth/u2m" + "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/config/experimental/auth" "github.com/stretchr/testify/assert" @@ -399,3 +400,65 @@ func TestCLICredentialsConfigure_HonorsConfigFileSecureMode(t *testing.T) { _, err := c.Configure(t.Context(), &config.Config{Host: "https://x.cloud.databricks.com"}) require.NoError(t, err) } + +// writeAuthStorageConfigFile writes a .databrickscfg with the given +// auth_storage under [__settings__] and returns its path. +func writeAuthStorageConfigFile(t *testing.T, mode string) string { + t.Helper() + configPath := filepath.Join(t.TempDir(), "databrickscfg") + body := "[__settings__]\nauth_storage = " + mode + "\n" + require.NoError(t, os.WriteFile(configPath, []byte(body), 0o600)) + return configPath +} + +// TestCLICredentialsConfigure_StorageModeFollowsConfigFile covers a config +// whose ConfigFile differs from DATABRICKS_CONFIG_FILE. The storage mode must +// come from the file the profile was loaded from, not from the environment. +// "bogus" is an unknown mode, so reading that file fails Configure. +func TestCLICredentialsConfigure_StorageModeFollowsConfigFile(t *testing.T) { + tests := []struct { + name string + envMode string // auth_storage in the file DATABRICKS_CONFIG_FILE names + configMode string // auth_storage in the file cfg.ConfigFile names + wantErrText string + }{ + { + name: "config file mode is used when env names an invalid file", + envMode: "bogus", + configMode: "plaintext", + }, + { + name: "env file mode is not used when config names an invalid file", + envMode: "plaintext", + configMode: "bogus", + wantErrText: "auth_storage", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + hermeticAuthStorage(t) + t.Setenv("DATABRICKS_CONFIG_FILE", writeAuthStorageConfigFile(t, tt.envMode)) + configPath := writeAuthStorageConfigFile(t, tt.configMode) + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) + + c := CLICredentials{ + persistentAuthFn: func(_ context.Context, _ ...u2m.PersistentAuthOption) (auth.TokenSource, error) { + return auth.TokenSourceFn(func(_ context.Context) (*oauth2.Token, error) { + return &oauth2.Token{AccessToken: "tok"}, nil + }), nil + }, + } + + _, err := c.Configure(ctx, &config.Config{ + Host: "https://workspace.test", + ConfigFile: configPath, + }) + if tt.wantErrText == "" { + require.NoError(t, err) + return + } + require.ErrorContains(t, err, tt.wantErrText) + }) + } +} From 084cf2a5e392de894e1e220c652f8a3ff53288f0 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 21:26:45 +0000 Subject: [PATCH 2/2] Link the changelog entry to #7733 Co-authored-by: Isaac --- .nextchanges/cli/auth-store-config-file.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/auth-store-config-file.md b/.nextchanges/cli/auth-store-config-file.md index e421e950c92..82e0bae38bb 100644 --- a/.nextchanges/cli/auth-store-config-file.md +++ b/.nextchanges/cli/auth-store-config-file.md @@ -1 +1 @@ -* Fix the token storage mode (`auth_storage`) being read from the file named by `DATABRICKS_CONFIG_FILE` instead of the config file the profile was loaded from. ([#0](https://github.com/databricks/cli/pull/0)) +* Fix the token storage mode (`auth_storage`) being read from the file named by `DATABRICKS_CONFIG_FILE` instead of the config file the profile was loaded from. ([#7733](https://github.com/databricks/cli/pull/7733))