Skip to content

Commit bda0d35

Browse files
committed
fix(backend): apply IPv6 cert check to makeApiClient k8s clients too
The IPv6-aware checkServerIdentity was only wired into proxyCall. The typed Kubernetes clients from kc.makeApiClient(...) use @kubernetes/client-node's own 'request' transport, which never received it — so startup reads (plugins/kube.ts) and the dashboard-config watcher (resourceUtils.ts) could still fail with ERR_TLS_CERT_ALTNAME_INVALID on IPv6 single-stack clusters. Root cause confirmed by reviewers: Node 22.23.1/.2 regression nodejs/node#64144 mis-normalizes IPv6 literals in the default identity check (fixed in later 22.x), so the fallback is genuinely load-bearing on the shipped UBI9 Node. Adds applyIpv6AwareIdentityCheck(kc): wraps KubeConfig.applyToRequest to attach an https.Agent carrying the kube TLS material plus ipv6AwareCheckServerIdentity. A pre-built agent is required because 'request' drops a bare checkServerIdentity option and pools agents by a key that ignores it. Applied to the shared kc and the per-user Feast userKc. Verified end-to-end against real client-node + makeApiClient; added unit coverage for the wiring.
1 parent d49e4ce commit bda0d35

5 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎backend/src/__tests__/featureStoreUtils.spec.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ const spyKubeConfigForMockApi = (getMockApi: () => ReturnType<typeof createMockA
120120
({
121121
loadFromClusterAndUser,
122122
makeApiClient: jest.fn().mockReturnValue(getMockApi()),
123+
// applyIpv6AwareIdentityCheck wraps this; provide a no-op so the wrapper can bind it.
124+
applyToRequest: jest.fn(),
123125
} as any),
124126
);
125127
return { loadFromClusterAndUser };

‎backend/src/plugins/kube.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import * as k8s from '@kubernetes/client-node';
66
import { errorHandler, isKubeFastifyInstance } from '../utils';
77
import { DEV_MODE } from '../utils/constants';
88
import { cleanupKserveRoleBindings, initializeWatchedResources } from '../utils/resourceUtils';
9+
import { applyIpv6AwareIdentityCheck } from '../utils/networkUtils';
910

1011
const CONSOLE_CONFIG_YAML_FIELD = 'console-config.yaml';
1112

1213
const kc = new k8s.KubeConfig();
1314
kc.loadFromDefault();
15+
// Ensure the typed API clients created below validate IPv6 API-server certs correctly (the fix
16+
// wired into proxyCall does not cover @kubernetes/client-node's own request transport).
17+
applyIpv6AwareIdentityCheck(kc);
1418

1519
const currentContext = kc.getCurrentContext();
1620
const customObjectsApi = kc.makeApiClient(k8s.CustomObjectsApi);

‎backend/src/routes/api/featurestores/featureStoreUtils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { KubeFastifyInstance } from '../../../types';
55
import { DEV_MODE } from '../../../utils/constants';
66
import { isImpersonating, getImpersonateAccessToken } from '../../../devFlags';
77
import { getAccessToken } from '../../../utils/directCallUtils';
8-
import { bracketHostIfIPv6 } from '../../../utils/networkUtils';
8+
import { applyIpv6AwareIdentityCheck, bracketHostIfIPv6 } from '../../../utils/networkUtils';
99

1010
const REGISTRY_CONDITION_TYPE = 'Registry';
1111

@@ -110,6 +110,7 @@ function getUserScopedCustomObjectsApi(
110110

111111
const userKc = new k8s.KubeConfig();
112112
userKc.loadFromClusterAndUser(cluster, { name: 'current-user', token });
113+
applyIpv6AwareIdentityCheck(userKc);
113114

114115
return {
115116
api: userKc.makeApiClient(k8s.CustomObjectsApi),

‎backend/src/utils/__tests__/networkUtils.spec.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { X509Certificate } from 'crypto';
2+
import https from 'https';
23
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';
411

512
describe('bracketHostIfIPv6', () => {
613
it('should wrap a raw IPv6 literal in brackets', () => {
@@ -200,3 +207,53 @@ FBGQ5ML+k9MgI2zBO32YBmZZX8G/rqHLlsYXGkfRC3AfLsyIpuFdjYXvox8tOH7G
200207
).toBe(error);
201208
});
202209
});
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+
});

‎backend/src/utils/networkUtils.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { X509Certificate } from 'crypto';
2+
import https from 'https';
23
import net from 'net';
34
import tls, { PeerCertificate } from 'tls';
5+
import type { KubeConfig } from '@kubernetes/client-node';
46

57
/**
68
* Wrap a raw IPv6 literal in square brackets so it can be safely concatenated into a
@@ -69,3 +71,36 @@ export const ipv6AwareCheckServerIdentity = (
6971

7072
return defaultError;
7173
};
74+
75+
/**
76+
* Make a `@kubernetes/client-node` KubeConfig apply the IPv6-aware TLS identity check to the typed
77+
* API clients created from it (`makeApiClient(...)`), which otherwise bypass the fix wired into
78+
* `proxyCall`.
79+
*
80+
* Those clients make requests through the `request` library, which on affected Node releases (see
81+
* nodejs/node#64144 — 22.23.1/.2 mis-normalize IPv6 literals) fail IPv6 API-server calls with
82+
* `ERR_TLS_CERT_ALTNAME_INVALID`. `request` drops a bare `checkServerIdentity` option and pools
83+
* agents by a key that ignores it, so the only reliable injection point is a pre-built `agent`. We
84+
* wrap `applyToRequest` to attach an `https.Agent` carrying the KubeConfig's own TLS material plus
85+
* `ipv6AwareCheckServerIdentity`. The agent is built once (TLS material is stable for a KubeConfig)
86+
* and reused for connection pooling.
87+
*/
88+
export const applyIpv6AwareIdentityCheck = (kubeConfig: KubeConfig): void => {
89+
const applyToRequest = kubeConfig.applyToRequest.bind(kubeConfig);
90+
let agent: https.Agent | undefined;
91+
kubeConfig.applyToRequest = async (opts) => {
92+
await applyToRequest(opts);
93+
if (!agent) {
94+
agent = new https.Agent({
95+
ca: opts.ca,
96+
cert: opts.cert,
97+
key: opts.key,
98+
...(typeof opts.rejectUnauthorized === 'boolean'
99+
? { rejectUnauthorized: opts.rejectUnauthorized }
100+
: {}),
101+
checkServerIdentity: ipv6AwareCheckServerIdentity,
102+
});
103+
}
104+
opts.agent = agent;
105+
};
106+
};

0 commit comments

Comments
 (0)