Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore environment variables for auth profiles #1189

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 7 additions & 4 deletions cmd/auth/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -47,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
}
Expand All @@ -57,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
Expand All @@ -68,14 +72,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 {
Expand Down
41 changes: 41 additions & 0 deletions cmd/auth/profiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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) {
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)
}
Loading