fix(backend): inject provider credentials from file
This commit is contained in:
@@ -46,3 +46,12 @@ test("loadConfig accepts an authenticated upstream trust boundary", () => {
|
||||
AUTH_MODE: "upstream",
|
||||
}).authMode).toBe("upstream");
|
||||
});
|
||||
|
||||
test("loadConfig accepts only an absolute generic model key file", () => {
|
||||
expect(loadConfig({ THT_MODEL_API_KEY_FILE: "/run/secrets/model_api_key" }).modelApiKeyFile)
|
||||
.toBe("/run/secrets/model_api_key");
|
||||
expect(() => loadConfig({ THT_MODEL_API_KEY_FILE: "relative/key" }))
|
||||
.toThrow(/model credential configuration is invalid/);
|
||||
expect(() => loadConfig({ THT_MODEL_API_KEY_FILE: " /run/secrets/key" }))
|
||||
.toThrow(/model credential configuration is invalid/);
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import { spawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { chmodSync, writeFileSync } from "node:fs";
|
||||
import { PiProcessManager } from "../src/pi/pi-process-manager.js";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
|
||||
@@ -149,12 +150,12 @@ test("production spawn uses explicit Pi path and passes portable data root witho
|
||||
expect(options.cwd).toBe("/app/harness");
|
||||
expect(options.env).toMatchObject({
|
||||
PATH: "/usr/local/bin:/usr/bin",
|
||||
PI_PROVIDER_API_KEY: "provider-secret",
|
||||
NODE_EXTRA_CA_CERTS: "/certs/company-ca.pem",
|
||||
THT_DATA_ROOT: "/data",
|
||||
THT_SESSION: "portable-session",
|
||||
THT_AUTHOR: "user@example.test",
|
||||
});
|
||||
expect(options.env).not.toHaveProperty("PI_PROVIDER_API_KEY");
|
||||
} finally {
|
||||
mgr.teardown("portable-session");
|
||||
vi.unstubAllEnvs();
|
||||
@@ -174,9 +175,100 @@ test("session Pi spawn omits ambient THT_DATA_ROOT when config does not provide
|
||||
try {
|
||||
await mgr.spawnFor("no-data-root", {});
|
||||
expect(calls[0][2].env).not.toHaveProperty("THT_DATA_ROOT");
|
||||
expect(calls[0][2].env.PI_PROVIDER_API_KEY).toBe("still-inherited");
|
||||
expect(calls[0][2].env).not.toHaveProperty("PI_PROVIDER_API_KEY");
|
||||
} finally {
|
||||
mgr.teardown("no-data-root");
|
||||
vi.unstubAllEnvs();
|
||||
}
|
||||
});
|
||||
|
||||
test.each([
|
||||
["anthropic", "ANTHROPIC_API_KEY"],
|
||||
["OpenAI", "OPENAI_API_KEY"],
|
||||
["gemini", "GEMINI_API_KEY"],
|
||||
["google", "GEMINI_API_KEY"],
|
||||
["deepseek", "DEEPSEEK_API_KEY"],
|
||||
["zai", "ZAI_API_KEY"],
|
||||
["openrouter", "OPENROUTER_API_KEY"],
|
||||
])("injects the generic file credential only as %s provider env", async (provider, expectedName) => {
|
||||
const secret = path.resolve(__dirname, `.model-key-${process.pid}-${provider}`);
|
||||
writeFileSync(secret, "provider-secret", { mode: 0o600 });
|
||||
const calls: any[][] = [];
|
||||
const child = recordingChild();
|
||||
child.stderr.resume = () => {};
|
||||
const mgr = new PiProcessManager(loadConfig({
|
||||
PI_BIN: "/usr/local/bin/pi", THT_MODEL_API_KEY_FILE: secret,
|
||||
}), { spawnFn: (...args: any[]) => { calls.push(args); return child as any; } });
|
||||
try {
|
||||
await mgr.spawnFor("credential-session", { provider });
|
||||
const env = calls[0][2].env;
|
||||
expect(env[expectedName]).toBe("provider-secret");
|
||||
expect(env).not.toHaveProperty("PI_PROVIDER_API_KEY");
|
||||
expect(env).not.toHaveProperty("THT_MODEL_API_KEY_FILE");
|
||||
expect(JSON.stringify(calls[0].slice(0, 2))).not.toContain("provider-secret");
|
||||
} finally {
|
||||
mgr.teardown("credential-session");
|
||||
await import("node:fs/promises").then((fs) => fs.unlink(secret));
|
||||
}
|
||||
});
|
||||
|
||||
test("local providers spawn without a model key and scrub ambient generic credentials", async () => {
|
||||
vi.stubEnv("PI_PROVIDER_API_KEY", "ambient-secret");
|
||||
vi.stubEnv("THT_MODEL_API_KEY_FILE", "/ambient/secret-path");
|
||||
vi.stubEnv("OPENAI_API_KEY", "unselected-provider-secret");
|
||||
const calls: any[][] = [];
|
||||
const child = recordingChild();
|
||||
child.stderr.resume = () => {};
|
||||
const mgr = new PiProcessManager(loadConfig({ PI_BIN: "/usr/local/bin/pi" }), {
|
||||
spawnFn: (...args: any[]) => { calls.push(args); return child as any; },
|
||||
});
|
||||
try {
|
||||
await mgr.spawnFor("local-session", { provider: "ollama" });
|
||||
expect(calls[0][2].env).not.toHaveProperty("PI_PROVIDER_API_KEY");
|
||||
expect(calls[0][2].env).not.toHaveProperty("THT_MODEL_API_KEY_FILE");
|
||||
expect(calls[0][2].env).not.toHaveProperty("OPENAI_API_KEY");
|
||||
} finally {
|
||||
mgr.teardown("local-session");
|
||||
vi.unstubAllEnvs();
|
||||
}
|
||||
});
|
||||
|
||||
test.each(["missing", "permissive", "unreadable", "directory", "symlink", "unsupported"])(
|
||||
"hosted provider credential failure is sanitized: %s", async (kind) => {
|
||||
const target = path.resolve(__dirname, `.bad-model-key-${process.pid}-${kind}`);
|
||||
if (kind === "permissive") {
|
||||
writeFileSync(target, "DO_NOT_LEAK", { mode: 0o644 });
|
||||
chmodSync(target, 0o644);
|
||||
} else if (kind === "unreadable") {
|
||||
writeFileSync(target, "DO_NOT_LEAK", { mode: 0o000 });
|
||||
} else if (kind === "directory") {
|
||||
await import("node:fs/promises").then((fs) => fs.mkdir(target));
|
||||
} else if (kind === "symlink") {
|
||||
const source = `${target}-source`;
|
||||
writeFileSync(source, "DO_NOT_LEAK", { mode: 0o600 });
|
||||
await import("node:fs/promises").then((fs) => fs.symlink(source, target));
|
||||
}
|
||||
const cfg = loadConfig({ THT_MODEL_API_KEY_FILE: target });
|
||||
const mgr = new PiProcessManager(cfg, { spawnFn: () => {
|
||||
throw new Error("spawn must not occur");
|
||||
} });
|
||||
try {
|
||||
const provider = kind === "unsupported" ? "unknown-hosted" : "anthropic";
|
||||
await expect(mgr.spawnFor("bad-secret", { provider }))
|
||||
.rejects.toThrow("model provider credential is unavailable");
|
||||
} finally {
|
||||
if (kind === "permissive") await import("node:fs/promises").then((fs) => fs.unlink(target));
|
||||
if (kind === "unreadable") {
|
||||
chmodSync(target, 0o600);
|
||||
await import("node:fs/promises").then((fs) => fs.unlink(target));
|
||||
}
|
||||
if (kind === "directory") await import("node:fs/promises").then((fs) => fs.rmdir(target));
|
||||
if (kind === "symlink") {
|
||||
await import("node:fs/promises").then(async (fs) => {
|
||||
await fs.unlink(target);
|
||||
await fs.unlink(`${target}-source`);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { test, expect } from "vitest";
|
||||
import { spawn as nodeSpawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import os from "node:os";
|
||||
import { chmodSync, unlinkSync, writeFileSync } from "node:fs";
|
||||
import { buildApp } from "../src/app.js";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
|
||||
@@ -16,9 +18,14 @@ function mutApp(thtRunner: any) {
|
||||
}
|
||||
|
||||
test("POST /sessions usa i settings (workspace/provider/model/thinking) e crea+avvia", async () => {
|
||||
const modelKey = path.join(os.tmpdir(), `thoth-model-key-${process.pid}`);
|
||||
writeFileSync(modelKey, "test-model-key", { mode: 0o600 });
|
||||
chmodSync(modelKey, 0o600);
|
||||
let sessionNewArg: any;
|
||||
let spawnArg: any;
|
||||
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
const app = buildApp(loadConfig({
|
||||
THT_HARNESS_DIR: "../harness", THT_MODEL_API_KEY_FILE: modelKey,
|
||||
}), {
|
||||
thtRunner: {
|
||||
ollamaEnsure: async () => ({ ok: true }),
|
||||
sessionNew: async (o: any) => { sessionNewArg = o; return { id: "s1" }; },
|
||||
@@ -36,6 +43,7 @@ test("POST /sessions usa i settings (workspace/provider/model/thinking) e crea+a
|
||||
expect(sessionNewArg.question).toBe("q");
|
||||
const list = await app.inject({ method: "GET", url: "/sessions" });
|
||||
expect(list.json()).toEqual([{ id: "s1" }]);
|
||||
unlinkSync(modelKey);
|
||||
});
|
||||
|
||||
test("POST /sessions/:id/response inoltra al bridge (no error)", async () => {
|
||||
|
||||
Reference in New Issue
Block a user