diff --git a/.nextchanges/cli/auth-store-config-file.md b/.nextchanges/cli/auth-store-config-file.md new file mode 100644 index 00000000000..82e0bae38bb --- /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. ([#7733](https://github.com/databricks/cli/pull/7733)) 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) + }) + } +}