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

Correctly use --profile flag passed for all bundle commands #571

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions bundle/config/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ func (w *Workspace) Client() (*databricks.WorkspaceClient, error) {
AzureLoginAppID: w.AzureLoginAppID,
}

// HACKY fix to not used host based auth when the profile is already set
profile := os.Getenv("DATABRICKS_CONFIG_PROFILE")

// If only the host is configured, we try and unambiguously match it to
// a profile in the user's databrickscfg file. Override the default loaders.
if w.Host != "" && w.Profile == "" && profile == "" {
if w.Host != "" && w.Profile == "" {
cfg.Loaders = []config.Loader{
// Load auth creds from env vars
config.ConfigAttributes,
Expand All @@ -91,6 +88,13 @@ func (w *Workspace) Client() (*databricks.WorkspaceClient, error) {
}
}

if w.Profile != "" {
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
err := databrickscfg.ValidateConfigAndProfileHost(&cfg, w.Profile)
if err != nil {
return nil, err
}
}

return databricks.NewWorkspaceClient(&cfg)
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/root/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func getEnvironment(cmd *cobra.Command) (value string) {
return os.Getenv(envName)
}

func getProfile(cmd *cobra.Command) (value string) {
// The command line flag takes precedence.
flag := cmd.Flag("profile")
if flag != nil {
value = flag.Value.String()
if value != "" {
return
}
}

// If it's not set, use the environment variable.
return os.Getenv("DATABRICKS_CONFIG_PROFILE")
}

// loadBundle loads the bundle configuration and applies default mutators.
func loadBundle(cmd *cobra.Command, args []string, load func() (*bundle.Bundle, error)) (*bundle.Bundle, error) {
b, err := load()
Expand All @@ -38,6 +52,11 @@ func loadBundle(cmd *cobra.Command, args []string, load func() (*bundle.Bundle,
return nil, nil
}

profile := getProfile(cmd)
if profile != "" {
b.Config.Workspace.Profile = profile
}

ctx := cmd.Context()
err = bundle.Apply(ctx, b, bundle.Seq(mutator.DefaultMutators()...))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion libs/databrickscfg/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (l profileFromHostLoader) Configure(cfg *config.Config) error {
}
if err, ok := err.(errMultipleProfiles); ok {
return fmt.Errorf(
"%s: %w: please set DATABRICKS_CONFIG_PROFILE to specify one",
"%s: %w: please set DATABRICKS_CONFIG_PROFILE or provide --profile flag to specify one",
host, err)
}
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions libs/databrickscfg/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/databricks/cli/libs/log"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/config"
"gopkg.in/ini.v1"
)
Expand Down Expand Up @@ -129,6 +130,29 @@ func SaveToProfile(ctx context.Context, cfg *config.Config) error {
return configFile.SaveTo(configFile.Path())
}

func ValidateConfigAndProfileHost(cfg *databricks.Config, profile string) error {
configFile, err := config.LoadFile(cfg.ConfigFile)
if err != nil {
return fmt.Errorf("cannot parse config file: %w", err)
}
// Normalized version of the configured host.
host := normalizeHost(cfg.Host)
match, err := findMatchingProfile(configFile, func(s *ini.Section) bool {
return profile == s.Name()
})

if err != nil {
return err
}

hostFromProfile := normalizeHost(match.Key("host").Value())
pietern marked this conversation as resolved.
Show resolved Hide resolved
if hostFromProfile != "" && host != "" && hostFromProfile != host {
return fmt.Errorf("config host mismatch, profile has %s host but CLI configured to use %s ", hostFromProfile, host)
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func init() {
// We document databrickscfg files with a [DEFAULT] header and wish to keep it that way.
// This, however, does mean we emit a [DEFAULT] section even if it's empty.
Expand Down