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.
This commit is contained in:
Pieter Noordhuis 2024-02-08 13:25:51 +01:00 committed by GitHub
parent f7d1a5862d
commit a835a3e564
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 4 deletions

View File

@ -31,8 +31,10 @@ func (c *profileMetadata) IsEmpty() bool {
} }
func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) { func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) {
// TODO: disable config loaders other than configfile cfg := &config.Config{
cfg := &config.Config{Profile: c.Name} Loaders: []config.Loader{config.ConfigFile},
Profile: c.Name,
}
_ = cfg.EnsureResolved() _ = cfg.EnsureResolved()
if cfg.IsAws() { if cfg.IsAws() {
c.Cloud = "aws" c.Cloud = "aws"
@ -49,6 +51,7 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) {
if err != nil { if err != nil {
return return
} }
c.Host = cfg.Host
c.AuthType = cfg.AuthType c.AuthType = cfg.AuthType
return return
} }
@ -59,6 +62,7 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) {
return return
} }
_, err = a.Workspaces.List(ctx) _, err = a.Workspaces.List(ctx)
c.Host = cfg.Host
c.AuthType = cfg.AuthType c.AuthType = cfg.AuthType
if err != nil { if err != nil {
return return
@ -70,14 +74,13 @@ func (c *profileMetadata) Load(ctx context.Context, skipValidate bool) {
return return
} }
_, err = w.CurrentUser.Me(ctx) _, err = w.CurrentUser.Me(ctx)
c.Host = cfg.Host
c.AuthType = cfg.AuthType c.AuthType = cfg.AuthType
if err != nil { if err != nil {
return return
} }
c.Valid = true c.Valid = true
} }
// set host again, this time normalized
c.Host = cfg.Host
} }
func newProfilesCommand() *cobra.Command { func newProfilesCommand() *cobra.Command {

45
cmd/auth/profiles_test.go Normal file
View File

@ -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)
}