|
1 | 1 | import { X509Certificate } from 'crypto'; |
| 2 | +import https from 'https'; |
2 | 3 | import tls, { PeerCertificate } from 'tls'; |
3 | | -import { bracketHostIfIPv6, ipv6AwareCheckServerIdentity, isIPv6Url } from '../networkUtils'; |
| 4 | +import type { KubeConfig } from '@kubernetes/client-node'; |
| 5 | +import { |
| 6 | + applyIpv6AwareIdentityCheck, |
| 7 | + bracketHostIfIPv6, |
| 8 | + ipv6AwareCheckServerIdentity, |
| 9 | + isIPv6Url, |
| 10 | +} from '../networkUtils'; |
4 | 11 |
|
5 | 12 | describe('bracketHostIfIPv6', () => { |
6 | 13 | it('should wrap a raw IPv6 literal in brackets', () => { |
@@ -200,3 +207,53 @@ FBGQ5ML+k9MgI2zBO32YBmZZX8G/rqHLlsYXGkfRC3AfLsyIpuFdjYXvox8tOH7G |
200 | 207 | ).toBe(error); |
201 | 208 | }); |
202 | 209 | }); |
| 210 | + |
| 211 | +describe('applyIpv6AwareIdentityCheck', () => { |
| 212 | + // Minimal KubeConfig stand-in whose applyToRequest sets TLS material like the real one does. |
| 213 | + const fakeKubeConfig = (): { kc: KubeConfig; original: jest.Mock } => { |
| 214 | + const original = jest.fn(async (opts: Record<string, unknown>) => { |
| 215 | + opts.ca = Buffer.from('ca-data'); |
| 216 | + opts.cert = Buffer.from('cert-data'); |
| 217 | + opts.key = Buffer.from('key-data'); |
| 218 | + }); |
| 219 | + return { kc: { applyToRequest: original } as unknown as KubeConfig, original }; |
| 220 | + }; |
| 221 | + |
| 222 | + it('should attach an https.Agent carrying ipv6AwareCheckServerIdentity and the TLS material', async () => { |
| 223 | + const { kc } = fakeKubeConfig(); |
| 224 | + applyIpv6AwareIdentityCheck(kc); |
| 225 | + |
| 226 | + const opts: Record<string, unknown> = {}; |
| 227 | + await kc.applyToRequest(opts as unknown as Parameters<KubeConfig['applyToRequest']>[0]); |
| 228 | + |
| 229 | + expect(opts.agent).toBeInstanceOf(https.Agent); |
| 230 | + const agentOptions = (opts.agent as https.Agent).options; |
| 231 | + expect(agentOptions.checkServerIdentity).toBe(ipv6AwareCheckServerIdentity); |
| 232 | + expect(agentOptions.ca).toEqual(Buffer.from('ca-data')); |
| 233 | + expect(agentOptions.cert).toEqual(Buffer.from('cert-data')); |
| 234 | + expect(agentOptions.key).toEqual(Buffer.from('key-data')); |
| 235 | + }); |
| 236 | + |
| 237 | + it('should still invoke the original applyToRequest (auth/TLS material preserved)', async () => { |
| 238 | + const { kc, original } = fakeKubeConfig(); |
| 239 | + applyIpv6AwareIdentityCheck(kc); |
| 240 | + |
| 241 | + const opts: Record<string, unknown> = {}; |
| 242 | + await kc.applyToRequest(opts as unknown as Parameters<KubeConfig['applyToRequest']>[0]); |
| 243 | + |
| 244 | + expect(original).toHaveBeenCalledTimes(1); |
| 245 | + expect(opts.ca).toEqual(Buffer.from('ca-data')); |
| 246 | + }); |
| 247 | + |
| 248 | + it('should reuse a single agent instance across requests (connection pooling)', async () => { |
| 249 | + const { kc } = fakeKubeConfig(); |
| 250 | + applyIpv6AwareIdentityCheck(kc); |
| 251 | + |
| 252 | + const opts1: Record<string, unknown> = {}; |
| 253 | + const opts2: Record<string, unknown> = {}; |
| 254 | + await kc.applyToRequest(opts1 as unknown as Parameters<KubeConfig['applyToRequest']>[0]); |
| 255 | + await kc.applyToRequest(opts2 as unknown as Parameters<KubeConfig['applyToRequest']>[0]); |
| 256 | + |
| 257 | + expect(opts1.agent).toBe(opts2.agent); |
| 258 | + }); |
| 259 | +}); |
0 commit comments