diff --git a/CHANGELOG.md b/CHANGELOG.md index c4140be5..c3451fe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Kernel backend: `port` is passed to the kernel as is, so a custom port works over HTTPS as on Thrift. IPv6 hosts need brackets, an invalid port or host fails with a `ParameterError`, and the published `@databricks/databricks-sql-kernel-*` native packages must be bumped to a release that includes databricks/databricks-sql-kernel#505, since older packages ignore `port`. - Release completed Thrift CloudFetch body-reader listeners so idle connections do not retain downloaded result buffers. ## 3.1.0 diff --git a/CONNECTION_PARAMETERS.md b/CONNECTION_PARAMETERS.md index edba6002..1c1f437e 100644 --- a/CONNECTION_PARAMETERS.md +++ b/CONNECTION_PARAMETERS.md @@ -38,15 +38,15 @@ backend (the default), the **SEA / Kernel** backend, or both. ## Connection identity and backend selection -| Option | Type | Thrift | Kernel | Default Value | Note | -| ----------------------- | --------- | :----: | :----: | ------------- | -------------------------------------------------------------------------------------- | -| `host` | `string` | ✅ | ✅ | — (required) | Required on both. | -| `path` | `string` | ✅ | ✅ | — (required) | Kernel also derives `x-databricks-org-id` from a `?o=` query parameter. | -| `port` | `number` | ✅ | ⚠️ | `443` / — | Kernel uses it only with `https: false`; otherwise put a non-default port in `host`. | -| `https` | `boolean` | ✅ | ✅ | `true` | `false` selects plain HTTP; see [Plain HTTP transport](#plain-http-transport). | -| `userAgentEntry` | `string` | ✅ | ✅ | — | Folded into the connector `User-Agent` on both. | -| `useKernel` (internal) | `boolean` | — | — | `false` | Selects the backend: `false`/omitted → Thrift, `true` → Kernel. | -| `clientId` (deprecated) | `string` | ✅ | ✅ | — | Runtime-only alias for `userAgentEntry`. `userAgentEntry` wins when both are supplied. | +| Option | Type | Thrift | Kernel | Default Value | Note | +| ----------------------- | --------- | :----: | :----: | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `host` | `string` | ✅ | ✅ | — (required) | Required on both. | +| `path` | `string` | ✅ | ✅ | — (required) | Kernel also derives `x-databricks-org-id` from a `?o=` query parameter. | +| `port` | `number` | ✅ | ✅ | `443` / — | Passed to the kernel as is, and the kernel folds it into `host`: a port other than the scheme default (443 over HTTPS, 80 with `https: false`) connects to `host:port`, while the default and `0` change nothing. An IPv6 `host` needs brackets (`[::1]`). A non-default port already in `host` wins over `port`, and a default port in `host` counts as none. The kernel rejects an invalid `port` (not an integer in `0..65535`) or `host` (credentials, a path, a query or a fragment) with a `ParameterError` when the session opens. Error messages never include the host. With OAuth, the kernel uses the same `host:port` for its OAuth endpoints; Thrift keeps OAuth on the bare `host` (port 443). | +| `https` | `boolean` | ✅ | ✅ | `true` | `false` selects plain HTTP; see [Plain HTTP transport](#plain-http-transport). | +| `userAgentEntry` | `string` | ✅ | ✅ | — | Folded into the connector `User-Agent` on both. | +| `useKernel` (internal) | `boolean` | — | — | `false` | Selects the backend: `false`/omitted → Thrift, `true` → Kernel. | +| `clientId` (deprecated) | `string` | ✅ | ✅ | — | Runtime-only alias for `userAgentEntry`. `userAgentEntry` wins when both are supplied. | Set `client.getConfig().useCloudFetch = false` (default `true`) before opening a session to fetch complete results without cloud-storage downloads. On the kernel backend this diff --git a/KERNEL_REV b/KERNEL_REV index 2add322e..101d21c5 100644 --- a/KERNEL_REV +++ b/KERNEL_REV @@ -1 +1 @@ -5c3a909771f749174567af074bfe18c24a335f5c +3b691d2a2c235ef847d61b308c31604979830994 diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 8d900485..556b4eb5 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -269,12 +269,21 @@ export interface KernelFederationOptions { identityFederationClientId?: string; } +/** + * Connection port forwarded as is to the napi binding's `ConnectionOptions.port`. + * The kernel validates it and folds it into the host. + */ +export interface KernelPortOptions { + port?: number; +} + export type KernelNativeConnectionOptions = KernelSessionDefaults & KernelTlsOptions & KernelHttpOptions & KernelTelemetryOptions & KernelProxyOptions & KernelFederationOptions & + KernelPortOptions & ( | { hostName: string; @@ -830,20 +839,13 @@ export function buildKernelProxyOptions(options: ConnectionOptions): KernelProxy return { proxy: out }; } -// The kernel prepends `https://` to a bare host but keeps an explicit scheme. -// It otherwise ignores `port`, so carry it into the URL unless `host` already has one. +// The host is passed through as given. The kernel owns port folding, IPv6 bracketing, +// userinfo and path checks, and trailing-slash handling, and it prepends `https://` to a +// bare host. The one rewrite left here is plain HTTP: `https: false` sends an explicit +// `http://` scheme, and the kernel's scheme-based default port then applies. function buildKernelHostName(options: ConnectionOptions): string { - if (options.https !== false) { - return options.host; - } - // The kernel accepts a full URL in `host`, so drop an existing `http://` or - // `https://` scheme before applying HTTP (`https: false` selects the scheme). - // Then strip a trailing `/` like the Thrift URL builder does. - const host = options.host.replace(/^https?:\/\//i, '').replace(/\/$/, ''); - // `name:port` or `[ipv6]:port`; a bare IPv6 literal like `::1` has no port. - const hasPort = /^(\[[^\]]+\]|[^:]+):\d+$/.test(host); - const port = options.port && !hasPort ? `:${options.port}` : ''; - return `http://${host}${port}`; + if (options.https !== false) return options.host; + return `http://${options.host.replace(/^https?:\/\//i, '')}`; } export function buildKernelConnectionOptions(options: ConnectionOptions): KernelNativeConnectionOptions { @@ -858,7 +860,8 @@ export function buildKernelConnectionOptions(options: ConnectionOptions): Kernel } & KernelTlsOptions & KernelHttpOptions & KernelProxyOptions & - KernelFederationOptions = { + KernelFederationOptions & + KernelPortOptions = { hostName: buildKernelHostName(options), httpPath: prependSlash(options.path), // Match the NodeJS Thrift driver, which surfaces INTERVAL columns as @@ -881,6 +884,11 @@ export function buildKernelConnectionOptions(options: ConnectionOptions): Kernel ...buildKernelProxyOptions(options), }; + // Passed through as is; the kernel validates it. Omitted when not set. + if (options.port !== undefined && options.port !== null) { + base.port = options.port; + } + if (options.enableGeoSpatialSupport !== undefined) { if (typeof options.enableGeoSpatialSupport !== 'boolean') { throw new HiveDriverError( diff --git a/native/kernel/index.d.ts b/native/kernel/index.d.ts index 87640ca0..0597773f 100644 --- a/native/kernel/index.d.ts +++ b/native/kernel/index.d.ts @@ -652,6 +652,16 @@ export interface ConnectionOptions { * kernel parses out the warehouse id. */ httpPath: string + /** + * Connection port. Omitted, `0`, or the scheme default (443 for + * `https`, 80 for `http`) changes nothing. Any other port is folded + * into the host by the kernel, so the connection goes to `host:port`. + * The host must not carry credentials, a path, a query or a fragment, + * an IPv6 address needs brackets, and a non-default port already in the + * host wins over this one. A value that is not an integer in + * `0..=65535` is an `InvalidArgument` error. + */ + port?: number /** * Authentication mode. Omitted ⇒ [`AuthMode::Pat`] (back-compat: * existing PAT callers pass only `token`). diff --git a/tests/unit/DBSQLClient.test.ts b/tests/unit/DBSQLClient.test.ts index 497889db..ad306c79 100644 --- a/tests/unit/DBSQLClient.test.ts +++ b/tests/unit/DBSQLClient.test.ts @@ -234,6 +234,40 @@ describe('DBSQLClient.connect', () => { expect(connectionOptions.port).to.equal(3015); }); + it('keeps the Thrift URL for a host with an embedded port unchanged (current behavior, not changed by the kernel fix)', async () => { + const client = new DBSQLClient(); + + // Thrift appends options.port (443 by default) after the host, so an embedded port becomes + // `h:8443:443`, which node-fetch rejects as an invalid URL. This pins that current behavior. + const https = client['getConnectionOptions']({ ...connectOptions, host: 'h.databricks.com:8443', port: 443 }); + expect(https.host).to.equal('h.databricks.com:8443'); + expect(https.port).to.equal(443); + const httpsConnection = await new HttpConnection(https, client).getThriftConnection(); + expect(httpsConnection.url).to.equal('https://h.databricks.com:8443:443/'); + + const http = client['getConnectionOptions']({ + ...connectOptions, + host: 'localhost:3015', + port: 80, + https: false, + }); + const httpConnection = await new HttpConnection(http, client).getThriftConnection(); + expect(httpConnection.url).to.equal('http://localhost:3015:80/'); + }); + + it('treats a falsy port as the default (443 over HTTPS, 80 over HTTP) on Thrift', async () => { + const client = new DBSQLClient(); + + for (const port of [0, Number.NaN, undefined, null, '', false]) { + const label = String(port); + expect(client['getConnectionOptions']({ ...connectOptions, port: port as any }).port, label).to.equal(443); + expect( + client['getConnectionOptions']({ ...connectOptions, port: port as any, https: false }).port, + label, + ).to.equal(80); + } + }); + it('should only switch to plain HTTP for an explicit https: false', async () => { const client = new DBSQLClient(); diff --git a/tests/unit/kernel/connectionOptions.test.ts b/tests/unit/kernel/connectionOptions.test.ts index 153e388f..69608636 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -64,71 +64,44 @@ describe('KernelAuth connection options — geospatial result representation', ( }); describe('KernelAuth connection options — https transport', () => { + const hostName = (extra: Record) => buildKernelConnectionOptions(opts(extra)).hostName; + it('forwards host unchanged by default (kernel prepends https://)', () => { - expect(buildKernelConnectionOptions(opts({})).hostName).to.equal('h.databricks.com'); - expect(buildKernelConnectionOptions(opts({ https: true })).hostName).to.equal('h.databricks.com'); + expect(hostName({})).to.equal('h.databricks.com'); + expect(hostName({ https: true })).to.equal('h.databricks.com'); }); it('selects plain HTTP via an explicit http:// host when https is false', () => { - expect(buildKernelConnectionOptions(opts({ https: false })).hostName).to.equal('http://h.databricks.com'); + expect(hostName({ https: false })).to.equal('http://h.databricks.com'); + expect(hostName({ host: 'localhost:3015', https: false })).to.equal('http://localhost:3015'); }); - it('carries the port into the http:// host (matching the Thrift URL)', () => { - expect(buildKernelConnectionOptions(opts({ host: 'localhost', port: 3015, https: false })).hostName).to.equal( - 'http://localhost:3015', - ); - // A port already embedded in `host` (the kernel convention) is preserved. - expect(buildKernelConnectionOptions(opts({ host: 'localhost:3015', https: false })).hostName).to.equal( - 'http://localhost:3015', - ); - }); - - it('does not double the port when it is set in both host and port', () => { - expect(buildKernelConnectionOptions(opts({ host: 'localhost:3015', port: 3015, https: false })).hostName).to.equal( - 'http://localhost:3015', - ); - expect(buildKernelConnectionOptions(opts({ host: '[::1]:3015', port: 3015, https: false })).hostName).to.equal( - 'http://[::1]:3015', - ); - }); - - it('strips a trailing slash from host before adding the port (matching Thrift)', () => { - expect(buildKernelConnectionOptions(opts({ host: 'localhost/', port: 3015, https: false })).hostName).to.equal( - 'http://localhost:3015', - ); - expect(buildKernelConnectionOptions(opts({ host: 'localhost:3015/', port: 3015, https: false })).hostName).to.equal( - 'http://localhost:3015', - ); - }); - - const hostName = (extra: Record) => buildKernelConnectionOptions(opts(extra)).hostName; - - it('normalizes an existing http:// scheme in host instead of prefixing it again', () => { + it('replaces an existing http(s):// scheme in host with http:// when https is false', () => { expect(hostName({ host: 'http://localhost:3015', https: false })).to.equal('http://localhost:3015'); - expect(hostName({ host: 'http://localhost:3015', port: 3015, https: false })).to.equal('http://localhost:3015'); - expect(hostName({ host: 'HTTP://localhost', port: 3015, https: false })).to.equal('http://localhost:3015'); - expect(hostName({ host: 'http://localhost:3015/', https: false })).to.equal('http://localhost:3015'); - }); - - it('replaces an existing https:// scheme in host with http:// when https is false', () => { + expect(hostName({ host: 'HTTP://localhost', https: false })).to.equal('http://localhost'); expect(hostName({ host: 'https://localhost:3015', https: false })).to.equal('http://localhost:3015'); - expect(hostName({ host: 'https://localhost', port: 3015, https: false })).to.equal('http://localhost:3015'); - expect(hostName({ host: 'https://[::1]:3015', port: 3015, https: false })).to.equal('http://[::1]:3015'); + expect(hostName({ host: 'https://[::1]:3015', https: false })).to.equal('http://[::1]:3015'); }); it('forwards a full-URL host unchanged when https is not false', () => { expect(hostName({ host: 'https://h.databricks.com' })).to.equal('https://h.databricks.com'); expect(hostName({ host: 'http://localhost:3015', https: true })).to.equal('http://localhost:3015'); }); +}); + +describe('KernelAuth connection options — port passthrough', () => { + const native = (extra: Record) => + buildKernelConnectionOptions(opts(extra)) as { hostName: string; port?: number }; + + it('passes a set port to the binding unchanged', () => { + expect(native({ port: 8443 })).to.include({ hostName: 'h.databricks.com', port: 8443 }); + expect(native({ port: 3015, https: false })).to.include({ hostName: 'http://h.databricks.com', port: 3015 }); + }); - it('does not mistake an IPv6 literal for an embedded port', () => { - expect(buildKernelConnectionOptions(opts({ host: '[::1]', port: 3015, https: false })).hostName).to.equal( - 'http://[::1]:3015', - ); - // Same URL shape as the Thrift transport, which also requires brackets. - expect(buildKernelConnectionOptions(opts({ host: '::1', port: 3015, https: false })).hostName).to.equal( - 'http://::1:3015', - ); + it('sends no port key when the port is omitted', () => { + for (const extra of [{}, { port: undefined }, { port: null }]) { + expect(native(extra), JSON.stringify(extra)).to.not.have.property('port'); + } }); });