From eec96f992bb8f1c027afe4d1cbc3aa745c7bc1d6 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 8 Feb 2024 12:35:29 +0100 Subject: [PATCH 1/4] Ignore environment variables for `auth profiles` --- cmd/auth/profiles.go | 11 ++++++---- cmd/auth/profiles_test.go | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 cmd/auth/profiles_test.go diff --git a/cmd/auth/profiles.go b/cmd/auth/profiles.go index 51ae9b1850..1084bb86e3 100644 --- a/cmd/auth/profiles.go +++ b/cmd/auth/profiles.go @@ -29,8 +29,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" @@ -40,6 +42,9 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { c.Cloud = "gcp" } + // set host again, this time normalized + c.Host = cfg.Host + if skipValidate { err := cfg.Authenticate(&http.Request{ Header: make(http.Header), @@ -74,8 +79,6 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { } 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 0000000000..6e212bf946 --- /dev/null +++ b/cmd/auth/profiles_test.go @@ -0,0 +1,43 @@ +package auth + +import ( + "context" + "path/filepath" + "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) { + var err error + + 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) + + // 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) +} From 290630cb2e7855b376761a2cc547040c3b9cbc04 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 8 Feb 2024 12:38:30 +0100 Subject: [PATCH 2/4] Set host after auth call --- cmd/auth/profiles.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/auth/profiles.go b/cmd/auth/profiles.go index 1084bb86e3..a4572a9946 100644 --- a/cmd/auth/profiles.go +++ b/cmd/auth/profiles.go @@ -42,9 +42,6 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { c.Cloud = "gcp" } - // set host again, this time normalized - c.Host = cfg.Host - if skipValidate { err := cfg.Authenticate(&http.Request{ Header: make(http.Header), @@ -52,6 +49,7 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { if err != nil { return } + c.Host = cfg.Host c.AuthType = cfg.AuthType return } @@ -62,6 +60,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 @@ -73,6 +72,7 @@ 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 From 0356af80524ac901f30f97b0f3eea57eb516e19d Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 8 Feb 2024 12:39:03 +0100 Subject: [PATCH 3/4] . --- cmd/auth/profiles_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/auth/profiles_test.go b/cmd/auth/profiles_test.go index 6e212bf946..5a86a769e8 100644 --- a/cmd/auth/profiles_test.go +++ b/cmd/auth/profiles_test.go @@ -12,14 +12,12 @@ import ( ) func TestProfiles(t *testing.T) { - var err error - ctx := context.Background() dir := t.TempDir() configFile := filepath.Join(dir, ".databrickscfg") // Create a config file with a profile - err = databrickscfg.SaveToProfile(ctx, &config.Config{ + err := databrickscfg.SaveToProfile(ctx, &config.Config{ ConfigFile: configFile, Profile: "profile1", Host: "https://abc.cloud.databricks.com", From 02565885dd3bad2d979704bfa9e235cc254e7450 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 8 Feb 2024 13:19:11 +0100 Subject: [PATCH 4/4] Fix test on Windows --- cmd/auth/profiles_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/auth/profiles_test.go b/cmd/auth/profiles_test.go index 5a86a769e8..c1971705fa 100644 --- a/cmd/auth/profiles_test.go +++ b/cmd/auth/profiles_test.go @@ -3,6 +3,7 @@ package auth import ( "context" "path/filepath" + "runtime" "testing" "github.com/databricks/cli/libs/databrickscfg" @@ -28,6 +29,9 @@ func TestProfiles(t *testing.T) { // 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"}