opc-agent 3.0.1 → 4.0.1
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/README.md +404 -74
- package/README.zh-CN.md +82 -0
- package/dist/channels/dingtalk.d.ts +17 -0
- package/dist/channels/dingtalk.js +38 -0
- package/dist/channels/googlechat.d.ts +14 -0
- package/dist/channels/googlechat.js +37 -0
- package/dist/channels/imessage.d.ts +13 -0
- package/dist/channels/imessage.js +28 -0
- package/dist/channels/irc.d.ts +20 -0
- package/dist/channels/irc.js +71 -0
- package/dist/channels/line.d.ts +14 -0
- package/dist/channels/line.js +28 -0
- package/dist/channels/matrix.d.ts +15 -0
- package/dist/channels/matrix.js +28 -0
- package/dist/channels/mattermost.d.ts +18 -0
- package/dist/channels/mattermost.js +49 -0
- package/dist/channels/msteams.d.ts +14 -0
- package/dist/channels/msteams.js +28 -0
- package/dist/channels/nostr.d.ts +14 -0
- package/dist/channels/nostr.js +28 -0
- package/dist/channels/qq.d.ts +15 -0
- package/dist/channels/qq.js +28 -0
- package/dist/channels/signal.d.ts +14 -0
- package/dist/channels/signal.js +28 -0
- package/dist/channels/sms.d.ts +15 -0
- package/dist/channels/sms.js +28 -0
- package/dist/channels/twitch.d.ts +17 -0
- package/dist/channels/twitch.js +59 -0
- package/dist/channels/voice-call.d.ts +27 -0
- package/dist/channels/voice-call.js +82 -0
- package/dist/channels/whatsapp.d.ts +14 -0
- package/dist/channels/whatsapp.js +28 -0
- package/dist/cli/chat.d.ts +2 -0
- package/dist/cli/chat.js +134 -0
- package/dist/cli/setup.d.ts +4 -0
- package/dist/cli/setup.js +303 -0
- package/dist/cli.js +142 -6
- package/dist/core/api-server.d.ts +25 -0
- package/dist/core/api-server.js +286 -0
- package/dist/core/audio.d.ts +50 -0
- package/dist/core/audio.js +68 -0
- package/dist/core/context-discovery.d.ts +16 -0
- package/dist/core/context-discovery.js +107 -0
- package/dist/core/context-refs.d.ts +29 -0
- package/dist/core/context-refs.js +162 -0
- package/dist/core/gateway.d.ts +53 -0
- package/dist/core/gateway.js +80 -0
- package/dist/core/heartbeat.d.ts +19 -0
- package/dist/core/heartbeat.js +50 -0
- package/dist/core/hooks.d.ts +28 -0
- package/dist/core/hooks.js +82 -0
- package/dist/core/ide-bridge.d.ts +53 -0
- package/dist/core/ide-bridge.js +97 -0
- package/dist/core/node-network.d.ts +23 -0
- package/dist/core/node-network.js +77 -0
- package/dist/core/profiles.d.ts +27 -0
- package/dist/core/profiles.js +131 -0
- package/dist/core/sandbox.d.ts +25 -0
- package/dist/core/sandbox.js +84 -1
- package/dist/core/session-manager.d.ts +33 -0
- package/dist/core/session-manager.js +157 -0
- package/dist/core/vision.d.ts +45 -0
- package/dist/core/vision.js +177 -0
- package/dist/hub/brain-seed.d.ts +14 -0
- package/dist/hub/brain-seed.js +77 -0
- package/dist/hub/client.d.ts +25 -0
- package/dist/hub/client.js +44 -0
- package/dist/index.d.ts +66 -1
- package/dist/index.js +95 -3
- package/dist/memory/context-compressor.d.ts +43 -0
- package/dist/memory/context-compressor.js +167 -0
- package/dist/memory/index.d.ts +4 -0
- package/dist/memory/index.js +5 -1
- package/dist/memory/user-profiler.d.ts +50 -0
- package/dist/memory/user-profiler.js +201 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +54 -1
- package/dist/scheduler/cron-engine.d.ts +41 -0
- package/dist/scheduler/cron-engine.js +200 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +7 -0
- package/dist/schema/oad.d.ts +12 -12
- package/dist/security/approvals.d.ts +53 -0
- package/dist/security/approvals.js +115 -0
- package/dist/security/elevated.d.ts +41 -0
- package/dist/security/elevated.js +89 -0
- package/dist/security/index.d.ts +6 -0
- package/dist/security/index.js +7 -1
- package/dist/security/secrets.d.ts +34 -0
- package/dist/security/secrets.js +115 -0
- package/dist/skills/builtin/index.d.ts +6 -0
- package/dist/skills/builtin/index.js +402 -0
- package/dist/skills/marketplace.d.ts +30 -0
- package/dist/skills/marketplace.js +142 -0
- package/dist/skills/types.d.ts +34 -0
- package/dist/skills/types.js +16 -0
- package/dist/studio/server.d.ts +25 -0
- package/dist/studio/server.js +780 -0
- package/dist/studio/templates-data.d.ts +21 -0
- package/dist/studio/templates-data.js +148 -0
- package/dist/studio-ui/index.html +2502 -1073
- package/dist/tools/builtin/browser.d.ts +47 -0
- package/dist/tools/builtin/browser.js +284 -0
- package/dist/tools/builtin/home-assistant.d.ts +12 -0
- package/dist/tools/builtin/home-assistant.js +126 -0
- package/dist/tools/builtin/index.d.ts +7 -1
- package/dist/tools/builtin/index.js +23 -2
- package/dist/tools/builtin/rl-tools.d.ts +13 -0
- package/dist/tools/builtin/rl-tools.js +228 -0
- package/dist/tools/builtin/vision.d.ts +6 -0
- package/dist/tools/builtin/vision.js +61 -0
- package/dist/tools/builtin/web-search.d.ts +9 -0
- package/dist/tools/builtin/web-search.js +150 -0
- package/dist/tools/document-processor.d.ts +39 -0
- package/dist/tools/document-processor.js +188 -0
- package/dist/tools/image-generator.d.ts +42 -0
- package/dist/tools/image-generator.js +136 -0
- package/dist/tools/web-scraper.d.ts +20 -0
- package/dist/tools/web-scraper.js +148 -0
- package/dist/tools/web-search.d.ts +51 -0
- package/dist/tools/web-search.js +152 -0
- package/install.ps1 +154 -0
- package/install.sh +164 -0
- package/package.json +63 -52
- package/src/channels/dingtalk.ts +46 -0
- package/src/channels/googlechat.ts +42 -0
- package/src/channels/imessage.ts +32 -0
- package/src/channels/irc.ts +82 -0
- package/src/channels/line.ts +33 -0
- package/src/channels/matrix.ts +34 -0
- package/src/channels/mattermost.ts +57 -0
- package/src/channels/msteams.ts +33 -0
- package/src/channels/nostr.ts +33 -0
- package/src/channels/qq.ts +34 -0
- package/src/channels/signal.ts +33 -0
- package/src/channels/sms.ts +34 -0
- package/src/channels/twitch.ts +65 -0
- package/src/channels/voice-call.ts +100 -0
- package/src/channels/whatsapp.ts +33 -0
- package/src/cli/chat.ts +99 -0
- package/src/cli/setup.ts +314 -0
- package/src/cli.ts +148 -6
- package/src/core/api-server.ts +277 -0
- package/src/core/audio.ts +98 -0
- package/src/core/context-discovery.ts +85 -0
- package/src/core/context-refs.ts +140 -0
- package/src/core/gateway.ts +106 -0
- package/src/core/heartbeat.ts +51 -0
- package/src/core/hooks.ts +105 -0
- package/src/core/ide-bridge.ts +133 -0
- package/src/core/node-network.ts +86 -0
- package/src/core/profiles.ts +122 -0
- package/src/core/sandbox.ts +100 -0
- package/src/core/session-manager.ts +137 -0
- package/src/core/vision.ts +180 -0
- package/src/hub/brain-seed.ts +54 -0
- package/src/hub/client.ts +60 -0
- package/src/index.ts +86 -1
- package/src/memory/context-compressor.ts +189 -0
- package/src/memory/index.ts +4 -0
- package/src/memory/user-profiler.ts +215 -0
- package/src/providers/index.ts +64 -1
- package/src/scheduler/cron-engine.ts +191 -0
- package/src/scheduler/index.ts +2 -0
- package/src/security/approvals.ts +143 -0
- package/src/security/elevated.ts +105 -0
- package/src/security/index.ts +6 -0
- package/src/security/secrets.ts +129 -0
- package/src/skills/builtin/index.ts +408 -0
- package/src/skills/marketplace.ts +113 -0
- package/src/skills/types.ts +42 -0
- package/src/studio/server.ts +1591 -791
- package/src/studio/templates-data.ts +178 -0
- package/src/studio-ui/index.html +2502 -1073
- package/src/tools/builtin/browser.ts +299 -0
- package/src/tools/builtin/home-assistant.ts +116 -0
- package/src/tools/builtin/index.ts +37 -28
- package/src/tools/builtin/rl-tools.ts +243 -0
- package/src/tools/builtin/vision.ts +64 -0
- package/src/tools/builtin/web-search.ts +126 -0
- package/src/tools/document-processor.ts +213 -0
- package/src/tools/image-generator.ts +150 -0
- package/src/tools/web-scraper.ts +179 -0
- package/src/tools/web-search.ts +180 -0
- package/tests/api-server.test.ts +148 -0
- package/tests/approvals.test.ts +89 -0
- package/tests/audio.test.ts +40 -0
- package/tests/browser.test.ts +179 -0
- package/tests/builtin-tools.test.ts +83 -83
- package/tests/channels-extra.test.ts +45 -0
- package/tests/context-compressor.test.ts +172 -0
- package/tests/context-refs.test.ts +121 -0
- package/tests/cron-engine.test.ts +101 -0
- package/tests/document-processor.test.ts +69 -0
- package/tests/e2e-nocode.test.ts +442 -0
- package/tests/elevated.test.ts +69 -0
- package/tests/gateway.test.ts +63 -71
- package/tests/home-assistant.test.ts +40 -0
- package/tests/hooks.test.ts +79 -0
- package/tests/ide-bridge.test.ts +38 -0
- package/tests/image-generator.test.ts +84 -0
- package/tests/node-network.test.ts +74 -0
- package/tests/profiles.test.ts +61 -0
- package/tests/rl-tools.test.ts +93 -0
- package/tests/sandbox-manager.test.ts +46 -0
- package/tests/secrets.test.ts +107 -0
- package/tests/settings-api.test.ts +148 -0
- package/tests/setup.test.ts +73 -0
- package/tests/studio.test.ts +402 -229
- package/tests/tools/builtin-extended.test.ts +138 -138
- package/tests/user-profiler.test.ts +169 -0
- package/tests/v090-features.test.ts +254 -0
- package/tests/vision.test.ts +61 -0
- package/tests/voice-call.test.ts +47 -0
- package/tests/voice-interaction.test.ts +38 -0
- package/tests/web-search.test.ts +155 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Approvals Module - v1.0.0
|
|
3
|
+
* Policy-based exec approval system with queue, expiry, history, and callbacks.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { randomUUID } from 'crypto';
|
|
7
|
+
|
|
8
|
+
export type ExecApprovalPolicy = 'always' | 'elevated-only' | 'never' | 'allowlist';
|
|
9
|
+
|
|
10
|
+
export interface ExecApprovalRequest {
|
|
11
|
+
id: string;
|
|
12
|
+
command: string;
|
|
13
|
+
elevated: boolean;
|
|
14
|
+
requestedAt: number;
|
|
15
|
+
expiresAt: number;
|
|
16
|
+
status: 'pending' | 'approved' | 'denied' | 'expired';
|
|
17
|
+
approvedBy?: string;
|
|
18
|
+
reason?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ExecApprovalHistory {
|
|
22
|
+
request: ExecApprovalRequest;
|
|
23
|
+
resolvedAt: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ApprovalRequestCallback = (request: ExecApprovalRequest) => void;
|
|
27
|
+
|
|
28
|
+
export class ExecApprovalManager {
|
|
29
|
+
private policy: ExecApprovalPolicy;
|
|
30
|
+
private pending: Map<string, ExecApprovalRequest> = new Map();
|
|
31
|
+
private history: ExecApprovalHistory[] = [];
|
|
32
|
+
private allowedCommands: Set<string> = new Set();
|
|
33
|
+
private expiryMs: number;
|
|
34
|
+
private onRequestCallback?: ApprovalRequestCallback;
|
|
35
|
+
private expiryTimer?: ReturnType<typeof setInterval>;
|
|
36
|
+
|
|
37
|
+
constructor(options: {
|
|
38
|
+
policy?: ExecApprovalPolicy;
|
|
39
|
+
expiryMs?: number;
|
|
40
|
+
allowedCommands?: string[];
|
|
41
|
+
onRequest?: ApprovalRequestCallback;
|
|
42
|
+
} = {}) {
|
|
43
|
+
this.policy = options.policy ?? 'elevated-only';
|
|
44
|
+
this.expiryMs = options.expiryMs ?? 300_000; // 5 min default
|
|
45
|
+
this.onRequestCallback = options.onRequest;
|
|
46
|
+
if (options.allowedCommands) {
|
|
47
|
+
for (const cmd of options.allowedCommands) this.allowedCommands.add(cmd);
|
|
48
|
+
}
|
|
49
|
+
this.expiryTimer = setInterval(() => this.expirePending(), 10_000);
|
|
50
|
+
if (this.expiryTimer.unref) this.expiryTimer.unref();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getPolicy(): ExecApprovalPolicy { return this.policy; }
|
|
54
|
+
setPolicy(p: ExecApprovalPolicy): void { this.policy = p; }
|
|
55
|
+
|
|
56
|
+
addAllowedCommand(cmd: string): void { this.allowedCommands.add(cmd); }
|
|
57
|
+
removeAllowedCommand(cmd: string): void { this.allowedCommands.delete(cmd); }
|
|
58
|
+
getAllowedCommands(): string[] { return [...this.allowedCommands]; }
|
|
59
|
+
|
|
60
|
+
needsApproval(command: string, elevated: boolean): boolean {
|
|
61
|
+
switch (this.policy) {
|
|
62
|
+
case 'never': return false;
|
|
63
|
+
case 'always': return true;
|
|
64
|
+
case 'elevated-only': return elevated;
|
|
65
|
+
case 'allowlist': return !this.isAllowed(command);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private isAllowed(command: string): boolean {
|
|
70
|
+
for (const allowed of this.allowedCommands) {
|
|
71
|
+
if (command.startsWith(allowed) || command === allowed) return true;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
request(command: string, elevated: boolean = false): ExecApprovalRequest {
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
const req: ExecApprovalRequest = {
|
|
79
|
+
id: randomUUID(),
|
|
80
|
+
command,
|
|
81
|
+
elevated,
|
|
82
|
+
requestedAt: now,
|
|
83
|
+
expiresAt: now + this.expiryMs,
|
|
84
|
+
status: 'pending',
|
|
85
|
+
};
|
|
86
|
+
this.pending.set(req.id, req);
|
|
87
|
+
this.onRequestCallback?.(req);
|
|
88
|
+
return req;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
approve(id: string, approver: string): ExecApprovalRequest {
|
|
92
|
+
const req = this.pending.get(id);
|
|
93
|
+
if (!req) throw new Error(`Request ${id} not found`);
|
|
94
|
+
if (req.status !== 'pending') throw new Error(`Request ${id} already ${req.status}`);
|
|
95
|
+
req.status = 'approved';
|
|
96
|
+
req.approvedBy = approver;
|
|
97
|
+
this.pending.delete(id);
|
|
98
|
+
this.history.push({ request: req, resolvedAt: Date.now() });
|
|
99
|
+
return req;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
deny(id: string, approver: string, reason?: string): ExecApprovalRequest {
|
|
103
|
+
const req = this.pending.get(id);
|
|
104
|
+
if (!req) throw new Error(`Request ${id} not found`);
|
|
105
|
+
if (req.status !== 'pending') throw new Error(`Request ${id} already ${req.status}`);
|
|
106
|
+
req.status = 'denied';
|
|
107
|
+
req.approvedBy = approver;
|
|
108
|
+
req.reason = reason;
|
|
109
|
+
this.pending.delete(id);
|
|
110
|
+
this.history.push({ request: req, resolvedAt: Date.now() });
|
|
111
|
+
return req;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getPending(): ExecApprovalRequest[] {
|
|
115
|
+
return [...this.pending.values()];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getHistory(): ExecApprovalHistory[] {
|
|
119
|
+
return [...this.history];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getRequest(id: string): ExecApprovalRequest | undefined {
|
|
123
|
+
return this.pending.get(id) ?? this.history.find(h => h.request.id === id)?.request;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private expirePending(): void {
|
|
127
|
+
const now = Date.now();
|
|
128
|
+
for (const [id, req] of this.pending) {
|
|
129
|
+
if (now >= req.expiresAt) {
|
|
130
|
+
req.status = 'expired';
|
|
131
|
+
this.pending.delete(id);
|
|
132
|
+
this.history.push({ request: req, resolvedAt: now });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Force expire check (for testing) */
|
|
138
|
+
checkExpiry(): void { this.expirePending(); }
|
|
139
|
+
|
|
140
|
+
destroy(): void {
|
|
141
|
+
if (this.expiryTimer) clearInterval(this.expiryTimer);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elevated Permissions Module - v1.0.0
|
|
3
|
+
* Elevation mode management with allowed commands, auto-revoke, and audit log.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type ElevationMode = 'off' | 'ask' | 'on';
|
|
7
|
+
|
|
8
|
+
export interface ElevationAuditEntry {
|
|
9
|
+
timestamp: number;
|
|
10
|
+
action: 'elevate' | 'revoke' | 'execute' | 'deny';
|
|
11
|
+
command?: string;
|
|
12
|
+
reason?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class ElevatedManager {
|
|
16
|
+
private mode: ElevationMode;
|
|
17
|
+
private elevated: boolean = false;
|
|
18
|
+
private allowedCommands: RegExp[] = [];
|
|
19
|
+
private auditLog: ElevationAuditEntry[] = [];
|
|
20
|
+
private revokeTimer?: ReturnType<typeof setTimeout>;
|
|
21
|
+
private autoRevokeMs: number;
|
|
22
|
+
|
|
23
|
+
constructor(options: {
|
|
24
|
+
mode?: ElevationMode;
|
|
25
|
+
allowedCommands?: (string | RegExp)[];
|
|
26
|
+
autoRevokeMs?: number;
|
|
27
|
+
} = {}) {
|
|
28
|
+
this.mode = options.mode ?? 'ask';
|
|
29
|
+
this.autoRevokeMs = options.autoRevokeMs ?? 600_000; // 10 min
|
|
30
|
+
if (options.allowedCommands) {
|
|
31
|
+
for (const cmd of options.allowedCommands) {
|
|
32
|
+
this.allowedCommands.push(cmd instanceof RegExp ? cmd : new RegExp(`^${cmd.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getMode(): ElevationMode { return this.mode; }
|
|
38
|
+
setMode(mode: ElevationMode): void { this.mode = mode; }
|
|
39
|
+
isElevated(): boolean { return this.elevated; }
|
|
40
|
+
|
|
41
|
+
addAllowedCommand(pattern: string | RegExp): void {
|
|
42
|
+
this.allowedCommands.push(pattern instanceof RegExp ? pattern : new RegExp(`^${pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
isCommandAllowed(command: string): boolean {
|
|
46
|
+
return this.allowedCommands.some(r => r.test(command));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
elevate(reason?: string): boolean {
|
|
50
|
+
if (this.mode === 'off') return false;
|
|
51
|
+
this.elevated = true;
|
|
52
|
+
this.auditLog.push({ timestamp: Date.now(), action: 'elevate', reason });
|
|
53
|
+
this.startAutoRevoke();
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
revoke(reason?: string): void {
|
|
58
|
+
this.elevated = false;
|
|
59
|
+
this.clearAutoRevoke();
|
|
60
|
+
this.auditLog.push({ timestamp: Date.now(), action: 'revoke', reason });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
canExecute(command: string): { allowed: boolean; needsElevation: boolean } {
|
|
64
|
+
if (this.isCommandAllowed(command)) return { allowed: true, needsElevation: false };
|
|
65
|
+
if (this.mode === 'off') return { allowed: true, needsElevation: false };
|
|
66
|
+
if (this.mode === 'on') {
|
|
67
|
+
if (!this.elevated) this.elevate('auto-on mode');
|
|
68
|
+
this.auditLog.push({ timestamp: Date.now(), action: 'execute', command });
|
|
69
|
+
return { allowed: true, needsElevation: false };
|
|
70
|
+
}
|
|
71
|
+
// ask mode
|
|
72
|
+
if (this.elevated) {
|
|
73
|
+
this.auditLog.push({ timestamp: Date.now(), action: 'execute', command });
|
|
74
|
+
return { allowed: true, needsElevation: false };
|
|
75
|
+
}
|
|
76
|
+
return { allowed: false, needsElevation: true };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getAuditLog(): ElevationAuditEntry[] {
|
|
80
|
+
return [...this.auditLog];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
clearAuditLog(): void {
|
|
84
|
+
this.auditLog = [];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private startAutoRevoke(): void {
|
|
88
|
+
this.clearAutoRevoke();
|
|
89
|
+
this.revokeTimer = setTimeout(() => {
|
|
90
|
+
this.revoke('auto-revoke timer');
|
|
91
|
+
}, this.autoRevokeMs);
|
|
92
|
+
if (this.revokeTimer.unref) this.revokeTimer.unref();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private clearAutoRevoke(): void {
|
|
96
|
+
if (this.revokeTimer) {
|
|
97
|
+
clearTimeout(this.revokeTimer);
|
|
98
|
+
this.revokeTimer = undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
destroy(): void {
|
|
103
|
+
this.clearAutoRevoke();
|
|
104
|
+
}
|
|
105
|
+
}
|
package/src/security/index.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
export { ApprovalManager } from './approval';
|
|
2
2
|
export type { ApprovalPolicy, ApprovalRequest } from './approval';
|
|
3
3
|
export { KeyManager } from './keys';
|
|
4
|
+
export { ExecApprovalManager } from './approvals';
|
|
5
|
+
export type { ExecApprovalPolicy, ExecApprovalRequest, ExecApprovalHistory, ApprovalRequestCallback } from './approvals';
|
|
6
|
+
export { ElevatedManager } from './elevated';
|
|
7
|
+
export type { ElevationMode, ElevationAuditEntry } from './elevated';
|
|
8
|
+
export { SecretsManager } from './secrets';
|
|
9
|
+
export type { SecretsStore } from './secrets';
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets Manager - v1.0.0
|
|
3
|
+
* AES-256-GCM encrypted secrets storage with rotation, export/import.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { randomBytes, createCipheriv, createDecipheriv, scryptSync } from 'crypto';
|
|
7
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
|
|
8
|
+
import { dirname, join } from 'path';
|
|
9
|
+
import { homedir } from 'os';
|
|
10
|
+
|
|
11
|
+
const ALGORITHM = 'aes-256-gcm';
|
|
12
|
+
const KEY_LEN = 32;
|
|
13
|
+
const IV_LEN = 12;
|
|
14
|
+
const SALT_LEN = 16;
|
|
15
|
+
const TAG_LEN = 16;
|
|
16
|
+
|
|
17
|
+
export interface SecretsStore {
|
|
18
|
+
version: number;
|
|
19
|
+
secrets: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class SecretsManager {
|
|
23
|
+
private masterKey: Buffer;
|
|
24
|
+
private filePath: string;
|
|
25
|
+
private store: SecretsStore;
|
|
26
|
+
|
|
27
|
+
constructor(options: { password: string; filePath?: string }) {
|
|
28
|
+
this.filePath = options.filePath ?? join(homedir(), '.opc', 'secrets.enc');
|
|
29
|
+
// Derive a stable key from password (we store salt in the file)
|
|
30
|
+
this.masterKey = Buffer.alloc(KEY_LEN); // placeholder, set on load/init
|
|
31
|
+
this.store = { version: 1, secrets: {} };
|
|
32
|
+
this.init(options.password);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private init(password: string): void {
|
|
36
|
+
if (existsSync(this.filePath)) {
|
|
37
|
+
this.load(password);
|
|
38
|
+
} else {
|
|
39
|
+
const salt = randomBytes(SALT_LEN);
|
|
40
|
+
this.masterKey = scryptSync(password, salt, KEY_LEN) as Buffer;
|
|
41
|
+
this.store = { version: 1, secrets: {} };
|
|
42
|
+
this.save(salt);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private load(password: string): void {
|
|
47
|
+
const data = readFileSync(this.filePath);
|
|
48
|
+
const salt = data.subarray(0, SALT_LEN);
|
|
49
|
+
const iv = data.subarray(SALT_LEN, SALT_LEN + IV_LEN);
|
|
50
|
+
const tag = data.subarray(SALT_LEN + IV_LEN, SALT_LEN + IV_LEN + TAG_LEN);
|
|
51
|
+
const encrypted = data.subarray(SALT_LEN + IV_LEN + TAG_LEN);
|
|
52
|
+
|
|
53
|
+
this.masterKey = scryptSync(password, salt, KEY_LEN) as Buffer;
|
|
54
|
+
const decipher = createDecipheriv(ALGORITHM, this.masterKey, iv);
|
|
55
|
+
decipher.setAuthTag(tag);
|
|
56
|
+
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
|
|
57
|
+
this.store = JSON.parse(decrypted.toString('utf8'));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private save(salt?: Buffer): void {
|
|
61
|
+
const dir = dirname(this.filePath);
|
|
62
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
63
|
+
|
|
64
|
+
if (!salt && existsSync(this.filePath)) {
|
|
65
|
+
salt = readFileSync(this.filePath).subarray(0, SALT_LEN);
|
|
66
|
+
}
|
|
67
|
+
if (!salt) salt = randomBytes(SALT_LEN);
|
|
68
|
+
|
|
69
|
+
const iv = randomBytes(IV_LEN);
|
|
70
|
+
const cipher = createCipheriv(ALGORITHM, this.masterKey, iv);
|
|
71
|
+
const encrypted = Buffer.concat([cipher.update(JSON.stringify(this.store), 'utf8'), cipher.final()]);
|
|
72
|
+
const tag = cipher.getAuthTag();
|
|
73
|
+
|
|
74
|
+
writeFileSync(this.filePath, Buffer.concat([salt, iv, tag, encrypted]));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
set(key: string, value: string): void {
|
|
78
|
+
this.store.secrets[key] = value;
|
|
79
|
+
this.save();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get(key: string): string | undefined {
|
|
83
|
+
return this.store.secrets[key];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
delete(key: string): boolean {
|
|
87
|
+
if (!(key in this.store.secrets)) return false;
|
|
88
|
+
delete this.store.secrets[key];
|
|
89
|
+
this.save();
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
list(): string[] {
|
|
94
|
+
return Object.keys(this.store.secrets);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
has(key: string): boolean {
|
|
98
|
+
return key in this.store.secrets;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Inject secrets into env-like object */
|
|
102
|
+
inject(env: Record<string, string | undefined>, keys?: string[]): Record<string, string | undefined> {
|
|
103
|
+
const toInject = keys ?? this.list();
|
|
104
|
+
for (const k of toInject) {
|
|
105
|
+
if (this.has(k)) env[k] = this.store.secrets[k];
|
|
106
|
+
}
|
|
107
|
+
return env;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Rotate: re-encrypt with new password */
|
|
111
|
+
rotate(newPassword: string): void {
|
|
112
|
+
const salt = randomBytes(SALT_LEN);
|
|
113
|
+
this.masterKey = scryptSync(newPassword, salt, KEY_LEN) as Buffer;
|
|
114
|
+
this.save(salt);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Export as encrypted buffer */
|
|
118
|
+
exportEncrypted(): Buffer {
|
|
119
|
+
return readFileSync(this.filePath);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Import from encrypted buffer (must know password) */
|
|
123
|
+
static importEncrypted(data: Buffer, password: string, filePath: string): SecretsManager {
|
|
124
|
+
const dir = dirname(filePath);
|
|
125
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
126
|
+
writeFileSync(filePath, data);
|
|
127
|
+
return new SecretsManager({ password, filePath });
|
|
128
|
+
}
|
|
129
|
+
}
|