|
2 | 2 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 3 | import type { OpenClawConfig } from "../../../config/config.js"; |
4 | 4 | import { resolveAgentModelPrimaryValue } from "../../../config/model-input.js"; |
| 5 | +import { commitNonInteractiveOnboardConfig } from "../config-write.js"; |
5 | 6 | import { applyNonInteractiveAuthChoice } from "./auth-choice.js"; |
6 | 7 |
|
| 8 | +const writeWizardConfigFile = vi.hoisted(() => vi.fn(async (config: OpenClawConfig) => config)); |
| 9 | +vi.mock("../../../wizard/setup.shared.js", () => ({ writeWizardConfigFile })); |
| 10 | + |
7 | 11 | const formatAuthChoiceChoicesForCli = vi.hoisted(() => |
8 | 12 | vi.fn(() => "custom-api-key|skip|demo-provider-api-key"), |
9 | 13 | ); |
@@ -234,6 +238,252 @@ describe("applyNonInteractiveAuthChoice", () => { |
234 | 238 | expect(apiKeyParams?.secretInputMode).toBe("ref"); |
235 | 239 | }); |
236 | 240 |
|
| 241 | + it("never commits an existing profile key as plaintext during custom secret-ref onboarding", async () => { |
| 242 | + const runtime = createRuntime(); |
| 243 | + const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
| 244 | + const profileKey = "fixture-custom-profile-secret"; |
| 245 | + resolveNonInteractiveApiKey.mockResolvedValueOnce({ key: profileKey, source: "profile" }); |
| 246 | + |
| 247 | + const result = await applyNonInteractiveAuthChoice({ |
| 248 | + nextConfig, |
| 249 | + authChoice: "custom-api-key", |
| 250 | + opts: { |
| 251 | + customBaseUrl: "https://models.custom.local/v1", |
| 252 | + customModelId: "local-large", |
| 253 | + secretInputMode: "ref", |
| 254 | + } as never, |
| 255 | + runtime: runtime as never, |
| 256 | + baseConfig: nextConfig, |
| 257 | + target, |
| 258 | + }); |
| 259 | + |
| 260 | + expect(result).not.toBeNull(); |
| 261 | + await commitNonInteractiveOnboardConfig({ |
| 262 | + nextConfig: result!, |
| 263 | + baseConfig: nextConfig, |
| 264 | + }); |
| 265 | + |
| 266 | + const persistedConfig = writeWizardConfigFile.mock.calls.at(-1)?.[0]; |
| 267 | + expect( |
| 268 | + persistedConfig?.models?.providers?.["custom-models-custom-local"]?.apiKey, |
| 269 | + ).toBeUndefined(); |
| 270 | + expect(JSON.stringify(persistedConfig)).not.toContain(profileKey); |
| 271 | + expect(runtime.error).not.toHaveBeenCalled(); |
| 272 | + expect(runtime.exit).not.toHaveBeenCalled(); |
| 273 | + }); |
| 274 | + |
| 275 | + it.each([ |
| 276 | + { source: "flag", key: "fixture-custom-literal-secret" }, |
| 277 | + { source: "env", key: "fixture-custom-anonymous-env-secret" }, |
| 278 | + ] as const)( |
| 279 | + "never serializes an unreferenceable custom $source key in secret-ref mode", |
| 280 | + async (resolved) => { |
| 281 | + const runtime = createRuntime(); |
| 282 | + const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
| 283 | + resolveNonInteractiveApiKey.mockResolvedValueOnce(resolved); |
| 284 | + |
| 285 | + const result = await applyNonInteractiveAuthChoice({ |
| 286 | + nextConfig, |
| 287 | + authChoice: "custom-api-key", |
| 288 | + opts: { |
| 289 | + customBaseUrl: "https://models.custom.local/v1", |
| 290 | + customModelId: "local-large", |
| 291 | + secretInputMode: "ref", |
| 292 | + } as never, |
| 293 | + runtime: runtime as never, |
| 294 | + baseConfig: nextConfig, |
| 295 | + target, |
| 296 | + }); |
| 297 | + |
| 298 | + expect(result).toBeNull(); |
| 299 | + expect(writeWizardConfigFile).not.toHaveBeenCalled(); |
| 300 | + expect(runtime.exit).toHaveBeenCalledWith(1); |
| 301 | + const errorText = runtime.error.mock.calls.map(([message]) => String(message)).join("\n"); |
| 302 | + expect(errorText).toContain("CUSTOM_API_KEY"); |
| 303 | + expect(errorText).toContain("--secret-input-mode ref"); |
| 304 | + expect(errorText).not.toContain(resolved.key); |
| 305 | + }, |
| 306 | + ); |
| 307 | + |
| 308 | + it.each([ |
| 309 | + { source: "env", provider: "default", id: "EXISTING_CUSTOM_API_KEY" }, |
| 310 | + { source: "file", provider: "local", id: "/providers/custom" }, |
| 311 | + { source: "exec", provider: "vault", id: "custom-provider" }, |
| 312 | + ] as const)( |
| 313 | + "preserves existing $source custom SecretRefs when reusing an auth profile", |
| 314 | + async (ref) => { |
| 315 | + const runtime = createRuntime(); |
| 316 | + const providerId = "custom-models-custom-local"; |
| 317 | + const nextConfig = { |
| 318 | + models: { |
| 319 | + providers: { |
| 320 | + [providerId]: { |
| 321 | + baseUrl: "https://models.custom.local/v1", |
| 322 | + apiKey: ref, |
| 323 | + models: [], |
| 324 | + }, |
| 325 | + }, |
| 326 | + }, |
| 327 | + } as OpenClawConfig; |
| 328 | + resolveNonInteractiveApiKey.mockResolvedValueOnce({ |
| 329 | + key: "fixture-existing-profile-secret", |
| 330 | + source: "profile", |
| 331 | + }); |
| 332 | + |
| 333 | + const result = await applyNonInteractiveAuthChoice({ |
| 334 | + nextConfig, |
| 335 | + authChoice: "custom-api-key", |
| 336 | + opts: { |
| 337 | + customBaseUrl: "https://models.custom.local/v1", |
| 338 | + customModelId: "local-large", |
| 339 | + secretInputMode: "ref", |
| 340 | + } as never, |
| 341 | + runtime: runtime as never, |
| 342 | + baseConfig: nextConfig, |
| 343 | + target, |
| 344 | + }); |
| 345 | + |
| 346 | + expect(result?.models?.providers?.[providerId]?.apiKey).toEqual(ref); |
| 347 | + expect(runtime.error).not.toHaveBeenCalled(); |
| 348 | + }, |
| 349 | + ); |
| 350 | + |
| 351 | + it("preserves intentionally keyless custom setup in secret-ref mode", async () => { |
| 352 | + const runtime = createRuntime(); |
| 353 | + const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
| 354 | + resolveNonInteractiveApiKey.mockResolvedValueOnce(null); |
| 355 | + |
| 356 | + const result = await applyNonInteractiveAuthChoice({ |
| 357 | + nextConfig, |
| 358 | + authChoice: "custom-api-key", |
| 359 | + opts: { |
| 360 | + customBaseUrl: "https://models.custom.local/v1", |
| 361 | + customModelId: "local-large", |
| 362 | + secretInputMode: "ref", |
| 363 | + } as never, |
| 364 | + runtime: runtime as never, |
| 365 | + baseConfig: nextConfig, |
| 366 | + target, |
| 367 | + }); |
| 368 | + |
| 369 | + expect(result?.models?.providers?.["custom-models-custom-local"]?.apiKey).toBeUndefined(); |
| 370 | + expect(runtime.error).not.toHaveBeenCalled(); |
| 371 | + expect(runtime.exit).not.toHaveBeenCalled(); |
| 372 | + }); |
| 373 | + |
| 374 | + it("preserves existing custom profile serialization in explicit plaintext mode", async () => { |
| 375 | + const runtime = createRuntime(); |
| 376 | + const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
| 377 | + resolveNonInteractiveApiKey.mockResolvedValueOnce({ |
| 378 | + key: "fixture-plaintext-profile-key", |
| 379 | + source: "profile", |
| 380 | + }); |
| 381 | + |
| 382 | + const result = await applyNonInteractiveAuthChoice({ |
| 383 | + nextConfig, |
| 384 | + authChoice: "custom-api-key", |
| 385 | + opts: { |
| 386 | + customBaseUrl: "https://models.custom.local/v1", |
| 387 | + customModelId: "local-large", |
| 388 | + secretInputMode: "plaintext", |
| 389 | + } as never, |
| 390 | + runtime: runtime as never, |
| 391 | + baseConfig: nextConfig, |
| 392 | + target, |
| 393 | + }); |
| 394 | + |
| 395 | + expect(result?.models?.providers?.["custom-models-custom-local"]?.apiKey).toBe( |
| 396 | + "fixture-plaintext-profile-key", |
| 397 | + ); |
| 398 | + }); |
| 399 | + |
| 400 | + it.each([ |
| 401 | + { source: "profile", key: "fixture-plugin-profile-secret" }, |
| 402 | + { source: "flag", key: "fixture-plugin-literal-secret" }, |
| 403 | + { source: "env", key: "fixture-plugin-env-secret" }, |
| 404 | + ] as const)( |
| 405 | + "rejects non-referenceable $source plugin credentials in secret-ref mode", |
| 406 | + async (resolved) => { |
| 407 | + const runtime = createRuntime(); |
| 408 | + const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
| 409 | + applyNonInteractivePluginProviderChoice.mockResolvedValueOnce(nextConfig as never); |
| 410 | + |
| 411 | + await applyNonInteractiveAuthChoice({ |
| 412 | + nextConfig, |
| 413 | + authChoice: "demo-provider-api-key", |
| 414 | + opts: { secretInputMode: "ref" } as never, |
| 415 | + runtime: runtime as never, |
| 416 | + baseConfig: nextConfig, |
| 417 | + target, |
| 418 | + }); |
| 419 | + |
| 420 | + const [pluginParams] = applyNonInteractivePluginProviderChoice.mock.calls.at( |
| 421 | + -1, |
| 422 | + ) as unknown as [ |
| 423 | + { |
| 424 | + toApiKeyCredential: (params: { provider: string; resolved: typeof resolved }) => unknown; |
| 425 | + }, |
| 426 | + ]; |
| 427 | + const credential = pluginParams.toApiKeyCredential({ |
| 428 | + provider: "demo-provider", |
| 429 | + resolved, |
| 430 | + }); |
| 431 | + |
| 432 | + expect(credential).toBeNull(); |
| 433 | + expect(runtime.exit).toHaveBeenCalledWith(1); |
| 434 | + const errorText = runtime.error.mock.calls.map(([message]) => String(message)).join("\n"); |
| 435 | + expect(errorText).toContain("--secret-input-mode ref"); |
| 436 | + expect(errorText).toContain("demo-provider"); |
| 437 | + expect(errorText).not.toContain(resolved.key); |
| 438 | + }, |
| 439 | + ); |
| 440 | + |
| 441 | + it("preserves env-backed plugin credentials and profile metadata in secret-ref mode", async () => { |
| 442 | + const runtime = createRuntime(); |
| 443 | + const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
| 444 | + applyNonInteractivePluginProviderChoice.mockResolvedValueOnce(nextConfig as never); |
| 445 | + |
| 446 | + await applyNonInteractiveAuthChoice({ |
| 447 | + nextConfig, |
| 448 | + authChoice: "demo-provider-api-key", |
| 449 | + opts: { secretInputMode: "ref" } as never, |
| 450 | + runtime: runtime as never, |
| 451 | + baseConfig: nextConfig, |
| 452 | + target, |
| 453 | + }); |
| 454 | + |
| 455 | + const [pluginParams] = applyNonInteractivePluginProviderChoice.mock.calls.at(-1) as unknown as [ |
| 456 | + { |
| 457 | + toApiKeyCredential: (params: { |
| 458 | + provider: string; |
| 459 | + resolved: { key: string; source: "env"; envVarName: string }; |
| 460 | + email: string; |
| 461 | + metadata: Record<string, string>; |
| 462 | + }) => unknown; |
| 463 | + }, |
| 464 | + ]; |
| 465 | + expect( |
| 466 | + pluginParams.toApiKeyCredential({ |
| 467 | + provider: "demo-provider", |
| 468 | + resolved: { |
| 469 | + key: "fixture-valid-plugin-env-secret", |
| 470 | + source: "env", |
| 471 | + envVarName: "DEMO_PROVIDER_API_KEY", |
| 472 | + }, |
| 473 | + |
| 474 | + metadata: { account: "work" }, |
| 475 | + }), |
| 476 | + ).toEqual({ |
| 477 | + type: "api_key", |
| 478 | + provider: "demo-provider", |
| 479 | + keyRef: { source: "env", provider: "default", id: "DEMO_PROVIDER_API_KEY" }, |
| 480 | + |
| 481 | + metadata: { account: "work" }, |
| 482 | + }); |
| 483 | + expect(runtime.error).not.toHaveBeenCalled(); |
| 484 | + expect(runtime.exit).not.toHaveBeenCalled(); |
| 485 | + }); |
| 486 | + |
237 | 487 | it("stores custom provider OpenAI Responses compatibility", async () => { |
238 | 488 | const runtime = createRuntime(); |
239 | 489 | const nextConfig = { agents: { defaults: {} } } as OpenClawConfig; |
|
0 commit comments