From a835a3e564b5c5174936cb5a053dd5ad71a89698 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 8 Feb 2024 13:25:51 +0100 Subject: [PATCH] Ignore environment variables for `auth profiles` (#1189) ## Changes If environment variables related to unified authentication are set and a user runs `auth profiles`, the environment variables will interfere with the output. This change only takes profile data into account for the output. ## Tests Added a unit test. --- cmd/auth/profiles.go | 11 ++++++---- cmd/auth/profiles_test.go | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 cmd/auth/profiles_test.go diff --git a/cmd/auth/profiles.go b/cmd/auth/profiles.go index 7fdcb8f2..7c4a7ab2 100644 --- a/cmd/auth/profiles.go +++ b/cmd/auth/profiles.go @@ -31,8 +31,10 @@ func (c *profileMetadata) IsEmpty() bool { } func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { - // TODO: disable config loaders other than configfile - cfg := &config.Config{Profile: c.Name} + cfg := &config.Config{ + Loaders: []config.Loader{config.ConfigFile}, + Profile: c.Name, + } _ = cfg.EnsureResolved() if cfg.IsAws() { c.Cloud = "aws" @@ -49,6 +51,7 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { if err != nil { return } + c.Host = cfg.Host c.AuthType = cfg.AuthType return } @@ -59,6 +62,7 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { return } _, err = a.Workspaces.List(ctx) + c.Host = cfg.Host c.AuthType = cfg.AuthType if err != nil { return @@ -70,14 +74,13 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { return } _, err = w.CurrentUser.Me(ctx) + c.Host = cfg.Host c.AuthType = cfg.AuthType if err != nil { return } c.Valid = true } - // set host again, this time normalized - c.Host = cfg.Host } func newProfilesCommand() *cobra.Command { diff --git a/cmd/auth/profiles_test.go b/cmd/auth/profiles_test.go new file mode 100644 index 00000000..c1971705 --- /dev/null +++ b/cmd/auth/profiles_test.go @@ -0,0 +1,45 @@ +package auth + +import ( + "context" + "path/filepath" + "runtime" + "testing" + + "github.com/databricks/cli/libs/databrickscfg" + "github.com/databricks/databricks-sdk-go/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestProfiles(t *testing.T) { + ctx := context.Background() + dir := t.TempDir() + configFile := filepath.Join(dir, ".databrickscfg") + + // Create a config file with a profile + err := databrickscfg.SaveToProfile(ctx, &config.Config{ + ConfigFile: configFile, + Profile: "profile1", + Host: "https://abc.cloud.databricks.com", + Token: "token1", + }) + require.NoError(t, err) + + // Let the environment think we're using another profile + t.Setenv("DATABRICKS_HOST", "https://def.cloud.databricks.com") + t.Setenv("HOME", dir) + if runtime.GOOS == "windows" { + t.Setenv("USERPROFILE", dir) + } + + // Load the profile + profile := &profileMetadata{Name: "profile1"} + profile.Load(ctx, true) + + // Check the profile + assert.Equal(t, "profile1", profile.Name) + assert.Equal(t, "https://abc.cloud.databricks.com", profile.Host) + assert.Equal(t, "aws", profile.Cloud) + assert.Equal(t, "pat", profile.AuthType) +}