Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/auth-store-config-file.md
Original file line number Diff line number Diff line change
@@ -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. ([#7733](https://github.com/databricks/cli/pull/7733))
12 changes: 11 additions & 1 deletion libs/auth/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
63 changes: 63 additions & 0 deletions libs/auth/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
}
Loading