diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fbcb477..3844cef1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History +## Unreleased + +- Kernel backend: a custom `port` is honored over HTTPS, as on Thrift, and with `https: false`. The kernel applies the port (kernel databricks/databricks-sql-kernel#505): a port other than the scheme default (443 over HTTPS, 80 with `https: false`) is written into `host` as `scheme://host:port`, bare IPv6 hosts are bracketed, and the default or `0` changes nothing. A port already in `host` wins, with a warning when they differ. The kernel rejects credentials, an empty `host`, or a path, query or fragment in `host` when a port is applied. The driver still treats a falsy `port` as the default, accepts a numeric string, and throws `HiveDriverError` for an invalid port. With OAuth and a non-default port, the kernel reaches its OAuth endpoints at the same `host:port`, while Thrift keeps OAuth on the bare host (documented, not guarded). Customer npm installs require a follow-up bump to a published `@databricks/databricks-sql-kernel-*` native package that reads the `port` option; an older published binding does not read it and connects to the default port. + ## 3.1.0 - Upgrade to [kernel 1.5.0](https://github.com/databricks/databricks-sql-kernel/releases/tag/v1.5.0): fix CloudFetch pagination, ordering and cancellation, paged inline Arrow results with CloudFetch disabled, and affected-row counts. diff --git a/CONNECTION_PARAMETERS.md b/CONNECTION_PARAMETERS.md index edba6002..9c4f07f6 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` / — | The kernel applies the port. A port other than the scheme default (443 over HTTPS, 80 with `https: false`) is written into `host` as `scheme://host:port`, with a bare IPv6 host bracketed; the default and `0` change nothing. A port already in `host` wins, with a warning when they differ. The kernel rejects credentials in `host`, an empty `host`, and a path, query or fragment when a port is applied. The driver rejects a non-integer or out-of-range `port` (`HiveDriverError`); a falsy `port` means the default. 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..bb1a004e 100644 --- a/KERNEL_REV +++ b/KERNEL_REV @@ -1 +1 @@ -5c3a909771f749174567af074bfe18c24a335f5c +22ae6ec250837b38d0e3dc720310d4dcfb81fa98 diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 8d900485..5e11f18a 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -269,12 +269,23 @@ export interface KernelFederationOptions { identityFederationClientId?: string; } +/** + * Connection port forwarded to the napi binding's `ConnectionOptions.port`. + * The kernel folds it into the host (`fold_host_port`). Any explicit port is + * sent, 443 included; the kernel treats the scheme's default as a no-op (see + * `buildKernelPort`). + */ +export interface KernelPortOptions { + port?: number; +} + export type KernelNativeConnectionOptions = KernelSessionDefaults & KernelTlsOptions & KernelHttpOptions & KernelTelemetryOptions & KernelProxyOptions & KernelFederationOptions & + KernelPortOptions & ( | { hostName: string; @@ -830,20 +841,36 @@ 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. -function buildKernelHostName(options: ConnectionOptions): string { - if (options.https !== false) { - return options.host; +// An explicit `port` is read as Thrift reads it (`options.port || 443`): a falsy value +// (unset, null, '', 0, NaN, false) means the default. A truthy value must be a whole +// port number, and a decimal string such as `'8443'` is accepted. Anything else throws, +// as Thrift also fails on it. +function explicitPort(port: unknown): number | undefined { + if (!port) return undefined; + let value = Number.NaN; + if (typeof port === 'number') value = port; + else if (typeof port === 'string' && /^[0-9]+$/.test(port)) value = Number(port); + if (value === 0) return undefined; + if (!Number.isInteger(value) || value < 1 || value > 65535) { + throw new HiveDriverError(`kernel backend: \`port\` must be an integer between 1 and 65535; got ${String(port)}.`); } - // 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}`; + return value; +} + +// 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; + return `http://${options.host.replace(/^https?:\/\//i, '')}`; +} + +// The port sent to the kernel, or `undefined` for the default. Any explicit port is sent as +// is: the kernel knows the effective scheme (including an `http://` already in `host`) and +// treats that scheme's default port (443 for https, 80 for http) as a no-op. +function buildKernelPort(options: ConnectionOptions): number | undefined { + return explicitPort(options.port); } export function buildKernelConnectionOptions(options: ConnectionOptions): KernelNativeConnectionOptions { @@ -858,7 +885,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 +909,12 @@ export function buildKernelConnectionOptions(options: ConnectionOptions): Kernel ...buildKernelProxyOptions(options), }; + // Omitted when no port is set; any explicit port, 443 and 80 included, is sent. + const port = buildKernelPort(options); + if (port !== undefined) { + base.port = 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..3a484143 100644 --- a/native/kernel/index.d.ts +++ b/native/kernel/index.d.ts @@ -652,6 +652,15 @@ 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, + * and a 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..3ed48b4c 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), as the kernel path does', 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..353e7192 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -64,71 +64,99 @@ 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'); - }); - - 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', - ); + expect(hostName({ https: false })).to.equal('http://h.databricks.com'); + expect(hostName({ host: 'localhost:3015', https: false })).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'); }); +}); - 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', - ); +describe('KernelAuth connection options — port passthrough', () => { + const native = (extra: Record) => + buildKernelConnectionOptions(opts(extra)) as { hostName: string; port?: number }; + + // [connection options, expected hostName, expected native port]. The host is passed through + // unchanged, and the kernel folds the port into it, so these are not folded here. + const sent: Array<[Record, string, number]> = [ + [{ port: 8443 }, 'h.databricks.com', 8443], + [{ port: 8443, https: true }, 'h.databricks.com', 8443], + [{ port: '8443' }, 'h.databricks.com', 8443], // a decimal string is accepted, as on Thrift + [{ port: 3015, https: false }, 'http://h.databricks.com', 3015], + [{ port: 443, https: false }, 'http://h.databricks.com', 443], // 443 is not the plain-HTTP default + [{ host: 'localhost', port: 3015, https: false }, 'http://localhost', 3015], + [{ host: 'https://h.databricks.com', port: 8443 }, 'https://h.databricks.com', 8443], + [{ host: 'h.databricks.com/', port: 8443 }, 'h.databricks.com/', 8443], + [{ host: '::1', port: 8443 }, '::1', 8443], // not bracketed here; the kernel brackets it + [{ host: '::1', port: 3015, https: false }, 'http://::1', 3015], + [{ host: 'user:pw@h.databricks.com', port: 8443 }, 'user:pw@h.databricks.com', 8443], + // A port already in host is left for the kernel to resolve; the driver sends both values. + [{ host: 'h.databricks.com:8443', port: 8443 }, 'h.databricks.com:8443', 8443], + [{ host: 'h.databricks.com:7000', port: 8443 }, 'h.databricks.com:7000', 8443], + [{ host: 'h.io/path:8443', port: 8443 }, 'h.io/path:8443', 8443], + [{ host: '', port: 8443 }, '', 8443], + // An explicit port is always sent; the kernel decides whether it is the default for the + // effective scheme, including an `http://` already in host with https unset. + [{ port: 443 }, 'h.databricks.com', 443], + [{ port: 443, https: true }, 'h.databricks.com', 443], + [{ port: 80, https: false }, 'http://h.databricks.com', 80], + [{ host: 'http://localhost', port: 443 }, 'http://localhost', 443], + ]; + it('passes the host through unchanged and sends any explicit port', () => { + for (const [extra, wantHost, wantPort] of sent) { + const got = native(extra); + expect(got.hostName, JSON.stringify(extra)).to.equal(wantHost); + expect(got.port, JSON.stringify(extra)).to.equal(wantPort); + } + }); + + // No `port` key at all: unset or falsy, which means the default, as on Thrift. + const omitted: Array<[Record, string]> = [ + [{}, 'h.databricks.com'], + [{ port: undefined }, 'h.databricks.com'], + [{ port: null }, 'h.databricks.com'], + [{ port: 0 }, 'h.databricks.com'], + [{ port: '' }, 'h.databricks.com'], + [{ port: false }, 'h.databricks.com'], + [{ port: Number.NaN }, 'h.databricks.com'], + [{ port: 0, https: false }, 'http://h.databricks.com'], + [{ port: Number.NaN, https: false }, 'http://h.databricks.com'], + [{ host: 'h.databricks.com:8443' }, 'h.databricks.com:8443'], // port only in host + ]; + it('sends no port when the port is unset or falsy', () => { + for (const [extra, wantHost] of omitted) { + const got = native(extra); + expect(got.hostName, JSON.stringify(extra)).to.equal(wantHost); + expect(got, JSON.stringify(extra)).to.not.have.property('port'); + } + }); + + // Truthy values that are not a whole port number throw HiveDriverError before any native call, + // as Thrift does. The kernel does not see these. + const badPorts: unknown[] = [65536, -1, 1.5, Number.POSITIVE_INFINITY, true, 'abc', '8443x', '-1', '65536']; + it('rejects a truthy invalid port, with or without https: false', () => { + for (const port of badPorts) { + for (const extra of [{ port }, { port, https: false }]) { + expect(() => native(extra), String(port)).to.throw(HiveDriverError, /must be an integer between 1 and 65535/); + } + } }); });