diff --git a/CHANGELOG.md b/CHANGELOG.md index d5024d97c..347778362 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Fix OAuth M2M token requests hanging indefinitely when the token endpoint stops responding. Each request now uses a copy of `http.DefaultClient` with a timeout of at most 60s; `WithRetries` repeats failed fetches (databricks/databricks-sql-go#475). - 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). +- Kernel HTTPS traffic negotiates HTTP/2 with HTTP/1.1 fallback. Set `WithEnableHTTP2(false)` or DSN `enableHttp2=false` to force HTTP/1.1. Configured proxies also force HTTP/1.1. - Fix the module-root package conflict when building, linting, or running Gazelle with the `tools` build tag (databricks/databricks-sql-go#162). - **Behavior change:** Report declared DECIMAL precision and scale through `sql.ColumnType.DecimalSize()` on Thrift and SEA, including zero-row results. Tools such as ORM migrators can now act on these qualifiers (databricks/databricks-sql-go#500). - **Behavior change:** Support Arrow TIME results on Thrift and SEA as UTC `time.Time` values on `0001-01-01`, preserving precision and using clock-only nested JSON. TIME metadata now reports `TIME`/`time.Time` with no length, including empty and NULL-only results; previously Thrift reported `STRING`/`string` with unbounded length, and SEA reported an empty name with `*interface{}` (databricks/databricks-sql-go#505). diff --git a/CONNECTION_PARAMETERS.md b/CONNECTION_PARAMETERS.md index 28624b378..cd071efd9 100644 --- a/CONNECTION_PARAMETERS.md +++ b/CONNECTION_PARAMETERS.md @@ -46,6 +46,7 @@ wire. They are listed under [Kernel client-result confs](#kernel-client-result-c | `catalog` | `WithInitialNamespace` | ✅ | ✅ | | Initial catalog. Kernel applies it post-connect via `USE CATALOG`. | | `schema` | `WithInitialNamespace` | ✅ | ✅ | | Initial schema. Kernel applies it post-connect via `USE SCHEMA`. | | `useKernel` | `WithUseKernel` | ✅ | ✅ | `false` | Select the SEA/kernel backend. Requires a `databricks_kernel` build. | +| `enableHttp2` | `WithEnableHTTP2(b)` | ❌ | ✅ | `true` | Negotiate HTTP/2 for session HTTPS traffic, with HTTP/1.1 fallback. `false` or an explicit/environment proxy forces HTTP/1.1, including bypassed hosts. | ## Authentication diff --git a/README.md b/README.md index 77781de31..ff85c86e3 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,7 @@ session parameter on both backends. | `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`. | | `useKernel` | `WithUseKernel` | Both | `false` | Select the SEA/kernel backend. Requires a `databricks_kernel` build. | +| `enableHttp2` | `WithEnableHTTP2(b)` | SEA only | `true` | Negotiate HTTP/2 for session HTTPS traffic, with HTTP/1.1 fallback. `false` or a configured proxy forces HTTP/1.1, including bypassed hosts. | ### Query execution diff --git a/connector.go b/connector.go index 1b72c6190..3ab987f89 100644 --- a/connector.go +++ b/connector.go @@ -326,6 +326,10 @@ func withUserConfig(ucfg config.UserConfig) ConnOption { enabled := *ucfg.EnableGeoSpatialSupportDSN kernelExperimental(c).EnableGeoSpatialSupport = &enabled } + if ucfg.EnableHTTP2DSN != nil { + enabled := *ucfg.EnableHTTP2DSN + kernelExperimental(c).EnableHTTP2 = &enabled + } } } @@ -711,6 +715,17 @@ func WithEnableGeoSpatialSupport(enabled bool) ConnOption { } } +// WithEnableHTTP2 controls HTTPS protocol negotiation on the kernel backend. +// Defaults to HTTP/2 with HTTP/1.1 fallback. False or a configured proxy forces +// HTTP/1.1 for session traffic, including destinations that bypass the proxy. +// The Thrift backend rejects this option. DSN: enableHttp2=false. +func WithEnableHTTP2(enabled bool) ConnOption { + return func(c *config.Config) { + value := enabled + kernelExperimental(c).EnableHTTP2 = &value + } +} + // WithKernelTrustedCerts adds a PEM CA-certificate bundle to the kernel's TLS // trust store on top of the system roots — for a corporate re-signing proxy or an // on-prem CA. Required (rather than relying on SSL_CERT_FILE) because the kernel's diff --git a/connector_test.go b/connector_test.go index 684a8646f..62794b02c 100644 --- a/connector_test.go +++ b/connector_test.go @@ -464,6 +464,19 @@ func TestNewConnector(t *testing.T) { assert.False(t, *coni.cfg.KernelExperimental.EnableGeoSpatialSupport) }) + t.Run("Connector test enableHttp2=false DSN param preserves explicit false", func(t *testing.T) { + ucfg, err := config.ParseDSN("token:supersecret@databricks-host:443/sql/1.0/endpoints/abc?enableHttp2=false") + require.NoError(t, err) + con, err := NewConnector(withUserConfig(ucfg)) + require.NoError(t, err) + + coni, ok := con.(*connector) + require.True(t, ok) + got := buildKernelConfig(coni.cfg, kernel.Auth{}).EnableHTTP2 + require.NotNil(t, got) + assert.False(t, *got) + }) + t.Run("Connector test WithTransport sets HTTPClient in CloudFetchConfig", func(t *testing.T) { host := "databricks-host" accessToken := "token" diff --git a/internal/backend/kernel/backend.go b/internal/backend/kernel/backend.go index d8e20d08e..529750f63 100644 --- a/internal/backend/kernel/backend.go +++ b/internal/backend/kernel/backend.go @@ -267,6 +267,9 @@ func (k *KernelBackend) OpenSession(ctx context.Context) error { if err := k.applyEnableGeoSpatialSupport(cfg); err != nil { return err } + if err := k.applyEnableHTTP2(cfg); err != nil { + return err + } // Retry / backoff policy (WithRetries). See applyRetry. if err := k.applyRetry(cfg); err != nil { @@ -468,6 +471,18 @@ func (k *KernelBackend) applyEnableGeoSpatialSupport(cfg *C.KernelSessionConfig) return nil } +func (k *KernelBackend) applyEnableHTTP2(cfg *C.KernelSessionConfig) error { + if k.cfg.EnableHTTP2 == nil { + return nil + } + if err := call(func() C.KernelStatusCode { + return C.kernel_session_config_set_http2_enabled(cfg, C.bool(*k.cfg.EnableHTTP2)) + }); err != nil { + return fmt.Errorf("kernel: set_http2_enabled: %w", toConnError(err)) + } + return nil +} + // applyRetry forwards the driver's HTTP retry / backoff policy to the session // config. A no-op when Config.Retry is nil, so the kernel's own default policy // (exponential backoff with jitter, 5 retries, 1s..60s, 900s budget) is preserved diff --git a/internal/backend/kernel/config.go b/internal/backend/kernel/config.go index c3f97ed83..f48add431 100644 --- a/internal/backend/kernel/config.go +++ b/internal/backend/kernel/config.go @@ -108,6 +108,9 @@ type Config struct { // sets this field and defaults it to false. EnableGeoSpatialSupport bool + // EnableHTTP2 leaves the kernel default in effect when nil; false forces HTTP/1.1. + EnableHTTP2 *bool + // TokenCacheEnabled controls the kernel's on-disk OAuth U2M token-cache persistence // (WithTokenCache / tokenCache DSN param). When false (the default), tokens are held // in memory only; when true, the refresh token is persisted encrypted to diff --git a/internal/backend/kernel/include/databricks_kernel.h b/internal/backend/kernel/include/databricks_kernel.h index 965c09b6b..a3d47c280 100644 --- a/internal/backend/kernel/include/databricks_kernel.h +++ b/internal/backend/kernel/include/databricks_kernel.h @@ -366,6 +366,11 @@ KernelStatusCode kernel_session_config_set_custom_header(KernelSessionConfig* co KernelStatusCode kernel_session_config_set_sea_response_compression_enabled( KernelSessionConfig* config, bool enabled); +/* Negotiate HTTP/2 for session HTTPS requests, with HTTP/1.1 fallback. + * Enabled by default. False or a configured proxy forces HTTP/1.1. */ +KernelStatusCode kernel_session_config_set_http2_enabled( + KernelSessionConfig* config, bool enabled); + /* Configure the total timeout for one HTTP request, from connection start * through response-body completion. A separate 30s connection timeout also * applies, so connection establishment is bounded by the shorter limit. diff --git a/internal/config/config.go b/internal/config/config.go index f9c5143e2..5ccec964e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -144,6 +144,8 @@ type KernelExperimentalConfig struct { // struct Arrow value; false requests WKT / EWKT strings. A pointer // distinguishes the omitted value (default false) from an explicit setting. EnableGeoSpatialSupport *bool + // EnableHTTP2 is nil when the kernel owns the HTTPS protocol default. + EnableHTTP2 *bool } // DeepCopy returns a deep copy of the experimental config, or nil for a nil @@ -169,6 +171,10 @@ func (k *KernelExperimentalConfig) DeepCopy() *KernelExperimentalConfig { enabled := *k.EnableGeoSpatialSupport cp.EnableGeoSpatialSupport = &enabled } + if k.EnableHTTP2 != nil { + enabled := *k.EnableHTTP2 + cp.EnableHTTP2 = &enabled + } if k.MaxConnections != nil { max := *k.MaxConnections cp.MaxConnections = &max @@ -317,6 +323,8 @@ type UserConfig struct { // enableGeoSpatialSupport=true|false. Nil means omitted (the default is // false); the connector copies a present value into KernelExperimental. EnableGeoSpatialSupportDSN *bool + // EnableHTTP2DSN carries an explicit enableHttp2 DSN setting to the connector. + EnableHTTP2DSN *bool } // DeepCopy returns a true deep copy of UserConfig @@ -371,6 +379,10 @@ func (ucfg UserConfig) DeepCopy() UserConfig { enabled := *ucfg.EnableGeoSpatialSupportDSN result.EnableGeoSpatialSupportDSN = &enabled } + if ucfg.EnableHTTP2DSN != nil { + enabled := *ucfg.EnableHTTP2DSN + result.EnableHTTP2DSN = &enabled + } return result } @@ -556,6 +568,13 @@ func ParseDSN(dsn string) (UserConfig, error) { ucfg.EnableGeoSpatialSupportDSN = &enableGeoSpatialSupport } + if enabled, ok, err := params.extractAsBool("enableHttp2"); ok { + if err != nil { + return UserConfig{}, err + } + ucfg.EnableHTTP2DSN = &enabled + } + // Telemetry parameters if enableTelemetry, ok, err := params.extractAsBool("enableTelemetry"); ok { if err != nil { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 518ccaf29..feee451d1 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -730,6 +730,39 @@ func TestParseDSNEnableGeoSpatialSupport(t *testing.T) { } } +func TestParseDSNEnableHTTP2(t *testing.T) { + base := "token:supersecret@example.cloud.databricks.com:443/sql/1.0/endpoints/abc" + for _, tc := range []struct { + name string + query string + want *bool + }{ + {name: "omitted", query: "", want: nil}, + {name: "enabled", query: "?enableHttp2=true", want: func() *bool { v := true; return &v }()}, + {name: "disabled", query: "?enableHttp2=false", want: func() *bool { v := false; return &v }()}, + } { + t.Run(tc.name, func(t *testing.T) { + got, err := ParseDSN(base + tc.query) + if err != nil { + t.Fatal(err) + } + if tc.want == nil { + if got.EnableHTTP2DSN != nil { + t.Errorf("EnableHTTP2DSN = %v, want nil", *got.EnableHTTP2DSN) + } + return + } + if got.EnableHTTP2DSN == nil || *got.EnableHTTP2DSN != *tc.want { + t.Errorf("EnableHTTP2DSN = %v, want %v", got.EnableHTTP2DSN, *tc.want) + } + }) + } + + if _, err := ParseDSN(base + "?enableHttp2=invalid"); err == nil { + t.Error("malformed enableHttp2 should fail DSN parsing") + } +} + func TestParseDSNQueryTimeoutRange(t *testing.T) { if strconv.IntSize < 64 { t.Skip("strconv.Atoi rejects these values before the range check on 32-bit platforms") diff --git a/kernel_config.go b/kernel_config.go index 21a9f3cb1..9c3c1c22e 100644 --- a/kernel_config.go +++ b/kernel_config.go @@ -230,6 +230,7 @@ func buildKernelConfig(cfg *config.Config, kauth kernel.Auth) kernel.Config { if ke.EnableGeoSpatialSupport != nil { kc.EnableGeoSpatialSupport = *ke.EnableGeoSpatialSupport } + kc.EnableHTTP2 = ke.EnableHTTP2 // U2M token-cache control (WithTokenCache / tokenCache DSN param): forward to kernel. kc.TokenCacheEnabled = ke.TokenCacheEnabled } diff --git a/kernel_config_test.go b/kernel_config_test.go index d8cd3c0c4..ac3bfe703 100644 --- a/kernel_config_test.go +++ b/kernel_config_test.go @@ -472,6 +472,7 @@ var kernelConfigFieldDisposition = map[string]string{ "UseArrowNativeDecimalDSN": "inert", // DSN carrier; kernel renders decimals exactly regardless "TokenCacheEnabledDSN": "inert", // DSN carrier; forwarded to KernelExperimental.TokenCacheEnabled "EnableGeoSpatialSupportDSN": "inert", // DSN carrier; forwarded to KernelExperimental.EnableGeoSpatialSupport + "EnableHTTP2DSN": "inert", // DSN carrier; forwarded to KernelExperimental.EnableHTTP2 // Fields promoted from the embedded CloudFetchConfig. The kernel does // CloudFetch internally (below the C ABI); each option is diff --git a/kernel_experimental_test.go b/kernel_experimental_test.go index a17647f13..435f6e4f0 100644 --- a/kernel_experimental_test.go +++ b/kernel_experimental_test.go @@ -27,6 +27,7 @@ import ( // deliberate decision and a setter in KernelBackend.OpenSession so it can't be // silently dropped. var kernelExperimentalFieldDisposition = map[string]string{ + "EnableHTTP2": "forwarded", // set_http2_enabled "TLSTrustedCertsPEM": "forwarded", // set_tls_trusted_certs "TLSClientCertPEM": "forwarded", // set_tls_client_certificate (cert) "TLSClientKeyPEM": "forwarded", // set_tls_client_certificate (key) @@ -104,6 +105,9 @@ func TestWithKernelTLSOptionsSetExperimental(t *testing.T) { {"disable geospatial support", WithEnableGeoSpatialSupport(false), func(k *config.KernelExperimentalConfig) bool { return k.EnableGeoSpatialSupport != nil && !*k.EnableGeoSpatialSupport }}, + {"force HTTP/1.1", WithEnableHTTP2(false), func(k *config.KernelExperimentalConfig) bool { + return k.EnableHTTP2 != nil && !*k.EnableHTTP2 + }}, {"token cache", WithTokenCache(true), func(k *config.KernelExperimentalConfig) bool { return k.TokenCacheEnabled }}, @@ -143,6 +147,7 @@ func TestWithKernelOptionsRejectedOnThriftPath(t *testing.T) { {"max chunks in memory", WithKernelMaxChunksInMemory(4)}, {"decimal as float", WithKernelDecimalAsFloat(true)}, {"disable geospatial support", WithEnableGeoSpatialSupport(false)}, + {"force HTTP/1.1", WithEnableHTTP2(false)}, {"token cache", WithTokenCache(true)}, } for _, tc := range cases { @@ -180,6 +185,8 @@ func TestWithKernelOptionsRejectedOnThriftPath(t *testing.T) { for _, dsn := range []string{ "token:supersecret@example.cloud.databricks.com:443/sql/1.0/endpoints/12346a5b5b0e123a?enableGeoSpatialSupport=true", "token:supersecret@example.cloud.databricks.com:443/sql/1.0/endpoints/12346a5b5b0e123a?enableGeoSpatialSupport=false", + "token:supersecret@example.cloud.databricks.com:443/sql/1.0/endpoints/12346a5b5b0e123a?enableHttp2=true", + "token:supersecret@example.cloud.databricks.com:443/sql/1.0/endpoints/12346a5b5b0e123a?enableHttp2=false", } { ucfg, err := config.ParseDSN(dsn) if err != nil { @@ -242,6 +249,7 @@ func TestWithKernelClientCertificateCopiesPEM(t *testing.T) { func TestKernelExperimentalDeepCopy(t *testing.T) { maxConnections := 37 enableGeoSpatialSupport := false + enableHTTP2 := false orig := &config.KernelExperimentalConfig{ TLSTrustedCertsPEM: []byte("ca-bundle"), TLSClientCertPEM: []byte("client-cert"), @@ -256,6 +264,7 @@ func TestKernelExperimentalDeepCopy(t *testing.T) { MaxConnections: &maxConnections, MaxChunksInMemory: 4, EnableGeoSpatialSupport: &enableGeoSpatialSupport, + EnableHTTP2: &enableHTTP2, TokenCacheEnabled: true, } cp := orig.DeepCopy() @@ -282,6 +291,13 @@ func TestKernelExperimentalDeepCopy(t *testing.T) { } else if cp.EnableGeoSpatialSupport == orig.EnableGeoSpatialSupport { t.Error("DeepCopy aliased EnableGeoSpatialSupport pointer") } + if cp.EnableHTTP2 == nil || *cp.EnableHTTP2 { + t.Fatalf("DeepCopy lost EnableHTTP2: %v", cp.EnableHTTP2) + } + *cp.EnableHTTP2 = true + if *orig.EnableHTTP2 { + t.Error("DeepCopy aliased EnableHTTP2 pointer") + } if !cp.TokenCacheEnabled { t.Errorf("DeepCopy lost TokenCacheEnabled: %v", cp.TokenCacheEnabled) } diff --git a/kernel_http2_transport_test.go b/kernel_http2_transport_test.go new file mode 100644 index 000000000..db4e7674f --- /dev/null +++ b/kernel_http2_transport_test.go @@ -0,0 +1,74 @@ +//go:build cgo && databricks_kernel + +package dbsql + +import ( + "context" + "encoding/pem" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" +) + +func TestKernelHTTP2Transport(t *testing.T) { + for _, name := range []string{"HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "ALL_PROXY", "all_proxy"} { + if os.Getenv(name) != "" { + t.Skipf("unset %s to test direct HTTP/2 negotiation", name) + } + } + cases := []struct { + name string + options []ConnOption + wantVersion int + }{ + {name: "default", wantVersion: 2}, + {name: "opt_out", options: []ConnOption{WithEnableHTTP2(false)}, wantVersion: 1}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + versions := make(chan int, 4) + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + versions <- r.ProtoMajor + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"session_id":"http2-test"}`)) + })) + server.EnableHTTP2 = true + server.StartTLS() + defer server.Close() + ca := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: server.Certificate().Raw}) + options := []ConnOption{ + WithServerHostname(server.URL), + WithHTTPPath("/sql/1.0/warehouses/test"), + WithAccessToken("test-token"), + WithUseKernel(true), + WithKernelTrustedCerts(ca), + WithRetries(-1, 0, 0), + } + connector, err := NewConnector(append(options, tc.options...)...) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + connection, err := connector.Connect(ctx) + if err != nil { + t.Fatal(err) + } + if err := connection.Close(); err != nil { + t.Fatal(err) + } + for range 2 { + select { + case got := <-versions: + if got != tc.wantVersion { + t.Fatalf("HTTP/%d received, want HTTP/%d", got, tc.wantVersion) + } + case <-ctx.Done(): + t.Fatal(ctx.Err()) + } + } + }) + } +}