-
Notifications
You must be signed in to change notification settings - Fork 174
Detect mismatch between host defined in config and env variable #2549
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
Changes from 7 commits
eec36ef
32e4600
7296a7f
d74786d
80c6d9f
8678ebf
c7ddaab
3bd5782
01192f7
d404bc6
d2eab7d
3e1dfce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,3 +149,34 @@ func TestWorkspaceVerifyProfileForHost(t *testing.T) { | |
| assert.ErrorContains(t, err, "config host mismatch") | ||
| }) | ||
| } | ||
|
|
||
| func TestWorkspaceValidateConfigAndEnvHost(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you instead convert this to acceptance test? |
||
| t.Run("host from env mismatch", func(t *testing.T) { | ||
| w := Workspace{ | ||
| Host: "https://abc.cloud.databricks.com", | ||
| } | ||
| setupWorkspaceTest(t) | ||
| t.Setenv("DATABRICKS_HOST", "https://def.cloud.databricks.com") | ||
| _, err := w.Client() | ||
| assert.ErrorContains(t, err, "config host mismatch: DATABRICKS_HOST is defined as https://def.cloud.databricks.com, but CLI configured to use https://abc.cloud.databricks.com") | ||
| }) | ||
|
|
||
| t.Run("with profile defined", func(t *testing.T) { | ||
| w := Workspace{ | ||
| Host: "https://abc.cloud.databricks.com", | ||
| Profile: "abc", | ||
| } | ||
|
|
||
| setupWorkspaceTest(t) | ||
| // This works if there is a config file with a matching profile. | ||
| err := databrickscfg.SaveToProfile(context.Background(), &config.Config{ | ||
| Profile: "abc", | ||
| Host: "https://abc.cloud.databricks.com", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Setenv("DATABRICKS_HOST", "https://def.cloud.databricks.com") | ||
| _, err = w.Client() | ||
| require.NoError(t, err) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "context" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config/validate" | ||
| "github.com/databricks/cli/bundle/env" | ||
| "github.com/databricks/cli/bundle/phases" | ||
| "github.com/databricks/cli/libs/cmdctx" | ||
|
|
@@ -94,6 +95,8 @@ func configureBundle(cmd *cobra.Command, b *bundle.Bundle) (*bundle.Bundle, diag | |
| return b, diags | ||
| } | ||
|
|
||
| diags = diags.Extend(bundle.Apply(ctx, b, validate.NoInterpolationInAuthConfig())) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can actually convert the whole |
||
|
|
||
| // Set the auth configuration in the command context. This can be used | ||
| // downstream to initialize a API client. | ||
| // | ||
|
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/cli/libs/workspace" | ||
| "github.com/databricks/databricks-sdk-go/config" | ||
| "gopkg.in/ini.v1" | ||
| ) | ||
|
|
@@ -67,7 +68,7 @@ func matchOrCreateSection(ctx context.Context, configFile *config.File, cfg *con | |
| return false | ||
| } | ||
| // Check if this section matches the normalized host | ||
| return normalizeHost(host) == normalizeHost(cfg.Host) | ||
| return workspace.MatchHost(host, cfg.Host) | ||
| }) | ||
| if err == errNoMatchingProfiles { | ||
| section, err = configFile.NewSection(cfg.Profile) | ||
|
|
@@ -138,16 +139,16 @@ func ValidateConfigAndProfileHost(cfg *config.Config, profile string) error { | |
| } | ||
|
|
||
| // Normalized version of the configured host. | ||
| host := normalizeHost(cfg.Host) | ||
| host := workspace.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()) | ||
| if hostFromProfile != "" && host != "" && hostFromProfile != host { | ||
| hostFromProfile := workspace.NormalizeHost(match.Key("host").Value()) | ||
| if hostFromProfile != "" && host != "" && !workspace.MatchHost(hostFromProfile, host) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added same ignore-scheme-behavior for config-defined hosts |
||
| return fmt.Errorf("config host mismatch: profile uses host %s, but CLI configured to use %s", hostFromProfile, host) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package workspace | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| // NormalizeHost returns the string representation of only | ||
| // the scheme and host part of the specified host. | ||
| func NormalizeHost(host string) string { | ||
| u, err := url.Parse(host) | ||
| if err != nil { | ||
| return host | ||
| } | ||
| if u.Scheme == "" || u.Host == "" { | ||
| return host | ||
| } | ||
|
|
||
| normalized := &url.URL{ | ||
| Scheme: u.Scheme, | ||
| Host: u.Host, | ||
| } | ||
|
|
||
| return normalized.String() | ||
| } | ||
|
|
||
| // Match hosts using only Host part of the URL to allow cases when scheme is not specified | ||
| func MatchHost(host1, host2 string) bool { | ||
| if host1 == "" || host2 == "" { | ||
| return false | ||
| } | ||
| u1, err := url.Parse(fixUrlIfNeeded(host1)) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| u2, err := url.Parse(fixUrlIfNeeded(host2)) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return u1.Host == u2.Host | ||
| } | ||
|
|
||
| func fixUrlIfNeeded(s string) string { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently |
||
| if !strings.HasPrefix(s, "http") { | ||
| return "https://" + s | ||
| } | ||
| return s | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package workspace | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestNormalizeHost(t *testing.T) { | ||
| assert.Equal(t, "invalid", NormalizeHost("invalid")) | ||
|
|
||
| // With port. | ||
| assert.Equal(t, "http://foo:123", NormalizeHost("http://foo:123")) | ||
|
|
||
| // With trailing slash. | ||
| assert.Equal(t, "http://foo", NormalizeHost("http://foo/")) | ||
|
|
||
| // With path. | ||
| assert.Equal(t, "http://foo", NormalizeHost("http://foo/bar")) | ||
|
|
||
| // With query string. | ||
| assert.Equal(t, "http://foo", NormalizeHost("http://foo?bar")) | ||
|
|
||
| // With anchor. | ||
| assert.Equal(t, "http://foo", NormalizeHost("http://foo#bar")) | ||
| } | ||
|
|
||
| func TestMatchHost(t *testing.T) { | ||
| assert.True(t, MatchHost("https://foo.com", "https://foo.com")) | ||
| assert.True(t, MatchHost("https://foo.com", "foo.com")) | ||
|
|
||
| assert.False(t, MatchHost("https://foo.com", "bar.com")) | ||
| assert.False(t, MatchHost("https://foo.com", "::invalid")) | ||
| assert.False(t, MatchHost("foo", "bar")) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in case - we have a warning displayed above that explains incorrect usage of variables in auth-related fields