From 656d2f596a43913b22e3ec651aee07122559f102 Mon Sep 17 00:00:00 2001 From: Ilia Babanov Date: Thu, 9 Nov 2023 16:07:56 +0100 Subject: [PATCH 1/4] Save only explicit config fields to the profile file --- cmd/auth/login.go | 9 ++++++-- cmd/configure/configure.go | 6 ++++- cmd/configure/configure_test.go | 41 ++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/cmd/auth/login.go b/cmd/auth/login.go index c2b821b68c..669bc406b7 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -111,8 +111,13 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command { } if profileName != "" { - cfg.Profile = profileName - err = databrickscfg.SaveToProfile(ctx, &cfg) + err = databrickscfg.SaveToProfile(ctx, &config.Config{ + Profile: profileName, + Host: cfg.Host, + AuthType: cfg.AuthType, + AccountID: cfg.AccountID, + ClusterID: cfg.ClusterID, + }) if err != nil { return err } diff --git a/cmd/configure/configure.go b/cmd/configure/configure.go index 33ab918e88..55ede5385b 100644 --- a/cmd/configure/configure.go +++ b/cmd/configure/configure.go @@ -160,7 +160,11 @@ func newConfigureCommand() *cobra.Command { cfg.DatabricksCliPath = "" // Save profile to config file. - return databrickscfg.SaveToProfile(ctx, &cfg) + return databrickscfg.SaveToProfile(ctx, &config.Config{ + Profile: cfg.Profile, + Host: cfg.Host, + Token: cfg.Token, + }) } return cmd diff --git a/cmd/configure/configure_test.go b/cmd/configure/configure_test.go index cf0505eddf..79c67dc8a5 100644 --- a/cmd/configure/configure_test.go +++ b/cmd/configure/configure_test.go @@ -106,7 +106,46 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) { assertKeyValueInSection(t, defaultSection, "token", "token") } -func TestCustomProfileConfigureNoInteractive(t *testing.T) { +func TestEnvVarsConfigureNoInteractive(t *testing.T) { + ctx := context.Background() + tempHomeDir := setup(t) + cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") + inp := getTempFileWithContent(t, tempHomeDir, "token\n") + defer inp.Close() + oldStdin := os.Stdin + t.Cleanup(func() { os.Stdin = oldStdin }) + os.Stdin = inp + + t.Setenv("DATABRICKS_HOST", "https://host") + t.Setenv("DATABRICKS_AUTH_TYPE", "metadata-service") + t.Setenv("DATABRICKS_METADATA_SERVICE_URL", "https://metadata") + + cmd := cmd.New(ctx) + cmd.SetArgs([]string{"configure", "--token"}) + + err := cmd.ExecuteContext(ctx) + assert.NoError(t, err) + + _, err = os.Stat(cfgPath) + assert.NoError(t, err) + + cfg, err := ini.Load(cfgPath) + assert.NoError(t, err) + + defaultSection, err := cfg.GetSection("DEFAULT") + assert.NoError(t, err) + + assertKeyValueInSection(t, defaultSection, "host", "https://host") + assertKeyValueInSection(t, defaultSection, "token", "token") + + // We should only save host and token for a profile, other env variables should not be saved + _, err = defaultSection.GetKey("auth_type") + assert.NotNil(t, err) + _, err = defaultSection.GetKey("metadata_service_url") + assert.NotNil(t, err) +} + +func TestCustomProfileFromEnvConfigureNoInteractive(t *testing.T) { ctx := context.Background() tempHomeDir := setup(t) cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") From fe3f41595f50d9f5429c4ad3befa5bbabbee672d Mon Sep 17 00:00:00 2001 From: Ilia Babanov Date: Thu, 9 Nov 2023 16:59:59 +0100 Subject: [PATCH 2/4] Fix formatting --- cmd/auth/login.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/auth/login.go b/cmd/auth/login.go index 669bc406b7..826824bcdb 100644 --- a/cmd/auth/login.go +++ b/cmd/auth/login.go @@ -112,9 +112,9 @@ func newLoginCommand(persistentAuth *auth.PersistentAuth) *cobra.Command { if profileName != "" { err = databrickscfg.SaveToProfile(ctx, &config.Config{ - Profile: profileName, - Host: cfg.Host, - AuthType: cfg.AuthType, + Profile: profileName, + Host: cfg.Host, + AuthType: cfg.AuthType, AccountID: cfg.AccountID, ClusterID: cfg.ClusterID, }) From dfab68adb48e228b6b550e94212296c8962099ac Mon Sep 17 00:00:00 2001 From: Ilia Babanov Date: Fri, 10 Nov 2023 10:57:46 +0100 Subject: [PATCH 3/4] Rename a spec back to its original name --- cmd/configure/configure_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/configure/configure_test.go b/cmd/configure/configure_test.go index 79c67dc8a5..37f5b02bf4 100644 --- a/cmd/configure/configure_test.go +++ b/cmd/configure/configure_test.go @@ -145,7 +145,7 @@ func TestEnvVarsConfigureNoInteractive(t *testing.T) { assert.NotNil(t, err) } -func TestCustomProfileFromEnvConfigureNoInteractive(t *testing.T) { +func TestCustomProfileConfigureNoInteractive(t *testing.T) { ctx := context.Background() tempHomeDir := setup(t) cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") From 80058a3c810cc796a1004ea34dc8502ba4401323 Mon Sep 17 00:00:00 2001 From: Ilia Babanov Date: Fri, 10 Nov 2023 14:40:39 +0100 Subject: [PATCH 4/4] Test that `configure` can read host and token from the env vars --- cmd/configure/configure_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/cmd/configure/configure_test.go b/cmd/configure/configure_test.go index 37f5b02bf4..259c83adb3 100644 --- a/cmd/configure/configure_test.go +++ b/cmd/configure/configure_test.go @@ -145,6 +145,33 @@ func TestEnvVarsConfigureNoInteractive(t *testing.T) { assert.NotNil(t, err) } +func TestEnvVarsConfigureNoArgsNoInteractive(t *testing.T) { + ctx := context.Background() + tempHomeDir := setup(t) + cfgPath := filepath.Join(tempHomeDir, ".databrickscfg") + + t.Setenv("DATABRICKS_HOST", "https://host") + t.Setenv("DATABRICKS_TOKEN", "secret") + + cmd := cmd.New(ctx) + cmd.SetArgs([]string{"configure"}) + + err := cmd.ExecuteContext(ctx) + assert.NoError(t, err) + + _, err = os.Stat(cfgPath) + assert.NoError(t, err) + + cfg, err := ini.Load(cfgPath) + assert.NoError(t, err) + + defaultSection, err := cfg.GetSection("DEFAULT") + assert.NoError(t, err) + + assertKeyValueInSection(t, defaultSection, "host", "https://host") + assertKeyValueInSection(t, defaultSection, "token", "secret") +} + func TestCustomProfileConfigureNoInteractive(t *testing.T) { ctx := context.Background() tempHomeDir := setup(t)