diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f93aa25..f25e63f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased (v2.0.0 prototype) +- Honor `WithPort` / DSN port on the kernel backend, including the automatic Reyden recovery onto the kernel: a non-default port is folded into the host (`host:port`; an IPv6 literal must be bracketed, as `[::1]:8443`) instead of being rejected. A port embedded in the host wins over a differing `WithPort` (a warning is logged), a zero port means the default, and a trailing `/` on the host is ignored. An out-of-range port, a malformed or empty port in the host, or a non-default port with a host that has a path, query, fragment or credentials, is rejected with `ErrInvalidKernelConfig`. With OAuth and a non-default port, the kernel uses the same `host:port` for OAuth; Thrift keeps OAuth on the bare host (documented, not guarded). - Render non-finite FLOAT / DOUBLE values inside ARRAY / MAP / STRUCT results as the JSON strings `"NaN"`, `"Infinity"`, and `"-Infinity"` on both backends instead of failing row iteration. SQL NULL remains `null`; top-level floats remain numeric (databricks/databricks-sql-go#573). - **Behavior change:** Non-finite floating-point MAP keys use the same spellings as values on both backends. On the kernel backend, keys previously rendered as `"+Inf"` and `"-Inf"` now render as `"Infinity"` and `"-Infinity"`; `"NaN"` is unchanged. On Thrift, these keys previously failed row iteration (databricks/databricks-sql-go#573). - Fix the module-root package conflict when building, linting, or running Gazelle with the `tools` build tag (databricks/databricks-sql-go#162). diff --git a/CONNECTION_PARAMETERS.md b/CONNECTION_PARAMETERS.md index 28624b37..18920a25 100644 --- a/CONNECTION_PARAMETERS.md +++ b/CONNECTION_PARAMETERS.md @@ -40,7 +40,7 @@ wire. They are listed under [Kernel client-result confs](#kernel-client-result-c |---|---|:---:|:---:|---|---| | *(host)* | `WithServerHostname` | ✅ | ✅ | *(required)* | Workspace hostname. | | *(path)* | `WithHTTPPath` | ✅ | ✅ | required on Thrift except localhost; required on kernel unless `warehouseId` is set | Warehouse/endpoint HTTP path. On the kernel path, a bare warehouse id can be used instead. | -| *(port)* | `WithPort` | ✅ | ⚠️ | `443` for connector options; required in a DSN | Kernel accepts **443 only** and rejects any other port. | +| *(port)* | `WithPort` | ✅ | ✅ | `443` for connector options; required in a DSN | Kernel folds a non-default port into the host it connects to. A port embedded in the host wins over a differing `WithPort` (a warning is logged and the embedded port is used); an out-of-range port, a malformed port in the host, or a non-default port with a host that has a path, query, fragment or credentials, is rejected at connect. An IPv6 literal must be bracketed (`[::1]`, not `::1`). With OAuth the kernel uses the same `host:port` for its OAuth endpoints; Thrift keeps OAuth on the bare host. | | *(scheme)* | *(via `WithServerHostname`)* | ✅ | ⚠️ | `https` | Kernel accepts `https` only and rejects `http`. A bare `localhost` passed to `WithServerHostname` selects `http`; a scheme-less DSN, including localhost, is prefixed with `https`. | | `warehouseId` | `WithWarehouseID` | ⚠️ | ✅ | | Bare warehouse id. The kernel normally prefers it over the HTTP path; a canonical path carrying `?o=` takes precedence so workspace-org routing is preserved. The Thrift backend **silently ignores** it. | | `catalog` | `WithInitialNamespace` | ✅ | ✅ | | Initial catalog. Kernel applies it post-connect via `USE CATALOG`. | diff --git a/README.md b/README.md index 77781de3..e187e97c 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,7 @@ session parameter on both backends. |---|---|---|---|---| | *(host)* | `WithServerHostname` | Both | *(required)* | Workspace hostname. | | *(path)* | `WithHTTPPath` | Both | *(required)* | Warehouse/endpoint HTTP path. | -| *(port)* | `WithPort` | Thrift only | `443` | Kernel connects on **443 only** and rejects any other port. | +| *(port)* | `WithPort` | Both | `443` | Kernel folds a non-default port into the host it connects to. A port embedded in the host wins over a differing `WithPort` (a warning is logged); an out-of-range port, a malformed port in the host, or a non-default port with a host that has a path, query, fragment or credentials, is rejected at connect. An IPv6 literal must be bracketed (`[::1]`, not `::1`). With OAuth the kernel uses the same `host:port` for its OAuth endpoints; Thrift keeps OAuth on the bare host. | | `warehouseId` | `WithWarehouseID` | SEA only | | Bare warehouse id; the kernel routes by it (preferred over the HTTP path). **The Thrift backend ignores it.** | | `catalog` | `WithInitialNamespace` | Both | | Initial catalog. Kernel applies it post-connect via `USE CATALOG`. | | `schema` | `WithInitialNamespace` | Both | | Initial schema. Kernel applies it post-connect via `USE SCHEMA`. | diff --git a/connector_reyden_test.go b/connector_reyden_test.go index 858d8ca3..d789631c 100644 --- a/connector_reyden_test.go +++ b/connector_reyden_test.go @@ -216,6 +216,29 @@ func TestReydenReactiveRecovery(t *testing.T) { assert.True(t, warehouse_cache.IsKnownReyden(tt.host, tt.warehouseID), "warehouse should be marked Reyden after the rejection") }) + + t.Run("recovery onto the kernel keeps a custom WithPort", func(t *testing.T) { + defer warehouse_cache.ClearCache() + + tt := NewTestReydenFallback() + var kernelHost string + conn := tt.makeConnector( + func(ctx context.Context, cfg *config.Config, client *http.Client) (backend.Backend, error) { + return &fakeThriftBackend{openSessionErr: dbsqlerrint.NewReydenThriftUnsupportedError( + "Lakehouse/RT is not supported for Thrift protocol")}, nil + }, + func(ctx context.Context, cfg *config.Config) (backend.Backend, error) { + require.NoError(t, validateKernelHostPort(cfg.Host, cfg.Port)) + kernelHost = buildKernelConfig(cfg, kernel.Auth{Mode: kernel.AuthPAT, Token: "test-token"}).Host + return &fakeKernelBackend{}, nil + }, + ) + WithPort(8443)(conn.cfg) + + _, _, err := conn.openSessionWithReydenFallback(context.Background()) + require.NoError(t, err) + assert.Equal(t, "reyden.example.com:8443", kernelHost) + }) } func TestReydenPreCheck(t *testing.T) { diff --git a/internal/config/thrift_endpoint_test.go b/internal/config/thrift_endpoint_test.go new file mode 100644 index 00000000..efdcf13b --- /dev/null +++ b/internal/config/thrift_endpoint_test.go @@ -0,0 +1,32 @@ +package config + +import "testing" + +// TestThriftEndpointUnchanged pins the Thrift endpoint for the port inputs the kernel custom-port +// change decides on. Thrift is not changed by that work, so these strings are what it builds today. +// An embedded host port plus the default 443 yields an unparseable URL on Thrift; that is the baseline. +func TestThriftEndpointUnchanged(t *testing.T) { + cases := []struct { + name string + host string + port int + want string + }{ + {"bare host, default port", "h.io", 443, "https://h.io:443/sql/1.0/warehouses/x"}, + {"bare host, WithPort", "h.io", 8443, "https://h.io:8443/sql/1.0/warehouses/x"}, + {"embedded port, default port", "h.io:8443", 443, "https://h.io:8443:443/sql/1.0/warehouses/x"}, + {"embedded port, WithPort", "h.io:8443", 8443, "https://h.io:8443:8443/sql/1.0/warehouses/x"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + c := &Config{Protocol: "https", Host: tc.host, Port: tc.port, HTTPPath: "/sql/1.0/warehouses/x"} + got, err := c.ToEndpointURL() + if err != nil { + t.Fatalf("unexpected err %v", err) + } + if got != tc.want { + t.Errorf("ToEndpointURL() = %q, want %q", got, tc.want) + } + }) + } +} diff --git a/kernel_config.go b/kernel_config.go index 21a9f3cb..b1c04f0d 100644 --- a/kernel_config.go +++ b/kernel_config.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net" "net/url" "strconv" "strings" @@ -15,6 +16,7 @@ import ( "github.com/databricks/databricks-sql-go/internal/backend/kernel" "github.com/databricks/databricks-sql-go/internal/client" "github.com/databricks/databricks-sql-go/internal/config" + "github.com/databricks/databricks-sql-go/logger" "github.com/databricks/databricks-sql-go/telemetry" ) @@ -73,18 +75,15 @@ func validateKernelConfigContext(ctx context.Context, cfg *config.Config) (kerne // folds its server conf (spark.sql.thriftserver.metadata.metricview.enabled=true) // into SessionConf backend-neutrally, so the kernel path sends the identical conf // the Thrift path does. No per-backend handling needed here. - // Port / Protocol: the kernel C ABI takes only a bare host and connects over - // https:443; it has no port or scheme setter. The Thrift path honors a custom - // port/scheme via ToEndpointURL, so a non-default value here would be silently - // ignored on the kernel path (it would just hit 443) — reject it instead, per - // the "nothing silently ignored" contract. Defaults (https/443) are fine. + // Protocol: the kernel always connects over https, so a non-https value would be ignored. if cfg.Protocol != "" && cfg.Protocol != "https" { return kernel.Auth{}, fmt.Errorf("databricks: a non-https protocol is %w "+ "(it connects over https); use the default (Thrift) backend", dbsqlerr.ErrNotSupportedByKernel) } - if cfg.Port != 0 && cfg.Port != 443 { - return kernel.Auth{}, fmt.Errorf("databricks: a non-default port (WithPort) is %w "+ - "(it connects on 443); omit it or use the default (Thrift) backend", dbsqlerr.ErrNotSupportedByKernel) + // Port: the kernel folds host:port (see kernelHostWithPort); reject a bad port or host here. An + // embedded host port wins over a differing WithPort, with a warning rather than an error. + if err := validateKernelHostPort(cfg.Host, cfg.Port); err != nil { + return kernel.Auth{}, err } // Transport (WithTransport, a custom http.RoundTripper carrying a custom CA // bundle / mTLS / proxy): the kernel uses its own Rust HTTP stack below the C @@ -155,7 +154,7 @@ func validateKernelConfigContext(ctx context.Context, cfg *config.Config) (kerne // with newKernelBackend's kernel.Config assembly. func buildKernelConfig(cfg *config.Config, kauth kernel.Auth) kernel.Config { kc := kernel.Config{ - Host: cfg.Host, + Host: kernelHostWithPort(cfg.Host, cfg.Port), HTTPPath: cfg.HTTPPath, WarehouseID: cfg.WarehouseID, Auth: kauth, @@ -239,6 +238,100 @@ func buildKernelConfig(cfg *config.Config, kauth kernel.Auth) kernel.Config { return kc } +// kernelHost is cfg.Host split by net/url: the parts the kernel port logic needs. +type kernelHost struct { + raw string // cfg.Host with a trailing "/" trimmed; the kernel gets it unchanged when no port is folded + name string // host name without IPv6 brackets (url.URL.Hostname) + port string // digits after the authority's ":"; "" when there is none + hasPort bool // the authority has a ":", even with no digits after it ("h:") + hasPath bool // a path, query or fragment follows the host, or a bare "#" (url.URL drops an empty fragment) + userinfo bool // a user@ prefix is present + password bool // the user@ prefix carries a password +} + +// parseKernelHost parses cfg.Host (trailing "/" trimmed) as the authority of an https URL, which is the +// form the kernel connects to. The error is url.Parse's; the fields are set only when it is nil. +func parseKernelHost(host string) (kernelHost, error) { + h := kernelHost{raw: strings.TrimRight(host, "/")} + u, err := url.Parse("https://" + h.raw) + if err != nil { + return h, err + } + h.name = u.Hostname() + h.port = u.Port() + h.hasPort = h.port != "" || strings.HasSuffix(u.Host, ":") + h.hasPath = u.Path != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || strings.Contains(h.raw, "#") + h.userinfo = u.User != nil + _, h.password = u.User.Password() + return h, nil +} + +// validateKernelHostPort rejects an out-of-range WithPort, a host that net/url cannot parse (an IPv6 +// literal must be bracketed), and a non-default WithPort with a host that has a path, query, fragment or +// credentials. It also rejects unbalanced brackets and an empty, out-of-range or malformed port embedded +// in the host. WithDefaults fills 443 when no port is set. A port embedded in the host wins over a +// differing WithPort, with a warning rather than an error. +func validateKernelHostPort(host string, port int) error { + if port < 0 || port > 65535 { + return fmt.Errorf("databricks: WithPort %d is out of range (want 1..65535, or 0 for the default): %w", port, dbsqlerr.ErrInvalidKernelConfig) + } + h, err := parseKernelHost(host) + if err != nil { + return fmt.Errorf("databricks: host %q is not a valid host (bracket an IPv6 literal): %v: %w", h.raw, err, dbsqlerr.ErrInvalidKernelConfig) + } + nonDefault := port != 0 && port != 443 + // A path, query or fragment is checked before the embedded-port parse. With the default port the + // host goes to the kernel unchanged, as before (the kernel appends its API paths after it). A + // non-default port cannot be folded into such a host, so reject it. + if h.hasPath { + if nonDefault { + return fmt.Errorf("databricks: host %q has a path, query or fragment, so WithPort %d cannot be applied: %w", + h.raw, port, dbsqlerr.ErrInvalidKernelConfig) + } + return nil + } + // Credentials are kept only at the default port: a non-default port would have to be folded in without them. + if h.userinfo && nonDefault { + return fmt.Errorf("databricks: host %q carries credentials, so WithPort %d cannot be applied: %w", + h.raw, port, dbsqlerr.ErrInvalidKernelConfig) + } + if h.password { + return fmt.Errorf("databricks: host %q carries a password: %w", h.raw, dbsqlerr.ErrInvalidKernelConfig) + } + // url.Parse accepts a stray "]" in a plain name, so brackets are checked on the parsed host name + // (IPv6 brackets are already removed from it). + if strings.ContainsAny(h.name, "[]") { + return fmt.Errorf("databricks: host %q has unbalanced IPv6 brackets: %w", h.raw, dbsqlerr.ErrInvalidKernelConfig) + } + if !h.hasPort { + return nil // no embedded port + } + embedded, err := strconv.Atoi(h.port) + if err != nil || embedded < 1 || embedded > 65535 { + return fmt.Errorf("databricks: host %q carries an invalid port %q (want 1..65535): %w", h.raw, h.port, dbsqlerr.ErrInvalidKernelConfig) + } + // The embedded port is used, as on the kernel path before WithPort was honored. An explicit 443 is + // the default and is not warned about. + if nonDefault && embedded != port { + logger.Warn().Msgf("databricks: the host carries port %d, which differs from WithPort %d; connecting to port %d from the host", + embedded, port, embedded) + } + return nil +} + +// kernelHostWithPort returns the Host the kernel connects to. The kernel has no port setter but keeps a +// `host:port` authority, so a non-default port is folded in with net.JoinHostPort, which brackets an IPv6 +// literal. A trailing "/" is trimmed first. 0 and 443 add nothing. A host that already carries a port, a +// path, query or fragment, or credentials is returned unchanged; validateKernelHostPort rejects the last +// two with a non-default port before the kernel is built. +func kernelHostWithPort(host string, port int) string { + h, err := parseKernelHost(host) + if err != nil || port == 0 || port == 443 || h.hasPort || h.hasPath || h.userinfo { + return h.raw + } + return net.JoinHostPort(h.name, strconv.Itoa(port)) +} + func kernelTelemetryConfig(cfg *config.Config) *kernel.TelemetryConfig { var enabled *bool if val, isSet := cfg.EnableTelemetry.Get(); isSet { diff --git a/kernel_config_test.go b/kernel_config_test.go index d8cd3c0c..75f1a3e4 100644 --- a/kernel_config_test.go +++ b/kernel_config_test.go @@ -121,7 +121,6 @@ func TestValidateKernelConfig(t *testing.T) { }{ {"query timeout", func(c *config.Config) { c.QueryTimeout = 30 * time.Second }}, {"non-https protocol", func(c *config.Config) { c.Protocol = "http" }}, - {"non-default port", func(c *config.Config) { c.Port = 8443 }}, {"custom transport", func(c *config.Config) { c.Transport = http.DefaultTransport }}, } for _, tc := range rejections { @@ -443,10 +442,10 @@ var kernelConfigFieldDisposition = map[string]string{ "Schema": "forwarded", // Rejected loudly by validateKernelConfig. - "QueryTimeout": "rejected", // when > 0 (WithTimeout) - "Protocol": "rejected", // kernel is https-only; non-default rejected - "Port": "rejected", // kernel connects on 443; non-default rejected - "Transport": "rejected", // custom RoundTripper; kernel uses its own HTTP stack, so reject rather than drop + "QueryTimeout": "rejected", // when > 0 (WithTimeout) + "Protocol": "rejected", // kernel is https-only; non-default rejected + "Port": "forwarded", // folded into the kernel Host by kernelHostWithPort; validated by validateKernelHostPort + "Transport": "rejected", // custom RoundTripper; kernel uses its own HTTP stack, so reject rather than drop // Forwarded to the kernel's HTTP retry config via kernelRetryConfig → // set_retry_config (WithRetries: backoff bounds + max attempts, incl. disable). @@ -972,3 +971,151 @@ func TestKernelRetryConfig(t *testing.T) { } }) } + +// TestKernelCustomPort covers WithPort on the kernel backend: a non-default port is folded into the +// kernel Host, 0 and 443 add nothing, a port embedded in the host wins, a trailing "/" is trimmed, +// and a host with a path passes through unchanged unless a non-default port would be folded into it. +// Host parsing uses net/url. An out-of-range port, unbalanced IPv6 brackets, or a bare (unbracketed) +// IPv6 literal are rejected with ErrInvalidKernelConfig, so an IPv6 literal must be bracketed. +func TestKernelCustomPort(t *testing.T) { + cases := []struct { + name string + host string + port int + wantHost string // kernelHostWithPort result for an accepted pair + wantErr bool + }{ + {"default 443 adds nothing", "h.databricks.com", 443, "h.databricks.com", false}, + {"unset port adds nothing", "h.databricks.com", 0, "h.databricks.com", false}, + {"custom port", "h.databricks.com", 8443, "h.databricks.com:8443", false}, + {"IPv4 literal", "127.0.0.1", 8443, "127.0.0.1:8443", false}, + {"bare IPv6 is rejected: an IPv6 literal must be bracketed", "::1", 8443, "", true}, + {"bracketed IPv6", "[::1]", 8443, "[::1]:8443", false}, + {"port 1", "h.databricks.com", 1, "h.databricks.com:1", false}, + {"port 65535", "h.databricks.com", 65535, "h.databricks.com:65535", false}, + {"embedded port equal", "h.databricks.com:8443", 8443, "h.databricks.com:8443", false}, + {"embedded port equal, IPv6", "[::1]:8443", 8443, "[::1]:8443", false}, + {"443 with embedded 443", "h.databricks.com:443", 443, "h.databricks.com:443", false}, + {"443 with embedded port: embedded wins", "h.databricks.com:8443", 443, "h.databricks.com:8443", false}, + {"embedded port differs: embedded wins", "h.databricks.com:7000", 8443, "h.databricks.com:7000", false}, + {"embedded port differs, IPv6: embedded wins", "[::1]:7000", 8443, "[::1]:7000", false}, + {"trailing slash on bare host", "h.databricks.com/", 8443, "h.databricks.com:8443", false}, + {"trailing slash on bare host, default port", "h.databricks.com/", 443, "h.databricks.com", false}, + {"trailing slash with embedded port, default port", "h.databricks.com:8443/", 443, "h.databricks.com:8443", false}, + {"trailing slash with embedded port, equal port", "h.databricks.com:8443/", 8443, "h.databricks.com:8443", false}, + {"trailing slash, IPv6", "[::1]/", 8443, "[::1]:8443", false}, + {"path, default port: unchanged", "h.databricks.com/x", 443, "h.databricks.com/x", false}, + {"path, unset port: unchanged", "h.databricks.com/x", 0, "h.databricks.com/x", false}, + {"path with colon, default port: unchanged", "h.io/path:8443", 443, "h.io/path:8443", false}, + {"path with custom port", "h.databricks.com/x", 8443, "", true}, + {"path with colon and custom port", "h.io/path:8443", 8443, "", true}, + {"query with colon and custom port", "h.io?q=1:8443", 8443, "", true}, + {"query with custom port", "h.databricks.com?x", 8443, "", true}, + {"fragment with custom port", "h.databricks.com#x", 8443, "", true}, + {"negative port", "h.databricks.com", -1, "", true}, + {"port above range", "h.databricks.com", 65536, "", true}, + {"embedded port not a number", "h.databricks.com:notaport", 8443, "", true}, + {"embedded port out of range", "h.databricks.com:70000", 0, "", true}, + {"embedded port zero", "h.databricks.com:0", 0, "", true}, + {"unclosed IPv6 bracket", "[::1", 8443, "", true}, + {"text after IPv6 bracket", "[::1]x", 8443, "", true}, + {"stray closing bracket", "h.databricks.com]", 8443, "", true}, + {"stray closing bracket, default port", "h.databricks.com]", 443, "", true}, + {"text after IPv6 bracket, default port", "[::1]x", 443, "", true}, + {"unclosed IPv6 bracket, default port", "[::1", 443, "", true}, + // net/url rules: an empty port, credentials, an empty fragment and bare IPv6 literals. + {"empty port after colon", "h:", 8443, "", true}, + {"empty port after colon, default port", "h:", 443, "", true}, + {"bare IPv6, default port: rejected (bracket it)", "::1", 443, "", true}, + {"bare IPv6 with zone is rejected", "fe80::1%eth0", 8443, "", true}, + {"userinfo, default port: unchanged", "user@h", 443, "user@h", false}, + {"userinfo, custom port: rejected", "user@h", 8443, "", true}, + {"userinfo with an embedded port, custom port: rejected", "user@h:8443", 9443, "", true}, + {"userinfo with an embedded port, default port: unchanged", "user@h:8443", 443, "user@h:8443", false}, + {"password, default port: rejected", "user:pw@h", 443, "", true}, + {"password, custom port: rejected", "user:pw@h", 8443, "", true}, + {"empty fragment, custom port: rejected (h#)", "h#", 8443, "", true}, + {"empty fragment, default port: unchanged (h#)", "h#", 443, "h#", false}, + {"embedded port out of range (h:70000)", "h:70000", 8443, "", true}, + {"embedded port not a number (h:abc)", "h:abc", 8443, "", true}, + {"mixed-case host is kept (custom port)", "H.Example.COM", 8443, "H.Example.COM:8443", false}, + {"mixed-case host is kept (default port)", "H.Example.COM", 443, "H.Example.COM", false}, + {"trailing slash after embedded port", "h:8443/", 8443, "h:8443", false}, + {"path with custom port (h/x)", "h/x", 8443, "", true}, + {"path, default port: unchanged (h/x)", "h/x", 443, "h/x", false}, + {"query with custom port (h?q)", "h?q", 8443, "", true}, + {"query, default port: unchanged (h?q)", "h?q", 443, "h?q", false}, + {"fragment with custom port (h#f)", "h#f", 8443, "", true}, + {"fragment, default port: unchanged (h#f)", "h#f", 443, "h#f", false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := validateKernelHostPort(tc.host, tc.port) + if tc.wantErr { + if !errors.Is(err, dbsqlerr.ErrInvalidKernelConfig) { + t.Errorf("err = %v, want ErrInvalidKernelConfig", err) + } + return + } + if err != nil { + t.Errorf("unexpected err %v", err) + } + if got := kernelHostWithPort(tc.host, tc.port); got != tc.wantHost { + t.Errorf("kernelHostWithPort(%q, %d) = %q, want %q", tc.host, tc.port, got, tc.wantHost) + } + }) + } + + t.Run("explicit WithPort(443) differing from an embedded port connects to the embedded port", func(t *testing.T) { + c := baseKernelConfig() + c.Host = "h.databricks.com:8443" + WithPort(443)(c) + kauth, err := validateKernelConfig(c) + if err != nil { + t.Fatalf("unexpected err %v", err) + } + if got := buildKernelConfig(c, kauth).Host; got != "h.databricks.com:8443" { + t.Errorf("kernel Host = %q, want the embedded port h.databricks.com:8443", got) + } + }) + + t.Run("WithServerHostname with a trailing slash plus WithPort folds to host:port", func(t *testing.T) { + c := baseKernelConfig() + WithServerHostname("https://h.databricks.com/")(c) + WithPort(8443)(c) + kauth, err := validateKernelConfig(c) + if err != nil { + t.Fatalf("unexpected err %v", err) + } + if got := buildKernelConfig(c, kauth).Host; got != "h.databricks.com:8443" { + t.Errorf("kernel Host = %q, want h.databricks.com:8443", got) + } + }) + + t.Run("WithServerHostname with an embedded port and a trailing slash is still accepted", func(t *testing.T) { + c := baseKernelConfig() + WithServerHostname("https://h.databricks.com:8443/")(c) + kauth, err := validateKernelConfig(c) + if err != nil { + t.Fatalf("unexpected err %v", err) + } + if got := buildKernelConfig(c, kauth).Host; got != "h.databricks.com:8443" { + t.Errorf("kernel Host = %q, want h.databricks.com:8443", got) + } + }) + + t.Run("kernel Host: unset port keeps the embedded port, custom port folds in", func(t *testing.T) { + c := baseKernelConfig() + c.Host = "h.databricks.com:8443" + kauth, err := validateKernelConfig(c) + if err != nil || buildKernelConfig(c, kauth).Host != "h.databricks.com:8443" { + t.Errorf("embedded port: err=%v host=%q", err, buildKernelConfig(c, kauth).Host) + } + c = baseKernelConfig() + WithPort(8443)(c) + kauth, err = validateKernelConfig(c) + if err != nil || buildKernelConfig(c, kauth).Host != "h.databricks.com:8443" { + t.Errorf("WithPort(8443): err=%v host=%q", err, buildKernelConfig(c, kauth).Host) + } + }) +} diff --git a/kernel_proxy.go b/kernel_proxy.go index 9975e0ab..1c37b445 100644 --- a/kernel_proxy.go +++ b/kernel_proxy.go @@ -43,10 +43,9 @@ func proxyForEndpointFunc(cfg *config.Config, resolve func(*http.Request) (*url. if scheme == "" { scheme = "https" } - endpoint := fmt.Sprintf("%s://%s", scheme, cfg.Host) - if cfg.Port != 0 { - endpoint = fmt.Sprintf("%s://%s:%d", scheme, cfg.Host, cfg.Port) - } + // Same authority the kernel connects to: an embedded port is not appended twice + // and an IPv6 literal is bracketed (kernelHostWithPort). + endpoint := fmt.Sprintf("%s://%s", scheme, kernelHostWithPort(cfg.Host, cfg.Port)) req, err := http.NewRequest(http.MethodPost, endpoint, nil) if err != nil { return "" diff --git a/kernel_proxy_test.go b/kernel_proxy_test.go index 3bfed871..4890c742 100644 --- a/kernel_proxy_test.go +++ b/kernel_proxy_test.go @@ -55,6 +55,37 @@ func TestProxyForEndpoint(t *testing.T) { resolve: func(*http.Request) (*url.URL, error) { return proxyURL, nil }, want: "http://corp-proxy:3128", }, + { + name: "embedded port equal to Port is not doubled", + cfg: func() *config.Config { + c := config.WithDefaults() + c.Host = "my-workspace.databricks.com:8443" + c.Port = 8443 + c.HTTPPath = "/sql/1.0/warehouses/abc" + return c + }(), + resolve: func(*http.Request) (*url.URL, error) { return proxyURL, nil }, + want: "http://corp-proxy:3128", + }, + { + // A bracketed IPv6 literal with a port is folded into the endpoint as [addr]:port, + // the same way the kernel Host is built. + name: "bracketed IPv6 literal with Port is folded", + cfg: func() *config.Config { + c := config.WithDefaults() + c.Host = "[::1]" + c.Port = 8443 + c.HTTPPath = "/sql/1.0/warehouses/abc" + return c + }(), + resolve: func(req *http.Request) (*url.URL, error) { + if req.URL.Host != "[::1]:8443" { + t.Errorf("endpoint host = %q, want %q", req.URL.Host, "[::1]:8443") + } + return proxyURL, nil + }, + want: "http://corp-proxy:3128", + }, { name: "host excluded by NO_PROXY -> direct", cfg: validCfg(), @@ -80,6 +111,25 @@ func TestProxyForEndpoint(t *testing.T) { }, want: "", }, + { + // A trailing "/" (WithServerHostname("https://h/")) is trimmed before the port is + // folded, so the endpoint is h:8443, not h/:8443. + name: "trailing slash host with Port folds to host:port", + cfg: func() *config.Config { + c := config.WithDefaults() + c.Host = "my-workspace.databricks.com/" + c.Port = 8443 + c.HTTPPath = "/sql/1.0/warehouses/abc" + return c + }(), + resolve: func(req *http.Request) (*url.URL, error) { + if req.URL.Host != "my-workspace.databricks.com:8443" { + t.Errorf("endpoint host = %q, want %q", req.URL.Host, "my-workspace.databricks.com:8443") + } + return proxyURL, nil + }, + want: "http://corp-proxy:3128", + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) {