@vellumai/assistant 0.11.7-dev.202608310050.dde6a86 → 0.11.7-dev.202608311418.27c51fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +1 -1
- package/docs/architecture/security.md +1 -1
- package/node_modules/@vellumai/credential-storage/src/static-credentials.ts +43 -12
- package/package.json +1 -1
- package/src/__tests__/credential-record-retire.test.ts +256 -0
- package/src/__tests__/credential-record-write-through.test.ts +7 -0
- package/src/runtime/migrations/vbundle-metadata-merge.ts +4 -7
- package/src/tools/credentials/metadata-store.ts +109 -10
package/ARCHITECTURE.md
CHANGED
|
@@ -2066,7 +2066,7 @@ Connected channels are resolved at signal emission time: vellum is always includ
|
|
|
2066
2066
|
| ---------------------------------------- | ------------------------------------------------------ | ----------------------------------- | ---------------------------------- | ------------------------------------------------------- |
|
|
2067
2067
|
| API key | CES / encrypted file store | Encrypted binary | CES API / `secure-keys.ts` | Permanent |
|
|
2068
2068
|
| Credential secrets | CES / encrypted file store | Encrypted binary | `secure-keys.ts` wrapper | Permanent (until deleted via tool) |
|
|
2069
|
-
| Credential
|
|
2069
|
+
| Credential records | CES `<cesDataRoot>/metadata.json` | JSON | CES RPC/HTTP + atomic file write | Permanent (until deleted via tool) |
|
|
2070
2070
|
| Integration OAuth tokens | CES / encrypted file store (via `secure-keys.ts`) | Encrypted binary | `TokenManager` auto-refresh | Until disconnected or revoked |
|
|
2071
2071
|
| User preferences | UserDefaults | plist | Foundation | Permanent |
|
|
2072
2072
|
| Session logs | `~/Library/.../logs/session-*.json` | JSON per session | Swift Codable | Unbounded |
|
|
@@ -229,7 +229,7 @@ User messages are scanned at ingress (`secret-ingress.ts`) against known credent
|
|
|
229
229
|
| Component | Location | What it stores |
|
|
230
230
|
| ------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
231
231
|
| Secret values | CES credential store or encrypted file store | Encrypted credential values keyed as `credential/{service}/{field}`. Stored via CES RPC (primary), CES HTTP (containerized), or encrypted file store (fallback). |
|
|
232
|
-
| Credential
|
|
232
|
+
| Credential records | CES `<cesDataRoot>/metadata.json` | Service, field, alias, policy (`allowedTools`, `allowedDomains`, `injectionTemplates`), timestamps. Served over CES RPC/HTTP. |
|
|
233
233
|
| Config | `$VELLUM_WORKSPACE_DIR/config.*` | `secretDetection` settings: enabled, blockIngress, allowOneTimeSend, blockTokenShapedMessages (default `true` — whole-message token-shape heuristic) |
|
|
234
234
|
|
|
235
235
|
### Key Files
|
|
@@ -216,6 +216,8 @@ export interface StaticCredentialPolicyInput {
|
|
|
216
216
|
*/
|
|
217
217
|
export class StaticCredentialMetadataStore {
|
|
218
218
|
private metadataPath: string;
|
|
219
|
+
/** When set, reads and writes stay in process and do not touch the file. */
|
|
220
|
+
private memory: MetadataFile | null = null;
|
|
219
221
|
|
|
220
222
|
constructor(metadataPath: string) {
|
|
221
223
|
this.metadataPath = metadataPath;
|
|
@@ -224,6 +226,17 @@ export class StaticCredentialMetadataStore {
|
|
|
224
226
|
/** Update the metadata file path (primarily for testing). */
|
|
225
227
|
setPath(path: string): void {
|
|
226
228
|
this.metadataPath = path;
|
|
229
|
+
this.memory = null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Switch to an in-memory backing filled with `records`.
|
|
234
|
+
*/
|
|
235
|
+
useMemory(records: StaticCredentialRecord[] = []): void {
|
|
236
|
+
this.memory = {
|
|
237
|
+
version: CURRENT_VERSION,
|
|
238
|
+
credentials: records.map((record) => ({ ...record })),
|
|
239
|
+
};
|
|
227
240
|
}
|
|
228
241
|
|
|
229
242
|
/** Get the current metadata file path. */
|
|
@@ -231,13 +244,28 @@ export class StaticCredentialMetadataStore {
|
|
|
231
244
|
return this.metadataPath;
|
|
232
245
|
}
|
|
233
246
|
|
|
247
|
+
private load(): LoadResult {
|
|
248
|
+
if (this.memory) {
|
|
249
|
+
return this.memory;
|
|
250
|
+
}
|
|
251
|
+
return loadFile(this.metadataPath, saveFile);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private persist(data: MetadataFile): void {
|
|
255
|
+
if (this.memory) {
|
|
256
|
+
this.memory = data;
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
saveFile(data, this.metadataPath);
|
|
260
|
+
}
|
|
261
|
+
|
|
234
262
|
/**
|
|
235
263
|
* Throws if the metadata file has an unrecognized version.
|
|
236
264
|
* Call this before performing irreversible credential store operations
|
|
237
265
|
* so the operation fails cleanly before any side effects.
|
|
238
266
|
*/
|
|
239
267
|
assertWritable(): void {
|
|
240
|
-
const result =
|
|
268
|
+
const result = this.load();
|
|
241
269
|
if (isUnknownVersion(result)) {
|
|
242
270
|
throw new Error(
|
|
243
271
|
"Credential metadata file has an unrecognized version; refusing to mutate to avoid data loss"
|
|
@@ -254,7 +282,7 @@ export class StaticCredentialMetadataStore {
|
|
|
254
282
|
field: string,
|
|
255
283
|
policy?: StaticCredentialPolicyInput
|
|
256
284
|
): StaticCredentialRecord {
|
|
257
|
-
const result =
|
|
285
|
+
const result = this.load();
|
|
258
286
|
if (isUnknownVersion(result)) {
|
|
259
287
|
throw new Error(
|
|
260
288
|
"Credential metadata file has an unrecognized version; refusing to mutate to avoid data loss"
|
|
@@ -268,12 +296,15 @@ export class StaticCredentialMetadataStore {
|
|
|
268
296
|
);
|
|
269
297
|
|
|
270
298
|
if (existing) {
|
|
271
|
-
if (policy?.allowedTools !== undefined)
|
|
299
|
+
if (policy?.allowedTools !== undefined) {
|
|
272
300
|
existing.allowedTools = policy.allowedTools;
|
|
273
|
-
|
|
301
|
+
}
|
|
302
|
+
if (policy?.allowedDomains !== undefined) {
|
|
274
303
|
existing.allowedDomains = policy.allowedDomains;
|
|
275
|
-
|
|
304
|
+
}
|
|
305
|
+
if (policy?.usageDescription !== undefined) {
|
|
276
306
|
existing.usageDescription = policy.usageDescription;
|
|
307
|
+
}
|
|
277
308
|
if (policy?.alias !== undefined) {
|
|
278
309
|
if (policy.alias == null) {
|
|
279
310
|
delete existing.alias;
|
|
@@ -289,7 +320,7 @@ export class StaticCredentialMetadataStore {
|
|
|
289
320
|
}
|
|
290
321
|
}
|
|
291
322
|
existing.updatedAt = now;
|
|
292
|
-
|
|
323
|
+
this.persist(data);
|
|
293
324
|
return existing;
|
|
294
325
|
}
|
|
295
326
|
|
|
@@ -307,7 +338,7 @@ export class StaticCredentialMetadataStore {
|
|
|
307
338
|
};
|
|
308
339
|
|
|
309
340
|
data.credentials.push(record);
|
|
310
|
-
|
|
341
|
+
this.persist(data);
|
|
311
342
|
return record;
|
|
312
343
|
}
|
|
313
344
|
|
|
@@ -318,7 +349,7 @@ export class StaticCredentialMetadataStore {
|
|
|
318
349
|
service: string,
|
|
319
350
|
field: string
|
|
320
351
|
): StaticCredentialRecord | undefined {
|
|
321
|
-
const result =
|
|
352
|
+
const result = this.load();
|
|
322
353
|
if (isUnknownVersion(result)) return undefined;
|
|
323
354
|
return result.credentials.find(
|
|
324
355
|
(c) => c.service === service && c.field === field
|
|
@@ -329,7 +360,7 @@ export class StaticCredentialMetadataStore {
|
|
|
329
360
|
* Get metadata for a credential by its opaque ID.
|
|
330
361
|
*/
|
|
331
362
|
getById(credentialId: string): StaticCredentialRecord | undefined {
|
|
332
|
-
const result =
|
|
363
|
+
const result = this.load();
|
|
333
364
|
if (isUnknownVersion(result)) return undefined;
|
|
334
365
|
return result.credentials.find((c) => c.credentialId === credentialId);
|
|
335
366
|
}
|
|
@@ -338,7 +369,7 @@ export class StaticCredentialMetadataStore {
|
|
|
338
369
|
* List all credential metadata records.
|
|
339
370
|
*/
|
|
340
371
|
list(): StaticCredentialRecord[] {
|
|
341
|
-
const result =
|
|
372
|
+
const result = this.load();
|
|
342
373
|
if (isUnknownVersion(result)) return [];
|
|
343
374
|
return result.credentials;
|
|
344
375
|
}
|
|
@@ -347,7 +378,7 @@ export class StaticCredentialMetadataStore {
|
|
|
347
378
|
* Delete metadata for a credential.
|
|
348
379
|
*/
|
|
349
380
|
delete(service: string, field: string): boolean {
|
|
350
|
-
const result =
|
|
381
|
+
const result = this.load();
|
|
351
382
|
if (isUnknownVersion(result)) {
|
|
352
383
|
throw new Error(
|
|
353
384
|
"Credential metadata file has an unrecognized version; refusing to mutate to avoid data loss"
|
|
@@ -359,7 +390,7 @@ export class StaticCredentialMetadataStore {
|
|
|
359
390
|
);
|
|
360
391
|
if (idx === -1) return false;
|
|
361
392
|
data.credentials.splice(idx, 1);
|
|
362
|
-
|
|
393
|
+
this.persist(data);
|
|
363
394
|
return true;
|
|
364
395
|
}
|
|
365
396
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { credentialKey } from "@vellumai/credential-storage";
|
|
6
|
+
import type { CredentialRecord } from "@vellumai/service-contracts/credential-rpc";
|
|
7
|
+
|
|
8
|
+
import type { CredentialRecordBackend } from "../security/ces-rpc-record-backend.js";
|
|
9
|
+
import {
|
|
10
|
+
_ensureCesRecordsLoaded,
|
|
11
|
+
_setMetadataPath,
|
|
12
|
+
getCredentialMetadata,
|
|
13
|
+
setCredentialRecordBackend,
|
|
14
|
+
upsertCredentialMetadata,
|
|
15
|
+
} from "../tools/credentials/metadata-store.js";
|
|
16
|
+
import { getDataDir } from "../util/platform.js";
|
|
17
|
+
|
|
18
|
+
function leftoverPath(): string {
|
|
19
|
+
return join(getDataDir(), "credentials", "metadata.json");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function writeLeftover(record: CredentialRecord): void {
|
|
23
|
+
const path = leftoverPath();
|
|
24
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
25
|
+
writeFileSync(
|
|
26
|
+
path,
|
|
27
|
+
JSON.stringify({
|
|
28
|
+
version: 5,
|
|
29
|
+
credentials: [record],
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function leftoverContents(): string {
|
|
35
|
+
return readFileSync(leftoverPath(), "utf-8");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function makeBackend(): CredentialRecordBackend & {
|
|
39
|
+
store: Map<string, CredentialRecord>;
|
|
40
|
+
bulkSetCalls: number;
|
|
41
|
+
listCalls: number;
|
|
42
|
+
} {
|
|
43
|
+
const store = new Map<string, CredentialRecord>();
|
|
44
|
+
const backend: CredentialRecordBackend & {
|
|
45
|
+
store: Map<string, CredentialRecord>;
|
|
46
|
+
bulkSetCalls: number;
|
|
47
|
+
listCalls: number;
|
|
48
|
+
} = {
|
|
49
|
+
store,
|
|
50
|
+
bulkSetCalls: 0,
|
|
51
|
+
listCalls: 0,
|
|
52
|
+
isAvailable: () => true,
|
|
53
|
+
get: async (account) => store.get(account),
|
|
54
|
+
set: async (account, record) => {
|
|
55
|
+
store.set(account, record);
|
|
56
|
+
return true;
|
|
57
|
+
},
|
|
58
|
+
delete: async (account) => {
|
|
59
|
+
if (!store.has(account)) {
|
|
60
|
+
return "not-found";
|
|
61
|
+
}
|
|
62
|
+
store.delete(account);
|
|
63
|
+
return "deleted";
|
|
64
|
+
},
|
|
65
|
+
list: async () => {
|
|
66
|
+
backend.listCalls += 1;
|
|
67
|
+
return [...store.entries()].map(([account, record]) => ({
|
|
68
|
+
account,
|
|
69
|
+
record,
|
|
70
|
+
}));
|
|
71
|
+
},
|
|
72
|
+
bulkSet: async (records) => {
|
|
73
|
+
backend.bulkSetCalls += 1;
|
|
74
|
+
for (const { account, record } of records) {
|
|
75
|
+
store.set(account, record);
|
|
76
|
+
}
|
|
77
|
+
return records.map((entry) => ({ account: entry.account, ok: true }));
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
return backend;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
describe("CES credential record cache", () => {
|
|
84
|
+
beforeEach(() => {
|
|
85
|
+
setCredentialRecordBackend(undefined);
|
|
86
|
+
_setMetadataPath(null);
|
|
87
|
+
const leftover = leftoverPath();
|
|
88
|
+
if (existsSync(leftover)) {
|
|
89
|
+
rmSync(leftover, { force: true });
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
afterEach(() => {
|
|
94
|
+
setCredentialRecordBackend(undefined);
|
|
95
|
+
_setMetadataPath(null);
|
|
96
|
+
const leftover = leftoverPath();
|
|
97
|
+
if (existsSync(leftover)) {
|
|
98
|
+
rmSync(leftover, { force: true });
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("first catalog read lazy-loads CES records into the in-process cache", async () => {
|
|
103
|
+
const backend = makeBackend();
|
|
104
|
+
const record: CredentialRecord = {
|
|
105
|
+
credentialId: "cred-1",
|
|
106
|
+
service: "github",
|
|
107
|
+
field: "token",
|
|
108
|
+
allowedTools: ["bash"],
|
|
109
|
+
allowedDomains: [],
|
|
110
|
+
createdAt: 1,
|
|
111
|
+
updatedAt: 2,
|
|
112
|
+
};
|
|
113
|
+
backend.store.set(credentialKey("github", "token"), record);
|
|
114
|
+
setCredentialRecordBackend(backend);
|
|
115
|
+
expect(backend.listCalls).toBe(0);
|
|
116
|
+
|
|
117
|
+
getCredentialMetadata("github", "token");
|
|
118
|
+
await _ensureCesRecordsLoaded();
|
|
119
|
+
expect(backend.listCalls).toBe(1);
|
|
120
|
+
|
|
121
|
+
expect(getCredentialMetadata("github", "token")?.credentialId).toBe(
|
|
122
|
+
"cred-1",
|
|
123
|
+
);
|
|
124
|
+
expect(getCredentialMetadata("github", "token")?.allowedTools).toEqual([
|
|
125
|
+
"bash",
|
|
126
|
+
]);
|
|
127
|
+
getCredentialMetadata("github", "token");
|
|
128
|
+
await _ensureCesRecordsLoaded();
|
|
129
|
+
expect(backend.listCalls).toBe(1);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("lazy load leaves leftover metadata.json in place and ignores it", async () => {
|
|
133
|
+
const leftoverRecord: CredentialRecord = {
|
|
134
|
+
credentialId: "cred-leftover",
|
|
135
|
+
service: "github",
|
|
136
|
+
field: "token",
|
|
137
|
+
allowedTools: ["bash"],
|
|
138
|
+
allowedDomains: ["github.com"],
|
|
139
|
+
createdAt: 1,
|
|
140
|
+
updatedAt: 2,
|
|
141
|
+
};
|
|
142
|
+
writeLeftover(leftoverRecord);
|
|
143
|
+
const before = leftoverContents();
|
|
144
|
+
|
|
145
|
+
const backend = makeBackend();
|
|
146
|
+
backend.store.set(credentialKey("github", "token"), leftoverRecord);
|
|
147
|
+
setCredentialRecordBackend(backend);
|
|
148
|
+
|
|
149
|
+
getCredentialMetadata("github", "token");
|
|
150
|
+
await _ensureCesRecordsLoaded();
|
|
151
|
+
|
|
152
|
+
expect(existsSync(leftoverPath())).toBe(true);
|
|
153
|
+
expect(leftoverContents()).toBe(before);
|
|
154
|
+
expect(backend.bulkSetCalls).toBe(0);
|
|
155
|
+
expect(getCredentialMetadata("github", "token")?.allowedTools).toEqual([
|
|
156
|
+
"bash",
|
|
157
|
+
]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("lazy load ignores leftover metadata.json when CES list fails", async () => {
|
|
161
|
+
const leftoverRecord: CredentialRecord = {
|
|
162
|
+
credentialId: "cred-keep",
|
|
163
|
+
service: "slack_channel",
|
|
164
|
+
field: "bot_token",
|
|
165
|
+
allowedTools: ["bash"],
|
|
166
|
+
allowedDomains: [],
|
|
167
|
+
createdAt: 1,
|
|
168
|
+
updatedAt: 2,
|
|
169
|
+
};
|
|
170
|
+
writeLeftover(leftoverRecord);
|
|
171
|
+
|
|
172
|
+
const backend = makeBackend();
|
|
173
|
+
backend.list = async () => null;
|
|
174
|
+
setCredentialRecordBackend(backend);
|
|
175
|
+
|
|
176
|
+
getCredentialMetadata("slack_channel", "bot_token");
|
|
177
|
+
await _ensureCesRecordsLoaded();
|
|
178
|
+
|
|
179
|
+
expect(existsSync(leftoverPath())).toBe(true);
|
|
180
|
+
expect(backend.bulkSetCalls).toBe(0);
|
|
181
|
+
expect(
|
|
182
|
+
getCredentialMetadata("slack_channel", "bot_token"),
|
|
183
|
+
).toBeUndefined();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test("lazy load ignores leftover metadata.json when CES has no leftover accounts", async () => {
|
|
187
|
+
const leftoverRecord: CredentialRecord = {
|
|
188
|
+
credentialId: "cred-missing",
|
|
189
|
+
service: "github",
|
|
190
|
+
field: "token",
|
|
191
|
+
allowedTools: ["bash"],
|
|
192
|
+
allowedDomains: [],
|
|
193
|
+
createdAt: 1,
|
|
194
|
+
updatedAt: 2,
|
|
195
|
+
};
|
|
196
|
+
writeLeftover(leftoverRecord);
|
|
197
|
+
|
|
198
|
+
const backend = makeBackend();
|
|
199
|
+
setCredentialRecordBackend(backend);
|
|
200
|
+
|
|
201
|
+
getCredentialMetadata("github", "token");
|
|
202
|
+
await _ensureCesRecordsLoaded();
|
|
203
|
+
|
|
204
|
+
expect(existsSync(leftoverPath())).toBe(true);
|
|
205
|
+
expect(backend.store.size).toBe(0);
|
|
206
|
+
expect(backend.bulkSetCalls).toBe(0);
|
|
207
|
+
expect(getCredentialMetadata("github", "token")).toBeUndefined();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("lazy load does not throw when CES isAvailable throws", async () => {
|
|
211
|
+
const backend = makeBackend();
|
|
212
|
+
backend.isAvailable = () => {
|
|
213
|
+
throw new Error("CES boom");
|
|
214
|
+
};
|
|
215
|
+
setCredentialRecordBackend(backend);
|
|
216
|
+
|
|
217
|
+
getCredentialMetadata("github", "token");
|
|
218
|
+
await _ensureCesRecordsLoaded();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test("upsert write-throughs to CES without updating leftover metadata.json", async () => {
|
|
222
|
+
const leftoverRecord: CredentialRecord = {
|
|
223
|
+
credentialId: "cred-leftover",
|
|
224
|
+
service: "github",
|
|
225
|
+
field: "token",
|
|
226
|
+
allowedTools: ["bash"],
|
|
227
|
+
allowedDomains: [],
|
|
228
|
+
createdAt: 1,
|
|
229
|
+
updatedAt: 2,
|
|
230
|
+
};
|
|
231
|
+
writeLeftover(leftoverRecord);
|
|
232
|
+
const leftoverBefore = leftoverContents();
|
|
233
|
+
|
|
234
|
+
const backend = makeBackend();
|
|
235
|
+
backend.store.set(credentialKey("github", "token"), leftoverRecord);
|
|
236
|
+
setCredentialRecordBackend(backend);
|
|
237
|
+
getCredentialMetadata("github", "token");
|
|
238
|
+
await _ensureCesRecordsLoaded();
|
|
239
|
+
expect(existsSync(leftoverPath())).toBe(true);
|
|
240
|
+
|
|
241
|
+
const created = upsertCredentialMetadata("slack_channel", "bot_token", {
|
|
242
|
+
allowedTools: ["bash"],
|
|
243
|
+
});
|
|
244
|
+
await Promise.resolve();
|
|
245
|
+
|
|
246
|
+
expect(existsSync(leftoverPath())).toBe(true);
|
|
247
|
+
expect(leftoverContents()).toBe(leftoverBefore);
|
|
248
|
+
expect(
|
|
249
|
+
backend.store.get(credentialKey("slack_channel", "bot_token"))
|
|
250
|
+
?.credentialId,
|
|
251
|
+
).toBe(created.credentialId);
|
|
252
|
+
expect(getCredentialMetadata("slack_channel", "bot_token")?.allowedTools).toEqual(
|
|
253
|
+
["bash"],
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { existsSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
1
3
|
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
4
|
|
|
3
5
|
import { credentialKey } from "@vellumai/credential-storage";
|
|
@@ -9,6 +11,7 @@ import {
|
|
|
9
11
|
setCredentialRecordBackend,
|
|
10
12
|
upsertCredentialMetadata,
|
|
11
13
|
} from "../tools/credentials/metadata-store.js";
|
|
14
|
+
import { getDataDir } from "../util/platform.js";
|
|
12
15
|
|
|
13
16
|
function makeBackend(): CredentialRecordBackend & {
|
|
14
17
|
store: Map<string, CredentialRecord>;
|
|
@@ -44,6 +47,10 @@ describe("CES credential record write-through", () => {
|
|
|
44
47
|
afterEach(() => {
|
|
45
48
|
setCredentialRecordBackend(undefined);
|
|
46
49
|
_setMetadataPath(null);
|
|
50
|
+
const leftover = join(getDataDir(), "credentials", "metadata.json");
|
|
51
|
+
if (existsSync(leftover)) {
|
|
52
|
+
rmSync(leftover, { force: true });
|
|
53
|
+
}
|
|
47
54
|
});
|
|
48
55
|
|
|
49
56
|
test("upsert write-through updates the CES backend", async () => {
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Merge helper for `data/credentials/metadata.json` on bundle import.
|
|
3
3
|
*
|
|
4
|
-
* The credential metadata file
|
|
5
|
-
*
|
|
6
|
-
* `
|
|
7
|
-
*
|
|
8
|
-
* `assistant_api_key`, `platform_assistant_id`, `webhook_secret`) to be
|
|
9
|
-
* present in metadata before the gateway will even look up their values in
|
|
10
|
-
* CES.
|
|
4
|
+
* The leftover workspace credential metadata file may remain on disk after
|
|
5
|
+
* CES import. CES owns identity and policy records. Bundle import still
|
|
6
|
+
* merges `vellum:*` catalog rows so platform identity survives a
|
|
7
|
+
* local-to-platform teleport.
|
|
11
8
|
*
|
|
12
9
|
* On a local→platform teleport, the bundle carries the SOURCE's metadata
|
|
13
10
|
* (no `vellum:*` entries, since the source is local), and a naive overwrite
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Credential metadata store.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* CES
|
|
4
|
+
* Production: CES owns identity + policy records. This module keeps a
|
|
5
|
+
* synchronous in-process cache so existing call sites stay sync, and
|
|
6
|
+
* write-throughs to CES when a record backend is injected.
|
|
7
|
+
*
|
|
8
|
+
* The cache is filled lazily: the first catalog read or write starts a
|
|
9
|
+
* single-flight CES `list()`. Leftover workspace `metadata.json` is not
|
|
10
|
+
* read. CES import (003) may leave that file in place; this module
|
|
11
|
+
* ignores it.
|
|
7
12
|
*
|
|
8
13
|
* Tests: `_setMetadataPath` keeps the file-backed store and skips CES
|
|
9
|
-
* write-through.
|
|
14
|
+
* load and write-through.
|
|
10
15
|
*/
|
|
11
16
|
|
|
12
|
-
import { join } from "node:path";
|
|
13
|
-
|
|
14
17
|
import {
|
|
15
18
|
credentialKey,
|
|
16
19
|
StaticCredentialMetadataStore,
|
|
@@ -19,7 +22,6 @@ import type { CredentialRecord } from "@vellumai/service-contracts/credential-rp
|
|
|
19
22
|
|
|
20
23
|
import type { CredentialRecordBackend } from "../../security/ces-rpc-record-backend.js";
|
|
21
24
|
import { getLogger } from "../../util/logger.js";
|
|
22
|
-
import { getDataDir } from "../../util/platform.js";
|
|
23
25
|
import type { CredentialInjectionTemplate } from "./policy-types.js";
|
|
24
26
|
|
|
25
27
|
const log = getLogger("credential-metadata-store");
|
|
@@ -48,15 +50,23 @@ export interface CredentialMetadata {
|
|
|
48
50
|
// Singleton store instance
|
|
49
51
|
// ---------------------------------------------------------------------------
|
|
50
52
|
|
|
53
|
+
const CES_MEMORY_PATH = "ces-in-memory-credential-metadata";
|
|
54
|
+
|
|
51
55
|
let _store: StaticCredentialMetadataStore | undefined;
|
|
52
56
|
let _overridePath: string | null = null;
|
|
53
57
|
let _recordBackend: CredentialRecordBackend | undefined;
|
|
58
|
+
let _cesRecordsLoaded = false;
|
|
59
|
+
let _cesLoad: Promise<void> | undefined;
|
|
60
|
+
let _cesLoadGeneration = 0;
|
|
54
61
|
|
|
55
62
|
function getStore(): StaticCredentialMetadataStore {
|
|
56
63
|
if (!_store) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
if (_overridePath) {
|
|
65
|
+
_store = new StaticCredentialMetadataStore(_overridePath);
|
|
66
|
+
} else {
|
|
67
|
+
_store = new StaticCredentialMetadataStore(CES_MEMORY_PATH);
|
|
68
|
+
_store.useMemory([]);
|
|
69
|
+
}
|
|
60
70
|
}
|
|
61
71
|
return _store;
|
|
62
72
|
}
|
|
@@ -76,6 +86,12 @@ function toRecord(metadata: CredentialMetadata): CredentialRecord {
|
|
|
76
86
|
};
|
|
77
87
|
}
|
|
78
88
|
|
|
89
|
+
function resetCesLoad(): void {
|
|
90
|
+
_cesRecordsLoaded = false;
|
|
91
|
+
_cesLoad = undefined;
|
|
92
|
+
_cesLoadGeneration += 1;
|
|
93
|
+
}
|
|
94
|
+
|
|
79
95
|
async function persistCredentialMetadata(
|
|
80
96
|
metadata: CredentialMetadata,
|
|
81
97
|
): Promise<void> {
|
|
@@ -96,6 +112,77 @@ export function setCredentialRecordBackend(
|
|
|
96
112
|
backend: CredentialRecordBackend | undefined,
|
|
97
113
|
): void {
|
|
98
114
|
_recordBackend = backend;
|
|
115
|
+
resetCesLoad();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function ensureCesRecordsLoaded(): Promise<void> {
|
|
119
|
+
if (_overridePath || !_recordBackend || _cesRecordsLoaded) {
|
|
120
|
+
return Promise.resolve();
|
|
121
|
+
}
|
|
122
|
+
if (_cesLoad) {
|
|
123
|
+
return _cesLoad;
|
|
124
|
+
}
|
|
125
|
+
const generation = _cesLoadGeneration;
|
|
126
|
+
_cesLoad = loadCesRecords(generation).finally(() => {
|
|
127
|
+
if (_cesLoadGeneration === generation) {
|
|
128
|
+
_cesLoad = undefined;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
return _cesLoad;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function loadCesRecords(generation: number): Promise<void> {
|
|
135
|
+
try {
|
|
136
|
+
if (!_recordBackend || _overridePath || generation !== _cesLoadGeneration) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
let available = false;
|
|
140
|
+
try {
|
|
141
|
+
available = _recordBackend.isAvailable();
|
|
142
|
+
} catch {
|
|
143
|
+
available = false;
|
|
144
|
+
}
|
|
145
|
+
if (!available) {
|
|
146
|
+
log.warn("CES record backend unavailable; keeping in-process cache");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const remote = await _recordBackend.list();
|
|
151
|
+
if (generation !== _cesLoadGeneration) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (remote === null) {
|
|
155
|
+
log.warn("CES record list failed; keeping in-process cache");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const store = getStore();
|
|
160
|
+
const localByAccount = new Map(
|
|
161
|
+
store
|
|
162
|
+
.list()
|
|
163
|
+
.map((record) => [credentialKey(record.service, record.field), record]),
|
|
164
|
+
);
|
|
165
|
+
const merged = remote.map((entry) => {
|
|
166
|
+
const local = localByAccount.get(entry.account);
|
|
167
|
+
if (local) {
|
|
168
|
+
localByAccount.delete(entry.account);
|
|
169
|
+
return local;
|
|
170
|
+
}
|
|
171
|
+
return entry.record;
|
|
172
|
+
});
|
|
173
|
+
for (const leftover of localByAccount.values()) {
|
|
174
|
+
merged.push(leftover);
|
|
175
|
+
}
|
|
176
|
+
store.useMemory(merged);
|
|
177
|
+
_cesRecordsLoaded = true;
|
|
178
|
+
log.info({ count: remote.length }, "Loaded credential records from CES");
|
|
179
|
+
} catch (err) {
|
|
180
|
+
log.warn({ err }, "CES record load failed");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function touchCesCache(): void {
|
|
185
|
+
void ensureCesRecordsLoaded();
|
|
99
186
|
}
|
|
100
187
|
|
|
101
188
|
// ---------------------------------------------------------------------------
|
|
@@ -108,6 +195,7 @@ export function setCredentialRecordBackend(
|
|
|
108
195
|
* so the operation fails cleanly before any side effects.
|
|
109
196
|
*/
|
|
110
197
|
export function assertMetadataWritable(): void {
|
|
198
|
+
touchCesCache();
|
|
111
199
|
getStore().assertWritable();
|
|
112
200
|
}
|
|
113
201
|
|
|
@@ -130,6 +218,7 @@ export function upsertCredentialMetadata(
|
|
|
130
218
|
injectionTemplates?: CredentialInjectionTemplate[] | null;
|
|
131
219
|
},
|
|
132
220
|
): CredentialMetadata {
|
|
221
|
+
touchCesCache();
|
|
133
222
|
const record = getStore().upsert(service, field, policy) as CredentialMetadata;
|
|
134
223
|
void persistCredentialMetadata(record);
|
|
135
224
|
return record;
|
|
@@ -142,6 +231,7 @@ export function getCredentialMetadata(
|
|
|
142
231
|
service: string,
|
|
143
232
|
field: string,
|
|
144
233
|
): CredentialMetadata | undefined {
|
|
234
|
+
touchCesCache();
|
|
145
235
|
return getStore().getByServiceField(service, field) as
|
|
146
236
|
| CredentialMetadata
|
|
147
237
|
| undefined;
|
|
@@ -153,6 +243,7 @@ export function getCredentialMetadata(
|
|
|
153
243
|
export function getCredentialMetadataById(
|
|
154
244
|
credentialId: string,
|
|
155
245
|
): CredentialMetadata | undefined {
|
|
246
|
+
touchCesCache();
|
|
156
247
|
return getStore().getById(credentialId) as CredentialMetadata | undefined;
|
|
157
248
|
}
|
|
158
249
|
|
|
@@ -160,6 +251,7 @@ export function getCredentialMetadataById(
|
|
|
160
251
|
* List all credential metadata records.
|
|
161
252
|
*/
|
|
162
253
|
export function listCredentialMetadata(): CredentialMetadata[] {
|
|
254
|
+
touchCesCache();
|
|
163
255
|
return getStore().list() as CredentialMetadata[];
|
|
164
256
|
}
|
|
165
257
|
|
|
@@ -170,6 +262,7 @@ export function deleteCredentialMetadata(
|
|
|
170
262
|
service: string,
|
|
171
263
|
field: string,
|
|
172
264
|
): boolean {
|
|
265
|
+
touchCesCache();
|
|
173
266
|
const deleted = getStore().delete(service, field);
|
|
174
267
|
if (deleted && _recordBackend && !_overridePath) {
|
|
175
268
|
void _recordBackend.delete(credentialKey(service, field));
|
|
@@ -177,9 +270,15 @@ export function deleteCredentialMetadata(
|
|
|
177
270
|
return deleted;
|
|
178
271
|
}
|
|
179
272
|
|
|
273
|
+
/** @internal Test-only: wait for the in-flight CES list to populate the cache. */
|
|
274
|
+
export function _ensureCesRecordsLoaded(): Promise<void> {
|
|
275
|
+
return ensureCesRecordsLoaded();
|
|
276
|
+
}
|
|
277
|
+
|
|
180
278
|
/** @internal Test-only: override the metadata file path. */
|
|
181
279
|
export function _setMetadataPath(path: string | null): void {
|
|
182
280
|
_overridePath = path;
|
|
281
|
+
resetCesLoad();
|
|
183
282
|
if (_store) {
|
|
184
283
|
if (path) {
|
|
185
284
|
_store.setPath(path);
|