@agent-native/dispatch 0.42.5-nightly-20261111212500 → 0.42.5
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 +8 -46
- package/dist/actions/create-vault-secret.js +2 -2
- package/dist/actions/create-vault-secret.js.map +1 -1
- package/dist/actions/index.d.ts.map +1 -1
- package/dist/actions/index.js +0 -2
- package/dist/actions/index.js.map +1 -1
- package/dist/actions/list-vault-secrets.d.ts +1 -1
- package/dist/actions/list-vault-secrets.js +3 -4
- package/dist/actions/list-vault-secrets.js.map +1 -1
- package/dist/actions/provider-api-register.d.ts +12 -12
- package/dist/actions/update-vault-secret.js +2 -2
- package/dist/actions/update-vault-secret.js.map +1 -1
- package/dist/components/app-keys-popover.d.ts.map +1 -1
- package/dist/components/app-keys-popover.js +8 -19
- package/dist/components/app-keys-popover.js.map +1 -1
- package/dist/routes/pages/vault.d.ts.map +1 -1
- package/dist/routes/pages/vault.js +3 -9
- package/dist/routes/pages/vault.js.map +1 -1
- package/dist/server/lib/vault-store.d.ts +18 -68
- package/dist/server/lib/vault-store.d.ts.map +1 -1
- package/dist/server/lib/vault-store.js +98 -273
- package/dist/server/lib/vault-store.js.map +1 -1
- package/package.json +4 -4
- package/src/actions/create-vault-secret.ts +2 -2
- package/src/actions/index.spec.ts +1 -1
- package/src/actions/index.ts +0 -2
- package/src/actions/list-vault-secrets.ts +3 -4
- package/src/actions/update-vault-secret.ts +3 -2
- package/src/components/app-keys-popover.tsx +1 -39
- package/src/routes/pages/vault.tsx +21 -31
- package/src/server/lib/vault-store.spec.ts +377 -450
- package/src/server/lib/vault-store.ts +141 -340
- package/dist/actions/sync-vault-to-app.d.ts +0 -32
- package/dist/actions/sync-vault-to-app.d.ts.map +0 -1
- package/dist/actions/sync-vault-to-app.js +0 -13
- package/dist/actions/sync-vault-to-app.js.map +0 -1
- package/src/actions/sync-vault-to-app.ts +0 -15
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
2
|
import { and, desc, eq, isNull, or, sql } from "@agent-native/core/db/schema";
|
|
3
3
|
import { ssrfSafeFetch } from "@agent-native/core/extensions/url-safety";
|
|
4
|
-
import { deleteAppSecret,
|
|
4
|
+
import { deleteAppSecret, listAppSecretsForScope, readAppSecret, VAULT_SYNC_DESCRIPTION_PREFIX, writeAppSecret, } from "@agent-native/core/secrets";
|
|
5
5
|
import { readOrgMemberRole } from "@agent-native/core/server";
|
|
6
6
|
import { discoverAgents } from "@agent-native/core/server/agent-discovery";
|
|
7
7
|
import { getOrgSetting, getUserSetting, putOrgSetting, putUserSetting, } from "@agent-native/core/settings";
|
|
8
|
-
import { asc, gt } from "drizzle-orm";
|
|
9
8
|
import { getDb, schema } from "../../db/index.js";
|
|
10
9
|
import { currentOwnerEmail, currentOrgId, recordAudit, } from "./dispatch-store.js";
|
|
11
10
|
const VAULT_ACCESS_SETTINGS_KEY = "dispatch-vault-access-settings";
|
|
11
|
+
/**
|
|
12
|
+
* `vault_secrets.value` is NOT NULL and migrations can't drop columns, so it
|
|
13
|
+
* stays as an always-empty column; the value lives only, encrypted, in
|
|
14
|
+
* `app_secrets`. Treat "" as "no plaintext here", never as "no secret".
|
|
15
|
+
*/
|
|
16
|
+
const SEALED_VAULT_VALUE = "";
|
|
12
17
|
export function requireVaultCtx() {
|
|
13
18
|
const ownerEmail = currentOwnerEmail();
|
|
14
19
|
if (!ownerEmail) {
|
|
@@ -43,43 +48,6 @@ function now() {
|
|
|
43
48
|
function safeJson(value) {
|
|
44
49
|
return JSON.stringify(value ?? null);
|
|
45
50
|
}
|
|
46
|
-
function workspaceBaseOrigins() {
|
|
47
|
-
const out = new Set();
|
|
48
|
-
for (const value of [
|
|
49
|
-
process.env.WORKSPACE_GATEWAY_URL,
|
|
50
|
-
process.env.APP_URL,
|
|
51
|
-
process.env.URL,
|
|
52
|
-
process.env.DEPLOY_URL,
|
|
53
|
-
process.env.BETTER_AUTH_URL,
|
|
54
|
-
]) {
|
|
55
|
-
if (!value)
|
|
56
|
-
continue;
|
|
57
|
-
try {
|
|
58
|
-
out.add(new URL(value).origin);
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
// Ignore malformed deploy metadata.
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return out;
|
|
65
|
-
}
|
|
66
|
-
export function isTrustedEnvVarSyncAgentUrl(agentUrl) {
|
|
67
|
-
let parsed;
|
|
68
|
-
try {
|
|
69
|
-
parsed = new URL(agentUrl);
|
|
70
|
-
}
|
|
71
|
-
catch {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
const hostname = parsed.hostname.toLowerCase();
|
|
75
|
-
if (hostname === "localhost" ||
|
|
76
|
-
hostname === "127.0.0.1" ||
|
|
77
|
-
hostname === "::1" ||
|
|
78
|
-
hostname.endsWith(".localhost")) {
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
return workspaceBaseOrigins().has(parsed.origin);
|
|
82
|
-
}
|
|
83
51
|
function scopedFilter(table) {
|
|
84
52
|
return ctxScope(table, requireVaultCtx());
|
|
85
53
|
}
|
|
@@ -218,6 +186,8 @@ export async function createSecret(input, ctx = requireVaultCtx()) {
|
|
|
218
186
|
const credentialKey = normalizeCredentialKey(input.credentialKey);
|
|
219
187
|
if (!credentialKey)
|
|
220
188
|
throw new Error("Credential key is required");
|
|
189
|
+
if (!input.value)
|
|
190
|
+
throw new Error("Secret value is required");
|
|
221
191
|
const existing = await db
|
|
222
192
|
.select()
|
|
223
193
|
.from(schema.vaultSecrets)
|
|
@@ -225,34 +195,32 @@ export async function createSecret(input, ctx = requireVaultCtx()) {
|
|
|
225
195
|
.orderBy(desc(schema.vaultSecrets.updatedAt))
|
|
226
196
|
.limit(1);
|
|
227
197
|
if (existing[0]) {
|
|
198
|
+
const found = existing[0];
|
|
228
199
|
await db
|
|
229
200
|
.update(schema.vaultSecrets)
|
|
230
201
|
.set({
|
|
231
202
|
name: input.name,
|
|
232
203
|
credentialKey,
|
|
233
|
-
value:
|
|
204
|
+
value: SEALED_VAULT_VALUE,
|
|
234
205
|
provider: input.provider || null,
|
|
235
206
|
description: input.description || null,
|
|
236
207
|
updatedAt: timestamp,
|
|
237
208
|
})
|
|
238
|
-
.where(and(eq(schema.vaultSecrets.id,
|
|
209
|
+
.where(and(eq(schema.vaultSecrets.id, found.id), ctxScope(schema.vaultSecrets, ctx)));
|
|
210
|
+
await storeSecretValue(ctxForSecretRow(found, ctx), { credentialKey, name: input.name }, input.value);
|
|
239
211
|
await recordVaultAudit({
|
|
240
212
|
action: "secret.updated",
|
|
241
|
-
secretId:
|
|
213
|
+
secretId: found.id,
|
|
242
214
|
summary: `Updated secret "${input.name}" (${credentialKey})`,
|
|
243
215
|
metadata: { credentialKey, provider: input.provider },
|
|
244
216
|
});
|
|
245
217
|
await recordAudit({
|
|
246
218
|
action: "vault.secret.updated",
|
|
247
219
|
targetType: "vault-secret",
|
|
248
|
-
targetId:
|
|
220
|
+
targetId: found.id,
|
|
249
221
|
summary: `Updated vault secret "${input.name}" (${credentialKey})`,
|
|
250
222
|
});
|
|
251
|
-
|
|
252
|
-
if (updated) {
|
|
253
|
-
await syncSecretsToCredentialStore([updated], ctxForSecretRow(updated, ctx));
|
|
254
|
-
}
|
|
255
|
-
return updated;
|
|
223
|
+
return getSecret(found.id, ctx);
|
|
256
224
|
}
|
|
257
225
|
const secretId = id();
|
|
258
226
|
const actor = ctx.ownerEmail;
|
|
@@ -262,13 +230,14 @@ export async function createSecret(input, ctx = requireVaultCtx()) {
|
|
|
262
230
|
orgId: ctx.orgId,
|
|
263
231
|
name: input.name,
|
|
264
232
|
credentialKey,
|
|
265
|
-
value:
|
|
233
|
+
value: SEALED_VAULT_VALUE,
|
|
266
234
|
provider: input.provider || null,
|
|
267
235
|
description: input.description || null,
|
|
268
236
|
createdBy: actor,
|
|
269
237
|
createdAt: timestamp,
|
|
270
238
|
updatedAt: timestamp,
|
|
271
239
|
});
|
|
240
|
+
await storeSecretValue(ctx, { credentialKey, name: input.name }, input.value);
|
|
272
241
|
await recordVaultAudit({
|
|
273
242
|
action: "secret.created",
|
|
274
243
|
secretId,
|
|
@@ -281,11 +250,7 @@ export async function createSecret(input, ctx = requireVaultCtx()) {
|
|
|
281
250
|
targetId: secretId,
|
|
282
251
|
summary: `Created vault secret "${input.name}" (${credentialKey})`,
|
|
283
252
|
});
|
|
284
|
-
|
|
285
|
-
if (created) {
|
|
286
|
-
await syncSecretsToCredentialStore([created], ctxForSecretRow(created, ctx));
|
|
287
|
-
}
|
|
288
|
-
return created;
|
|
253
|
+
return getSecret(secretId, ctx);
|
|
289
254
|
}
|
|
290
255
|
export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
291
256
|
await assertCanManageVault();
|
|
@@ -302,9 +267,9 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
302
267
|
const name = patch.name !== undefined ? patch.name.trim() : existing.name;
|
|
303
268
|
if (!name)
|
|
304
269
|
throw new Error("Secret name is required");
|
|
305
|
-
|
|
306
|
-
if (!value)
|
|
270
|
+
if (patch.value !== undefined && !patch.value) {
|
|
307
271
|
throw new Error("Secret value is required");
|
|
272
|
+
}
|
|
308
273
|
const provider = patch.provider !== undefined ? patch.provider || null : existing.provider;
|
|
309
274
|
const description = patch.description !== undefined
|
|
310
275
|
? patch.description || null
|
|
@@ -319,17 +284,34 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
319
284
|
throw new Error(`Credential key "${credentialKey}" is already in use`);
|
|
320
285
|
}
|
|
321
286
|
}
|
|
287
|
+
const storeCtx = ctxForSecretRow(existing, ctx);
|
|
288
|
+
let valueForKey = patch.value;
|
|
289
|
+
if (!valueForKey && credentialKey !== existing.credentialKey) {
|
|
290
|
+
// A rename carries the stored value to the new key. A failed read throws;
|
|
291
|
+
// only a missing copy means there is nothing to move.
|
|
292
|
+
const current = await readAppSecret({
|
|
293
|
+
key: existing.credentialKey,
|
|
294
|
+
...credentialStoreScopeForVaultCtx(storeCtx),
|
|
295
|
+
});
|
|
296
|
+
valueForKey = current?.value;
|
|
297
|
+
}
|
|
322
298
|
await db
|
|
323
299
|
.update(schema.vaultSecrets)
|
|
324
300
|
.set({
|
|
325
301
|
name,
|
|
326
302
|
credentialKey,
|
|
327
|
-
value
|
|
303
|
+
// Only a new value empties the row. A row that still holds an older
|
|
304
|
+
// plaintext value keeps it until it is cleared in bulk, so a label edit
|
|
305
|
+
// can never write that stale value over a newer one in the credential store.
|
|
306
|
+
...(patch.value !== undefined ? { value: SEALED_VAULT_VALUE } : {}),
|
|
328
307
|
provider,
|
|
329
308
|
description,
|
|
330
309
|
updatedAt: now(),
|
|
331
310
|
})
|
|
332
311
|
.where(and(eq(schema.vaultSecrets.id, secretId), ctxScope(schema.vaultSecrets, ctx)));
|
|
312
|
+
if (valueForKey) {
|
|
313
|
+
await storeSecretValue(storeCtx, { credentialKey, name }, valueForKey);
|
|
314
|
+
}
|
|
333
315
|
const auditMetadata = {
|
|
334
316
|
name,
|
|
335
317
|
previousName: name !== existing.name ? existing.name : undefined,
|
|
@@ -341,7 +323,7 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
341
323
|
previousProvider: provider !== existing.provider ? existing.provider : undefined,
|
|
342
324
|
description,
|
|
343
325
|
previousDescription: description !== existing.description ? existing.description : undefined,
|
|
344
|
-
valueChanged: value !==
|
|
326
|
+
valueChanged: patch.value !== undefined ? true : undefined,
|
|
345
327
|
};
|
|
346
328
|
await recordVaultAudit({
|
|
347
329
|
action: "secret.updated",
|
|
@@ -357,9 +339,6 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
357
339
|
metadata: auditMetadata,
|
|
358
340
|
});
|
|
359
341
|
const updated = await getSecret(secretId, ctx);
|
|
360
|
-
if (updated) {
|
|
361
|
-
await syncSecretsToCredentialStore([updated], ctxForSecretRow(updated, ctx));
|
|
362
|
-
}
|
|
363
342
|
if (updated && credentialKey !== existing.credentialKey) {
|
|
364
343
|
await cleanupSyncedCredentialKeysIfUnused(ctxForRow(existing), [
|
|
365
344
|
existing.credentialKey,
|
|
@@ -552,14 +531,13 @@ export async function revokeGrant(grantId, ctx = requireVaultCtx()) {
|
|
|
552
531
|
});
|
|
553
532
|
return getGrant(grantId, ctx);
|
|
554
533
|
}
|
|
555
|
-
|
|
556
|
-
if (!secret)
|
|
557
|
-
return null;
|
|
534
|
+
function vaultSecretMetadata(secret, last4Value, valueStatus) {
|
|
558
535
|
return {
|
|
559
536
|
id: secret.id,
|
|
560
537
|
name: secret.name,
|
|
561
538
|
credentialKey: secret.credentialKey,
|
|
562
|
-
last4:
|
|
539
|
+
last4: last4Value,
|
|
540
|
+
valueStatus,
|
|
563
541
|
provider: secret.provider,
|
|
564
542
|
description: secret.description,
|
|
565
543
|
createdBy: secret.createdBy,
|
|
@@ -572,108 +550,68 @@ export function credentialStoreScopeForVaultCtx(ctx) {
|
|
|
572
550
|
return { scope: "org", scopeId: ctx.orgId };
|
|
573
551
|
return { scope: "workspace", scopeId: `solo:${ctx.ownerEmail}` };
|
|
574
552
|
}
|
|
575
|
-
export async function syncSecretsToCredentialStore(secrets, ctx) {
|
|
576
|
-
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
577
|
-
const syncedKeys = [];
|
|
578
|
-
for (const secret of secrets) {
|
|
579
|
-
if (!secret.credentialKey || !secret.value)
|
|
580
|
-
continue;
|
|
581
|
-
await writeAppSecret({
|
|
582
|
-
key: secret.credentialKey,
|
|
583
|
-
value: secret.value,
|
|
584
|
-
scope: target.scope,
|
|
585
|
-
scopeId: target.scopeId,
|
|
586
|
-
description: `${VAULT_SYNC_DESCRIPTION_PREFIX} ${secret.name}`,
|
|
587
|
-
});
|
|
588
|
-
syncedKeys.push(secret.credentialKey);
|
|
589
|
-
}
|
|
590
|
-
return { ...target, keys: syncedKeys };
|
|
591
|
-
}
|
|
592
553
|
/**
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
* under whoever happens to be syncing: `writeAppSecret` upserts, so syncing
|
|
597
|
-
* with the caller's ctx copies credential material into the caller's org
|
|
598
|
-
* instead of moving it, and it accumulates there permanently.
|
|
554
|
+
* Masked metadata for a page of vault rows. The preview is read from the
|
|
555
|
+
* encrypted copy in the credential store, one read per store scope, because
|
|
556
|
+
* the vault row itself holds no value.
|
|
599
557
|
*/
|
|
600
|
-
function
|
|
601
|
-
const
|
|
558
|
+
export async function listVaultSecretMetadata(rows, fallback = requireVaultCtx()) {
|
|
559
|
+
const storeKey = (row) => {
|
|
560
|
+
const target = credentialStoreScopeForVaultCtx(ctxForSecretRow(row, fallback));
|
|
561
|
+
return { target, scopeKey: `${target.scope}\u0000${target.scopeId}` };
|
|
562
|
+
};
|
|
563
|
+
const scopes = new Map();
|
|
602
564
|
for (const row of rows) {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
groups.set(groupKey, { ctx, rows: [row] });
|
|
565
|
+
const { target, scopeKey } = storeKey(row);
|
|
566
|
+
scopes.set(scopeKey, target);
|
|
567
|
+
}
|
|
568
|
+
const previews = new Map();
|
|
569
|
+
for (const [scopeKey, target] of scopes) {
|
|
570
|
+
for (const meta of await listAppSecretsForScope(target.scope, target.scopeId)) {
|
|
571
|
+
previews.set(`${scopeKey}\u0000${meta.key}`, meta.last4);
|
|
613
572
|
}
|
|
614
573
|
}
|
|
615
|
-
return
|
|
574
|
+
return rows.map((row) => {
|
|
575
|
+
const preview = previews.get(`${storeKey(row).scopeKey}\u0000${row.credentialKey}`);
|
|
576
|
+
// The scope listing reports a copy it cannot decrypt as an empty preview,
|
|
577
|
+
// and a stored value is never empty, so empty here means unreadable.
|
|
578
|
+
if (preview === undefined)
|
|
579
|
+
return vaultSecretMetadata(row, "", "missing");
|
|
580
|
+
return vaultSecretMetadata(row, preview, preview === "" ? "unreadable" : "stored");
|
|
581
|
+
});
|
|
616
582
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
* fails, `nextCursor` rewinds to before its first row so the next pass retries
|
|
634
|
-
* it; check `failedGroups` before treating a null cursor as completion.
|
|
635
|
-
*/
|
|
636
|
-
export async function resyncVaultSecretsToCredentialStorePage(options = {}) {
|
|
637
|
-
const requestedLimit = options.limit ?? 100;
|
|
638
|
-
const limit = Number.isFinite(requestedLimit)
|
|
639
|
-
? Math.min(500, Math.max(1, Math.floor(requestedLimit)))
|
|
640
|
-
: 100;
|
|
641
|
-
const db = getDb();
|
|
642
|
-
const rowsWithNext = await db
|
|
643
|
-
.select()
|
|
644
|
-
.from(schema.vaultSecrets)
|
|
645
|
-
.where(options.afterId ? gt(schema.vaultSecrets.id, options.afterId) : undefined)
|
|
646
|
-
.orderBy(asc(schema.vaultSecrets.id))
|
|
647
|
-
.limit(limit + 1);
|
|
648
|
-
const hasMore = rowsWithNext.length > limit;
|
|
649
|
-
const rows = hasMore ? rowsWithNext.slice(0, limit) : rowsWithNext;
|
|
650
|
-
const groups = groupSecretsByTenant(rows, ctxForRow);
|
|
651
|
-
let failedGroups = 0;
|
|
652
|
-
let syncedKeys = 0;
|
|
653
|
-
let firstFailedRowIndex = Number.POSITIVE_INFINITY;
|
|
654
|
-
for (const { ctx, rows: groupRows } of groups) {
|
|
655
|
-
try {
|
|
656
|
-
const result = await syncSecretsToCredentialStore(groupRows, ctx);
|
|
657
|
-
syncedKeys += result.keys.length;
|
|
658
|
-
}
|
|
659
|
-
catch (error) {
|
|
660
|
-
failedGroups++;
|
|
661
|
-
const firstRowIndex = rows.findIndex((row) => row.id === groupRows[0]?.id);
|
|
662
|
-
firstFailedRowIndex = Math.min(firstFailedRowIndex, firstRowIndex);
|
|
663
|
-
const keyNames = groupRows.map((row) => row.credentialKey).join(", ");
|
|
664
|
-
console.warn(`[dispatch] vault boot resync failed for org=${ctx.orgId ?? "(solo)"} owner=${ctx.ownerEmail}; affected keys: ${keyNames}`, error instanceof Error ? error.message : error);
|
|
665
|
-
}
|
|
583
|
+
export async function getVaultSecretMetadata(row, fallback = requireVaultCtx()) {
|
|
584
|
+
if (!row)
|
|
585
|
+
return null;
|
|
586
|
+
try {
|
|
587
|
+
const copy = await readAppSecret({
|
|
588
|
+
key: row.credentialKey,
|
|
589
|
+
...credentialStoreScopeForVaultCtx(ctxForSecretRow(row, fallback)),
|
|
590
|
+
});
|
|
591
|
+
return copy
|
|
592
|
+
? vaultSecretMetadata(row, copy.last4, "stored")
|
|
593
|
+
: vaultSecretMetadata(row, "", "missing");
|
|
594
|
+
}
|
|
595
|
+
catch (error) {
|
|
596
|
+
if (!isUnreadableSecretError(error))
|
|
597
|
+
throw error;
|
|
598
|
+
return vaultSecretMetadata(row, "", "unreadable");
|
|
666
599
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
600
|
+
}
|
|
601
|
+
/** Core does not export `AppSecretDecryptionError`; it is recognised by its code. */
|
|
602
|
+
function isUnreadableSecretError(error) {
|
|
603
|
+
return (error.errorCode === "app_secret_unreadable");
|
|
604
|
+
}
|
|
605
|
+
/** Encrypt a vault secret's value into the credential store. */
|
|
606
|
+
async function storeSecretValue(ctx, secret, value) {
|
|
607
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
608
|
+
await writeAppSecret({
|
|
609
|
+
key: secret.credentialKey,
|
|
610
|
+
value,
|
|
611
|
+
scope: target.scope,
|
|
612
|
+
scopeId: target.scopeId,
|
|
613
|
+
description: `${VAULT_SYNC_DESCRIPTION_PREFIX} ${secret.name}`,
|
|
614
|
+
});
|
|
677
615
|
}
|
|
678
616
|
export async function cleanupSyncedCredentialKeysIfUnused(ctx, candidateKeys) {
|
|
679
617
|
const db = getDb();
|
|
@@ -698,119 +636,6 @@ export async function cleanupSyncedCredentialKeysIfUnused(ctx, candidateKeys) {
|
|
|
698
636
|
}
|
|
699
637
|
}
|
|
700
638
|
}
|
|
701
|
-
export async function syncGrantsToApp(appId, ctx = requireVaultCtx()) {
|
|
702
|
-
await assertCanManageVault();
|
|
703
|
-
const db = getDb();
|
|
704
|
-
const access = await getVaultAccessSettings();
|
|
705
|
-
const agents = await discoverAgents("dispatch");
|
|
706
|
-
const agent = agents.find((a) => a.id === appId);
|
|
707
|
-
if (!agent)
|
|
708
|
-
throw new Error(`App "${appId}" not found in agent registry`);
|
|
709
|
-
const secretsToSync = [];
|
|
710
|
-
const activeGrants = access.mode === "manual"
|
|
711
|
-
? (await listGrants({ appId })).filter((g) => g.status === "active")
|
|
712
|
-
: [];
|
|
713
|
-
if (access.mode === "all-apps") {
|
|
714
|
-
const secrets = await listSecrets();
|
|
715
|
-
for (const secret of secrets) {
|
|
716
|
-
secretsToSync.push(secret);
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
else {
|
|
720
|
-
for (const grant of activeGrants) {
|
|
721
|
-
const secret = await getSecret(grant.secretId, ctx);
|
|
722
|
-
if (secret) {
|
|
723
|
-
secretsToSync.push(secret);
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
if (secretsToSync.length === 0) {
|
|
728
|
-
return {
|
|
729
|
-
appId,
|
|
730
|
-
accessMode: access.mode,
|
|
731
|
-
synced: 0,
|
|
732
|
-
keys: [],
|
|
733
|
-
credentialStores: [],
|
|
734
|
-
};
|
|
735
|
-
}
|
|
736
|
-
const credentialStoreGroups = groupSecretsByTenant(secretsToSync, (row) => ctxForSecretRow(row, ctx));
|
|
737
|
-
const credentialStores = [];
|
|
738
|
-
const credentialStoreKeys = [];
|
|
739
|
-
for (const group of credentialStoreGroups) {
|
|
740
|
-
const result = await syncSecretsToCredentialStore(group.rows, group.ctx);
|
|
741
|
-
credentialStores.push({
|
|
742
|
-
scope: result.scope,
|
|
743
|
-
scopeId: result.scopeId,
|
|
744
|
-
synced: result.keys.length,
|
|
745
|
-
});
|
|
746
|
-
credentialStoreKeys.push(...result.keys);
|
|
747
|
-
}
|
|
748
|
-
const vars = secretsToSync.map((secret) => ({
|
|
749
|
-
key: secret.credentialKey,
|
|
750
|
-
value: secret.value,
|
|
751
|
-
}));
|
|
752
|
-
let envVarSync;
|
|
753
|
-
if (!isTrustedEnvVarSyncAgentUrl(agent.url)) {
|
|
754
|
-
envVarSync = {
|
|
755
|
-
status: "skipped",
|
|
756
|
-
reason: "env-var sync is limited to localhost or workspace-owned apps",
|
|
757
|
-
};
|
|
758
|
-
}
|
|
759
|
-
else {
|
|
760
|
-
try {
|
|
761
|
-
const res = await ssrfSafeFetch(`${agent.url}/_agent-native/env-vars`, {
|
|
762
|
-
method: "POST",
|
|
763
|
-
headers: { "Content-Type": "application/json" },
|
|
764
|
-
body: JSON.stringify({ vars }),
|
|
765
|
-
signal: AbortSignal.timeout(10_000),
|
|
766
|
-
}, { maxRedirects: 3 });
|
|
767
|
-
if (res.ok) {
|
|
768
|
-
const result = await res.json();
|
|
769
|
-
envVarSync = { status: "synced", keys: result.saved || [] };
|
|
770
|
-
}
|
|
771
|
-
else {
|
|
772
|
-
const err = await res.text().catch(() => "Unknown error");
|
|
773
|
-
envVarSync = { status: "skipped", reason: err };
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
catch (err) {
|
|
777
|
-
envVarSync = {
|
|
778
|
-
status: "failed",
|
|
779
|
-
reason: err instanceof Error ? err.message : String(err),
|
|
780
|
-
};
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
const syncedKeys = credentialStoreKeys;
|
|
784
|
-
const timestamp = now();
|
|
785
|
-
for (const grant of activeGrants) {
|
|
786
|
-
const secret = await getSecret(grant.secretId, ctx);
|
|
787
|
-
if (secret && syncedKeys.includes(secret.credentialKey)) {
|
|
788
|
-
await db
|
|
789
|
-
.update(schema.vaultGrants)
|
|
790
|
-
.set({ syncedAt: timestamp, updatedAt: timestamp })
|
|
791
|
-
.where(eq(schema.vaultGrants.id, grant.id));
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
await recordVaultAudit({
|
|
795
|
-
action: "secret.synced",
|
|
796
|
-
appId,
|
|
797
|
-
summary: `Synced ${syncedKeys.length} secret(s) to ${appId}: ${syncedKeys.join(", ")}`,
|
|
798
|
-
metadata: {
|
|
799
|
-
syncedKeys,
|
|
800
|
-
accessMode: access.mode,
|
|
801
|
-
credentialStores,
|
|
802
|
-
envVars: envVarSync,
|
|
803
|
-
},
|
|
804
|
-
});
|
|
805
|
-
return {
|
|
806
|
-
appId,
|
|
807
|
-
accessMode: access.mode,
|
|
808
|
-
synced: syncedKeys.length,
|
|
809
|
-
keys: syncedKeys,
|
|
810
|
-
credentialStores,
|
|
811
|
-
envVars: envVarSync,
|
|
812
|
-
};
|
|
813
|
-
}
|
|
814
639
|
export async function listRequests(filter) {
|
|
815
640
|
const db = getDb();
|
|
816
641
|
const ctx = requireVaultCtx();
|