diff --git a/.nextchanges/bundles/7948.md b/.nextchanges/bundles/7948.md new file mode 100644 index 00000000000..f712af6dcc5 --- /dev/null +++ b/.nextchanges/bundles/7948.md @@ -0,0 +1 @@ +* Fix the bundle auth error "the host in the profile doesn’t match the host configured in the bundle" when only one of the hosts includes the default port, such as `:443`. ([#7948](https://github.com/databricks/cli/pull/7948)) diff --git a/bundle/config/workspace_test.go b/bundle/config/workspace_test.go index bf5c66a9e50..cb17c1a090c 100644 --- a/bundle/config/workspace_test.go +++ b/bundle/config/workspace_test.go @@ -276,6 +276,23 @@ func TestWorkspaceVerifyProfileForHost(t *testing.T) { assert.NoError(t, err) }) + t.Run("default config file with match on the default port", func(t *testing.T) { + setupWorkspaceTest(t) + + err := databrickscfg.SaveToProfile(t.Context(), &config.Config{ + Profile: "abc", + Host: "https://abc.test", + }) + require.NoError(t, err) + + w := Workspace{ + Host: "https://abc.test:443", + Profile: "abc", + } + _, err = w.Client(t.Context()) + assert.NoError(t, err) + }) + t.Run("default config file with mismatch", func(t *testing.T) { setupWorkspaceTest(t) diff --git a/cmd/configure/host.go b/cmd/configure/host.go index 8f823bee655..6d201085fe7 100644 --- a/cmd/configure/host.go +++ b/cmd/configure/host.go @@ -7,7 +7,8 @@ import ( ) // normalizeHost ensures a https:// scheme is present and returns only scheme -// and host, consistent with the normalizeHost in libs/databrickscfg/host.go. +// and host, like the normalizeHost in libs/databrickscfg/host.go, which also +// drops the default port when matching profiles. func normalizeHost(input string) string { input = strings.TrimSpace(input) u, err := url.Parse(input) diff --git a/libs/databrickscfg/host.go b/libs/databrickscfg/host.go index dc9f503c92b..858a555276c 100644 --- a/libs/databrickscfg/host.go +++ b/libs/databrickscfg/host.go @@ -1,9 +1,13 @@ package databrickscfg -import "net/url" +import ( + "net/url" + "strings" +) // normalizeHost returns the string representation of only -// the scheme and host part of the specified host. +// the scheme and host part of the specified host, without +// the scheme's default port. func normalizeHost(host string) string { u, err := url.Parse(host) if err != nil { @@ -17,6 +21,9 @@ func normalizeHost(host string) string { Scheme: u.Scheme, Host: u.Host, } + if port := u.Port(); (u.Scheme == "https" && port == "443") || (u.Scheme == "http" && port == "80") { + normalized.Host = strings.TrimSuffix(u.Host, ":"+port) + } return normalized.String() } diff --git a/libs/databrickscfg/host_test.go b/libs/databrickscfg/host_test.go index 0117aa612a6..2ba12dfaffc 100644 --- a/libs/databrickscfg/host_test.go +++ b/libs/databrickscfg/host_test.go @@ -11,6 +11,12 @@ func TestNormalizeHost(t *testing.T) { // With port. assert.Equal(t, "http://foo:123", normalizeHost("http://foo:123")) + assert.Equal(t, "https://foo:80", normalizeHost("https://foo:80")) + + // With the scheme's default port. + assert.Equal(t, "https://foo", normalizeHost("https://foo:443")) + assert.Equal(t, "http://foo", normalizeHost("http://foo:80/")) + assert.Equal(t, "https://[::1]", normalizeHost("https://[::1]:443")) // With trailing slash. assert.Equal(t, "http://foo", normalizeHost("http://foo/"))