Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnester committed Mar 4, 2024
1 parent d6010d7 commit ef16d3c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 82 deletions.
2 changes: 1 addition & 1 deletion bundle/config/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (w *Workspace) Config() *config.Config {
for k := range config.ConfigAttributes {
attr := &config.ConfigAttributes[k]
if !attr.IsZero(cfg) {
attr.SetSource(&config.Source{Type: config.SourceType("bundle")})
cfg.SetAttrSource(attr, &config.Source{Type: config.SourceType("bundle")})
}
}

Expand Down
93 changes: 15 additions & 78 deletions cmd/auth/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"slices"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/databricks-sdk-go/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -88,7 +86,7 @@ func getWorkspaceAuthStatus(cmd *cobra.Command, args []string, showSensitive boo
return &authStatus{
Status: "error",
Error: err,
Details: getAuthDetails(ctx, cmd, cfg, showSensitive),
Details: getAuthDetails(cmd, cfg, showSensitive),
}, nil
}

Expand All @@ -100,7 +98,7 @@ func getWorkspaceAuthStatus(cmd *cobra.Command, args []string, showSensitive boo

status := authStatus{
Status: "success",
Details: getAuthDetails(ctx, cmd, w.Config, showSensitive),
Details: getAuthDetails(cmd, w.Config, showSensitive),
}
status.Details.Username = me.UserName

Expand All @@ -114,14 +112,14 @@ func getAccountAuthStatus(cmd *cobra.Command, args []string, showSensitive bool,
return &authStatus{
Status: "error",
Error: err,
Details: getAuthDetails(ctx, cmd, cfg, showSensitive),
Details: getAuthDetails(cmd, cfg, showSensitive),
}, nil
}

a := root.AccountClient(ctx)
status := authStatus{
Status: "success",
Details: getAuthDetails(ctx, cmd, a.Config, showSensitive),
Details: getAuthDetails(cmd, a.Config, showSensitive),
}

status.Details.AccountID = a.Config.AccountID
Expand All @@ -147,83 +145,22 @@ func render(ctx context.Context, cmd *cobra.Command, status *authStatus, templat
}

type authStatus struct {
Status string `json:"status"`
Error error `json:"error,omitempty"`
Details authDetails `json:"details"`
Status string `json:"status"`
Error error `json:"error,omitempty"`
Details config.AuthDetails `json:"details"`
}

type authDetails struct {
AuthType string `json:"auth_type"`
Username string `json:"username,omitempty"`
Host string `json:"host,omitempty"`
AccountID string `json:"account_id,omitempty"`
Configuration map[string]attrConfig `json:"configuration"`
}

type attrConfig struct {
Value string `json:"value"`
Source *config.Source `json:"source"`
AuthTypeMismatch bool `json:"auth_type_mismatch"`
}

func getAuthDetails(ctx context.Context, cmd *cobra.Command, cfg *config.Config, showSensitive bool) authDetails {
attrSet := make(map[string]attrConfig, 0)
for _, a := range config.ConfigAttributes {
if a.IsZero(cfg) {
continue
}

attrSet[a.Name] = attrConfig{
Value: getValue(cfg, a, showSensitive),
Source: getSource(ctx, cmd, cfg, &a),
AuthTypeMismatch: a.IsAuthAttribute() && !slices.Contains(a.Auth, cfg.AuthType),
func getAuthDetails(cmd *cobra.Command, cfg *config.Config, showSensitive bool) config.AuthDetails {
details := cfg.GetAuthDetails(showSensitive)
for k, v := range details.Configuration {
if k == "profile" && cmd.Flag("profile").Changed {
v.Source = &config.Source{Type: config.SourceType("flag"), Name: "--profile"}
}
}

return authDetails{
AuthType: cfg.AuthType,
Host: cfg.Host,
Configuration: attrSet,
}
}

func getValue(cfg *config.Config, a config.ConfigAttribute, showSensitive bool) string {
if !showSensitive && a.Sensitive {
return "********"
}

return a.GetString(cfg)
}

func getSource(ctx context.Context, cmd *cobra.Command, cfg *config.Config, a *config.ConfigAttribute) *config.Source {
v := a.GetString(cfg)

// Check if the value is from an environment variable
for _, envVar := range a.EnvVars {
if v == env.Get(ctx, envVar) {
return &config.Source{
Type: config.SourceEnv,
Name: envVar,
}
}
}

// Check if the value is from command flags
// Only profile and host can be set like this
// The rest of the values are set from the config file
if a.Name == "profile" && cmd.Flag("profile").Changed {
return &config.Source{
Type: config.SourceType("flag"),
Name: "--profile",
}
}

if a.Name == "host" && cmd.Flag("host").Changed {
return &config.Source{
Type: config.SourceType("flag"),
Name: "--host",
if k == "host" && cmd.Flag("host").Changed {
v.Source = &config.Source{Type: config.SourceType("flag"), Name: "--host"}
}
}

return a.Source
return details
}
4 changes: 4 additions & 0 deletions cmd/auth/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestGetWorkspaceAuthStatus(t *testing.T) {
}
m.WorkspaceClient.Config = cfg
t.Setenv("DATABRICKS_AUTH_TYPE", "azure-cli")
config.ConfigAttributes.Configure(cfg)

status, err := getWorkspaceAuthStatus(cmd, []string{}, showSensitive, func(cmd *cobra.Command, args []string) (*config.Config, error) {
config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{
Expand Down Expand Up @@ -88,6 +89,7 @@ func TestGetWorkspaceAuthStatusError(t *testing.T) {
}
m.WorkspaceClient.Config = cfg
t.Setenv("DATABRICKS_AUTH_TYPE", "azure-cli")
config.ConfigAttributes.Configure(cfg)

status, err := getWorkspaceAuthStatus(cmd, []string{}, showSensitive, func(cmd *cobra.Command, args []string) (*config.Config, error) {
config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{
Expand Down Expand Up @@ -134,6 +136,7 @@ func TestGetWorkspaceAuthStatusSensitive(t *testing.T) {
}
m.WorkspaceClient.Config = cfg
t.Setenv("DATABRICKS_AUTH_TYPE", "azure-cli")
config.ConfigAttributes.Configure(cfg)

status, err := getWorkspaceAuthStatus(cmd, []string{}, showSensitive, func(cmd *cobra.Command, args []string) (*config.Config, error) {
config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{
Expand Down Expand Up @@ -176,6 +179,7 @@ func TestGetAccountAuthStatus(t *testing.T) {
}
m.AccountClient.Config = cfg
t.Setenv("DATABRICKS_AUTH_TYPE", "azure-cli")
config.ConfigAttributes.Configure(cfg)

status, err := getAccountAuthStatus(cmd, []string{}, showSensitive, func(cmd *cobra.Command, args []string) (*config.Config, error) {
config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/databrickscfg/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (l profileFromHostLoader) Configure(cfg *config.Config) error {
func (l profileFromHostLoader) isAnyAuthConfigured(cfg *config.Config) bool {
// If any of the auth-specific attributes are set, we can skip profile resolution.
for _, a := range config.ConfigAttributes {
if !a.IsAuthAttribute() {
if !a.HasAuthAttribute() {

Check failure on line 116 in libs/databrickscfg/loader.go

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

a.HasAuthAttribute undefined (type config.ConfigAttribute has no field or method HasAuthAttribute) (compile)

Check failure on line 116 in libs/databrickscfg/loader.go

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

a.HasAuthAttribute undefined (type config.ConfigAttribute has no field or method HasAuthAttribute) (compile)

Check failure on line 116 in libs/databrickscfg/loader.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

a.HasAuthAttribute undefined (type config.ConfigAttribute has no field or method HasAuthAttribute) (compile)

Check failure on line 116 in libs/databrickscfg/loader.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

a.HasAuthAttribute undefined (type config.ConfigAttribute has no field or method HasAuthAttribute) (compile)

Check failure on line 116 in libs/databrickscfg/loader.go

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

a.HasAuthAttribute undefined (type config.ConfigAttribute has no field or method HasAuthAttribute) (compile)

Check failure on line 116 in libs/databrickscfg/loader.go

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

a.HasAuthAttribute undefined (type config.ConfigAttribute has no field or method HasAuthAttribute) (compile)
continue
}
if !a.IsZero(cfg) {
Expand Down

0 comments on commit ef16d3c

Please sign in to comment.