-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathlocal-vllm-auth.test.ts
More file actions
491 lines (440 loc) · 16.4 KB
/
Copy pathlocal-vllm-auth.test.ts
File metadata and controls
491 lines (440 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import fs from "node:fs";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { DUAL_STATION_VLLM_RUNTIME } from "./vllm-station-cluster";
interface ManagedBaseUrlOverrides {
loadApiKey?: () => string | null;
onManagedHeadObserved?: () => void;
}
const lifecycle = vi.hoisted(() => ({
baseUrl: vi.fn<(overrides?: ManagedBaseUrlOverrides) => string | null>(),
}));
const managedClusterRecovery = vi.hoisted(() => ({
endpoint: vi.fn(),
}));
const hostLocalRecovery = vi.hoisted(() => ({
endpoint: vi.fn(),
}));
const managedBridge = vi.hoisted(() => ({
host: vi.fn(() => "172.18.0.1"),
}));
const managedKey = vi.hoisted(() => ({
load: vi.fn(),
}));
vi.mock("./vllm-station-cluster-lifecycle", () => ({
getDualStationManagedVllmBaseUrl: lifecycle.baseUrl,
}));
vi.mock("./serving/managed-cluster-runtime-receipt", () => ({
recoverInstalledManagedClusterVllmEndpoint: managedClusterRecovery.endpoint,
}));
vi.mock("./serving/vllm-host-local-lifecycle", async (importOriginal) => ({
...(await importOriginal<typeof import("./serving/vllm-host-local-lifecycle")>()),
recoverHostLocalManagedVllmEndpoint: hostLocalRecovery.endpoint,
resolveManagedVllmBridgeHost: managedBridge.host,
}));
vi.mock("./vllm-api-key", async (importOriginal) => ({
...(await importOriginal<typeof import("./vllm-api-key")>()),
loadManagedVllmApiKey: managedKey.load,
}));
import {
CONTAINER_REACHABILITY_IMAGE,
getLocalProviderBaseUrl,
getLocalProviderContainerReachabilityCheck,
getLocalProviderHealthCheck,
getLocalProviderHealthEndpoint,
getManagedVllmProviderBinding,
getManagedVllmProviderState,
LOCAL_INFERENCE_SANDBOX_HOST_URL_ENV,
probeLocalProviderHealth,
probeVllmModels,
validateLocalProvider,
} from "./local";
const BASE_URL = "http://10.40.0.1:8000";
const API_KEY = "e".repeat(64);
const OTHER_API_KEY = "f".repeat(64);
const MANAGED_BASE_URL_BY_API_KEY = new Map<string | null | undefined, string | null>([
[undefined, BASE_URL],
[API_KEY, BASE_URL],
[null, null],
]);
let actualLifecycle: typeof import("./vllm-station-cluster-lifecycle");
beforeAll(async () => {
actualLifecycle = await vi.importActual("./vllm-station-cluster-lifecycle");
});
function productionManagedBaseUrlResolver(
expectedApiKey = API_KEY,
apiKeyFingerprint = actualLifecycle.dualStationVllmApiKeyFingerprint(expectedApiKey),
) {
const row = [
"a".repeat(64),
actualLifecycle.DUAL_STATION_VLLM_HEAD_CONTAINER_NAME,
"running",
DUAL_STATION_VLLM_RUNTIME.image,
"true",
"head",
BASE_URL,
"b".repeat(64),
"GPU-12345678",
"2",
"c".repeat(64),
apiKeyFingerprint,
"d".repeat(32),
].join("\t");
return (overrides: ManagedBaseUrlOverrides = {}): string | null =>
actualLifecycle.getDualStationManagedVllmBaseUrl({
dockerCapture: () => row,
buildLocalDockerEnv: () => ({}),
loadApiKey: overrides.loadApiKey ?? (() => null),
onManagedHeadObserved: overrides.onManagedHeadObserved ?? (() => undefined),
localInterfaceAddresses: () => ["10.40.0.1"],
});
}
function hostLocalDiagnosticCapture(argv: readonly string[]): string {
return argv[0] === "curl"
? "200"
: argv.at(-1) === "/etc/hosts"
? "172.18.0.1\thost.openshell.internal"
: argv.includes("-o")
? "503"
: "";
}
beforeEach(() => {
vi.stubEnv(LOCAL_INFERENCE_SANDBOX_HOST_URL_ENV, undefined);
managedClusterRecovery.endpoint.mockReset();
managedClusterRecovery.endpoint.mockReturnValue(null);
hostLocalRecovery.endpoint.mockReset();
hostLocalRecovery.endpoint.mockReturnValue(null);
managedBridge.host.mockReset();
managedBridge.host.mockReturnValue("172.18.0.1");
managedKey.load.mockReset();
managedKey.load.mockReturnValue(API_KEY);
lifecycle.baseUrl.mockReset();
lifecycle.baseUrl.mockImplementation(
(overrides) => MANAGED_BASE_URL_BY_API_KEY.get(overrides?.loadApiKey?.()) ?? null,
);
});
afterEach(() => vi.unstubAllEnvs());
describe("managed vLLM authentication", () => {
it("keeps an explicit sandbox host override ahead of managed endpoint recovery", () => {
const loadApiKeyImpl = vi.fn(() => API_KEY);
expect(getLocalProviderBaseUrl("vllm-local", { hostUrl: "http://explicit-host" })).toBe(
"http://explicit-host:8000/v1",
);
expect(
getManagedVllmProviderBinding({
hostUrl: "http://explicit-host",
loadApiKeyImpl,
}),
).toBeNull();
expect(loadApiKeyImpl).not.toHaveBeenCalled();
expect(managedClusterRecovery.endpoint).not.toHaveBeenCalled();
});
it("recovers a receipt-owned managed cluster endpoint before checking Station state", () => {
const events: string[] = [];
const recoverManagedClusterVllmEndpointImpl = vi.fn(() => {
events.push("managed-cluster");
return { baseUrl: BASE_URL, apiKey: API_KEY };
});
const getManagedBaseUrlImpl = vi.fn(() => {
events.push("station");
return null;
});
expect(
getManagedVllmProviderBinding({
getManagedBaseUrlImpl,
recoverManagedClusterVllmEndpointImpl,
}),
).toEqual({ baseUrl: `${BASE_URL}/v1`, apiKey: API_KEY });
expect(events).toEqual(["managed-cluster", "station"]);
});
it("does not fall through to Station when managed cluster recovery is unsafe", () => {
const getManagedBaseUrlImpl = vi.fn(() => BASE_URL);
expect(() =>
getManagedVllmProviderBinding({
getManagedBaseUrlImpl,
recoverManagedClusterVllmEndpointImpl: () => {
throw new Error("managed cluster receipt identity changed");
},
}),
).toThrow("managed cluster receipt identity changed");
expect(getManagedBaseUrlImpl).not.toHaveBeenCalled();
});
it("fails URL and validation boundaries closed when managed recovery is unsafe", () => {
managedClusterRecovery.endpoint.mockImplementation(() => {
throw new Error("managed cluster receipt identity changed");
});
const capture = vi.fn(() => "200");
expect(getLocalProviderBaseUrl("vllm-local")).toBeNull();
expect(getLocalProviderHealthEndpoint("vllm-local")).toBeNull();
expect(getLocalProviderHealthCheck("vllm-local")).toBeNull();
expect(getLocalProviderContainerReachabilityCheck("vllm-local")).toBeNull();
expect(validateLocalProvider("vllm-local", capture)).toEqual({
ok: false,
message:
"Managed vLLM state could not be inspected safely. Re-run `nemoclaw onboard` to repair the provider.",
});
expect(capture).not.toHaveBeenCalled();
expect(lifecycle.baseUrl).not.toHaveBeenCalled();
});
it("stops when managed cluster and Station state are both present", () => {
expect(() =>
getManagedVllmProviderBinding({
loadApiKeyImpl: () => API_KEY,
recoverManagedClusterVllmEndpointImpl: () => ({ baseUrl: BASE_URL, apiKey: API_KEY }),
getManagedBaseUrlImpl: (overrides) => {
overrides?.onManagedHeadObserved?.();
overrides?.loadApiKey?.();
return BASE_URL;
},
}),
).toThrow("Both managed cluster and Station vLLM state are present");
});
it("does not load a stale or unsafe key without a recovered managed endpoint", () => {
const loadApiKeyImpl = vi.fn(() => {
throw new Error(`unsafe ${API_KEY}`);
});
lifecycle.baseUrl.mockReturnValue(null);
expect(getManagedVllmProviderBinding({ loadApiKeyImpl })).toBeNull();
expect(loadApiKeyImpl).not.toHaveBeenCalled();
});
it("keeps legacy health unauthenticated even when stale key loading would fail", () => {
const loadVllmApiKeyImpl = vi.fn(() => {
throw new Error(`unsafe ${API_KEY}`);
});
const runCurlProbeImpl = vi.fn(() => ({
ok: true,
httpStatus: 200,
curlStatus: 0,
body: '{"data":[{"id":"served/model"}]}',
stderr: "",
message: "HTTP 200",
}));
lifecycle.baseUrl.mockReturnValue(null);
const result = probeLocalProviderHealth("vllm-local", {
model: "served/model",
loadVllmApiKeyImpl,
runCurlProbeImpl,
});
expect(result?.ok).toBe(true);
expect(loadVllmApiKeyImpl).not.toHaveBeenCalled();
expect(runCurlProbeImpl).toHaveBeenCalledWith([
"-sS",
"--connect-timeout",
"3",
"--max-time",
"5",
"http://127.0.0.1:8000/v1/models",
]);
});
it("returns one atomic provider endpoint and credential binding", () => {
expect(getManagedVllmProviderBinding({ loadApiKeyImpl: () => API_KEY })).toEqual({
baseUrl: `${BASE_URL}/v1`,
apiKey: API_KEY,
});
expect(getManagedVllmProviderState({ loadApiKeyImpl: () => API_KEY })).toEqual({
kind: "ready",
baseUrl: `${BASE_URL}/v1`,
apiKey: API_KEY,
});
});
it("separates the host-local validation URL from the sandbox route (#8379)", () => {
lifecycle.baseUrl.mockReturnValue(null);
const recoverHostLocalManagedVllmEndpointImpl = vi.fn(() => ({
baseUrl: "http://127.0.0.1:8000",
apiKey: API_KEY,
}));
expect(
getManagedVllmProviderBinding({
loadApiKeyImpl: () => API_KEY,
recoverHostLocalManagedVllmEndpointImpl,
}),
).toEqual({
baseUrl: "http://host.openshell.internal:8000/v1",
validationBaseUrl: "http://127.0.0.1:8000/v1",
apiKey: API_KEY,
});
});
it("pins host-local reachability to the exact OpenShell bridge (#8379)", () => {
lifecycle.baseUrl.mockReturnValue(null);
hostLocalRecovery.endpoint.mockReturnValue({
baseUrl: "http://127.0.0.1:8000",
apiKey: API_KEY,
});
const command = getLocalProviderContainerReachabilityCheck("vllm-local");
expect(command).toContain("host.openshell.internal:172.18.0.1");
expect(command).not.toContain("host.openshell.internal:host-gateway");
expect(command?.at(-1)).toBe("http://host.openshell.internal:8000/health");
expect(managedBridge.host).toHaveBeenCalledOnce();
});
it("pins host-local diagnostics to the exact OpenShell bridge (#8379)", () => {
lifecycle.baseUrl.mockReturnValue(null);
hostLocalRecovery.endpoint.mockReturnValue({
baseUrl: "http://127.0.0.1:8000",
apiKey: API_KEY,
});
const capture = vi.fn(hostLocalDiagnosticCapture);
const result = validateLocalProvider("vllm-local", capture, () => undefined);
const dockerCommands = capture.mock.calls
.map(([argv]) => argv)
.filter((argv) => argv[0] === "docker");
expect(result.ok).toBe(false);
expect(result.diagnostic).toContain("Container curl returned HTTP 503");
expect(result.diagnostic).toContain("host.openshell.internal resolved to: 172.18.0.1");
expect(dockerCommands).toHaveLength(5);
expect(
dockerCommands.every((argv) => argv.includes("host.openshell.internal:172.18.0.1")),
).toBe(true);
expect(
dockerCommands.every((argv) => !argv.includes("host.openshell.internal:host-gateway")),
).toBe(true);
expect(managedBridge.host).toHaveBeenCalledOnce();
});
it("uses /health only for unauthenticated availability checks", () => {
expect(getLocalProviderHealthEndpoint("vllm-local")).toBe(`${BASE_URL}/v1/models`);
expect(getLocalProviderHealthCheck("vllm-local")).toEqual([
"curl",
"-sf",
"--connect-timeout",
"3",
"--max-time",
"5",
"--noproxy",
"*",
"--write-out",
"%{http_code}",
`${BASE_URL}/health`,
]);
expect(getLocalProviderContainerReachabilityCheck("vllm-local")).toEqual([
"docker",
"--context",
"default",
"run",
"--rm",
"--add-host",
"host.openshell.internal:host-gateway",
CONTAINER_REACHABILITY_IMAGE,
"--connect-timeout",
"5",
"--max-time",
"10",
"--noproxy",
"*",
"-sf",
"-w",
"%{http_code}",
`${BASE_URL}/health`,
]);
});
it("pins reachability and diagnostics to the local daemon despite a persisted remote context", () => {
const capture = vi.fn((argv: readonly string[]) => (argv[0] === "curl" ? "200" : ""));
const result = validateLocalProvider("vllm-local", capture, () => undefined);
const dockerCommands = capture.mock.calls
.map(([argv]) => argv)
.filter((argv) => argv[0] === "docker");
expect(result.ok).toBe(false);
// 3 reachability probes, 2 diagnostic re-probes, and the daemon probe of
// the image-pull classifier (#9308) — every one context-pinned.
expect(dockerCommands).toHaveLength(6);
expect(
dockerCommands.every((argv) => argv.slice(0, 3).join(" ") === "docker --context default"),
).toBe(true);
});
it("passes bearer auth through a private curl config and cleans it up", () => {
let configPath = "";
const result = probeVllmModels(`${BASE_URL}/v1`, API_KEY, {
runCurlProbeImpl: (argv, options) => {
expect(argv).not.toContain(API_KEY);
expect(argv.at(-1)).toBe(`${BASE_URL}/v1/models`);
const configIndex = argv.indexOf("--config");
configPath = argv[configIndex + 1] ?? "";
expect(options?.trustedConfigFiles).toEqual([configPath]);
expect(options?.pinnedAddresses).toEqual([]);
expect(fs.readFileSync(configPath, "utf8")).toContain(`Authorization: Bearer ${API_KEY}`);
return {
ok: true,
httpStatus: 200,
curlStatus: 0,
body: '{"data":[{"id":"served/model"}]}',
stderr: "",
message: "HTTP 200",
};
},
});
expect(result.ok).toBe(true);
expect(configPath).not.toBe("");
expect(fs.existsSync(configPath)).toBe(false);
});
it("keeps authenticated model inventory authoritative for configured-model health", () => {
const runCurlProbeImpl = vi.fn((argv: string[]) => ({
ok: true,
httpStatus: 200,
curlStatus: 0,
body: '{"data":[{"id":"different/model"}]}',
stderr: "",
message: "HTTP 200",
}));
const result = probeLocalProviderHealth("vllm-local", {
model: "required/model",
loadVllmApiKeyImpl: () => API_KEY,
runCurlProbeImpl,
});
expect(runCurlProbeImpl).toHaveBeenCalledOnce();
const probeArgv = runCurlProbeImpl.mock.calls[0]?.[0] ?? [];
expect(probeArgv).not.toContain(API_KEY);
expect(result?.ok).toBe(false);
expect(result?.failureLabel).toBe("unhealthy");
expect(result?.detail).toContain("required/model");
expect(result?.detail).toContain("different/model");
});
it("fails closed through production lifecycle recovery when the managed key is absent", () => {
const runCurlProbeImpl = vi.fn();
const result = probeLocalProviderHealth("vllm-local", {
getManagedVllmBaseUrlImpl: productionManagedBaseUrlResolver(),
model: "required/model",
loadVllmApiKeyImpl: () => null,
runCurlProbeImpl,
});
expect(runCurlProbeImpl).not.toHaveBeenCalled();
expect(result?.ok).toBe(false);
expect(result?.failureLabel).toBe("unauthorized");
expect(result?.detail).not.toContain(API_KEY);
});
it("fails closed through production lifecycle recovery when managed key state is unsafe", () => {
const runCurlProbeImpl = vi.fn();
const result = probeLocalProviderHealth("vllm-local", {
getManagedVllmBaseUrlImpl: productionManagedBaseUrlResolver(),
model: "required/model",
loadVllmApiKeyImpl: () => {
throw new Error(`unsafe ${API_KEY}`);
},
runCurlProbeImpl,
});
expect(runCurlProbeImpl).not.toHaveBeenCalled();
expect(result?.ok).toBe(false);
expect(result?.failureLabel).toBe("unhealthy");
expect(result?.detail).not.toContain(API_KEY);
});
it("returns invalid-auth when the private key does not match the managed lifecycle", () => {
expect(
getManagedVllmProviderState({
getManagedBaseUrlImpl: productionManagedBaseUrlResolver(OTHER_API_KEY),
loadApiKeyImpl: () => API_KEY,
}),
).toEqual({ kind: "invalid-auth", reason: "mismatched" });
});
it("fails closed when an owned managed head has invalid auth fingerprint metadata", () => {
const runCurlProbeImpl = vi.fn();
const result = probeLocalProviderHealth("vllm-local", {
getManagedVllmBaseUrlImpl: productionManagedBaseUrlResolver(API_KEY, ""),
loadVllmApiKeyImpl: () => API_KEY,
runCurlProbeImpl,
});
expect(runCurlProbeImpl).not.toHaveBeenCalled();
expect(result).toMatchObject({
ok: false,
endpoint: "managed vLLM",
failureLabel: "unhealthy",
});
});
});