From b3f8bd3057fe0b50f013d55f48126bf1e3a52701 Mon Sep 17 00:00:00 2001 From: brendandburns <5751682+brendandburns@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:21:12 +0000 Subject: [PATCH] feat: support configuring the minimum TLS version Add KubeConfig.setTLSMinVersion(), propagate the setting through direct and proxy connections, and destroy cached agents when it changes. Document the configuration and cover transport paths with regression tests. --- README.md | 19 ++++++++ src/config.ts | 24 ++++++++++ src/config_test.ts | 106 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) diff --git a/README.md b/README.md index 1983ff92664..6d3cb407d87 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,25 @@ const k8sApi = kc.makeApiClient(k8s.CoreV1Api); ... ``` +## Configure the minimum TLS version + +Set the minimum TLS version before making API requests: + +```javascript +const kc = new k8s.KubeConfig(); +kc.loadFromDefault(); +kc.setTLSMinVersion('TLSv1.3'); +const k8sApi = kc.makeApiClient(k8s.CoreV1Api); +``` + +`setTLSMinVersion` accepts Node.js TLS version names (`TLSv1`, `TLSv1.1`, `TLSv1.2`, or `TLSv1.3`). +The setting applies to API-server TLS connections, including direct connections and connections +through HTTP(S) or SOCKS proxies. It does not configure TLS to the proxy itself. +When unset, or reset with `kc.setTLSMinVersion(undefined)`, the Node.js runtime default is preserved +(including command-line TLS settings); the client does not impose its own minimum. +Calling `setTLSMinVersion` destroys cached agents and their sockets, which can abort in-flight +requests. Configure it before starting requests. + ## Call APIs with protocol buffers ```javascript diff --git a/src/config.ts b/src/config.ts index eb6c533ff1f..59e09ebe3ca 100644 --- a/src/config.ts +++ b/src/config.ts @@ -141,6 +141,7 @@ export interface ApiType {} export class KubeConfig implements SecurityAuthentication { private execAuth: ExecAuth = new ExecAuth(); + private tlsMinVersion?: tls.SecureVersion; private authenticators: Authenticator[] = [ new AzureAuth(), @@ -165,6 +166,19 @@ export class KubeConfig implements SecurityAuthentication { this.custom_authenticators.push(authenticator); } + /** + * Sets the minimum TLS version for API connections. Configure before making requests. + * Destroys cached agents and their sockets, which can abort in-flight requests. + * @param minVersion undefined preserves the Node.js runtime default. + */ + public setTLSMinVersion(minVersion?: tls.SecureVersion): void { + this.tlsMinVersion = minVersion; + for (const agent of this.agentCache.values()) { + agent.destroy(); + } + this.agentCache.clear(); + } + /** * Sets the application policy for kubeconfig `exec` credential plugins. * The semantics match the `credentialPluginPolicy` and `credentialPluginAllowlist` @@ -322,6 +336,9 @@ export class KubeConfig implements SecurityAuthentication { agentOptions.key = opts.key; agentOptions.pfx = opts.pfx; agentOptions.passphrase = opts.passphrase; + if (opts.minVersion !== undefined) { + agentOptions.minVersion = opts.minVersion; + } // Only set rejectUnauthorized if explicitly configured. When not set, fetch will use NODE_TLS_REJECT_UNAUTHORIZED env var if (opts.rejectUnauthorized !== undefined) { agentOptions.rejectUnauthorized = opts.rejectUnauthorized; @@ -374,6 +391,9 @@ export class KubeConfig implements SecurityAuthentication { agentOptions.key = httpsOptions.key; agentOptions.pfx = httpsOptions.pfx; agentOptions.passphrase = httpsOptions.passphrase; + if (httpsOptions.minVersion !== undefined) { + agentOptions.minVersion = httpsOptions.minVersion; + } // Only set rejectUnauthorized if explicitly configured. When not set, fetch will use NODE_TLS_REJECT_UNAUTHORIZED env var if (httpsOptions.rejectUnauthorized !== undefined) { agentOptions.rejectUnauthorized = httpsOptions.rejectUnauthorized; @@ -718,6 +738,7 @@ export class KubeConfig implements SecurityAuthentication { if (agentOptions.key !== undefined) tlsOptions.key = agentOptions.key; if (agentOptions.pfx !== undefined) tlsOptions.pfx = agentOptions.pfx; if (agentOptions.passphrase !== undefined) tlsOptions.passphrase = agentOptions.passphrase; + if (agentOptions.minVersion !== undefined) tlsOptions.minVersion = agentOptions.minVersion; if (agentOptions.rejectUnauthorized !== undefined) tlsOptions.rejectUnauthorized = agentOptions.rejectUnauthorized; if ((agentOptions as any).servername !== undefined) @@ -770,6 +791,9 @@ export class KubeConfig implements SecurityAuthentication { } private applyHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): void { + if (this.tlsMinVersion !== undefined) { + opts.minVersion = this.tlsMinVersion; + } const cluster = this.getCurrentCluster(); const user = this.getCurrentUser(); if (!user) { diff --git a/src/config_test.ts b/src/config_test.ts index 9dd2b599a79..5ec8b715d16 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -443,6 +443,112 @@ describe('KubeConfig', () => { expectedCA.toString(), ); }); + for (const proxyUrl of [undefined, 'http://proxy:8080', 'socks5://proxy:1080']) { + it(`should pass the minimum TLS version to dispatcher options (${proxyUrl ?? 'direct'})`, () => { + const kc = new KubeConfig(); + const cluster = { + name: 'cluster', + server: 'https://example.com', + skipTLSVerify: false, + proxyUrl, + }; + const options = kc.createDispatcherOptions(cluster, { minVersion: 'TLSv1.3' }); + assert(options.type !== 'none'); + const tlsOptions = options.type === 'agent' ? options.connect : options.requestTls; + strictEqual(tlsOptions.minVersion, 'TLSv1.3'); + }); + + it(`should configure the minimum TLS version through authentication (${proxyUrl ?? 'direct'})`, async () => { + const kc = new KubeConfig(); + kc.loadFromClusterAndUser( + { name: 'cluster', server: 'https://example.com', skipTLSVerify: false, proxyUrl }, + { name: 'user' }, + ); + kc.setTLSMinVersion('TLSv1.3'); + const createOptions = kc.createDispatcherOptions.bind(kc); + let observedMinVersion: tls.SecureVersion | undefined; + mock.method(kc, 'createDispatcherOptions', (cluster, agentOptions) => { + const options = createOptions(cluster, agentOptions); + assert(options.type !== 'none'); + const tlsOptions = options.type === 'agent' ? options.connect : options.requestTls; + observedMinVersion = tlsOptions.minVersion; + return options; + }); + const rc = new RequestContext('https://example.com', HttpMethod.GET); + await kc.applySecurityAuthentication(rc); + try { + strictEqual(observedMinVersion, 'TLSv1.3'); + } finally { + await rc.getDispatcher()?.close(); + } + + const opts: https.RequestOptions = {}; + await kc.applyToHTTPSOptions(opts); + strictEqual(opts.minVersion, 'TLSv1.3'); + strictEqual((opts.agent as https.Agent).options.minVersion, 'TLSv1.3'); + const destroy = mock.method(opts.agent as https.Agent, 'destroy'); + + kc.setTLSMinVersion(undefined); + strictEqual(destroy.mock.callCount(), 1); + const defaultOpts: https.RequestOptions = {}; + await kc.applyToHTTPSOptions(defaultOpts); + strictEqual(defaultOpts.minVersion, undefined); + strictEqual((defaultOpts.agent as https.Agent).options.minVersion, undefined); + notStrictEqual(defaultOpts.agent, opts.agent); + (opts.agent as https.Agent).destroy(); + (defaultOpts.agent as https.Agent).destroy(); + }); + + it(`should destroy all cached agents when setting the minimum TLS version (${proxyUrl ?? 'direct'})`, async (t) => { + const kc = new KubeConfig(); + const cluster = { + name: 'cluster', + server: 'https://example.com', + skipTLSVerify: false, + proxyUrl, + }; + const agents: http.Agent[] = []; + try { + for (const name of ['user1', 'user2']) { + kc.loadFromClusterAndUser(cluster, { name }); + const opts: https.RequestOptions = {}; + await kc.applyToHTTPSOptions(opts); + assert(opts.agent instanceof http.Agent); + agents.push(opts.agent); + } + const destroyMocks = agents.map((agent) => t.mock.method(agent, 'destroy')); + + kc.setTLSMinVersion('TLSv1.3'); + + for (const destroy of destroyMocks) { + strictEqual(destroy.mock.callCount(), 1); + } + const opts: https.RequestOptions = {}; + await kc.applyToHTTPSOptions(opts); + assert(opts.agent instanceof http.Agent); + strictEqual(agents.includes(opts.agent), false); + agents.push(opts.agent); + } finally { + for (const agent of agents) { + agent.destroy(); + } + } + }); + + it(`should preserve the runtime TLS default when unset (${proxyUrl ?? 'direct'})`, () => { + const kc = new KubeConfig(); + const options = kc.createDispatcherOptions( + { name: 'cluster', server: 'https://example.com', skipTLSVerify: false, proxyUrl }, + {}, + ); + if (options.type !== 'none') { + const tlsOptions = options.type === 'agent' ? options.connect : options.requestTls; + strictEqual(tlsOptions.minVersion, undefined); + } else { + strictEqual(proxyUrl, undefined); + } + }); + } it('should override default 60s keep-alive with 30s for cloud LB compatibility', () => { const kc = new KubeConfig(); kc.loadFromClusterAndUser(