Skip to content

Commit 199e044

Browse files
test(state): verify credential pins through push preflight
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 3eae89a commit 199e044

3 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎src/push.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { checkDriftForUpdate, type DriftCheckResult } from "./drift.ts";
2626
import { deleteBaseline, writeBaseline } from "./hash-store.ts";
2727
import { prepareResourceLinks } from "./link-preflight.ts";
2828
import { NameCollisionReport } from "./name-collisions.ts";
29-
import { computePushPlan, pushBlockingFindings } from "./push-plan.ts";
3029
import { orgPolicyRead } from "./org-policy.ts";
30+
import { computePushPlan, pushBlockingFindings } from "./push-plan.ts";
3131
import { reconcileStateKeyForResource } from "./reconcile-state-key.ts";
3232
import { writeSnapshot } from "./snapshot.ts";
3333
import { mergeScoped } from "./state-merge.ts";

‎tests/credential-pins-cli.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ for (const layout of LAYOUTS)
204204
const state = JSON.parse(
205205
readFileSync(join(dir, layout.stateFile), "utf-8"),
206206
);
207-
assert.deepEqual(state.credentials.elevenlabs, { uuid: id(3) });
207+
assert.deepEqual(state.credentials.elevenlabs, {
208+
uuid: id(3),
209+
origin: "pulled",
210+
});
208211

209212
// A warning by default: validate passes and says what is hidden.
210213
const warned = run(dir, port, "validate-cmd.ts");

‎tests/push-preflight.test.ts‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { fileURLToPath } from "node:url";
1717
import { computePushPlan, pushBlockingFindings } from "../src/push-plan.ts";
1818
import type { LoadedResources, ResourceType, StateFile } from "../src/types.ts";
1919
import { VALID_RESOURCE_TYPES } from "../src/types.ts";
20+
import { validateCredentialPins } from "../src/validate-credential-pins.ts";
2021

2122
process.argv = ["node", "test", "preflight"];
2223
process.env.VAPI_TOKEN = "fake";
@@ -129,18 +130,71 @@ test("plan: squad auto-applied assistant errors block, including existing assist
129130
assert.deepEqual(pushBlockingFindings([], new Set(), false), []);
130131
});
131132

133+
test("credential-pin severity and scope survive push preflight", () => {
134+
const local = loaded({
135+
assistants: {
136+
bot: { credentialIds: ["model"], voice: { provider: "11labs" } },
137+
},
138+
squads: { team: { members: [{ assistantId: "bot" }] } },
139+
});
140+
const tracked = state();
141+
tracked.credentials = { model: { uuid: UUID }, voice: { uuid: "voice-id" } };
142+
const kinds = {
143+
model: { provider: "openai" },
144+
voice: { provider: "11labs" },
145+
};
146+
for (const customerKeysOnly of [false, true]) {
147+
for (const recorded of [kinds, undefined]) {
148+
const findings = validateCredentialPins({
149+
loaded: local,
150+
org: "preflight",
151+
state: tracked,
152+
kinds: recorded,
153+
kindsFile: "state/preflight/credential-providers.json",
154+
customerKeysOnly,
155+
});
156+
assert.equal(findings.length, recorded ? 2 : 1);
157+
assert.ok(
158+
findings.every(
159+
(finding) =>
160+
finding.rule ===
161+
(recorded
162+
? "credential-pin-hides-key"
163+
: "credential-pin-unchecked"),
164+
),
165+
);
166+
const plan = computePushPlan(
167+
{ ...loaded(), squads: local.squads },
168+
local,
169+
tracked,
170+
);
171+
assert.equal(
172+
pushBlockingFindings(findings, plan, false).length,
173+
customerKeysOnly ? findings.length : 0,
174+
);
175+
assert.deepEqual(pushBlockingFindings(findings, new Set(), false), []);
176+
assert.equal(
177+
pushBlockingFindings(findings, new Set(), true).length,
178+
customerKeysOnly ? findings.length : 0,
179+
);
180+
}
181+
}
182+
assert.deepEqual(pushBlockingFindings([], new Set(), true), []);
183+
});
184+
132185
async function runFixture(options: {
133186
scoped?: boolean;
134187
dryRun?: boolean;
135188
credential?: boolean;
136189
strict?: boolean;
190+
credentialPin?: boolean;
137191
}) {
138192
const dir = mkdtempSync(join(tmpdir(), "push-preflight-"));
139193
const mutations: string[] = [];
140194
const good = { id: UUID, name: "Good" };
141195
const bad = {
142196
id: "b0000000-0000-4000-8000-000000000002",
143-
name: options.credential ? "Bad" : BAD,
197+
name: options.credential || options.credentialPin ? "Bad" : BAD,
144198
...(options.credential
145199
? { server: { url: "https://example.com", credentialId: "missing" } }
146200
: {}),
@@ -173,11 +227,25 @@ async function runFixture(options: {
173227
writeFileSync(join(folder, "good.yml"), "name: Good\n");
174228
writeFileSync(
175229
join(folder, "zbad.yml"),
176-
`name: ${bad.name}\n${options.credential ? "server:\n url: https://example.com\n credentialId: missing\n" : ""}`,
230+
`name: ${bad.name}\n${options.credential ? "server:\n url: https://example.com\n credentialId: missing\n" : ""}${options.credentialPin ? "credentialIds:\n - unused\nvoice:\n provider: 11labs\n voiceId: sarah\n" : ""}`,
177231
);
178232
const initial = state();
179233
initial.assistants.good = { uuid: good.id };
180234
initial.assistants.zbad = { uuid: bad.id };
235+
if (options.credentialPin) {
236+
initial.credentials.voice = { uuid: "voice-id" };
237+
writeFileSync(
238+
join(dir, "resources/preflight/.vapi-policy.yml"),
239+
"customerKeysOnly: true\n",
240+
);
241+
writeFileSync(
242+
join(dir, "resources/preflight/.vapi-credential-providers.json"),
243+
JSON.stringify({
244+
unused: { provider: "openai" },
245+
voice: { provider: "11labs" },
246+
}),
247+
);
248+
}
181249
writeFileSync(
182250
join(dir, ".vapi-state.preflight.json"),
183251
JSON.stringify(initial),
@@ -225,6 +293,12 @@ for (const [label, options] of [
225293
["dry-run", { dryRun: true }],
226294
["unresolved credential", { credential: true }],
227295
["strict scoped push", { scoped: true, strict: true }],
296+
["credential pin", { credentialPin: true }],
297+
["credential pin dry-run", { credentialPin: true, dryRun: true }],
298+
[
299+
"credential pin strict scoped push",
300+
{ credentialPin: true, scoped: true, strict: true },
301+
],
228302
] as const) {
229303
test(`preflight blocks ${label} before any mutation`, async () => {
230304
const result = await runFixture(options);
@@ -233,6 +307,8 @@ for (const [label, options] of [
233307
assert.deepEqual(result.mutations, []);
234308
if ("credential" in options)
235309
assert.match(result.output, /unresolved-credential/);
310+
if ("credentialPin" in options)
311+
assert.match(result.output, /credential-pin-hides-key/);
236312
});
237313
}
238314

0 commit comments

Comments
 (0)