dsh-workbench-vault 0.0.0-stage → 0.3.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.
@@ -0,0 +1,532 @@
1
+ /**
2
+ * 保险箱 action 契约:所有写操作按 origin 门禁,agent 永远拿不到明文。
3
+ * 分组与关联对 plugin 开放,业务模块用自身标识注册,不需要依赖 sites。
4
+ */
5
+ import { KINDS } from "./secret-input.js";
6
+ import { createSets } from "./sets.js";
7
+ import { registerSetActions } from "./actions-sets.js";
8
+
9
+ function only(ctx, allowed, action) {
10
+ const origin = ctx && ctx.origin ? ctx.origin : "agent";
11
+ if (!allowed.includes(origin)) throw new Error(action + " 不允许 " + origin + " 调用");
12
+ }
13
+
14
+ export function registerVaultActions(api, vault, groups) {
15
+ const sets = createSets({ db: api.db });
16
+ api.action("vault.status", {
17
+ description: "只读:保险箱是否初始化、是否锁定、解锁模式、凭据/分组数量与同步状态;不含任何密文。",
18
+ input: { type: "object", properties: {}, additionalProperties: false },
19
+ execute: (_args, ctx) => {
20
+ only(ctx, ["ui", "agent", "plugin"], "vault.status");
21
+ return vault.status();
22
+ },
23
+ });
24
+
25
+ api.action("vault.setup", {
26
+ agentLocked: true,
27
+ description: "首次初始化保险箱:设置口令与解锁模式 A/B/C,默认 A(每次启动锁定)。口令包裹会上云。",
28
+ input: {
29
+ type: "object",
30
+ properties: {
31
+ passphrase: { type: "string", description: "主口令,至少 12 个字符" },
32
+ mode: { type: "string", enum: ["A", "B", "C"], description: "解锁模式,默认 A" },
33
+ },
34
+ required: ["passphrase"],
35
+ additionalProperties: false,
36
+ },
37
+ examples: [{ passphrase: "correct horse battery", mode: "A" }],
38
+ execute: (args, ctx) => {
39
+ only(ctx, ["ui"], "vault.setup");
40
+ return vault.setup(args);
41
+ },
42
+ });
43
+
44
+ api.action("vault.unlock", {
45
+ agentLocked: true,
46
+ description: "用主口令解锁保险箱;连续 5 次失败锁定 5 分钟。",
47
+ input: {
48
+ type: "object",
49
+ properties: { passphrase: { type: "string", description: "主口令" } },
50
+ required: ["passphrase"],
51
+ additionalProperties: false,
52
+ },
53
+ execute: (args, ctx) => {
54
+ only(ctx, ["ui"], "vault.unlock");
55
+ return vault.unlock(args);
56
+ },
57
+ });
58
+
59
+ api.action("vault.lock", {
60
+ agentLocked: true,
61
+ description: "立即清除内存中的 DEK;插件后台可用它主动锁定。",
62
+ input: { type: "object", properties: {}, additionalProperties: false },
63
+ execute: (_args, ctx) => {
64
+ only(ctx, ["ui", "plugin"], "vault.lock");
65
+ vault.lock();
66
+ return { ok: true };
67
+ },
68
+ });
69
+
70
+ api.action("vault.setUnlockMode", {
71
+ agentLocked: true,
72
+ description: "切换解锁模式:A 全锁 / B 自动解锁但查看需口令 / C 自动解锁且查看免口令。",
73
+ input: {
74
+ type: "object",
75
+ properties: {
76
+ mode: { type: "string", enum: ["A", "B", "C"] },
77
+ passphrase: { type: "string", description: "锁定状态下切换 B/C 需要主口令" },
78
+ },
79
+ required: ["mode"],
80
+ additionalProperties: false,
81
+ },
82
+ execute: (args, ctx) => {
83
+ only(ctx, ["ui"], "vault.setUnlockMode");
84
+ return vault.setUnlockMode(args);
85
+ },
86
+ });
87
+
88
+ api.action("vault.changePassphrase", {
89
+ agentLocked: true,
90
+ description: "修改主口令:只重包 DEK 并让 passphrase_rev 加一,不重新加密所有 secret。",
91
+ input: {
92
+ type: "object",
93
+ properties: {
94
+ oldPassphrase: { type: "string" },
95
+ newPassphrase: { type: "string", description: "至少 12 个字符" },
96
+ },
97
+ required: ["oldPassphrase", "newPassphrase"],
98
+ additionalProperties: false,
99
+ },
100
+ execute: (args, ctx) => {
101
+ only(ctx, ["ui"], "vault.changePassphrase");
102
+ return vault.changePassphrase(args);
103
+ },
104
+ });
105
+
106
+ api.action("vault.secretList", {
107
+ description: "只读:列出凭据名称/类型/集合归属/备注/最近使用时间,永远不含明文。",
108
+ input: {
109
+ type: "object",
110
+ properties: {
111
+ q: { type: "string", description: "按名称或备注模糊搜索" },
112
+ kind: { type: "string", enum: KINDS },
113
+ groupId: { type: "string", description: "只看某个分组(经集合关联)" },
114
+ setId: { type: "string", description: "只看某个凭据集合" },
115
+ },
116
+ additionalProperties: false,
117
+ },
118
+ execute: (args, ctx) => {
119
+ only(ctx, ["ui", "agent"], "vault.secretList");
120
+ return vault.listSecrets(args);
121
+ },
122
+ });
123
+
124
+ api.action("vault.secretCreate", {
125
+ agentLocked: true,
126
+ description: "新增凭据;secret 只在内存加密后落库,审计中该字段显示为 ***。",
127
+ input: {
128
+ type: "object",
129
+ properties: {
130
+ name: { type: "string", description: "唯一名称,1–100 字" },
131
+ kind: { type: "string", enum: KINDS },
132
+ secret: { type: "string", description: "明文密码/token,不会落审计" },
133
+ meta: { type: "object", description: "非敏感备注:用户名、URL、过期提醒" },
134
+ groupId: { type: "string", description: "所属分组,可空;写入时自动挂到该组的默认集合" },
135
+ setId: { type: "string", description: "直接指定凭据集合,优先于 groupId" },
136
+ },
137
+ required: ["name", "kind", "secret"],
138
+ additionalProperties: false,
139
+ },
140
+ examples: [{ name: "sspai-basic", kind: "password", secret: "app-password", meta: { username: "me" } }],
141
+ execute: (args, ctx) => {
142
+ only(ctx, ["ui"], "vault.secretCreate");
143
+ return vault.createSecret(args);
144
+ },
145
+ });
146
+
147
+ api.action("vault.secretUpdate", {
148
+ agentLocked: true,
149
+ description: "更新名称/类型/备注或轮换明文;改名称或类型会重加密,改归属不会。",
150
+ input: {
151
+ type: "object",
152
+ properties: {
153
+ id: { type: "string" },
154
+ name: { type: "string" },
155
+ kind: { type: "string", enum: KINDS },
156
+ secret: { type: "string", description: "留空表示不轮换" },
157
+ meta: { type: "object" },
158
+ groupId: { type: "string", description: "再挂到某个分组的默认集合(不改动已有集合)" },
159
+ setId: { type: "string", description: "指定集合;P2 起可用" },
160
+ },
161
+ required: ["id"],
162
+ additionalProperties: false,
163
+ },
164
+ execute: (args, ctx) => {
165
+ only(ctx, ["ui"], "vault.secretUpdate");
166
+ return vault.updateSecret(args);
167
+ },
168
+ });
169
+
170
+ api.action("vault.secretDelete", {
171
+ agentLocked: true,
172
+ description: "软删除凭据;已绑定它的连接器会在下次运行时提示凭据失效。",
173
+ input: {
174
+ type: "object",
175
+ properties: { id: { type: "string" } },
176
+ required: ["id"],
177
+ additionalProperties: false,
178
+ },
179
+ execute: (args, ctx) => {
180
+ only(ctx, ["ui"], "vault.secretDelete");
181
+ return vault.deleteSecret(args);
182
+ },
183
+ });
184
+
185
+ registerSetActions({ api, sets, groups, vault, only, KINDS });
186
+
187
+ api.action("vault.secretReveal", {
188
+ agentLocked: true,
189
+ description: "在界面上显示一条凭据明文;B 模式必须再输口令,agent/plugin 一律拒绝。",
190
+ input: {
191
+ type: "object",
192
+ properties: {
193
+ id: { type: "string" },
194
+ passphrase: { type: "string", description: "B 模式必需" },
195
+ },
196
+ required: ["id"],
197
+ additionalProperties: false,
198
+ },
199
+ execute: (args, ctx) => {
200
+ only(ctx, ["ui"], "vault.secretReveal");
201
+ return vault.reveal(args);
202
+ },
203
+ });
204
+
205
+ api.action("vault.lease", {
206
+ agentLocked: true,
207
+ description: "后台插件租用凭据明文,ttl 30–300 秒;只允许 plugin origin,传 subject 时校验绑定。",
208
+ input: {
209
+ type: "object",
210
+ properties: {
211
+ id: { type: "string" },
212
+ purpose: { type: "string", description: "用途,写入审计" },
213
+ ttlSeconds: { type: "number", description: "30–300,默认 60" },
214
+ subjectType: { type: "string", description: "调用方业务类型;与 subjectId 一起校验绑定" },
215
+ subjectId: { type: "string", description: "调用方业务对象 id" },
216
+ },
217
+ required: ["id"],
218
+ additionalProperties: false,
219
+ },
220
+ execute: (args, ctx) => {
221
+ only(ctx, ["plugin"], "vault.lease");
222
+ return vault.lease(args);
223
+ },
224
+ });
225
+
226
+ api.action("vault.rotateDeviceKey", {
227
+ agentLocked: true,
228
+ description: "重写自动解锁的 device key 包裹;B/C 模式可用,需要主口令。",
229
+ input: {
230
+ type: "object",
231
+ properties: { passphrase: { type: "string" } },
232
+ required: ["passphrase"],
233
+ additionalProperties: false,
234
+ },
235
+ execute: (args, ctx) => {
236
+ only(ctx, ["ui"], "vault.rotateDeviceKey");
237
+ return vault.rotateDeviceKey(args);
238
+ },
239
+ });
240
+
241
+ api.action("vault.groupList", {
242
+ description: "只读:分组列表(含凭据数、关联对象数);不含密文。",
243
+ input: {
244
+ type: "object",
245
+ properties: { q: { type: "string", description: "按组名/描述搜索" } },
246
+ additionalProperties: false,
247
+ },
248
+ execute: (args, ctx) => {
249
+ only(ctx, ["ui", "agent", "plugin"], "vault.groupList");
250
+ return groups.groupList(args);
251
+ },
252
+ });
253
+
254
+ api.action("vault.groupCreate", {
255
+ agentLocked: true,
256
+ description: "新建凭据分组;组名唯一。",
257
+ input: {
258
+ type: "object",
259
+ properties: {
260
+ name: { type: "string", description: "1–60 字" },
261
+ description: { type: "string" },
262
+ color: { type: "string", description: "可选色标" },
263
+ },
264
+ required: ["name"],
265
+ additionalProperties: false,
266
+ },
267
+ execute: (args, ctx) => {
268
+ only(ctx, ["ui"], "vault.groupCreate");
269
+ return groups.groupCreate(args);
270
+ },
271
+ });
272
+
273
+ api.action("vault.groupUpdate", {
274
+ agentLocked: true,
275
+ description: "改名/描述/色标;不改动组内凭据的密文。",
276
+ input: {
277
+ type: "object",
278
+ properties: {
279
+ id: { type: "string" },
280
+ name: { type: "string" },
281
+ description: { type: "string" },
282
+ color: { type: "string" },
283
+ },
284
+ required: ["id"],
285
+ additionalProperties: false,
286
+ },
287
+ execute: (args, ctx) => {
288
+ only(ctx, ["ui"], "vault.groupUpdate");
289
+ return groups.groupUpdate(args);
290
+ },
291
+ });
292
+
293
+ api.action("vault.groupDelete", {
294
+ agentLocked: true,
295
+ description: "软删除分组:组内凭据移到 moveTo,未指定则移出分组;组上的绑定一并软删。",
296
+ input: {
297
+ type: "object",
298
+ properties: {
299
+ id: { type: "string" },
300
+ moveTo: { type: "string", description: "可选,接收凭据的分组 id" },
301
+ },
302
+ required: ["id"],
303
+ additionalProperties: false,
304
+ },
305
+ execute: (args, ctx) => {
306
+ only(ctx, ["ui"], "vault.groupDelete");
307
+ return groups.groupDelete(args);
308
+ },
309
+ });
310
+
311
+ api.action("vault.subjectRegister", {
312
+ agentLocked: true,
313
+ description: "业务插件注册自己的关联类型(幂等),供组详情页显示可读标题。",
314
+ input: {
315
+ type: "object",
316
+ properties: {
317
+ type: { type: "string", description: "小写标识,如 site" },
318
+ title: { type: "string", description: "展示名,如 站点" },
319
+ description: { type: "string" },
320
+ },
321
+ required: ["type", "title"],
322
+ additionalProperties: false,
323
+ },
324
+ execute: (args, ctx) => {
325
+ only(ctx, ["plugin"], "vault.subjectRegister");
326
+ return groups.subjectRegister(args);
327
+ },
328
+ });
329
+
330
+ api.action("vault.bind", {
331
+ agentLocked: true,
332
+ description: "把业务对象关联到凭据分组(重复绑定幂等);vault 不校验对象是否存在。",
333
+ input: {
334
+ type: "object",
335
+ properties: {
336
+ subjectType: { type: "string" },
337
+ subjectId: { type: "string" },
338
+ groupId: { type: "string" },
339
+ },
340
+ required: ["subjectType", "subjectId", "groupId"],
341
+ additionalProperties: false,
342
+ },
343
+ execute: (args, ctx) => {
344
+ only(ctx, ["ui", "plugin"], "vault.bind");
345
+ return groups.bind(args);
346
+ },
347
+ });
348
+
349
+ api.action("vault.unbind", {
350
+ agentLocked: true,
351
+ description: "解除一个业务对象与分组的关联(软删)。",
352
+ input: {
353
+ type: "object",
354
+ properties: {
355
+ subjectType: { type: "string" },
356
+ subjectId: { type: "string" },
357
+ groupId: { type: "string" },
358
+ },
359
+ required: ["subjectType", "subjectId", "groupId"],
360
+ additionalProperties: false,
361
+ },
362
+ execute: (args, ctx) => {
363
+ only(ctx, ["ui", "plugin"], "vault.unbind");
364
+ return groups.unbind(args);
365
+ },
366
+ });
367
+
368
+ api.action("vault.bindingsOf", {
369
+ description: "只读:某个业务对象关联了哪些分组(含组名、凭据数)。",
370
+ input: {
371
+ type: "object",
372
+ properties: { subjectType: { type: "string" }, subjectId: { type: "string" } },
373
+ required: ["subjectType", "subjectId"],
374
+ additionalProperties: false,
375
+ },
376
+ execute: (args, ctx) => {
377
+ only(ctx, ["ui", "agent", "plugin"], "vault.bindingsOf");
378
+ return groups.bindingsOf(args);
379
+ },
380
+ });
381
+
382
+ api.action("vault.subjectsOf", {
383
+ description: "只读:某个分组关联了哪些业务对象。",
384
+ input: {
385
+ type: "object",
386
+ properties: {
387
+ groupId: { type: "string" },
388
+ limit: { type: "number" },
389
+ offset: { type: "number" },
390
+ },
391
+ required: ["groupId"],
392
+ additionalProperties: false,
393
+ },
394
+ execute: (args, ctx) => {
395
+ only(ctx, ["ui", "agent"], "vault.subjectsOf");
396
+ return groups.subjectsOf(args);
397
+ },
398
+ });
399
+
400
+ api.action("vault.subjectSecrets", {
401
+ agentLocked: true,
402
+ description: "后台插件只读:某个业务对象经分组可见的凭据清单(不含明文),拿到 id 后再 lease。",
403
+ input: {
404
+ type: "object",
405
+ properties: { subjectType: { type: "string" }, subjectId: { type: "string" } },
406
+ required: ["subjectType", "subjectId"],
407
+ additionalProperties: false,
408
+ },
409
+ execute: (args, ctx) => {
410
+ only(ctx, ["plugin"], "vault.subjectSecrets");
411
+ return groups.subjectSecrets(args);
412
+ },
413
+ });
414
+
415
+ api.action("vault.bindingPurge", {
416
+ agentLocked: true,
417
+ description: "插件卸载/业务对象删除时清理自己的绑定引用(硬删)。",
418
+ input: {
419
+ type: "object",
420
+ properties: { subjectType: { type: "string" }, subjectId: { type: "string" } },
421
+ required: ["subjectType"],
422
+ additionalProperties: false,
423
+ },
424
+ execute: (args, ctx) => {
425
+ only(ctx, ["plugin"], "vault.bindingPurge");
426
+ return groups.bindingPurge(args);
427
+ },
428
+ });
429
+
430
+ api.action("vault.syncStatus", {
431
+ description: "只读:同步 schema 版本、口令修订号与同步表清单;不含密文,冲突列表在宿主同步页。",
432
+ input: { type: "object", properties: {}, additionalProperties: false },
433
+ execute: (_args, ctx) => {
434
+ only(ctx, ["ui", "agent"], "vault.syncStatus");
435
+ const status = vault.status();
436
+ return {
437
+ schemaVersion: status.schemaVersion,
438
+ passphraseRev: status.passphraseRev,
439
+ cloudSync: true,
440
+ tables: ["vault_header", "vault_secret", "vault_group", "vault_binding", "vault_subject_type"],
441
+ localOnly: ["vault_device", "vault_secret_usage"],
442
+ pending: null,
443
+ conflicts: null,
444
+ note: "同步状态与冲突在宿主的同步页查看;device key 与使用记录不上云。",
445
+ };
446
+ },
447
+ });
448
+
449
+ api.action("vault.dataTables", {
450
+ agentLocked: true,
451
+ description: "只读:数据页表清单。",
452
+ input: { type: "object", properties: {}, additionalProperties: false },
453
+ execute: (_args, ctx) => {
454
+ only(ctx, ["ui"], "vault.dataTables");
455
+ return {
456
+ tables: [
457
+ {
458
+ name: "vault_secret",
459
+ columns: ["id", "name", "kind", "meta", "ciphertext", "created_at", "updated_at", "deleted_at", "rev"],
460
+ },
461
+ { name: "vault_group", columns: ["id", "name", "description", "color", "created_at", "updated_at"] },
462
+ {
463
+ name: "vault_binding",
464
+ columns: ["id", "subject_type", "subject_id", "group_id", "created_at", "updated_at"],
465
+ },
466
+ {
467
+ name: "vault_set",
468
+ columns: ["id", "name", "description", "color", "created_at", "updated_at", "deleted_at", "rev"],
469
+ },
470
+ {
471
+ name: "vault_group_set",
472
+ columns: ["id", "group_id", "set_id", "created_at", "updated_at", "deleted_at", "rev"],
473
+ },
474
+ {
475
+ name: "vault_set_secret",
476
+ columns: ["id", "set_id", "secret_id", "created_at", "updated_at", "deleted_at", "rev"],
477
+ },
478
+ ],
479
+ };
480
+ },
481
+ });
482
+
483
+ api.action("vault.dataQuery", {
484
+ agentLocked: true,
485
+ description: "只读:数据页查询;密文列一律掩码。",
486
+ input: {
487
+ type: "object",
488
+ properties: {
489
+ table: {
490
+ type: "string",
491
+ enum: ["vault_secret", "vault_group", "vault_binding", "vault_set", "vault_group_set", "vault_set_secret"],
492
+ },
493
+ limit: { type: "number", description: "默认 50,上限 200" },
494
+ offset: { type: "number", description: "默认 0" },
495
+ },
496
+ required: ["table"],
497
+ additionalProperties: false,
498
+ },
499
+ execute: (args, ctx) => {
500
+ only(ctx, ["ui"], "vault.dataQuery");
501
+ const table = String(args.table);
502
+ const limit = Math.max(1, Math.min(200, Number(args.limit) || 50));
503
+ const offset = Math.max(0, Number(args.offset) || 0);
504
+ if (table === "vault_secret") {
505
+ const rows = api.db
506
+ .prepare(
507
+ "SELECT id, name, kind, meta, ciphertext, created_at, updated_at, deleted_at, rev " +
508
+ "FROM vault_secret ORDER BY updated_at DESC LIMIT ? OFFSET ?",
509
+ )
510
+ .all(limit, offset);
511
+ return {
512
+ table,
513
+ rows: rows.map((row) => ({ ...row, ciphertext: "***" })),
514
+ limit,
515
+ offset,
516
+ };
517
+ }
518
+ const safe = {
519
+ vault_group: "vault_group",
520
+ vault_binding: "vault_binding",
521
+ vault_set: "vault_set",
522
+ vault_group_set: "vault_group_set",
523
+ vault_set_secret: "vault_set_secret",
524
+ }[table];
525
+ if (!safe) throw new Error("不支持的数据表:" + table);
526
+ const rows = api.db
527
+ .prepare("SELECT * FROM " + safe + " ORDER BY updated_at DESC LIMIT ? OFFSET ?")
528
+ .all(limit, offset);
529
+ return { table, rows, limit, offset };
530
+ },
531
+ });
532
+ }