bricks auth profiles tweaks (#178)

- add `validate` flag
- return empty list if config file doesn't exist
This commit is contained in:
Fabian Jakobs 2023-01-24 14:54:28 +00:00 committed by GitHub
parent 03c863f49b
commit b97e90acf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 7 deletions

View File

@ -3,8 +3,8 @@ package auth
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -55,6 +55,18 @@ func (c *profileMetadata) Load(ctx context.Context) {
} else if cfg.IsGcp() { } else if cfg.IsGcp() {
c.Cloud = "gcp" c.Cloud = "gcp"
} }
if skipValidate {
err := cfg.Authenticate(&http.Request{
Header: make(http.Header),
})
if err != nil {
return
}
c.AuthType = cfg.AuthType
return
}
if cfg.IsAccountClient() { if cfg.IsAccountClient() {
a, err := databricks.NewAccountClient((*databricks.Config)(cfg)) a, err := databricks.NewAccountClient((*databricks.Config)(cfg))
if err != nil { if err != nil {
@ -71,7 +83,7 @@ func (c *profileMetadata) Load(ctx context.Context) {
if err != nil { if err != nil {
return return
} }
_, err = w.Tokens.ListAll(ctx) _, err = w.CurrentUser.Me(ctx)
c.AuthType = cfg.AuthType c.AuthType = cfg.AuthType
if err != nil { if err != nil {
return return
@ -86,15 +98,14 @@ var profilesCmd = &cobra.Command{
Use: "profiles", Use: "profiles",
Short: "Lists profiles from ~/.databrickscfg", Short: "Lists profiles from ~/.databrickscfg",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var profiles []*profileMetadata
iniFile, err := getDatabricksCfg() iniFile, err := getDatabricksCfg()
if os.IsNotExist(err) { if os.IsNotExist(err) {
// early return for non-configured machines // return empty list for non-configured machines
return errors.New("~/.databrickcfg not found on current host") iniFile = ini.Empty()
} } else if err != nil {
if err != nil {
return fmt.Errorf("cannot parse config file: %w", err) return fmt.Errorf("cannot parse config file: %w", err)
} }
var profiles []*profileMetadata
var wg sync.WaitGroup var wg sync.WaitGroup
for _, v := range iniFile.Sections() { for _, v := range iniFile.Sections() {
hash := v.KeysHash() hash := v.KeysHash()
@ -126,6 +137,9 @@ var profilesCmd = &cobra.Command{
}, },
} }
var skipValidate bool
func init() { func init() {
authCmd.AddCommand(profilesCmd) authCmd.AddCommand(profilesCmd)
profilesCmd.Flags().BoolVar(&skipValidate, "skip-validate", false, "Whether to skip validating the profiles")
} }