mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 04:51:25 +00:00
Onboard OpenAI: explicit secret-input-mode behavior
This commit is contained in:
committed by
Peter Steinberger
parent
e8d1725187
commit
2ef109f00a
@@ -26,13 +26,13 @@ describe("applyAuthChoiceOpenAI", () => {
|
|||||||
await lifecycle.cleanup();
|
await lifecycle.cleanup();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("writes env-backed OpenAI key as keyRef in auth profiles", async () => {
|
it("writes env-backed OpenAI key as plaintext by default", async () => {
|
||||||
const agentDir = await setupTempState();
|
const agentDir = await setupTempState();
|
||||||
process.env.OPENAI_API_KEY = "sk-openai-env";
|
process.env.OPENAI_API_KEY = "sk-openai-env";
|
||||||
|
|
||||||
const confirm = vi.fn(async () => true);
|
const confirm = vi.fn(async () => true);
|
||||||
const text = vi.fn(async () => "unused");
|
const text = vi.fn(async () => "unused");
|
||||||
const prompter = createWizardPrompter({ confirm, text }, { defaultSelect: "" });
|
const prompter = createWizardPrompter({ confirm, text }, { defaultSelect: "plaintext" });
|
||||||
const runtime = createExitThrowingRuntime();
|
const runtime = createExitThrowingRuntime();
|
||||||
|
|
||||||
const result = await applyAuthChoiceOpenAI({
|
const result = await applyAuthChoiceOpenAI({
|
||||||
@@ -53,6 +53,31 @@ describe("applyAuthChoiceOpenAI", () => {
|
|||||||
expect(primaryModel).toBe("openai/gpt-5.1-codex");
|
expect(primaryModel).toBe("openai/gpt-5.1-codex");
|
||||||
expect(text).not.toHaveBeenCalled();
|
expect(text).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
const parsed = await readAuthProfilesForAgent<{
|
||||||
|
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||||
|
}>(agentDir);
|
||||||
|
expect(parsed.profiles?.["openai:default"]?.key).toBe("sk-openai-env");
|
||||||
|
expect(parsed.profiles?.["openai:default"]?.keyRef).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("writes env-backed OpenAI key as keyRef when secret-input-mode=ref", async () => {
|
||||||
|
const agentDir = await setupTempState();
|
||||||
|
process.env.OPENAI_API_KEY = "sk-openai-env";
|
||||||
|
|
||||||
|
const confirm = vi.fn(async () => true);
|
||||||
|
const text = vi.fn(async () => "unused");
|
||||||
|
const prompter = createWizardPrompter({ confirm, text }, { defaultSelect: "ref" });
|
||||||
|
const runtime = createExitThrowingRuntime();
|
||||||
|
|
||||||
|
const result = await applyAuthChoiceOpenAI({
|
||||||
|
authChoice: "openai-api-key",
|
||||||
|
config: {},
|
||||||
|
prompter,
|
||||||
|
runtime,
|
||||||
|
setDefaultModel: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).not.toBeNull();
|
||||||
const parsed = await readAuthProfilesForAgent<{
|
const parsed = await readAuthProfilesForAgent<{
|
||||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||||
}>(agentDir);
|
}>(agentDir);
|
||||||
|
|||||||
@@ -4,7 +4,11 @@ import {
|
|||||||
normalizeApiKeyInput,
|
normalizeApiKeyInput,
|
||||||
validateApiKeyInput,
|
validateApiKeyInput,
|
||||||
} from "./auth-choice.api-key.js";
|
} from "./auth-choice.api-key.js";
|
||||||
import { createAuthChoiceAgentModelNoter } from "./auth-choice.apply-helpers.js";
|
import {
|
||||||
|
createAuthChoiceAgentModelNoter,
|
||||||
|
normalizeSecretInputModeInput,
|
||||||
|
resolveSecretInputModeForEnvSelection,
|
||||||
|
} from "./auth-choice.apply-helpers.js";
|
||||||
import type { ApplyAuthChoiceParams, ApplyAuthChoiceResult } from "./auth-choice.apply.js";
|
import type { ApplyAuthChoiceParams, ApplyAuthChoiceResult } from "./auth-choice.apply.js";
|
||||||
import { applyDefaultModelChoice } from "./auth-choice.default-model.js";
|
import { applyDefaultModelChoice } from "./auth-choice.default-model.js";
|
||||||
import { isRemoteEnvironment } from "./oauth-env.js";
|
import { isRemoteEnvironment } from "./oauth-env.js";
|
||||||
@@ -24,6 +28,7 @@ import {
|
|||||||
export async function applyAuthChoiceOpenAI(
|
export async function applyAuthChoiceOpenAI(
|
||||||
params: ApplyAuthChoiceParams,
|
params: ApplyAuthChoiceParams,
|
||||||
): Promise<ApplyAuthChoiceResult | null> {
|
): Promise<ApplyAuthChoiceResult | null> {
|
||||||
|
const requestedSecretInputMode = normalizeSecretInputModeInput(params.opts?.secretInputMode);
|
||||||
const noteAgentModel = createAuthChoiceAgentModelNoter(params);
|
const noteAgentModel = createAuthChoiceAgentModelNoter(params);
|
||||||
let authChoice = params.authChoice;
|
let authChoice = params.authChoice;
|
||||||
if (authChoice === "apiKey" && params.opts?.tokenProvider === "openai") {
|
if (authChoice === "apiKey" && params.opts?.tokenProvider === "openai") {
|
||||||
@@ -57,7 +62,11 @@ export async function applyAuthChoiceOpenAI(
|
|||||||
initialValue: true,
|
initialValue: true,
|
||||||
});
|
});
|
||||||
if (useExisting) {
|
if (useExisting) {
|
||||||
await setOpenaiApiKey(envKey.apiKey, params.agentDir);
|
const mode = await resolveSecretInputModeForEnvSelection({
|
||||||
|
prompter: params.prompter,
|
||||||
|
explicitMode: requestedSecretInputMode,
|
||||||
|
});
|
||||||
|
await setOpenaiApiKey(envKey.apiKey, params.agentDir, { secretInputMode: mode });
|
||||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||||
profileId: "openai:default",
|
profileId: "openai:default",
|
||||||
provider: "openai",
|
provider: "openai",
|
||||||
@@ -78,7 +87,9 @@ export async function applyAuthChoiceOpenAI(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const trimmed = normalizeApiKeyInput(String(key));
|
const trimmed = normalizeApiKeyInput(String(key));
|
||||||
await setOpenaiApiKey(trimmed, params.agentDir);
|
await setOpenaiApiKey(trimmed, params.agentDir, {
|
||||||
|
secretInputMode: requestedSecretInputMode,
|
||||||
|
});
|
||||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||||
profileId: "openai:default",
|
profileId: "openai:default",
|
||||||
provider: "openai",
|
provider: "openai",
|
||||||
|
|||||||
@@ -106,13 +106,29 @@ describe("onboard auth credentials secret refs", () => {
|
|||||||
expect(parsed.profiles?.["cloudflare-ai-gateway:default"]?.key).toBeUndefined();
|
expect(parsed.profiles?.["cloudflare-ai-gateway:default"]?.key).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("stores env-backed openai key as keyRef", async () => {
|
it("keeps env-backed openai key as plaintext by default", async () => {
|
||||||
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-openai-");
|
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-openai-");
|
||||||
lifecycle.setStateDir(env.stateDir);
|
lifecycle.setStateDir(env.stateDir);
|
||||||
process.env.OPENAI_API_KEY = "sk-openai-env";
|
process.env.OPENAI_API_KEY = "sk-openai-env";
|
||||||
|
|
||||||
await setOpenaiApiKey("sk-openai-env");
|
await setOpenaiApiKey("sk-openai-env");
|
||||||
|
|
||||||
|
const parsed = await readAuthProfilesForAgent<{
|
||||||
|
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||||
|
}>(env.agentDir);
|
||||||
|
expect(parsed.profiles?.["openai:default"]).toMatchObject({
|
||||||
|
key: "sk-openai-env",
|
||||||
|
});
|
||||||
|
expect(parsed.profiles?.["openai:default"]?.keyRef).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stores env-backed openai key as keyRef in ref mode", async () => {
|
||||||
|
const env = await setupAuthTestEnv("openclaw-onboard-auth-credentials-openai-ref-");
|
||||||
|
lifecycle.setStateDir(env.stateDir);
|
||||||
|
process.env.OPENAI_API_KEY = "sk-openai-env";
|
||||||
|
|
||||||
|
await setOpenaiApiKey("sk-openai-env", env.agentDir, { secretInputMode: "ref" });
|
||||||
|
|
||||||
const parsed = await readAuthProfilesForAgent<{
|
const parsed = await readAuthProfilesForAgent<{
|
||||||
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
profiles?: Record<string, { key?: string; keyRef?: unknown }>;
|
||||||
}>(env.agentDir);
|
}>(env.agentDir);
|
||||||
|
|||||||
Reference in New Issue
Block a user