Allow account client auth with environment variables when no .databrickscfg file present (#1097)

## Changes
Allow account client auth with environment variables when no
.databrickscfg file present

Makes the behaviour to be in line with WorkspaceClient auth.
## Tests
Added regression test
This commit is contained in:
Andrew Nester 2024-01-02 16:34:43 +01:00 committed by GitHub
parent 9cb098506a
commit e80882b5af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 4 deletions

View File

@ -83,12 +83,14 @@ func MustAccountClient(cmd *cobra.Command, args []string) error {
// 2. 99% of admins will have access to just one account // 2. 99% of admins will have access to just one account
// hence, we don't need to create a special "DEFAULT_ACCOUNT" profile yet // hence, we don't need to create a special "DEFAULT_ACCOUNT" profile yet
_, profiles, err := databrickscfg.LoadProfiles(cmd.Context(), databrickscfg.MatchAccountProfiles) _, profiles, err := databrickscfg.LoadProfiles(cmd.Context(), databrickscfg.MatchAccountProfiles)
if err != nil { if err == nil && len(profiles) == 1 {
return err
}
if len(profiles) == 1 {
cfg.Profile = profiles[0].Name cfg.Profile = profiles[0].Name
} }
// if there is no config file, we don't want to fail and instead just skip it
if err != nil && !errors.Is(err, databrickscfg.ErrNoConfiguration) {
return err
}
} }
allowPrompt := !hasProfileFlag && !shouldSkipPrompt(cmd.Context()) allowPrompt := !hasProfileFlag && !shouldSkipPrompt(cmd.Context())

View File

@ -181,3 +181,51 @@ func TestWorkspaceClientOrPrompt(t *testing.T) {
}) })
}) })
} }
func TestMustAccountClientWorksWithDatabricksCfg(t *testing.T) {
testutil.CleanupEnvironment(t)
dir := t.TempDir()
configFile := filepath.Join(dir, ".databrickscfg")
err := os.WriteFile(
configFile,
[]byte(`
[account-1111]
host = https://accounts.azuredatabricks.net/
account_id = 1111
token = foobar
`),
0755)
require.NoError(t, err)
cmd := New(context.Background())
t.Setenv("DATABRICKS_CONFIG_FILE", configFile)
err = MustAccountClient(cmd, []string{})
require.NoError(t, err)
}
func TestMustAccountClientWorksWithNoDatabricksCfgButEnvironmentVariables(t *testing.T) {
testutil.CleanupEnvironment(t)
ctx, tt := cmdio.SetupTest(context.Background())
t.Cleanup(tt.Done)
cmd := New(ctx)
t.Setenv("DATABRICKS_HOST", "https://accounts.azuredatabricks.net/")
t.Setenv("DATABRICKS_TOKEN", "foobar")
t.Setenv("DATABRICKS_ACCOUNT_ID", "1111")
err := MustAccountClient(cmd, []string{})
require.NoError(t, err)
}
func TestMustAccountClientErrorsWithNoDatabricksCfg(t *testing.T) {
testutil.CleanupEnvironment(t)
ctx, tt := cmdio.SetupTest(context.Background())
t.Cleanup(tt.Done)
cmd := New(ctx)
err := MustAccountClient(cmd, []string{})
require.ErrorContains(t, err, "no configuration file found at")
}