@agent-native/dispatch 0.42.7-nightly-20261113152307 → 0.42.7
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/CHANGELOG.md +5 -13
- package/README.md +20 -18
- package/dist/actions/create-vault-grant.d.ts +2 -1
- package/dist/actions/create-vault-secret.d.ts +1 -1
- package/dist/actions/create-vault-secret.js +2 -2
- package/dist/actions/create-vault-secret.js.map +1 -1
- package/dist/actions/grant-vault-secrets-to-app.d.ts +1 -0
- package/dist/actions/list-vault-grants.d.ts +1 -0
- package/dist/actions/list-vault-secret-options.js +1 -1
- package/dist/actions/list-vault-secret-options.js.map +1 -1
- package/dist/actions/list-vault-secrets.js +3 -5
- package/dist/actions/list-vault-secrets.js.map +1 -1
- package/dist/actions/revoke-vault-grant.d.ts +2 -1
- package/dist/actions/update-vault-secret.d.ts +1 -1
- package/dist/actions/update-vault-secret.js +2 -2
- package/dist/actions/update-vault-secret.js.map +1 -1
- package/dist/db/migrations.d.ts.map +1 -1
- package/dist/db/migrations.js +19 -0
- package/dist/db/migrations.js.map +1 -1
- package/dist/db/schema.d.ts +8 -185
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/db/schema.js +3 -13
- package/dist/db/schema.js.map +1 -1
- package/dist/routes/pages/vault.d.ts.map +1 -1
- package/dist/routes/pages/vault.js +1 -1
- package/dist/routes/pages/vault.js.map +1 -1
- package/dist/server/lib/vault-store.d.ts +67 -93
- package/dist/server/lib/vault-store.d.ts.map +1 -1
- package/dist/server/lib/vault-store.js +227 -280
- package/dist/server/lib/vault-store.js.map +1 -1
- package/dist/server/plugins/db.d.ts.map +1 -1
- package/dist/server/plugins/db.js +2 -0
- package/dist/server/plugins/db.js.map +1 -1
- package/package.json +4 -4
- package/src/actions/create-vault-secret.ts +2 -5
- package/src/actions/list-vault-secret-options.ts +1 -1
- package/src/actions/list-vault-secrets.ts +3 -8
- package/src/actions/update-vault-secret.ts +2 -6
- package/src/db/migrations.spec.ts +117 -1
- package/src/db/migrations.ts +19 -0
- package/src/db/schema.ts +3 -14
- package/src/routes/pages/vault.tsx +0 -4
- package/src/server/lib/vault-store.spec.ts +1188 -475
- package/src/server/lib/vault-store.ts +343 -405
- package/src/server/plugins/db.ts +2 -0
|
@@ -3,11 +3,16 @@ import crypto from "node:crypto";
|
|
|
3
3
|
import { and, desc, eq, isNull, or, sql } from "@agent-native/core/db/schema";
|
|
4
4
|
import { ssrfSafeFetch } from "@agent-native/core/extensions/url-safety";
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
deleteAppSecretById,
|
|
7
|
+
isVaultManagedSecret,
|
|
7
8
|
listAppSecretsForScope,
|
|
8
|
-
|
|
9
|
+
listAppSecretSummaries,
|
|
10
|
+
readAppSecretMetaById,
|
|
11
|
+
updateAppSecretById,
|
|
9
12
|
VAULT_SYNC_DESCRIPTION_PREFIX,
|
|
10
13
|
writeAppSecret,
|
|
14
|
+
type AppSecretSummary,
|
|
15
|
+
type SecretMeta,
|
|
11
16
|
type SecretScope,
|
|
12
17
|
} from "@agent-native/core/secrets";
|
|
13
18
|
import { readOrgMemberRole } from "@agent-native/core/server";
|
|
@@ -18,6 +23,7 @@ import {
|
|
|
18
23
|
putOrgSetting,
|
|
19
24
|
putUserSetting,
|
|
20
25
|
} from "@agent-native/core/settings";
|
|
26
|
+
import type { SQL } from "drizzle-orm";
|
|
21
27
|
|
|
22
28
|
import { getDb, schema } from "../../db/index.js";
|
|
23
29
|
import {
|
|
@@ -28,13 +34,6 @@ import {
|
|
|
28
34
|
|
|
29
35
|
const VAULT_ACCESS_SETTINGS_KEY = "dispatch-vault-access-settings";
|
|
30
36
|
|
|
31
|
-
/**
|
|
32
|
-
* `vault_secrets.value` is NOT NULL and migrations can't drop columns, so it
|
|
33
|
-
* stays as an always-empty column; the value lives only, encrypted, in
|
|
34
|
-
* `app_secrets`. Treat "" as "no plaintext here", never as "no secret".
|
|
35
|
-
*/
|
|
36
|
-
const SEALED_VAULT_VALUE = "";
|
|
37
|
-
|
|
38
37
|
export type VaultAccessMode = "all-apps" | "manual";
|
|
39
38
|
|
|
40
39
|
export interface VaultAccessSettings {
|
|
@@ -49,8 +48,10 @@ export interface VaultAccessSettings {
|
|
|
49
48
|
* Every getSecret / updateSecret / deleteSecret / createGrant call must
|
|
50
49
|
* pass the ctx of the *current request* so the row is scoped to that
|
|
51
50
|
* caller's tenant. Looking up a vault secret by id alone is unsafe — UUIDs
|
|
52
|
-
* are not authorization. A
|
|
53
|
-
*
|
|
51
|
+
* are not authorization. A key is found only in the credential-store scope
|
|
52
|
+
* the ctx maps to (see `credentialStoreScopeForVaultCtx`); a grant, request
|
|
53
|
+
* or audit row matches the ctx if either the caller owns it or it lives in
|
|
54
|
+
* the caller's active org.
|
|
54
55
|
*/
|
|
55
56
|
export interface VaultCtx {
|
|
56
57
|
ownerEmail: string;
|
|
@@ -91,10 +92,6 @@ function ctxForRow(row: {
|
|
|
91
92
|
return { ownerEmail: row.ownerEmail, orgId: row.orgId };
|
|
92
93
|
}
|
|
93
94
|
|
|
94
|
-
function ctxForSecretRow(row: VaultSecretRow, fallback: VaultCtx): VaultCtx {
|
|
95
|
-
return row.ownerEmail ? ctxForRow(row) : fallback;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
95
|
function id() {
|
|
99
96
|
return crypto.randomUUID();
|
|
100
97
|
}
|
|
@@ -197,12 +194,23 @@ export async function setVaultAccessSettings(input: {
|
|
|
197
194
|
export async function recordVaultAudit(input: {
|
|
198
195
|
action: string;
|
|
199
196
|
secretId?: string | null;
|
|
197
|
+
/** The key the event is about. Recorded in the metadata, which outlives the key's row. */
|
|
198
|
+
credentialKey?: string | null;
|
|
200
199
|
appId?: string | null;
|
|
201
200
|
summary: string;
|
|
202
|
-
metadata?: unknown
|
|
201
|
+
metadata?: Record<string, unknown>;
|
|
203
202
|
actor?: string;
|
|
204
203
|
}) {
|
|
205
204
|
const db = getDb();
|
|
205
|
+
const metadata =
|
|
206
|
+
input.credentialKey || input.metadata
|
|
207
|
+
? {
|
|
208
|
+
...(input.credentialKey
|
|
209
|
+
? { credentialKey: input.credentialKey }
|
|
210
|
+
: {}),
|
|
211
|
+
...input.metadata,
|
|
212
|
+
}
|
|
213
|
+
: null;
|
|
206
214
|
await db.insert(schema.vaultAuditLog).values({
|
|
207
215
|
id: id(),
|
|
208
216
|
ownerEmail: currentOwnerEmail(),
|
|
@@ -212,7 +220,7 @@ export async function recordVaultAudit(input: {
|
|
|
212
220
|
action: input.action,
|
|
213
221
|
actor: input.actor || currentOwnerEmail(),
|
|
214
222
|
summary: input.summary,
|
|
215
|
-
metadata:
|
|
223
|
+
metadata: metadata ? safeJson(metadata) : null,
|
|
216
224
|
createdAt: now(),
|
|
217
225
|
});
|
|
218
226
|
}
|
|
@@ -228,47 +236,162 @@ export async function listVaultAudit(limit = 50) {
|
|
|
228
236
|
.limit(limit);
|
|
229
237
|
}
|
|
230
238
|
|
|
231
|
-
|
|
239
|
+
/**
|
|
240
|
+
* A key in the vault's credential-store scope: its `app_secrets` row without
|
|
241
|
+
* the value. `name` is the label, or the key itself for a row saved before
|
|
242
|
+
* labels existed.
|
|
243
|
+
*/
|
|
244
|
+
export interface VaultSecretRow {
|
|
245
|
+
id: string;
|
|
246
|
+
name: string;
|
|
247
|
+
credentialKey: string;
|
|
248
|
+
provider: string | null;
|
|
249
|
+
description: string | null;
|
|
250
|
+
createdBy: string | null;
|
|
251
|
+
createdAt: number;
|
|
252
|
+
updatedAt: number;
|
|
253
|
+
/**
|
|
254
|
+
* Created or edited through the Vault. Without it the key is one an app
|
|
255
|
+
* registered for itself in the same scope.
|
|
256
|
+
*/
|
|
257
|
+
managed: boolean;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* What the credential store holds for a key: a readable value, or one this
|
|
262
|
+
* deployment cannot decrypt (a rotated key, a copied database).
|
|
263
|
+
*/
|
|
264
|
+
export type VaultValueStatus = "stored" | "unreadable";
|
|
265
|
+
|
|
266
|
+
export interface VaultSecretMetadata {
|
|
267
|
+
id: string;
|
|
268
|
+
name: string;
|
|
269
|
+
credentialKey: string;
|
|
270
|
+
last4: string;
|
|
271
|
+
valueStatus: VaultValueStatus;
|
|
272
|
+
provider: string | null;
|
|
273
|
+
description: string | null;
|
|
274
|
+
createdBy: string | null;
|
|
275
|
+
createdAt: number;
|
|
276
|
+
updatedAt: number;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** A key an earlier release saved carries that release's marker in its description; it is not the user's text. */
|
|
280
|
+
function visibleDescription(description: string | null): string | null {
|
|
281
|
+
return description?.startsWith(VAULT_SYNC_DESCRIPTION_PREFIX)
|
|
282
|
+
? null
|
|
283
|
+
: description;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function vaultSecretRow(row: AppSecretSummary): VaultSecretRow {
|
|
287
|
+
return {
|
|
288
|
+
id: row.id,
|
|
289
|
+
name: row.label || row.key,
|
|
290
|
+
credentialKey: row.key,
|
|
291
|
+
provider: row.provider,
|
|
292
|
+
description: visibleDescription(row.description),
|
|
293
|
+
createdBy: row.createdBy,
|
|
294
|
+
createdAt: row.createdAt,
|
|
295
|
+
updatedAt: row.updatedAt,
|
|
296
|
+
managed: isVaultManagedSecret(row),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function vaultSecretMetadata(meta: SecretMeta): VaultSecretMetadata {
|
|
301
|
+
return {
|
|
302
|
+
id: meta.id,
|
|
303
|
+
name: meta.label || meta.key,
|
|
304
|
+
credentialKey: meta.key,
|
|
305
|
+
last4: meta.last4,
|
|
306
|
+
// The scope listing reports a value it cannot decrypt as an empty
|
|
307
|
+
// preview, and a stored value is never empty.
|
|
308
|
+
valueStatus: meta.last4 === "" ? "unreadable" : "stored",
|
|
309
|
+
provider: meta.provider,
|
|
310
|
+
description: visibleDescription(meta.description),
|
|
311
|
+
createdBy: meta.createdBy,
|
|
312
|
+
createdAt: meta.createdAt,
|
|
313
|
+
updatedAt: meta.updatedAt,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Where a vault's keys live in the credential store: the organization's scope,
|
|
319
|
+
* or the caller's own `solo:` scope when no organization is active. A key is
|
|
320
|
+
* its `app_secrets` row there, and its id is that row's id.
|
|
321
|
+
*/
|
|
322
|
+
export function credentialStoreScopeForVaultCtx(ctx: VaultCtx): {
|
|
323
|
+
scope: Extract<SecretScope, "org" | "workspace">;
|
|
324
|
+
scopeId: string;
|
|
325
|
+
} {
|
|
326
|
+
if (ctx.orgId) return { scope: "org", scopeId: ctx.orgId };
|
|
327
|
+
return { scope: "workspace", scopeId: `solo:${ctx.ownerEmail}` };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function listScopeSecrets(ctx: VaultCtx): Promise<VaultSecretRow[]> {
|
|
331
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
332
|
+
const rows = await listAppSecretSummaries(target.scope, target.scopeId);
|
|
333
|
+
return rows.map(vaultSecretRow);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function findSecret(
|
|
337
|
+
secretId: string,
|
|
338
|
+
ctx: VaultCtx,
|
|
339
|
+
): Promise<VaultSecretRow | null> {
|
|
340
|
+
const rows = await listScopeSecrets(ctx);
|
|
341
|
+
return rows.find((row) => row.id === secretId) ?? null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function requireSecretMetadata(
|
|
345
|
+
secretId: string,
|
|
346
|
+
ctx: VaultCtx,
|
|
347
|
+
): Promise<VaultSecretMetadata> {
|
|
348
|
+
const secret = await getSecret(secretId, ctx);
|
|
349
|
+
if (!secret) throw new Error("Secret not found");
|
|
350
|
+
return secret;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Every key in the vault's scope with a masked preview. Never returns a value. */
|
|
354
|
+
export async function listSecrets(): Promise<VaultSecretMetadata[]> {
|
|
232
355
|
await assertCanManageVault();
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
.from(schema.vaultSecrets)
|
|
237
|
-
.where(scopedFilter(schema.vaultSecrets))
|
|
238
|
-
.orderBy(desc(schema.vaultSecrets.updatedAt));
|
|
356
|
+
const target = credentialStoreScopeForVaultCtx(requireVaultCtx());
|
|
357
|
+
const rows = await listAppSecretsForScope(target.scope, target.scopeId);
|
|
358
|
+
return rows.map(vaultSecretMetadata);
|
|
239
359
|
}
|
|
240
360
|
|
|
361
|
+
/**
|
|
362
|
+
* The keys the caller may pick from. Admins get everything in the vault's
|
|
363
|
+
* scope, as on the Vault page. Members get only keys the Vault manages, so an
|
|
364
|
+
* app's own organization keys stay out of reach of a picker any member can open.
|
|
365
|
+
*/
|
|
241
366
|
export async function listSecretOptions() {
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
.orderBy(desc(schema.vaultSecrets.updatedAt));
|
|
367
|
+
const rows = await listScopeSecrets(requireVaultCtx());
|
|
368
|
+
const visible = (await canManageVault())
|
|
369
|
+
? rows
|
|
370
|
+
: rows.filter((row) => row.managed);
|
|
371
|
+
return visible.map(({ id, name, credentialKey, provider, description }) => ({
|
|
372
|
+
id,
|
|
373
|
+
name,
|
|
374
|
+
credentialKey,
|
|
375
|
+
provider,
|
|
376
|
+
description,
|
|
377
|
+
}));
|
|
254
378
|
}
|
|
255
379
|
|
|
256
|
-
export async function getSecret(
|
|
380
|
+
export async function getSecret(
|
|
381
|
+
secretId: string,
|
|
382
|
+
ctx: VaultCtx = requireVaultCtx(),
|
|
383
|
+
): Promise<VaultSecretMetadata | null> {
|
|
257
384
|
await assertCanManageVault();
|
|
258
|
-
const
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
.from(schema.vaultSecrets)
|
|
262
|
-
.where(
|
|
263
|
-
and(
|
|
264
|
-
eq(schema.vaultSecrets.id, secretId),
|
|
265
|
-
ctxScope(schema.vaultSecrets, ctx),
|
|
266
|
-
),
|
|
267
|
-
)
|
|
268
|
-
.limit(1);
|
|
269
|
-
return row ?? null;
|
|
385
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
386
|
+
const meta = await readAppSecretMetaById({ id: secretId, ...target });
|
|
387
|
+
return meta ? vaultSecretMetadata(meta) : null;
|
|
270
388
|
}
|
|
271
389
|
|
|
390
|
+
/**
|
|
391
|
+
* Save a key. A key that already exists in the scope is updated, which is the
|
|
392
|
+
* caller asking for exactly that; nothing else here ever overwrites a row.
|
|
393
|
+
* The value goes straight into the encrypted column of its `app_secrets` row.
|
|
394
|
+
*/
|
|
272
395
|
export async function createSecret(
|
|
273
396
|
input: {
|
|
274
397
|
credentialKey: string;
|
|
@@ -278,89 +401,63 @@ export async function createSecret(
|
|
|
278
401
|
description?: string | null | undefined;
|
|
279
402
|
},
|
|
280
403
|
ctx: VaultCtx = requireVaultCtx(),
|
|
281
|
-
) {
|
|
404
|
+
): Promise<VaultSecretMetadata> {
|
|
282
405
|
await assertCanManageVault();
|
|
283
|
-
const db = getDb();
|
|
284
|
-
const timestamp = now();
|
|
285
406
|
const credentialKey = normalizeCredentialKey(input.credentialKey);
|
|
286
407
|
if (!credentialKey) throw new Error("Credential key is required");
|
|
287
408
|
if (!input.value) throw new Error("Secret value is required");
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
and(
|
|
293
|
-
eq(schema.vaultSecrets.credentialKey, credentialKey),
|
|
294
|
-
ctxScope(schema.vaultSecrets, ctx),
|
|
295
|
-
),
|
|
296
|
-
)
|
|
297
|
-
.orderBy(desc(schema.vaultSecrets.updatedAt))
|
|
298
|
-
.limit(1);
|
|
409
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
410
|
+
const existing = (await listScopeSecrets(ctx)).find(
|
|
411
|
+
(row) => row.credentialKey === credentialKey,
|
|
412
|
+
);
|
|
299
413
|
|
|
300
|
-
if (existing
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
credentialKey,
|
|
307
|
-
value: SEALED_VAULT_VALUE,
|
|
414
|
+
if (existing) {
|
|
415
|
+
const updated = await updateAppSecretById(
|
|
416
|
+
{ id: existing.id, ...target },
|
|
417
|
+
{
|
|
418
|
+
value: input.value,
|
|
419
|
+
label: input.name,
|
|
308
420
|
provider: input.provider || null,
|
|
309
421
|
description: input.description || null,
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
.where(
|
|
313
|
-
and(
|
|
314
|
-
eq(schema.vaultSecrets.id, found.id),
|
|
315
|
-
ctxScope(schema.vaultSecrets, ctx),
|
|
316
|
-
),
|
|
317
|
-
);
|
|
318
|
-
await storeSecretValue(
|
|
319
|
-
ctxForSecretRow(found, ctx),
|
|
320
|
-
{ credentialKey, name: input.name },
|
|
321
|
-
input.value,
|
|
422
|
+
adoptedBy: ctx.ownerEmail,
|
|
423
|
+
},
|
|
322
424
|
);
|
|
425
|
+
if (updated) {
|
|
426
|
+
await recordVaultAudit({
|
|
427
|
+
action: "secret.updated",
|
|
428
|
+
secretId: existing.id,
|
|
429
|
+
credentialKey,
|
|
430
|
+
summary: `Updated secret "${input.name}" (${credentialKey})`,
|
|
431
|
+
metadata: { provider: input.provider },
|
|
432
|
+
});
|
|
323
433
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
await recordAudit({
|
|
332
|
-
action: "vault.secret.updated",
|
|
333
|
-
targetType: "vault-secret",
|
|
334
|
-
targetId: found.id,
|
|
335
|
-
summary: `Updated vault secret "${input.name}" (${credentialKey})`,
|
|
336
|
-
});
|
|
434
|
+
await recordAudit({
|
|
435
|
+
action: "vault.secret.updated",
|
|
436
|
+
targetType: "vault-secret",
|
|
437
|
+
targetId: existing.id,
|
|
438
|
+
summary: `Updated vault secret "${input.name}" (${credentialKey})`,
|
|
439
|
+
});
|
|
337
440
|
|
|
338
|
-
|
|
441
|
+
return requireSecretMetadata(existing.id, ctx);
|
|
442
|
+
}
|
|
339
443
|
}
|
|
340
444
|
|
|
341
|
-
const secretId =
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
ownerEmail: actor,
|
|
347
|
-
orgId: ctx.orgId,
|
|
348
|
-
name: input.name,
|
|
349
|
-
credentialKey,
|
|
350
|
-
value: SEALED_VAULT_VALUE,
|
|
445
|
+
const secretId = await writeAppSecret({
|
|
446
|
+
key: credentialKey,
|
|
447
|
+
value: input.value,
|
|
448
|
+
...target,
|
|
449
|
+
label: input.name,
|
|
351
450
|
provider: input.provider || null,
|
|
352
|
-
description: input.description
|
|
353
|
-
createdBy:
|
|
354
|
-
createdAt: timestamp,
|
|
355
|
-
updatedAt: timestamp,
|
|
451
|
+
...(input.description ? { description: input.description } : {}),
|
|
452
|
+
createdBy: ctx.ownerEmail,
|
|
356
453
|
});
|
|
357
|
-
await storeSecretValue(ctx, { credentialKey, name: input.name }, input.value);
|
|
358
454
|
|
|
359
455
|
await recordVaultAudit({
|
|
360
456
|
action: "secret.created",
|
|
361
457
|
secretId,
|
|
458
|
+
credentialKey,
|
|
362
459
|
summary: `Created secret "${input.name}" (${credentialKey})`,
|
|
363
|
-
metadata: {
|
|
460
|
+
metadata: { provider: input.provider },
|
|
364
461
|
});
|
|
365
462
|
|
|
366
463
|
await recordAudit({
|
|
@@ -370,7 +467,7 @@ export async function createSecret(
|
|
|
370
467
|
summary: `Created vault secret "${input.name}" (${credentialKey})`,
|
|
371
468
|
});
|
|
372
469
|
|
|
373
|
-
return
|
|
470
|
+
return requireSecretMetadata(secretId, ctx);
|
|
374
471
|
}
|
|
375
472
|
|
|
376
473
|
export async function updateSecret(
|
|
@@ -385,10 +482,9 @@ export async function updateSecret(
|
|
|
385
482
|
description?: string | null | undefined;
|
|
386
483
|
},
|
|
387
484
|
ctx: VaultCtx = requireVaultCtx(),
|
|
388
|
-
) {
|
|
485
|
+
): Promise<VaultSecretMetadata> {
|
|
389
486
|
await assertCanManageVault();
|
|
390
|
-
const
|
|
391
|
-
const existing = await getSecret(secretId, ctx);
|
|
487
|
+
const existing = await findSecret(secretId, ctx);
|
|
392
488
|
if (!existing) throw new Error("Secret not found");
|
|
393
489
|
const patch = typeof input === "string" ? { value: input } : input;
|
|
394
490
|
const credentialKey =
|
|
@@ -407,66 +503,46 @@ export async function updateSecret(
|
|
|
407
503
|
patch.description !== undefined
|
|
408
504
|
? patch.description || null
|
|
409
505
|
: existing.description;
|
|
506
|
+
const renamed = credentialKey !== existing.credentialKey;
|
|
507
|
+
|
|
508
|
+
// One statement renames, rotates and relabels together, and refuses a key
|
|
509
|
+
// another row in the scope already holds. A rename moves the stored
|
|
510
|
+
// ciphertext with the row, so the value is never read or rewritten.
|
|
511
|
+
const stored = await updateAppSecretById(
|
|
512
|
+
{ id: secretId, ...credentialStoreScopeForVaultCtx(ctx) },
|
|
513
|
+
{
|
|
514
|
+
...(renamed ? { key: credentialKey } : {}),
|
|
515
|
+
...(patch.value !== undefined ? { value: patch.value } : {}),
|
|
516
|
+
...(patch.name !== undefined ? { label: name } : {}),
|
|
517
|
+
...(patch.provider !== undefined ? { provider } : {}),
|
|
518
|
+
...(patch.description !== undefined ? { description } : {}),
|
|
519
|
+
adoptedBy: ctx.ownerEmail,
|
|
520
|
+
},
|
|
521
|
+
);
|
|
522
|
+
if (!stored) throw new Error("Secret not found");
|
|
410
523
|
|
|
411
|
-
if (
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
524
|
+
if (renamed) {
|
|
525
|
+
// Grants name their key, so they follow it. A failure here leaves them on
|
|
526
|
+
// the old key, which grants nothing: access fails closed.
|
|
527
|
+
await getDb()
|
|
528
|
+
.update(schema.vaultGrants)
|
|
529
|
+
.set({ credentialKey, secretId, updatedAt: now() })
|
|
415
530
|
.where(
|
|
416
|
-
|
|
417
|
-
eq(schema.
|
|
418
|
-
|
|
531
|
+
or(
|
|
532
|
+
eq(schema.vaultGrants.secretId, secretId),
|
|
533
|
+
and(
|
|
534
|
+
eq(schema.vaultGrants.credentialKey, existing.credentialKey),
|
|
535
|
+
tenantScope(schema.vaultGrants, ctx),
|
|
536
|
+
),
|
|
419
537
|
),
|
|
420
|
-
)
|
|
421
|
-
.limit(1);
|
|
422
|
-
if (conflict[0] && conflict[0].id !== secretId) {
|
|
423
|
-
throw new Error(`Credential key "${credentialKey}" is already in use`);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
const storeCtx = ctxForSecretRow(existing, ctx);
|
|
428
|
-
let valueForKey = patch.value;
|
|
429
|
-
if (!valueForKey && credentialKey !== existing.credentialKey) {
|
|
430
|
-
// A rename carries the stored value to the new key. A failed read throws;
|
|
431
|
-
// only a missing copy means there is nothing to move.
|
|
432
|
-
const current = await readAppSecret({
|
|
433
|
-
key: existing.credentialKey,
|
|
434
|
-
...credentialStoreScopeForVaultCtx(storeCtx),
|
|
435
|
-
});
|
|
436
|
-
valueForKey = current?.value;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
await db
|
|
440
|
-
.update(schema.vaultSecrets)
|
|
441
|
-
.set({
|
|
442
|
-
name,
|
|
443
|
-
credentialKey,
|
|
444
|
-
// Only a new value empties the row. A row that still holds an older
|
|
445
|
-
// plaintext value keeps it until it is cleared in bulk, so a label edit
|
|
446
|
-
// can never write that stale value over a newer one in the credential store.
|
|
447
|
-
...(patch.value !== undefined ? { value: SEALED_VAULT_VALUE } : {}),
|
|
448
|
-
provider,
|
|
449
|
-
description,
|
|
450
|
-
updatedAt: now(),
|
|
451
|
-
})
|
|
452
|
-
.where(
|
|
453
|
-
and(
|
|
454
|
-
eq(schema.vaultSecrets.id, secretId),
|
|
455
|
-
ctxScope(schema.vaultSecrets, ctx),
|
|
456
|
-
),
|
|
457
|
-
);
|
|
458
|
-
if (valueForKey) {
|
|
459
|
-
await storeSecretValue(storeCtx, { credentialKey, name }, valueForKey);
|
|
538
|
+
);
|
|
460
539
|
}
|
|
461
540
|
|
|
462
541
|
const auditMetadata = {
|
|
463
542
|
name,
|
|
464
543
|
previousName: name !== existing.name ? existing.name : undefined,
|
|
465
544
|
credentialKey,
|
|
466
|
-
previousCredentialKey:
|
|
467
|
-
credentialKey !== existing.credentialKey
|
|
468
|
-
? existing.credentialKey
|
|
469
|
-
: undefined,
|
|
545
|
+
previousCredentialKey: renamed ? existing.credentialKey : undefined,
|
|
470
546
|
provider,
|
|
471
547
|
previousProvider:
|
|
472
548
|
provider !== existing.provider ? existing.provider : undefined,
|
|
@@ -479,6 +555,7 @@ export async function updateSecret(
|
|
|
479
555
|
await recordVaultAudit({
|
|
480
556
|
action: "secret.updated",
|
|
481
557
|
secretId,
|
|
558
|
+
credentialKey,
|
|
482
559
|
summary: `Updated secret "${name}" (${credentialKey})`,
|
|
483
560
|
metadata: auditMetadata,
|
|
484
561
|
});
|
|
@@ -491,15 +568,7 @@ export async function updateSecret(
|
|
|
491
568
|
metadata: auditMetadata,
|
|
492
569
|
});
|
|
493
570
|
|
|
494
|
-
|
|
495
|
-
if (updated && credentialKey !== existing.credentialKey) {
|
|
496
|
-
await cleanupSyncedCredentialKeysIfUnused(ctxForRow(existing), [
|
|
497
|
-
existing.credentialKey,
|
|
498
|
-
]);
|
|
499
|
-
} else if (updated && patch.credentialKey !== undefined) {
|
|
500
|
-
await cleanupSyncedCredentialKeysIfUnused(ctxForSecretRow(updated, ctx));
|
|
501
|
-
}
|
|
502
|
-
return updated;
|
|
571
|
+
return requireSecretMetadata(secretId, ctx);
|
|
503
572
|
}
|
|
504
573
|
|
|
505
574
|
export async function deleteSecret(
|
|
@@ -507,8 +576,7 @@ export async function deleteSecret(
|
|
|
507
576
|
ctx: VaultCtx = requireVaultCtx(),
|
|
508
577
|
) {
|
|
509
578
|
await assertCanManageVault();
|
|
510
|
-
const
|
|
511
|
-
const existing = await getSecret(secretId, ctx);
|
|
579
|
+
const existing = await findSecret(secretId, ctx);
|
|
512
580
|
if (!existing) throw new Error("Secret not found");
|
|
513
581
|
|
|
514
582
|
const grants = await listGrants({ secretId });
|
|
@@ -518,21 +586,19 @@ export async function deleteSecret(
|
|
|
518
586
|
}
|
|
519
587
|
}
|
|
520
588
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
await cleanupSyncedCredentialKeysIfUnused(ctxForRow(existing), [
|
|
530
|
-
existing.credentialKey,
|
|
531
|
-
]);
|
|
589
|
+
// By id, so a rename since the lookup cannot point this at another key that
|
|
590
|
+
// took over the old name. Gone already means another admin deleted it: say
|
|
591
|
+
// so rather than record a delete that never happened.
|
|
592
|
+
const removed = await deleteAppSecretById({
|
|
593
|
+
id: secretId,
|
|
594
|
+
...credentialStoreScopeForVaultCtx(ctx),
|
|
595
|
+
});
|
|
596
|
+
if (!removed) throw new Error("Secret not found");
|
|
532
597
|
|
|
533
598
|
await recordVaultAudit({
|
|
534
599
|
action: "secret.deleted",
|
|
535
600
|
secretId,
|
|
601
|
+
credentialKey: existing.credentialKey,
|
|
536
602
|
summary: `Deleted secret "${existing.name}" (${existing.credentialKey})`,
|
|
537
603
|
});
|
|
538
604
|
|
|
@@ -546,24 +612,57 @@ export async function deleteSecret(
|
|
|
546
612
|
return existing;
|
|
547
613
|
}
|
|
548
614
|
|
|
615
|
+
/**
|
|
616
|
+
* A grant belongs to a key by its name. The id match covers a grant whose key
|
|
617
|
+
* column a rename has not reached yet; the name match covers grants backfilled
|
|
618
|
+
* from before the key column existed, whose `secretId` is stale.
|
|
619
|
+
*/
|
|
620
|
+
function grantsOfSecret(secret: VaultSecretRow, ctx: VaultCtx) {
|
|
621
|
+
return or(
|
|
622
|
+
eq(schema.vaultGrants.secretId, secret.id),
|
|
623
|
+
and(
|
|
624
|
+
eq(schema.vaultGrants.credentialKey, secret.credentialKey),
|
|
625
|
+
tenantScope(schema.vaultGrants, ctx),
|
|
626
|
+
),
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** Report the grant against the key's current id, so callers can match it to a listed key. */
|
|
631
|
+
function withCurrentSecretId<
|
|
632
|
+
T extends { secretId: string; credentialKey: string | null },
|
|
633
|
+
>(grant: T, secrets: VaultSecretRow[]): T {
|
|
634
|
+
const secret = grant.credentialKey
|
|
635
|
+
? secrets.find((row) => row.credentialKey === grant.credentialKey)
|
|
636
|
+
: undefined;
|
|
637
|
+
return secret ? { ...grant, secretId: secret.id } : grant;
|
|
638
|
+
}
|
|
639
|
+
|
|
549
640
|
export async function listGrants(filter?: {
|
|
550
641
|
secretId?: string;
|
|
551
642
|
appId?: string;
|
|
552
643
|
}) {
|
|
553
644
|
await assertCanManageVault();
|
|
645
|
+
const ctx = requireVaultCtx();
|
|
554
646
|
const db = getDb();
|
|
555
|
-
const
|
|
647
|
+
const secrets = await listScopeSecrets(ctx);
|
|
648
|
+
const conditions: Array<SQL | undefined> = [scopedFilter(schema.vaultGrants)];
|
|
556
649
|
if (filter?.secretId) {
|
|
557
|
-
|
|
650
|
+
const secret = secrets.find((row) => row.id === filter.secretId);
|
|
651
|
+
conditions.push(
|
|
652
|
+
secret
|
|
653
|
+
? grantsOfSecret(secret, ctx)
|
|
654
|
+
: eq(schema.vaultGrants.secretId, filter.secretId),
|
|
655
|
+
);
|
|
558
656
|
}
|
|
559
657
|
if (filter?.appId) {
|
|
560
|
-
conditions.push(eq(schema.vaultGrants.appId, filter.appId)
|
|
658
|
+
conditions.push(eq(schema.vaultGrants.appId, filter.appId));
|
|
561
659
|
}
|
|
562
|
-
|
|
660
|
+
const rows = await db
|
|
563
661
|
.select()
|
|
564
662
|
.from(schema.vaultGrants)
|
|
565
663
|
.where(and(...conditions))
|
|
566
664
|
.orderBy(desc(schema.vaultGrants.updatedAt));
|
|
665
|
+
return rows.map((grant) => withCurrentSecretId(grant, secrets));
|
|
567
666
|
}
|
|
568
667
|
|
|
569
668
|
export async function getGrant(
|
|
@@ -572,7 +671,7 @@ export async function getGrant(
|
|
|
572
671
|
) {
|
|
573
672
|
await assertCanManageVault();
|
|
574
673
|
const db = getDb();
|
|
575
|
-
const
|
|
674
|
+
const rows = await db
|
|
576
675
|
.select()
|
|
577
676
|
.from(schema.vaultGrants)
|
|
578
677
|
.where(
|
|
@@ -582,7 +681,8 @@ export async function getGrant(
|
|
|
582
681
|
),
|
|
583
682
|
)
|
|
584
683
|
.limit(1);
|
|
585
|
-
|
|
684
|
+
const row = rows.at(0);
|
|
685
|
+
return row ? withCurrentSecretId(row, await listScopeSecrets(ctx)) : null;
|
|
586
686
|
}
|
|
587
687
|
|
|
588
688
|
export async function createGrant(
|
|
@@ -592,26 +692,27 @@ export async function createGrant(
|
|
|
592
692
|
) {
|
|
593
693
|
await assertCanManageVault();
|
|
594
694
|
const db = getDb();
|
|
595
|
-
const secret = await
|
|
695
|
+
const secret = await findSecret(secretId, ctx);
|
|
596
696
|
if (!secret) throw new Error("Secret not found");
|
|
597
697
|
|
|
598
698
|
const timestamp = now();
|
|
599
699
|
const actor = ctx.ownerEmail;
|
|
600
|
-
const
|
|
700
|
+
const matches = await db
|
|
601
701
|
.select()
|
|
602
702
|
.from(schema.vaultGrants)
|
|
603
703
|
.where(
|
|
604
704
|
and(
|
|
605
|
-
eq(schema.vaultGrants.secretId, secretId),
|
|
606
705
|
eq(schema.vaultGrants.appId, appId),
|
|
706
|
+
grantsOfSecret(secret, ctx),
|
|
607
707
|
ctxScope(schema.vaultGrants, ctx),
|
|
608
708
|
),
|
|
609
709
|
)
|
|
610
|
-
.orderBy(desc(schema.vaultGrants.updatedAt))
|
|
611
|
-
|
|
710
|
+
.orderBy(desc(schema.vaultGrants.updatedAt));
|
|
711
|
+
const existing =
|
|
712
|
+
matches.find((grant) => grant.status === "active") ?? matches[0];
|
|
612
713
|
|
|
613
714
|
if (existing?.status === "active") {
|
|
614
|
-
return existing;
|
|
715
|
+
return getGrant(existing.id, ctx);
|
|
615
716
|
}
|
|
616
717
|
|
|
617
718
|
if (existing) {
|
|
@@ -621,6 +722,8 @@ export async function createGrant(
|
|
|
621
722
|
grantedBy: actor,
|
|
622
723
|
status: "active",
|
|
623
724
|
syncedAt: null,
|
|
725
|
+
secretId: secret.id,
|
|
726
|
+
credentialKey: secret.credentialKey,
|
|
624
727
|
updatedAt: timestamp,
|
|
625
728
|
})
|
|
626
729
|
.where(
|
|
@@ -633,6 +736,7 @@ export async function createGrant(
|
|
|
633
736
|
await recordVaultAudit({
|
|
634
737
|
action: "grant.reinstated",
|
|
635
738
|
secretId,
|
|
739
|
+
credentialKey: secret.credentialKey,
|
|
636
740
|
appId,
|
|
637
741
|
summary: `Reinstated "${secret.name}" (${secret.credentialKey}) for ${appId}`,
|
|
638
742
|
metadata: { grantId: existing.id },
|
|
@@ -654,7 +758,8 @@ export async function createGrant(
|
|
|
654
758
|
id: grantId,
|
|
655
759
|
ownerEmail: actor,
|
|
656
760
|
orgId: ctx.orgId,
|
|
657
|
-
secretId,
|
|
761
|
+
secretId: secret.id,
|
|
762
|
+
credentialKey: secret.credentialKey,
|
|
658
763
|
appId,
|
|
659
764
|
grantedBy: actor,
|
|
660
765
|
status: "active",
|
|
@@ -666,6 +771,7 @@ export async function createGrant(
|
|
|
666
771
|
await recordVaultAudit({
|
|
667
772
|
action: "grant.created",
|
|
668
773
|
secretId,
|
|
774
|
+
credentialKey: secret.credentialKey,
|
|
669
775
|
appId,
|
|
670
776
|
summary: `Granted "${secret.name}" (${secret.credentialKey}) to ${appId}`,
|
|
671
777
|
metadata: { grantId },
|
|
@@ -678,7 +784,7 @@ export async function createGrant(
|
|
|
678
784
|
summary: `Granted vault secret "${secret.name}" to ${appId}`,
|
|
679
785
|
});
|
|
680
786
|
|
|
681
|
-
return getGrant(grantId);
|
|
787
|
+
return getGrant(grantId, ctx);
|
|
682
788
|
}
|
|
683
789
|
|
|
684
790
|
export async function grantSecretsToApp(
|
|
@@ -730,7 +836,8 @@ export async function revokeGrant(
|
|
|
730
836
|
const grant = await getGrant(grantId, ctx);
|
|
731
837
|
if (!grant) throw new Error("Grant not found");
|
|
732
838
|
|
|
733
|
-
const secret = await
|
|
839
|
+
const secret = await findSecret(grant.secretId, ctx);
|
|
840
|
+
const credentialKey = secret?.credentialKey ?? grant.credentialKey;
|
|
734
841
|
|
|
735
842
|
await db
|
|
736
843
|
.update(schema.vaultGrants)
|
|
@@ -745,8 +852,9 @@ export async function revokeGrant(
|
|
|
745
852
|
await recordVaultAudit({
|
|
746
853
|
action: "grant.revoked",
|
|
747
854
|
secretId: grant.secretId,
|
|
855
|
+
credentialKey,
|
|
748
856
|
appId: grant.appId,
|
|
749
|
-
summary: `Revoked ${
|
|
857
|
+
summary: `Revoked ${credentialKey || grant.secretId} from ${grant.appId}`,
|
|
750
858
|
metadata: { grantId },
|
|
751
859
|
});
|
|
752
860
|
|
|
@@ -760,175 +868,6 @@ export async function revokeGrant(
|
|
|
760
868
|
return getGrant(grantId, ctx);
|
|
761
869
|
}
|
|
762
870
|
|
|
763
|
-
type VaultSecretRow = typeof schema.vaultSecrets.$inferSelect;
|
|
764
|
-
|
|
765
|
-
/**
|
|
766
|
-
* What the credential store holds for a vault row: a readable value, nothing,
|
|
767
|
-
* or a value this deployment cannot decrypt (a rotated key, a copied database).
|
|
768
|
-
*/
|
|
769
|
-
export type VaultValueStatus = "stored" | "missing" | "unreadable";
|
|
770
|
-
|
|
771
|
-
export interface VaultSecretMetadata {
|
|
772
|
-
id: string;
|
|
773
|
-
name: string;
|
|
774
|
-
credentialKey: string;
|
|
775
|
-
last4: string;
|
|
776
|
-
valueStatus: VaultValueStatus;
|
|
777
|
-
provider: string | null;
|
|
778
|
-
description: string | null;
|
|
779
|
-
createdBy: string;
|
|
780
|
-
createdAt: number;
|
|
781
|
-
updatedAt: number;
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
function vaultSecretMetadata(
|
|
785
|
-
secret: VaultSecretRow,
|
|
786
|
-
last4Value: string,
|
|
787
|
-
valueStatus: VaultValueStatus,
|
|
788
|
-
): VaultSecretMetadata {
|
|
789
|
-
return {
|
|
790
|
-
id: secret.id,
|
|
791
|
-
name: secret.name,
|
|
792
|
-
credentialKey: secret.credentialKey,
|
|
793
|
-
last4: last4Value,
|
|
794
|
-
valueStatus,
|
|
795
|
-
provider: secret.provider,
|
|
796
|
-
description: secret.description,
|
|
797
|
-
createdBy: secret.createdBy,
|
|
798
|
-
createdAt: secret.createdAt,
|
|
799
|
-
updatedAt: secret.updatedAt,
|
|
800
|
-
};
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
export function credentialStoreScopeForVaultCtx(ctx: VaultCtx): {
|
|
804
|
-
scope: Extract<SecretScope, "org" | "workspace">;
|
|
805
|
-
scopeId: string;
|
|
806
|
-
} {
|
|
807
|
-
if (ctx.orgId) return { scope: "org", scopeId: ctx.orgId };
|
|
808
|
-
return { scope: "workspace", scopeId: `solo:${ctx.ownerEmail}` };
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
/**
|
|
812
|
-
* Masked metadata for a page of vault rows. The preview is read from the
|
|
813
|
-
* encrypted copy in the credential store, one read per store scope, because
|
|
814
|
-
* the vault row itself holds no value.
|
|
815
|
-
*/
|
|
816
|
-
export async function listVaultSecretMetadata(
|
|
817
|
-
rows: VaultSecretRow[],
|
|
818
|
-
fallback: VaultCtx = requireVaultCtx(),
|
|
819
|
-
): Promise<VaultSecretMetadata[]> {
|
|
820
|
-
const storeKey = (row: VaultSecretRow) => {
|
|
821
|
-
const target = credentialStoreScopeForVaultCtx(
|
|
822
|
-
ctxForSecretRow(row, fallback),
|
|
823
|
-
);
|
|
824
|
-
return { target, scopeKey: `${target.scope}\u0000${target.scopeId}` };
|
|
825
|
-
};
|
|
826
|
-
const scopes = new Map<string, ReturnType<typeof storeKey>["target"]>();
|
|
827
|
-
for (const row of rows) {
|
|
828
|
-
const { target, scopeKey } = storeKey(row);
|
|
829
|
-
scopes.set(scopeKey, target);
|
|
830
|
-
}
|
|
831
|
-
const previews = new Map<string, string>();
|
|
832
|
-
for (const [scopeKey, target] of scopes) {
|
|
833
|
-
for (const meta of await listAppSecretsForScope(
|
|
834
|
-
target.scope,
|
|
835
|
-
target.scopeId,
|
|
836
|
-
)) {
|
|
837
|
-
previews.set(`${scopeKey}\u0000${meta.key}`, meta.last4);
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
return rows.map((row) => {
|
|
841
|
-
const preview = previews.get(
|
|
842
|
-
`${storeKey(row).scopeKey}\u0000${row.credentialKey}`,
|
|
843
|
-
);
|
|
844
|
-
// The scope listing reports a copy it cannot decrypt as an empty preview,
|
|
845
|
-
// and a stored value is never empty, so empty here means unreadable.
|
|
846
|
-
if (preview === undefined) return vaultSecretMetadata(row, "", "missing");
|
|
847
|
-
return vaultSecretMetadata(
|
|
848
|
-
row,
|
|
849
|
-
preview,
|
|
850
|
-
preview === "" ? "unreadable" : "stored",
|
|
851
|
-
);
|
|
852
|
-
});
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
export async function getVaultSecretMetadata(
|
|
856
|
-
row: VaultSecretRow | null,
|
|
857
|
-
fallback: VaultCtx = requireVaultCtx(),
|
|
858
|
-
): Promise<VaultSecretMetadata | null> {
|
|
859
|
-
if (!row) return null;
|
|
860
|
-
try {
|
|
861
|
-
const copy = await readAppSecret({
|
|
862
|
-
key: row.credentialKey,
|
|
863
|
-
...credentialStoreScopeForVaultCtx(ctxForSecretRow(row, fallback)),
|
|
864
|
-
});
|
|
865
|
-
return copy
|
|
866
|
-
? vaultSecretMetadata(row, copy.last4, "stored")
|
|
867
|
-
: vaultSecretMetadata(row, "", "missing");
|
|
868
|
-
} catch (error) {
|
|
869
|
-
if (!isUnreadableSecretError(error)) throw error;
|
|
870
|
-
return vaultSecretMetadata(row, "", "unreadable");
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
/** Core does not export `AppSecretDecryptionError`; it is recognised by its code. */
|
|
875
|
-
function isUnreadableSecretError(error: unknown): boolean {
|
|
876
|
-
return (
|
|
877
|
-
(error as { errorCode?: unknown }).errorCode === "app_secret_unreadable"
|
|
878
|
-
);
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
/** Encrypt a vault secret's value into the credential store. */
|
|
882
|
-
async function storeSecretValue(
|
|
883
|
-
ctx: VaultCtx,
|
|
884
|
-
secret: { credentialKey: string; name: string },
|
|
885
|
-
value: string,
|
|
886
|
-
): Promise<void> {
|
|
887
|
-
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
888
|
-
await writeAppSecret({
|
|
889
|
-
key: secret.credentialKey,
|
|
890
|
-
value,
|
|
891
|
-
scope: target.scope,
|
|
892
|
-
scopeId: target.scopeId,
|
|
893
|
-
description: `${VAULT_SYNC_DESCRIPTION_PREFIX} ${secret.name}`,
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
export async function cleanupSyncedCredentialKeysIfUnused(
|
|
898
|
-
ctx: VaultCtx,
|
|
899
|
-
candidateKeys?: string[],
|
|
900
|
-
) {
|
|
901
|
-
const db = getDb();
|
|
902
|
-
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
903
|
-
const keys = candidateKeys
|
|
904
|
-
? candidateKeys
|
|
905
|
-
: (await listAppSecretsForScope(target.scope, target.scopeId))
|
|
906
|
-
.filter((secret) =>
|
|
907
|
-
secret.description?.startsWith(VAULT_SYNC_DESCRIPTION_PREFIX),
|
|
908
|
-
)
|
|
909
|
-
.map((secret) => secret.key);
|
|
910
|
-
|
|
911
|
-
for (const key of new Set(keys.filter(Boolean))) {
|
|
912
|
-
const stillUsesKey = await db
|
|
913
|
-
.select({ id: schema.vaultSecrets.id })
|
|
914
|
-
.from(schema.vaultSecrets)
|
|
915
|
-
.where(
|
|
916
|
-
and(
|
|
917
|
-
eq(schema.vaultSecrets.credentialKey, key),
|
|
918
|
-
ctxScope(schema.vaultSecrets, ctx),
|
|
919
|
-
),
|
|
920
|
-
)
|
|
921
|
-
.limit(1);
|
|
922
|
-
if (!stillUsesKey[0]) {
|
|
923
|
-
await deleteAppSecret({
|
|
924
|
-
key,
|
|
925
|
-
scope: target.scope,
|
|
926
|
-
scopeId: target.scopeId,
|
|
927
|
-
});
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
|
|
932
871
|
export async function listRequests(filter?: { status?: string }) {
|
|
933
872
|
const db = getDb();
|
|
934
873
|
const ctx = requireVaultCtx();
|
|
@@ -1008,6 +947,7 @@ export async function createRequest(input: {
|
|
|
1008
947
|
|
|
1009
948
|
await recordVaultAudit({
|
|
1010
949
|
action: "request.created",
|
|
950
|
+
credentialKey: input.credentialKey,
|
|
1011
951
|
appId: input.appId,
|
|
1012
952
|
summary: `${actor} requested ${input.credentialKey} for ${input.appId}`,
|
|
1013
953
|
metadata: { requestId, reason: input.reason },
|
|
@@ -1061,7 +1001,8 @@ export async function approveRequest(
|
|
|
1061
1001
|
)
|
|
1062
1002
|
.returning();
|
|
1063
1003
|
|
|
1064
|
-
|
|
1004
|
+
const claimedRequest = claimed.at(0);
|
|
1005
|
+
if (!claimedRequest) {
|
|
1065
1006
|
const current = await getRequestForTenant(requestId, ctx);
|
|
1066
1007
|
if (current?.status === "applying") {
|
|
1067
1008
|
throw new Error("Vault request is already being applied");
|
|
@@ -1069,38 +1010,31 @@ export async function approveRequest(
|
|
|
1069
1010
|
return current;
|
|
1070
1011
|
}
|
|
1071
1012
|
|
|
1072
|
-
const claimedRequest = claimed[0];
|
|
1073
|
-
|
|
1074
1013
|
// Secret + grant must land in the REQUEST's tenant, not the approver's
|
|
1075
1014
|
// (the approver may be acting on behalf of another user in the same org).
|
|
1076
1015
|
const requestCtx = ctxForRow(claimedRequest);
|
|
1077
1016
|
|
|
1078
1017
|
try {
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
.
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
},
|
|
1097
|
-
requestCtx,
|
|
1098
|
-
);
|
|
1018
|
+
// A key that already exists in the scope is only granted: the approver's
|
|
1019
|
+
// value never overwrites a row, whether the Vault or an app wrote it.
|
|
1020
|
+
let secretId = (await listScopeSecrets(requestCtx)).find(
|
|
1021
|
+
(row) => row.credentialKey === claimedRequest.credentialKey,
|
|
1022
|
+
)?.id;
|
|
1023
|
+
|
|
1024
|
+
if (!secretId) {
|
|
1025
|
+
secretId = (
|
|
1026
|
+
await createSecret(
|
|
1027
|
+
{
|
|
1028
|
+
credentialKey: claimedRequest.credentialKey,
|
|
1029
|
+
value: secretValue,
|
|
1030
|
+
name: secretName || claimedRequest.credentialKey,
|
|
1031
|
+
},
|
|
1032
|
+
requestCtx,
|
|
1033
|
+
)
|
|
1034
|
+
).id;
|
|
1099
1035
|
}
|
|
1100
1036
|
|
|
1101
|
-
|
|
1102
|
-
await createGrant(secret.id, claimedRequest.appId, requestCtx);
|
|
1103
|
-
}
|
|
1037
|
+
await createGrant(secretId, claimedRequest.appId, requestCtx);
|
|
1104
1038
|
} catch (error) {
|
|
1105
1039
|
await db
|
|
1106
1040
|
.update(schema.vaultRequests)
|
|
@@ -1133,6 +1067,7 @@ export async function approveRequest(
|
|
|
1133
1067
|
|
|
1134
1068
|
await recordVaultAudit({
|
|
1135
1069
|
action: "request.approved",
|
|
1070
|
+
credentialKey: claimedRequest.credentialKey,
|
|
1136
1071
|
appId: claimedRequest.appId,
|
|
1137
1072
|
summary: `Approved ${claimedRequest.credentialKey} for ${claimedRequest.appId} (requested by ${claimedRequest.requestedBy})`,
|
|
1138
1073
|
metadata: { requestId, reviewer },
|
|
@@ -1171,14 +1106,14 @@ export async function denyRequest(
|
|
|
1171
1106
|
)
|
|
1172
1107
|
.returning();
|
|
1173
1108
|
|
|
1174
|
-
|
|
1109
|
+
const claimedRequest = claimed.at(0);
|
|
1110
|
+
if (!claimedRequest) {
|
|
1175
1111
|
return getRequestForTenant(requestId, ctx);
|
|
1176
1112
|
}
|
|
1177
1113
|
|
|
1178
|
-
const claimedRequest = claimed[0];
|
|
1179
|
-
|
|
1180
1114
|
await recordVaultAudit({
|
|
1181
1115
|
action: "request.denied",
|
|
1116
|
+
credentialKey: claimedRequest.credentialKey,
|
|
1182
1117
|
appId: claimedRequest.appId,
|
|
1183
1118
|
summary: `Denied ${claimedRequest.credentialKey} for ${claimedRequest.appId} (requested by ${claimedRequest.requestedBy})`,
|
|
1184
1119
|
metadata: { requestId, reviewer, reason },
|
|
@@ -1301,7 +1236,10 @@ export async function listVaultOverview() {
|
|
|
1301
1236
|
listRequests(),
|
|
1302
1237
|
getVaultAccessSettings(),
|
|
1303
1238
|
]);
|
|
1304
|
-
|
|
1239
|
+
// A grant with no key (written before grants named their key) grants nothing.
|
|
1240
|
+
const manualGrantCount = grants.filter(
|
|
1241
|
+
(g) => g.status === "active" && g.credentialKey,
|
|
1242
|
+
).length;
|
|
1305
1243
|
|
|
1306
1244
|
return {
|
|
1307
1245
|
accessMode: access.mode,
|