@agent-native/dispatch 0.42.7-nightly-20261113032548 → 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 -12
- 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
|
@@ -1,19 +1,13 @@
|
|
|
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 {
|
|
4
|
+
import { deleteAppSecretById, isVaultManagedSecret, listAppSecretsForScope, listAppSecretSummaries, readAppSecretMetaById, updateAppSecretById, 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
8
|
import { getDb, schema } from "../../db/index.js";
|
|
9
9
|
import { currentOwnerEmail, currentOrgId, recordAudit, } from "./dispatch-store.js";
|
|
10
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 = "";
|
|
17
11
|
export function requireVaultCtx() {
|
|
18
12
|
const ownerEmail = currentOwnerEmail();
|
|
19
13
|
if (!ownerEmail) {
|
|
@@ -36,9 +30,6 @@ function tenantScope(table, ctx) {
|
|
|
36
30
|
function ctxForRow(row) {
|
|
37
31
|
return { ownerEmail: row.ownerEmail, orgId: row.orgId };
|
|
38
32
|
}
|
|
39
|
-
function ctxForSecretRow(row, fallback) {
|
|
40
|
-
return row.ownerEmail ? ctxForRow(row) : fallback;
|
|
41
|
-
}
|
|
42
33
|
function id() {
|
|
43
34
|
return crypto.randomUUID();
|
|
44
35
|
}
|
|
@@ -123,6 +114,14 @@ export async function setVaultAccessSettings(input) {
|
|
|
123
114
|
}
|
|
124
115
|
export async function recordVaultAudit(input) {
|
|
125
116
|
const db = getDb();
|
|
117
|
+
const metadata = input.credentialKey || input.metadata
|
|
118
|
+
? {
|
|
119
|
+
...(input.credentialKey
|
|
120
|
+
? { credentialKey: input.credentialKey }
|
|
121
|
+
: {}),
|
|
122
|
+
...input.metadata,
|
|
123
|
+
}
|
|
124
|
+
: null;
|
|
126
125
|
await db.insert(schema.vaultAuditLog).values({
|
|
127
126
|
id: id(),
|
|
128
127
|
ownerEmail: currentOwnerEmail(),
|
|
@@ -132,7 +131,7 @@ export async function recordVaultAudit(input) {
|
|
|
132
131
|
action: input.action,
|
|
133
132
|
actor: input.actor || currentOwnerEmail(),
|
|
134
133
|
summary: input.summary,
|
|
135
|
-
metadata:
|
|
134
|
+
metadata: metadata ? safeJson(metadata) : null,
|
|
136
135
|
createdAt: now(),
|
|
137
136
|
});
|
|
138
137
|
}
|
|
@@ -146,103 +145,151 @@ export async function listVaultAudit(limit = 50) {
|
|
|
146
145
|
.orderBy(desc(schema.vaultAuditLog.createdAt))
|
|
147
146
|
.limit(limit);
|
|
148
147
|
}
|
|
148
|
+
/** A key an earlier release saved carries that release's marker in its description; it is not the user's text. */
|
|
149
|
+
function visibleDescription(description) {
|
|
150
|
+
return description?.startsWith(VAULT_SYNC_DESCRIPTION_PREFIX)
|
|
151
|
+
? null
|
|
152
|
+
: description;
|
|
153
|
+
}
|
|
154
|
+
function vaultSecretRow(row) {
|
|
155
|
+
return {
|
|
156
|
+
id: row.id,
|
|
157
|
+
name: row.label || row.key,
|
|
158
|
+
credentialKey: row.key,
|
|
159
|
+
provider: row.provider,
|
|
160
|
+
description: visibleDescription(row.description),
|
|
161
|
+
createdBy: row.createdBy,
|
|
162
|
+
createdAt: row.createdAt,
|
|
163
|
+
updatedAt: row.updatedAt,
|
|
164
|
+
managed: isVaultManagedSecret(row),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function vaultSecretMetadata(meta) {
|
|
168
|
+
return {
|
|
169
|
+
id: meta.id,
|
|
170
|
+
name: meta.label || meta.key,
|
|
171
|
+
credentialKey: meta.key,
|
|
172
|
+
last4: meta.last4,
|
|
173
|
+
// The scope listing reports a value it cannot decrypt as an empty
|
|
174
|
+
// preview, and a stored value is never empty.
|
|
175
|
+
valueStatus: meta.last4 === "" ? "unreadable" : "stored",
|
|
176
|
+
provider: meta.provider,
|
|
177
|
+
description: visibleDescription(meta.description),
|
|
178
|
+
createdBy: meta.createdBy,
|
|
179
|
+
createdAt: meta.createdAt,
|
|
180
|
+
updatedAt: meta.updatedAt,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Where a vault's keys live in the credential store: the organization's scope,
|
|
185
|
+
* or the caller's own `solo:` scope when no organization is active. A key is
|
|
186
|
+
* its `app_secrets` row there, and its id is that row's id.
|
|
187
|
+
*/
|
|
188
|
+
export function credentialStoreScopeForVaultCtx(ctx) {
|
|
189
|
+
if (ctx.orgId)
|
|
190
|
+
return { scope: "org", scopeId: ctx.orgId };
|
|
191
|
+
return { scope: "workspace", scopeId: `solo:${ctx.ownerEmail}` };
|
|
192
|
+
}
|
|
193
|
+
async function listScopeSecrets(ctx) {
|
|
194
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
195
|
+
const rows = await listAppSecretSummaries(target.scope, target.scopeId);
|
|
196
|
+
return rows.map(vaultSecretRow);
|
|
197
|
+
}
|
|
198
|
+
async function findSecret(secretId, ctx) {
|
|
199
|
+
const rows = await listScopeSecrets(ctx);
|
|
200
|
+
return rows.find((row) => row.id === secretId) ?? null;
|
|
201
|
+
}
|
|
202
|
+
async function requireSecretMetadata(secretId, ctx) {
|
|
203
|
+
const secret = await getSecret(secretId, ctx);
|
|
204
|
+
if (!secret)
|
|
205
|
+
throw new Error("Secret not found");
|
|
206
|
+
return secret;
|
|
207
|
+
}
|
|
208
|
+
/** Every key in the vault's scope with a masked preview. Never returns a value. */
|
|
149
209
|
export async function listSecrets() {
|
|
150
210
|
await assertCanManageVault();
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
.from(schema.vaultSecrets)
|
|
155
|
-
.where(scopedFilter(schema.vaultSecrets))
|
|
156
|
-
.orderBy(desc(schema.vaultSecrets.updatedAt));
|
|
211
|
+
const target = credentialStoreScopeForVaultCtx(requireVaultCtx());
|
|
212
|
+
const rows = await listAppSecretsForScope(target.scope, target.scopeId);
|
|
213
|
+
return rows.map(vaultSecretMetadata);
|
|
157
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* The keys the caller may pick from. Admins get everything in the vault's
|
|
217
|
+
* scope, as on the Vault page. Members get only keys the Vault manages, so an
|
|
218
|
+
* app's own organization keys stay out of reach of a picker any member can open.
|
|
219
|
+
*/
|
|
158
220
|
export async function listSecretOptions() {
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.orderBy(desc(schema.vaultSecrets.updatedAt));
|
|
221
|
+
const rows = await listScopeSecrets(requireVaultCtx());
|
|
222
|
+
const visible = (await canManageVault())
|
|
223
|
+
? rows
|
|
224
|
+
: rows.filter((row) => row.managed);
|
|
225
|
+
return visible.map(({ id, name, credentialKey, provider, description }) => ({
|
|
226
|
+
id,
|
|
227
|
+
name,
|
|
228
|
+
credentialKey,
|
|
229
|
+
provider,
|
|
230
|
+
description,
|
|
231
|
+
}));
|
|
171
232
|
}
|
|
172
|
-
export async function getSecret(secretId, ctx) {
|
|
233
|
+
export async function getSecret(secretId, ctx = requireVaultCtx()) {
|
|
173
234
|
await assertCanManageVault();
|
|
174
|
-
const
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
.from(schema.vaultSecrets)
|
|
178
|
-
.where(and(eq(schema.vaultSecrets.id, secretId), ctxScope(schema.vaultSecrets, ctx)))
|
|
179
|
-
.limit(1);
|
|
180
|
-
return row ?? null;
|
|
235
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
236
|
+
const meta = await readAppSecretMetaById({ id: secretId, ...target });
|
|
237
|
+
return meta ? vaultSecretMetadata(meta) : null;
|
|
181
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Save a key. A key that already exists in the scope is updated, which is the
|
|
241
|
+
* caller asking for exactly that; nothing else here ever overwrites a row.
|
|
242
|
+
* The value goes straight into the encrypted column of its `app_secrets` row.
|
|
243
|
+
*/
|
|
182
244
|
export async function createSecret(input, ctx = requireVaultCtx()) {
|
|
183
245
|
await assertCanManageVault();
|
|
184
|
-
const db = getDb();
|
|
185
|
-
const timestamp = now();
|
|
186
246
|
const credentialKey = normalizeCredentialKey(input.credentialKey);
|
|
187
247
|
if (!credentialKey)
|
|
188
248
|
throw new Error("Credential key is required");
|
|
189
249
|
if (!input.value)
|
|
190
250
|
throw new Error("Secret value is required");
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (existing[0]) {
|
|
198
|
-
const found = existing[0];
|
|
199
|
-
await db
|
|
200
|
-
.update(schema.vaultSecrets)
|
|
201
|
-
.set({
|
|
202
|
-
name: input.name,
|
|
203
|
-
credentialKey,
|
|
204
|
-
value: SEALED_VAULT_VALUE,
|
|
251
|
+
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
252
|
+
const existing = (await listScopeSecrets(ctx)).find((row) => row.credentialKey === credentialKey);
|
|
253
|
+
if (existing) {
|
|
254
|
+
const updated = await updateAppSecretById({ id: existing.id, ...target }, {
|
|
255
|
+
value: input.value,
|
|
256
|
+
label: input.name,
|
|
205
257
|
provider: input.provider || null,
|
|
206
258
|
description: input.description || null,
|
|
207
|
-
|
|
208
|
-
})
|
|
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);
|
|
211
|
-
await recordVaultAudit({
|
|
212
|
-
action: "secret.updated",
|
|
213
|
-
secretId: found.id,
|
|
214
|
-
summary: `Updated secret "${input.name}" (${credentialKey})`,
|
|
215
|
-
metadata: { credentialKey, provider: input.provider },
|
|
216
|
-
});
|
|
217
|
-
await recordAudit({
|
|
218
|
-
action: "vault.secret.updated",
|
|
219
|
-
targetType: "vault-secret",
|
|
220
|
-
targetId: found.id,
|
|
221
|
-
summary: `Updated vault secret "${input.name}" (${credentialKey})`,
|
|
259
|
+
adoptedBy: ctx.ownerEmail,
|
|
222
260
|
});
|
|
223
|
-
|
|
261
|
+
if (updated) {
|
|
262
|
+
await recordVaultAudit({
|
|
263
|
+
action: "secret.updated",
|
|
264
|
+
secretId: existing.id,
|
|
265
|
+
credentialKey,
|
|
266
|
+
summary: `Updated secret "${input.name}" (${credentialKey})`,
|
|
267
|
+
metadata: { provider: input.provider },
|
|
268
|
+
});
|
|
269
|
+
await recordAudit({
|
|
270
|
+
action: "vault.secret.updated",
|
|
271
|
+
targetType: "vault-secret",
|
|
272
|
+
targetId: existing.id,
|
|
273
|
+
summary: `Updated vault secret "${input.name}" (${credentialKey})`,
|
|
274
|
+
});
|
|
275
|
+
return requireSecretMetadata(existing.id, ctx);
|
|
276
|
+
}
|
|
224
277
|
}
|
|
225
|
-
const secretId =
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
orgId: ctx.orgId,
|
|
231
|
-
name: input.name,
|
|
232
|
-
credentialKey,
|
|
233
|
-
value: SEALED_VAULT_VALUE,
|
|
278
|
+
const secretId = await writeAppSecret({
|
|
279
|
+
key: credentialKey,
|
|
280
|
+
value: input.value,
|
|
281
|
+
...target,
|
|
282
|
+
label: input.name,
|
|
234
283
|
provider: input.provider || null,
|
|
235
|
-
description: input.description
|
|
236
|
-
createdBy:
|
|
237
|
-
createdAt: timestamp,
|
|
238
|
-
updatedAt: timestamp,
|
|
284
|
+
...(input.description ? { description: input.description } : {}),
|
|
285
|
+
createdBy: ctx.ownerEmail,
|
|
239
286
|
});
|
|
240
|
-
await storeSecretValue(ctx, { credentialKey, name: input.name }, input.value);
|
|
241
287
|
await recordVaultAudit({
|
|
242
288
|
action: "secret.created",
|
|
243
289
|
secretId,
|
|
290
|
+
credentialKey,
|
|
244
291
|
summary: `Created secret "${input.name}" (${credentialKey})`,
|
|
245
|
-
metadata: {
|
|
292
|
+
metadata: { provider: input.provider },
|
|
246
293
|
});
|
|
247
294
|
await recordAudit({
|
|
248
295
|
action: "vault.secret.created",
|
|
@@ -250,12 +297,11 @@ export async function createSecret(input, ctx = requireVaultCtx()) {
|
|
|
250
297
|
targetId: secretId,
|
|
251
298
|
summary: `Created vault secret "${input.name}" (${credentialKey})`,
|
|
252
299
|
});
|
|
253
|
-
return
|
|
300
|
+
return requireSecretMetadata(secretId, ctx);
|
|
254
301
|
}
|
|
255
302
|
export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
256
303
|
await assertCanManageVault();
|
|
257
|
-
const
|
|
258
|
-
const existing = await getSecret(secretId, ctx);
|
|
304
|
+
const existing = await findSecret(secretId, ctx);
|
|
259
305
|
if (!existing)
|
|
260
306
|
throw new Error("Secret not found");
|
|
261
307
|
const patch = typeof input === "string" ? { value: input } : input;
|
|
@@ -274,51 +320,33 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
274
320
|
const description = patch.description !== undefined
|
|
275
321
|
? patch.description || null
|
|
276
322
|
: existing.description;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (!
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
await db
|
|
299
|
-
.update(schema.vaultSecrets)
|
|
300
|
-
.set({
|
|
301
|
-
name,
|
|
302
|
-
credentialKey,
|
|
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 } : {}),
|
|
307
|
-
provider,
|
|
308
|
-
description,
|
|
309
|
-
updatedAt: now(),
|
|
310
|
-
})
|
|
311
|
-
.where(and(eq(schema.vaultSecrets.id, secretId), ctxScope(schema.vaultSecrets, ctx)));
|
|
312
|
-
if (valueForKey) {
|
|
313
|
-
await storeSecretValue(storeCtx, { credentialKey, name }, valueForKey);
|
|
323
|
+
const renamed = credentialKey !== existing.credentialKey;
|
|
324
|
+
// One statement renames, rotates and relabels together, and refuses a key
|
|
325
|
+
// another row in the scope already holds. A rename moves the stored
|
|
326
|
+
// ciphertext with the row, so the value is never read or rewritten.
|
|
327
|
+
const stored = await updateAppSecretById({ id: secretId, ...credentialStoreScopeForVaultCtx(ctx) }, {
|
|
328
|
+
...(renamed ? { key: credentialKey } : {}),
|
|
329
|
+
...(patch.value !== undefined ? { value: patch.value } : {}),
|
|
330
|
+
...(patch.name !== undefined ? { label: name } : {}),
|
|
331
|
+
...(patch.provider !== undefined ? { provider } : {}),
|
|
332
|
+
...(patch.description !== undefined ? { description } : {}),
|
|
333
|
+
adoptedBy: ctx.ownerEmail,
|
|
334
|
+
});
|
|
335
|
+
if (!stored)
|
|
336
|
+
throw new Error("Secret not found");
|
|
337
|
+
if (renamed) {
|
|
338
|
+
// Grants name their key, so they follow it. A failure here leaves them on
|
|
339
|
+
// the old key, which grants nothing: access fails closed.
|
|
340
|
+
await getDb()
|
|
341
|
+
.update(schema.vaultGrants)
|
|
342
|
+
.set({ credentialKey, secretId, updatedAt: now() })
|
|
343
|
+
.where(or(eq(schema.vaultGrants.secretId, secretId), and(eq(schema.vaultGrants.credentialKey, existing.credentialKey), tenantScope(schema.vaultGrants, ctx))));
|
|
314
344
|
}
|
|
315
345
|
const auditMetadata = {
|
|
316
346
|
name,
|
|
317
347
|
previousName: name !== existing.name ? existing.name : undefined,
|
|
318
348
|
credentialKey,
|
|
319
|
-
previousCredentialKey:
|
|
320
|
-
? existing.credentialKey
|
|
321
|
-
: undefined,
|
|
349
|
+
previousCredentialKey: renamed ? existing.credentialKey : undefined,
|
|
322
350
|
provider,
|
|
323
351
|
previousProvider: provider !== existing.provider ? existing.provider : undefined,
|
|
324
352
|
description,
|
|
@@ -328,6 +356,7 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
328
356
|
await recordVaultAudit({
|
|
329
357
|
action: "secret.updated",
|
|
330
358
|
secretId,
|
|
359
|
+
credentialKey,
|
|
331
360
|
summary: `Updated secret "${name}" (${credentialKey})`,
|
|
332
361
|
metadata: auditMetadata,
|
|
333
362
|
});
|
|
@@ -338,21 +367,11 @@ export async function updateSecret(secretId, input, ctx = requireVaultCtx()) {
|
|
|
338
367
|
summary: `Updated vault secret "${name}" (${credentialKey})`,
|
|
339
368
|
metadata: auditMetadata,
|
|
340
369
|
});
|
|
341
|
-
|
|
342
|
-
if (updated && credentialKey !== existing.credentialKey) {
|
|
343
|
-
await cleanupSyncedCredentialKeysIfUnused(ctxForRow(existing), [
|
|
344
|
-
existing.credentialKey,
|
|
345
|
-
]);
|
|
346
|
-
}
|
|
347
|
-
else if (updated && patch.credentialKey !== undefined) {
|
|
348
|
-
await cleanupSyncedCredentialKeysIfUnused(ctxForSecretRow(updated, ctx));
|
|
349
|
-
}
|
|
350
|
-
return updated;
|
|
370
|
+
return requireSecretMetadata(secretId, ctx);
|
|
351
371
|
}
|
|
352
372
|
export async function deleteSecret(secretId, ctx = requireVaultCtx()) {
|
|
353
373
|
await assertCanManageVault();
|
|
354
|
-
const
|
|
355
|
-
const existing = await getSecret(secretId, ctx);
|
|
374
|
+
const existing = await findSecret(secretId, ctx);
|
|
356
375
|
if (!existing)
|
|
357
376
|
throw new Error("Secret not found");
|
|
358
377
|
const grants = await listGrants({ secretId });
|
|
@@ -361,15 +380,19 @@ export async function deleteSecret(secretId, ctx = requireVaultCtx()) {
|
|
|
361
380
|
await revokeGrant(grant.id, ctx);
|
|
362
381
|
}
|
|
363
382
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
await
|
|
368
|
-
|
|
369
|
-
|
|
383
|
+
// By id, so a rename since the lookup cannot point this at another key that
|
|
384
|
+
// took over the old name. Gone already means another admin deleted it: say
|
|
385
|
+
// so rather than record a delete that never happened.
|
|
386
|
+
const removed = await deleteAppSecretById({
|
|
387
|
+
id: secretId,
|
|
388
|
+
...credentialStoreScopeForVaultCtx(ctx),
|
|
389
|
+
});
|
|
390
|
+
if (!removed)
|
|
391
|
+
throw new Error("Secret not found");
|
|
370
392
|
await recordVaultAudit({
|
|
371
393
|
action: "secret.deleted",
|
|
372
394
|
secretId,
|
|
395
|
+
credentialKey: existing.credentialKey,
|
|
373
396
|
summary: `Deleted secret "${existing.name}" (${existing.credentialKey})`,
|
|
374
397
|
});
|
|
375
398
|
await recordAudit({
|
|
@@ -380,48 +403,70 @@ export async function deleteSecret(secretId, ctx = requireVaultCtx()) {
|
|
|
380
403
|
});
|
|
381
404
|
return existing;
|
|
382
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* A grant belongs to a key by its name. The id match covers a grant whose key
|
|
408
|
+
* column a rename has not reached yet; the name match covers grants backfilled
|
|
409
|
+
* from before the key column existed, whose `secretId` is stale.
|
|
410
|
+
*/
|
|
411
|
+
function grantsOfSecret(secret, ctx) {
|
|
412
|
+
return or(eq(schema.vaultGrants.secretId, secret.id), and(eq(schema.vaultGrants.credentialKey, secret.credentialKey), tenantScope(schema.vaultGrants, ctx)));
|
|
413
|
+
}
|
|
414
|
+
/** Report the grant against the key's current id, so callers can match it to a listed key. */
|
|
415
|
+
function withCurrentSecretId(grant, secrets) {
|
|
416
|
+
const secret = grant.credentialKey
|
|
417
|
+
? secrets.find((row) => row.credentialKey === grant.credentialKey)
|
|
418
|
+
: undefined;
|
|
419
|
+
return secret ? { ...grant, secretId: secret.id } : grant;
|
|
420
|
+
}
|
|
383
421
|
export async function listGrants(filter) {
|
|
384
422
|
await assertCanManageVault();
|
|
423
|
+
const ctx = requireVaultCtx();
|
|
385
424
|
const db = getDb();
|
|
425
|
+
const secrets = await listScopeSecrets(ctx);
|
|
386
426
|
const conditions = [scopedFilter(schema.vaultGrants)];
|
|
387
427
|
if (filter?.secretId) {
|
|
388
|
-
|
|
428
|
+
const secret = secrets.find((row) => row.id === filter.secretId);
|
|
429
|
+
conditions.push(secret
|
|
430
|
+
? grantsOfSecret(secret, ctx)
|
|
431
|
+
: eq(schema.vaultGrants.secretId, filter.secretId));
|
|
389
432
|
}
|
|
390
433
|
if (filter?.appId) {
|
|
391
434
|
conditions.push(eq(schema.vaultGrants.appId, filter.appId));
|
|
392
435
|
}
|
|
393
|
-
|
|
436
|
+
const rows = await db
|
|
394
437
|
.select()
|
|
395
438
|
.from(schema.vaultGrants)
|
|
396
439
|
.where(and(...conditions))
|
|
397
440
|
.orderBy(desc(schema.vaultGrants.updatedAt));
|
|
441
|
+
return rows.map((grant) => withCurrentSecretId(grant, secrets));
|
|
398
442
|
}
|
|
399
443
|
export async function getGrant(grantId, ctx = requireVaultCtx()) {
|
|
400
444
|
await assertCanManageVault();
|
|
401
445
|
const db = getDb();
|
|
402
|
-
const
|
|
446
|
+
const rows = await db
|
|
403
447
|
.select()
|
|
404
448
|
.from(schema.vaultGrants)
|
|
405
449
|
.where(and(eq(schema.vaultGrants.id, grantId), ctxScope(schema.vaultGrants, ctx)))
|
|
406
450
|
.limit(1);
|
|
407
|
-
|
|
451
|
+
const row = rows.at(0);
|
|
452
|
+
return row ? withCurrentSecretId(row, await listScopeSecrets(ctx)) : null;
|
|
408
453
|
}
|
|
409
454
|
export async function createGrant(secretId, appId, ctx = requireVaultCtx()) {
|
|
410
455
|
await assertCanManageVault();
|
|
411
456
|
const db = getDb();
|
|
412
|
-
const secret = await
|
|
457
|
+
const secret = await findSecret(secretId, ctx);
|
|
413
458
|
if (!secret)
|
|
414
459
|
throw new Error("Secret not found");
|
|
415
460
|
const timestamp = now();
|
|
416
461
|
const actor = ctx.ownerEmail;
|
|
417
|
-
const
|
|
462
|
+
const matches = await db
|
|
418
463
|
.select()
|
|
419
464
|
.from(schema.vaultGrants)
|
|
420
|
-
.where(and(eq(schema.vaultGrants.
|
|
421
|
-
.orderBy(desc(schema.vaultGrants.updatedAt))
|
|
422
|
-
|
|
465
|
+
.where(and(eq(schema.vaultGrants.appId, appId), grantsOfSecret(secret, ctx), ctxScope(schema.vaultGrants, ctx)))
|
|
466
|
+
.orderBy(desc(schema.vaultGrants.updatedAt));
|
|
467
|
+
const existing = matches.find((grant) => grant.status === "active") ?? matches[0];
|
|
423
468
|
if (existing?.status === "active") {
|
|
424
|
-
return existing;
|
|
469
|
+
return getGrant(existing.id, ctx);
|
|
425
470
|
}
|
|
426
471
|
if (existing) {
|
|
427
472
|
await db
|
|
@@ -430,12 +475,15 @@ export async function createGrant(secretId, appId, ctx = requireVaultCtx()) {
|
|
|
430
475
|
grantedBy: actor,
|
|
431
476
|
status: "active",
|
|
432
477
|
syncedAt: null,
|
|
478
|
+
secretId: secret.id,
|
|
479
|
+
credentialKey: secret.credentialKey,
|
|
433
480
|
updatedAt: timestamp,
|
|
434
481
|
})
|
|
435
482
|
.where(and(eq(schema.vaultGrants.id, existing.id), ctxScope(schema.vaultGrants, ctx)));
|
|
436
483
|
await recordVaultAudit({
|
|
437
484
|
action: "grant.reinstated",
|
|
438
485
|
secretId,
|
|
486
|
+
credentialKey: secret.credentialKey,
|
|
439
487
|
appId,
|
|
440
488
|
summary: `Reinstated "${secret.name}" (${secret.credentialKey}) for ${appId}`,
|
|
441
489
|
metadata: { grantId: existing.id },
|
|
@@ -453,7 +501,8 @@ export async function createGrant(secretId, appId, ctx = requireVaultCtx()) {
|
|
|
453
501
|
id: grantId,
|
|
454
502
|
ownerEmail: actor,
|
|
455
503
|
orgId: ctx.orgId,
|
|
456
|
-
secretId,
|
|
504
|
+
secretId: secret.id,
|
|
505
|
+
credentialKey: secret.credentialKey,
|
|
457
506
|
appId,
|
|
458
507
|
grantedBy: actor,
|
|
459
508
|
status: "active",
|
|
@@ -464,6 +513,7 @@ export async function createGrant(secretId, appId, ctx = requireVaultCtx()) {
|
|
|
464
513
|
await recordVaultAudit({
|
|
465
514
|
action: "grant.created",
|
|
466
515
|
secretId,
|
|
516
|
+
credentialKey: secret.credentialKey,
|
|
467
517
|
appId,
|
|
468
518
|
summary: `Granted "${secret.name}" (${secret.credentialKey}) to ${appId}`,
|
|
469
519
|
metadata: { grantId },
|
|
@@ -474,7 +524,7 @@ export async function createGrant(secretId, appId, ctx = requireVaultCtx()) {
|
|
|
474
524
|
targetId: grantId,
|
|
475
525
|
summary: `Granted vault secret "${secret.name}" to ${appId}`,
|
|
476
526
|
});
|
|
477
|
-
return getGrant(grantId);
|
|
527
|
+
return getGrant(grantId, ctx);
|
|
478
528
|
}
|
|
479
529
|
export async function grantSecretsToApp(secretIds, appId, ctx = requireVaultCtx()) {
|
|
480
530
|
await assertCanManageVault();
|
|
@@ -511,7 +561,8 @@ export async function revokeGrant(grantId, ctx = requireVaultCtx()) {
|
|
|
511
561
|
const grant = await getGrant(grantId, ctx);
|
|
512
562
|
if (!grant)
|
|
513
563
|
throw new Error("Grant not found");
|
|
514
|
-
const secret = await
|
|
564
|
+
const secret = await findSecret(grant.secretId, ctx);
|
|
565
|
+
const credentialKey = secret?.credentialKey ?? grant.credentialKey;
|
|
515
566
|
await db
|
|
516
567
|
.update(schema.vaultGrants)
|
|
517
568
|
.set({ status: "revoked", updatedAt: now() })
|
|
@@ -519,8 +570,9 @@ export async function revokeGrant(grantId, ctx = requireVaultCtx()) {
|
|
|
519
570
|
await recordVaultAudit({
|
|
520
571
|
action: "grant.revoked",
|
|
521
572
|
secretId: grant.secretId,
|
|
573
|
+
credentialKey,
|
|
522
574
|
appId: grant.appId,
|
|
523
|
-
summary: `Revoked ${
|
|
575
|
+
summary: `Revoked ${credentialKey || grant.secretId} from ${grant.appId}`,
|
|
524
576
|
metadata: { grantId },
|
|
525
577
|
});
|
|
526
578
|
await recordAudit({
|
|
@@ -531,111 +583,6 @@ export async function revokeGrant(grantId, ctx = requireVaultCtx()) {
|
|
|
531
583
|
});
|
|
532
584
|
return getGrant(grantId, ctx);
|
|
533
585
|
}
|
|
534
|
-
function vaultSecretMetadata(secret, last4Value, valueStatus) {
|
|
535
|
-
return {
|
|
536
|
-
id: secret.id,
|
|
537
|
-
name: secret.name,
|
|
538
|
-
credentialKey: secret.credentialKey,
|
|
539
|
-
last4: last4Value,
|
|
540
|
-
valueStatus,
|
|
541
|
-
provider: secret.provider,
|
|
542
|
-
description: secret.description,
|
|
543
|
-
createdBy: secret.createdBy,
|
|
544
|
-
createdAt: secret.createdAt,
|
|
545
|
-
updatedAt: secret.updatedAt,
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
export function credentialStoreScopeForVaultCtx(ctx) {
|
|
549
|
-
if (ctx.orgId)
|
|
550
|
-
return { scope: "org", scopeId: ctx.orgId };
|
|
551
|
-
return { scope: "workspace", scopeId: `solo:${ctx.ownerEmail}` };
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
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.
|
|
557
|
-
*/
|
|
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();
|
|
564
|
-
for (const row of rows) {
|
|
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);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
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
|
-
});
|
|
582
|
-
}
|
|
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");
|
|
599
|
-
}
|
|
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
|
-
});
|
|
615
|
-
}
|
|
616
|
-
export async function cleanupSyncedCredentialKeysIfUnused(ctx, candidateKeys) {
|
|
617
|
-
const db = getDb();
|
|
618
|
-
const target = credentialStoreScopeForVaultCtx(ctx);
|
|
619
|
-
const keys = candidateKeys
|
|
620
|
-
? candidateKeys
|
|
621
|
-
: (await listAppSecretsForScope(target.scope, target.scopeId))
|
|
622
|
-
.filter((secret) => secret.description?.startsWith(VAULT_SYNC_DESCRIPTION_PREFIX))
|
|
623
|
-
.map((secret) => secret.key);
|
|
624
|
-
for (const key of new Set(keys.filter(Boolean))) {
|
|
625
|
-
const stillUsesKey = await db
|
|
626
|
-
.select({ id: schema.vaultSecrets.id })
|
|
627
|
-
.from(schema.vaultSecrets)
|
|
628
|
-
.where(and(eq(schema.vaultSecrets.credentialKey, key), ctxScope(schema.vaultSecrets, ctx)))
|
|
629
|
-
.limit(1);
|
|
630
|
-
if (!stillUsesKey[0]) {
|
|
631
|
-
await deleteAppSecret({
|
|
632
|
-
key,
|
|
633
|
-
scope: target.scope,
|
|
634
|
-
scopeId: target.scopeId,
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
586
|
export async function listRequests(filter) {
|
|
640
587
|
const db = getDb();
|
|
641
588
|
const ctx = requireVaultCtx();
|
|
@@ -693,6 +640,7 @@ export async function createRequest(input) {
|
|
|
693
640
|
});
|
|
694
641
|
await recordVaultAudit({
|
|
695
642
|
action: "request.created",
|
|
643
|
+
credentialKey: input.credentialKey,
|
|
696
644
|
appId: input.appId,
|
|
697
645
|
summary: `${actor} requested ${input.credentialKey} for ${input.appId}`,
|
|
698
646
|
metadata: { requestId, reason: input.reason },
|
|
@@ -724,33 +672,29 @@ export async function approveRequest(requestId, secretValue, secretName, ctx = r
|
|
|
724
672
|
})
|
|
725
673
|
.where(and(eq(schema.vaultRequests.id, requestId), tenantScope(schema.vaultRequests, ctx), or(eq(schema.vaultRequests.status, "pending"), and(eq(schema.vaultRequests.status, "applying"), sql `${schema.vaultRequests.updatedAt} < ${staleApplyingBefore}`))))
|
|
726
674
|
.returning();
|
|
727
|
-
|
|
675
|
+
const claimedRequest = claimed.at(0);
|
|
676
|
+
if (!claimedRequest) {
|
|
728
677
|
const current = await getRequestForTenant(requestId, ctx);
|
|
729
678
|
if (current?.status === "applying") {
|
|
730
679
|
throw new Error("Vault request is already being applied");
|
|
731
680
|
}
|
|
732
681
|
return current;
|
|
733
682
|
}
|
|
734
|
-
const claimedRequest = claimed[0];
|
|
735
683
|
// Secret + grant must land in the REQUEST's tenant, not the approver's
|
|
736
684
|
// (the approver may be acting on behalf of another user in the same org).
|
|
737
685
|
const requestCtx = ctxForRow(claimedRequest);
|
|
738
686
|
try {
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
if (!secret) {
|
|
745
|
-
secret = await createSecret({
|
|
687
|
+
// A key that already exists in the scope is only granted: the approver's
|
|
688
|
+
// value never overwrites a row, whether the Vault or an app wrote it.
|
|
689
|
+
let secretId = (await listScopeSecrets(requestCtx)).find((row) => row.credentialKey === claimedRequest.credentialKey)?.id;
|
|
690
|
+
if (!secretId) {
|
|
691
|
+
secretId = (await createSecret({
|
|
746
692
|
credentialKey: claimedRequest.credentialKey,
|
|
747
693
|
value: secretValue,
|
|
748
694
|
name: secretName || claimedRequest.credentialKey,
|
|
749
|
-
}, requestCtx);
|
|
750
|
-
}
|
|
751
|
-
if (secret) {
|
|
752
|
-
await createGrant(secret.id, claimedRequest.appId, requestCtx);
|
|
695
|
+
}, requestCtx)).id;
|
|
753
696
|
}
|
|
697
|
+
await createGrant(secretId, claimedRequest.appId, requestCtx);
|
|
754
698
|
}
|
|
755
699
|
catch (error) {
|
|
756
700
|
await db
|
|
@@ -770,6 +714,7 @@ export async function approveRequest(requestId, secretValue, secretName, ctx = r
|
|
|
770
714
|
.where(and(eq(schema.vaultRequests.id, requestId), tenantScope(schema.vaultRequests, ctx), eq(schema.vaultRequests.status, "applying")));
|
|
771
715
|
await recordVaultAudit({
|
|
772
716
|
action: "request.approved",
|
|
717
|
+
credentialKey: claimedRequest.credentialKey,
|
|
773
718
|
appId: claimedRequest.appId,
|
|
774
719
|
summary: `Approved ${claimedRequest.credentialKey} for ${claimedRequest.appId} (requested by ${claimedRequest.requestedBy})`,
|
|
775
720
|
metadata: { requestId, reviewer },
|
|
@@ -794,12 +739,13 @@ export async function denyRequest(requestId, reason, ctx = requireVaultCtx()) {
|
|
|
794
739
|
})
|
|
795
740
|
.where(and(eq(schema.vaultRequests.id, requestId), tenantScope(schema.vaultRequests, ctx), eq(schema.vaultRequests.status, "pending")))
|
|
796
741
|
.returning();
|
|
797
|
-
|
|
742
|
+
const claimedRequest = claimed.at(0);
|
|
743
|
+
if (!claimedRequest) {
|
|
798
744
|
return getRequestForTenant(requestId, ctx);
|
|
799
745
|
}
|
|
800
|
-
const claimedRequest = claimed[0];
|
|
801
746
|
await recordVaultAudit({
|
|
802
747
|
action: "request.denied",
|
|
748
|
+
credentialKey: claimedRequest.credentialKey,
|
|
803
749
|
appId: claimedRequest.appId,
|
|
804
750
|
summary: `Denied ${claimedRequest.credentialKey} for ${claimedRequest.appId} (requested by ${claimedRequest.requestedBy})`,
|
|
805
751
|
metadata: { requestId, reviewer, reason },
|
|
@@ -879,7 +825,8 @@ export async function listVaultOverview() {
|
|
|
879
825
|
listRequests(),
|
|
880
826
|
getVaultAccessSettings(),
|
|
881
827
|
]);
|
|
882
|
-
|
|
828
|
+
// A grant with no key (written before grants named their key) grants nothing.
|
|
829
|
+
const manualGrantCount = grants.filter((g) => g.status === "active" && g.credentialKey).length;
|
|
883
830
|
return {
|
|
884
831
|
accessMode: access.mode,
|
|
885
832
|
secretCount: secrets.length,
|