Skip to content

Commit

Permalink
Skip loading default profile if host is already configured (#363)
Browse files Browse the repository at this point in the history
## Changes

If a user specified a host to connect to without authentication details
(either directly or via the `DATABRICKS_HOST` environment variable), the
config file loader would load the profile named `DEFAULT` from the
config file and overwrite whatever was already configured. This means it
was possible to configure the host of workspace A and end up connecting
to workspace B (if configured as `DEFAULT` profile in the config file).
This PR prevents this behavior.

Also see #304.

## Tests

- Added unit tests.
- Confirmed behavior with manual test.
  • Loading branch information
pietern authored Apr 6, 2023
1 parent 70d2d43 commit cafc8c9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
11 changes: 11 additions & 0 deletions config/auth_permutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ func TestConfig_ConfigFile(t *testing.T) {
}.apply(t)
}

func TestConfig_ConfigFileSkipDefaultProfileIfHostSpecified(t *testing.T) {
configFixture{
Host: "x",
Env: map[string]string{
// This directory has a DEFAULT profile in databrickscfg
"HOME": "testdata",
},
AssertError: "default auth: cannot configure default credentials. Config: host=https://x",
}.apply(t)
}

func TestConfig_PatFromDatabricksCfg(t *testing.T) {
configFixture{
// loading with DEFAULT profile in databrickscfs
Expand Down
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Config) Authenticate(r *http.Request) error {
return c.auth(r)
}

// IsAzure returns true if client is configured for Azure Databricks
// IsAzure returns if the client is configured for Azure Databricks.
func (c *Config) IsAzure() bool {
isAzureHost := strings.Contains(c.Host, ".azuredatabricks.net") ||
strings.Contains(c.Host, "databricks.azure.cn") ||
Expand All @@ -143,14 +143,14 @@ func (c *Config) IsAzure() bool {
return isAzureHost || c.AzureResourceID != ""
}

// IsGcp returns true if client is configured for GCP
// IsGcp returns if the client is configured for Databricks on Google Cloud.
func (c *Config) IsGcp() bool {
return strings.Contains(c.Host, ".gcp.databricks.com")
}

// IsAws returns true if client is configured for AWS
// IsAws returns if the client is configured for Databricks on AWS.
func (c *Config) IsAws() bool {
return !c.IsAzure() && !c.IsGcp()
return c.Host != "" && !c.IsAzure() && !c.IsGcp()
}

// IsAccountClient returns true if client is configured for Accounts API
Expand Down
2 changes: 1 addition & 1 deletion config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (l configFileLoader) Configure(cfg *Config) error {
// Skip loading config file if some authentication is already explicitly
// configured directly in the config by a user.
// See: https://github.com/databricks/databricks-sdk-go/issues/304
if cfg.Profile == "" && (l.isAnyAuthConfigured(cfg) || cfg.IsAzure()) {
if cfg.Profile == "" && (l.isAnyAuthConfigured(cfg) || cfg.Host != "" || cfg.AzureResourceID != "") {
return nil
}

Expand Down

0 comments on commit cafc8c9

Please sign in to comment.