@brud/daemon 0.0.0-stage → 0.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/LICENSE +201 -0
- package/README.md +67 -2
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +88 -0
- package/dist/client/DaemonClient.d.ts +55 -0
- package/dist/client/DaemonClient.js +383 -0
- package/dist/client/connectToDaemon.d.ts +19 -0
- package/dist/client/connectToDaemon.js +131 -0
- package/dist/client/errors.d.ts +74 -0
- package/dist/client/errors.js +104 -0
- package/dist/client/identity.d.ts +3 -0
- package/dist/client/identity.js +7 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +23 -0
- package/dist/client/socket.d.ts +9 -0
- package/dist/client/socket.js +101 -0
- package/dist/client/spawn.d.ts +7 -0
- package/dist/client/spawn.js +171 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +29 -0
- package/dist/instanceRegistry.d.ts +130 -0
- package/dist/instanceRegistry.js +332 -0
- package/dist/lifecycle/endpoint.d.ts +19 -0
- package/dist/lifecycle/endpoint.js +124 -0
- package/dist/lifecycle/idle.d.ts +28 -0
- package/dist/lifecycle/idle.js +69 -0
- package/dist/lifecycle/index.d.ts +4 -0
- package/dist/lifecycle/index.js +21 -0
- package/dist/lifecycle/lock.d.ts +28 -0
- package/dist/lifecycle/lock.js +155 -0
- package/dist/lifecycle/shutdown.d.ts +39 -0
- package/dist/lifecycle/shutdown.js +115 -0
- package/dist/operatorConfig.d.ts +36 -0
- package/dist/operatorConfig.js +92 -0
- package/dist/security/channel.d.ts +113 -0
- package/dist/security/channel.js +448 -0
- package/dist/security/noise.d.ts +64 -0
- package/dist/security/noise.js +313 -0
- package/dist/security/ownership.d.ts +36 -0
- package/dist/security/ownership.js +86 -0
- package/dist/security/peerCredentials.d.ts +104 -0
- package/dist/security/peerCredentials.js +244 -0
- package/dist/server/Connection.d.ts +70 -0
- package/dist/server/Connection.js +412 -0
- package/dist/server/DaemonServer.d.ts +77 -0
- package/dist/server/DaemonServer.js +396 -0
- package/dist/server/createDaemon.d.ts +52 -0
- package/dist/server/createDaemon.js +165 -0
- package/dist/server/dispatch.d.ts +52 -0
- package/dist/server/dispatch.js +291 -0
- package/dist/server/framing.d.ts +63 -0
- package/dist/server/framing.js +196 -0
- package/dist/server/handshake.d.ts +22 -0
- package/dist/server/handshake.js +94 -0
- package/dist/server/protocolValidation.d.ts +47 -0
- package/dist/server/protocolValidation.js +366 -0
- package/dist/server/wireErrors.d.ts +5 -0
- package/dist/server/wireErrors.js +190 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +6 -0
- package/package.json +55 -4
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PeerCredentialError = void 0;
|
|
4
|
+
exports.loadPeerCredentialHost = loadPeerCredentialHost;
|
|
5
|
+
exports.readProcessStartTime = readProcessStartTime;
|
|
6
|
+
exports.captureExpectedPeer = captureExpectedPeer;
|
|
7
|
+
exports.verifyPeerCredentials = verifyPeerCredentials;
|
|
8
|
+
exports.isPipePeer = isPipePeer;
|
|
9
|
+
exports.verifyWindowsPeer = verifyWindowsPeer;
|
|
10
|
+
const node_fs_1 = require("node:fs");
|
|
11
|
+
class PeerCredentialError extends Error {
|
|
12
|
+
reason;
|
|
13
|
+
constructor(reason) {
|
|
14
|
+
super(reason);
|
|
15
|
+
this.name = 'PeerCredentialError';
|
|
16
|
+
this.reason = reason;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.PeerCredentialError = PeerCredentialError;
|
|
20
|
+
let hostPromise;
|
|
21
|
+
/**
|
|
22
|
+
* Loads the prebuilt peer-credential helper.
|
|
23
|
+
*
|
|
24
|
+
* `@cotal-ai/seat` is the package used for both platforms: its shipped
|
|
25
|
+
* `peercred.node` reads `SO_PEERCRED` on Linux and `LOCAL_PEERCRED` plus
|
|
26
|
+
* `LOCAL_PEERPID` on macOS. The package never compiles at install time.
|
|
27
|
+
*/
|
|
28
|
+
async function loadPeerCredentialHost() {
|
|
29
|
+
hostPromise ??= import('@cotal-ai/seat').then((module) => {
|
|
30
|
+
if (typeof module.localPeerCredentials !== 'function'
|
|
31
|
+
|| typeof module.processStartTime !== 'function') {
|
|
32
|
+
throw new Error('The installed peer-credential helper does not expose the required primitives');
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
localPeerCredentials: module.localPeerCredentials,
|
|
36
|
+
processStartTime: module.processStartTime,
|
|
37
|
+
...(typeof module.windowsUserSid === 'function'
|
|
38
|
+
? { windowsUserSid: module.windowsUserSid }
|
|
39
|
+
: {}),
|
|
40
|
+
...(typeof module.listenPipe === 'function'
|
|
41
|
+
? { listenPipe: module.listenPipe }
|
|
42
|
+
: {}),
|
|
43
|
+
...(typeof module.connectPipe === 'function'
|
|
44
|
+
? { connectPipe: module.connectPipe }
|
|
45
|
+
: {}),
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
return await hostPromise;
|
|
49
|
+
}
|
|
50
|
+
function readLinuxStartTime(pid) {
|
|
51
|
+
let stat;
|
|
52
|
+
let bootId;
|
|
53
|
+
try {
|
|
54
|
+
stat = (0, node_fs_1.readFileSync)(`/proc/${pid}/stat`, 'utf8');
|
|
55
|
+
bootId = (0, node_fs_1.readFileSync)('/proc/sys/kernel/random/boot_id', 'utf8').trim();
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
throw new PeerCredentialError(`cannot read the start time of process ${pid} from /proc: ${error instanceof Error ? error.message : String(error)}`);
|
|
59
|
+
}
|
|
60
|
+
// The command name is parenthesized and may contain spaces or parentheses.
|
|
61
|
+
const closing = stat.lastIndexOf(')');
|
|
62
|
+
if (closing === -1) {
|
|
63
|
+
throw new PeerCredentialError(`cannot parse /proc/${pid}/stat`);
|
|
64
|
+
}
|
|
65
|
+
// Field 22 (starttime) is index 19 after the command name and state.
|
|
66
|
+
const fields = stat.slice(closing + 2).trim().split(/\s+/);
|
|
67
|
+
const startTicks = fields[19];
|
|
68
|
+
if (startTicks === undefined || !/^\d+$/.test(startTicks)) {
|
|
69
|
+
throw new PeerCredentialError(`cannot parse the start time of process ${pid}`);
|
|
70
|
+
}
|
|
71
|
+
return `linux:${bootId}:${startTicks}`;
|
|
72
|
+
}
|
|
73
|
+
function readDarwinStartTime(host, pid) {
|
|
74
|
+
let startTime;
|
|
75
|
+
try {
|
|
76
|
+
startTime = host.processStartTime(pid);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
throw new PeerCredentialError(`cannot read the start time of process ${pid}: ${error instanceof Error ? error.message : String(error)}`);
|
|
80
|
+
}
|
|
81
|
+
if (startTime === undefined || startTime.length === 0) {
|
|
82
|
+
throw new PeerCredentialError(`process ${pid} has no readable start time`);
|
|
83
|
+
}
|
|
84
|
+
return `darwin:${startTime}`;
|
|
85
|
+
}
|
|
86
|
+
function readWindowsStartTime(host, pid) {
|
|
87
|
+
let startTime;
|
|
88
|
+
try {
|
|
89
|
+
startTime = host.processStartTime(pid);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
throw new PeerCredentialError(`cannot read the start time of process ${pid}: ${error instanceof Error ? error.message : String(error)}`);
|
|
93
|
+
}
|
|
94
|
+
if (startTime === undefined || startTime.length === 0) {
|
|
95
|
+
throw new PeerCredentialError(`process ${pid} has no readable start time`);
|
|
96
|
+
}
|
|
97
|
+
return `win32:${startTime}`;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Reads a process start time in a form that can be compared across processes.
|
|
101
|
+
*
|
|
102
|
+
* Linux reads `/proc` (boot id plus the kernel start tick); macOS and Windows
|
|
103
|
+
* ask the prebuilt helper for the kernel process creation time. Every value
|
|
104
|
+
* survives a numeric PID reuse.
|
|
105
|
+
*/
|
|
106
|
+
function readProcessStartTime(host, pid) {
|
|
107
|
+
if (process.platform === 'linux') {
|
|
108
|
+
return readLinuxStartTime(pid);
|
|
109
|
+
}
|
|
110
|
+
if (process.platform === 'darwin') {
|
|
111
|
+
return readDarwinStartTime(host, pid);
|
|
112
|
+
}
|
|
113
|
+
if (process.platform === 'win32') {
|
|
114
|
+
return readWindowsStartTime(host, pid);
|
|
115
|
+
}
|
|
116
|
+
throw new PeerCredentialError(`peer-credential verification is not implemented on ${process.platform}`);
|
|
117
|
+
}
|
|
118
|
+
function readWindowsUserSid(host) {
|
|
119
|
+
if (typeof host.windowsUserSid !== 'function') {
|
|
120
|
+
throw new PeerCredentialError('the installed peer-credential helper does not expose windowsUserSid');
|
|
121
|
+
}
|
|
122
|
+
let userSid;
|
|
123
|
+
try {
|
|
124
|
+
userSid = host.windowsUserSid();
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
throw new PeerCredentialError(`cannot read this process's Windows user SID: ${error instanceof Error ? error.message : String(error)}`);
|
|
128
|
+
}
|
|
129
|
+
if (typeof userSid !== 'string' || userSid.length === 0) {
|
|
130
|
+
throw new PeerCredentialError('the operating system returned an empty Windows user SID');
|
|
131
|
+
}
|
|
132
|
+
return userSid;
|
|
133
|
+
}
|
|
134
|
+
/** Captures the expected peer before the daemon accepts any connection. */
|
|
135
|
+
function captureExpectedPeer(host, pid) {
|
|
136
|
+
if (!Number.isInteger(pid) || pid <= 0) {
|
|
137
|
+
throw new PeerCredentialError(`invalid expected peer pid ${String(pid)}`);
|
|
138
|
+
}
|
|
139
|
+
if (process.platform === 'win32') {
|
|
140
|
+
return {
|
|
141
|
+
pid,
|
|
142
|
+
userSid: readWindowsUserSid(host),
|
|
143
|
+
startTime: readProcessStartTime(host, pid),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
const uid = typeof process.geteuid === 'function'
|
|
147
|
+
? process.geteuid()
|
|
148
|
+
: typeof process.getuid === 'function'
|
|
149
|
+
? process.getuid()
|
|
150
|
+
: undefined;
|
|
151
|
+
if (uid === undefined) {
|
|
152
|
+
throw new PeerCredentialError(`peer-credential verification requires process.geteuid() on ${process.platform}`);
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
pid,
|
|
156
|
+
uid,
|
|
157
|
+
startTime: readProcessStartTime(host, pid),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function assertCredentialShape(credentials) {
|
|
161
|
+
if (!Number.isInteger(credentials.pid)
|
|
162
|
+
|| credentials.pid <= 0
|
|
163
|
+
|| !Number.isInteger(credentials.uid)
|
|
164
|
+
|| credentials.uid < 0
|
|
165
|
+
|| !Number.isInteger(credentials.gid)
|
|
166
|
+
|| credentials.gid < 0) {
|
|
167
|
+
throw new PeerCredentialError(`the operating system returned an invalid peer credential ${JSON.stringify(credentials)}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Verifies the connected process against the host that spawned the daemon.
|
|
172
|
+
*
|
|
173
|
+
* The check runs on the accepted socket, before the Noise handshake or any
|
|
174
|
+
* dispatch work. A failure is final: the caller must close the connection.
|
|
175
|
+
*/
|
|
176
|
+
function verifyPeerCredentials(socket, expected, host) {
|
|
177
|
+
let credentials;
|
|
178
|
+
try {
|
|
179
|
+
credentials = host.localPeerCredentials(socket);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
throw new PeerCredentialError(`the operating system did not expose the peer credentials: ${error instanceof Error ? error.message : String(error)}`);
|
|
183
|
+
}
|
|
184
|
+
assertCredentialShape(credentials);
|
|
185
|
+
const expectedUid = expected.uid;
|
|
186
|
+
if (expectedUid === undefined) {
|
|
187
|
+
throw new PeerCredentialError('the expected peer has no captured POSIX uid');
|
|
188
|
+
}
|
|
189
|
+
if (credentials.uid !== expectedUid) {
|
|
190
|
+
throw new PeerCredentialError(`peer uid ${credentials.uid} does not match the daemon uid ${expectedUid}`);
|
|
191
|
+
}
|
|
192
|
+
if (credentials.pid !== expected.pid) {
|
|
193
|
+
throw new PeerCredentialError(`peer pid ${credentials.pid} does not match the spawning host pid ${expected.pid}`);
|
|
194
|
+
}
|
|
195
|
+
const peerStartTime = readProcessStartTime(host, credentials.pid);
|
|
196
|
+
if (peerStartTime !== expected.startTime) {
|
|
197
|
+
throw new PeerCredentialError(`peer pid ${credentials.pid} has start time "${peerStartTime}", but the spawning host started at "${expected.startTime}"`);
|
|
198
|
+
}
|
|
199
|
+
return credentials;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Distinguishes an authenticated Windows pipe client from one the helper dropped.
|
|
203
|
+
*
|
|
204
|
+
* The helper only returns `fd` for a connection whose process identity it read;
|
|
205
|
+
* a `refused` result was already closed unread and must never be admitted.
|
|
206
|
+
*/
|
|
207
|
+
function isPipePeer(value) {
|
|
208
|
+
return typeof value.fd === 'number';
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Verifies the Windows named-pipe client against the host that spawned the daemon.
|
|
212
|
+
*
|
|
213
|
+
* The helper reports the connecting process's PID, token user SID, and creation
|
|
214
|
+
* time from one process handle at accept. The check is process-level: another
|
|
215
|
+
* process of the same user has the same SID but a different PID (and usually a
|
|
216
|
+
* different creation time), so it is refused before the Noise handshake or any
|
|
217
|
+
* dispatch work.
|
|
218
|
+
*/
|
|
219
|
+
function verifyWindowsPeer(peer, expected) {
|
|
220
|
+
if (!Number.isInteger(peer.pid)
|
|
221
|
+
|| peer.pid <= 0
|
|
222
|
+
|| typeof peer.user !== 'string'
|
|
223
|
+
|| peer.user.length === 0
|
|
224
|
+
|| typeof peer.birth !== 'string'
|
|
225
|
+
|| !/^\d+$/.test(peer.birth)) {
|
|
226
|
+
throw new PeerCredentialError(`the operating system returned an invalid peer identity ${JSON.stringify(peer)}`);
|
|
227
|
+
}
|
|
228
|
+
const expectedSid = expected.userSid;
|
|
229
|
+
if (typeof expectedSid !== 'string' || expectedSid.length === 0) {
|
|
230
|
+
throw new PeerCredentialError('the daemon has no captured Windows user SID');
|
|
231
|
+
}
|
|
232
|
+
if (peer.user !== expectedSid) {
|
|
233
|
+
throw new PeerCredentialError(`peer user SID ${peer.user} does not match the daemon user SID ${expectedSid}`);
|
|
234
|
+
}
|
|
235
|
+
if (peer.pid !== expected.pid) {
|
|
236
|
+
throw new PeerCredentialError(`peer pid ${peer.pid} does not match the spawning host pid ${expected.pid}`);
|
|
237
|
+
}
|
|
238
|
+
const peerStartTime = `win32:${peer.birth}`;
|
|
239
|
+
if (peerStartTime !== expected.startTime) {
|
|
240
|
+
throw new PeerCredentialError(`peer pid ${peer.pid} has start time "${peerStartTime}", but the spawning host started at "${expected.startTime}"`);
|
|
241
|
+
}
|
|
242
|
+
return peer;
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=peerCredentials.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { DaemonCallerTier } from '@brud/protocol';
|
|
2
|
+
import type { SecureChannel, SecureChannelHandshakeContext } from '../security/channel.js';
|
|
3
|
+
import type { DispatchSession, RequestDispatcher } from './dispatch.js';
|
|
4
|
+
/** Default ceiling on concurrent in-flight requests per connection. */
|
|
5
|
+
export declare const DEFAULT_MAX_IN_FLIGHT_PER_CONNECTION = 64;
|
|
6
|
+
/** Default server-side deadline for one request. */
|
|
7
|
+
export declare const DEFAULT_SERVER_REQUEST_TIMEOUT_MS: number;
|
|
8
|
+
export interface ConnectionActivityHooks {
|
|
9
|
+
readonly onRequestStart?: () => void;
|
|
10
|
+
readonly onRequestEnd?: () => void;
|
|
11
|
+
}
|
|
12
|
+
export interface ConnectionLimits {
|
|
13
|
+
readonly maxInFlight?: number;
|
|
14
|
+
readonly requestTimeoutMs?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* One authenticated daemon connection.
|
|
18
|
+
*
|
|
19
|
+
* The principal and daemon home are already fixed by the Noise session. This
|
|
20
|
+
* class enforces the post-handshake JSON-RPC contract, request correlation,
|
|
21
|
+
* the in-flight ceiling, and the per-request deadline.
|
|
22
|
+
*/
|
|
23
|
+
export declare class Connection implements DispatchSession {
|
|
24
|
+
readonly connectionId: string;
|
|
25
|
+
/** Principal fixed by the authenticated handshake. */
|
|
26
|
+
tier: DaemonCallerTier | undefined;
|
|
27
|
+
private readonly channel;
|
|
28
|
+
private readonly dispatcher;
|
|
29
|
+
private readonly hooks;
|
|
30
|
+
private readonly closeListeners;
|
|
31
|
+
private readonly maxInFlight;
|
|
32
|
+
private readonly requestTimeoutMs;
|
|
33
|
+
/**
|
|
34
|
+
* Serializes ordinary request dispatch. Cancellation notifications
|
|
35
|
+
* deliberately bypass this chain.
|
|
36
|
+
*/
|
|
37
|
+
private requestChain;
|
|
38
|
+
private readonly inFlight;
|
|
39
|
+
private handshakeCompleted;
|
|
40
|
+
private closed;
|
|
41
|
+
constructor(connectionId: string, channel: SecureChannel, context: SecureChannelHandshakeContext, dispatcher: RequestDispatcher, hooks?: ConnectionActivityHooks, limits?: ConnectionLimits);
|
|
42
|
+
get handshaken(): boolean;
|
|
43
|
+
get isClosed(): boolean;
|
|
44
|
+
markHandshaken(): void;
|
|
45
|
+
onClose(listener: () => void): void;
|
|
46
|
+
destroy(): void;
|
|
47
|
+
end(): void;
|
|
48
|
+
private handleRecord;
|
|
49
|
+
private handleNotification;
|
|
50
|
+
private scheduleRequest;
|
|
51
|
+
private registerInFlight;
|
|
52
|
+
private dispatchRequest;
|
|
53
|
+
private timeoutInFlight;
|
|
54
|
+
private finishInFlight;
|
|
55
|
+
private abortInFlight;
|
|
56
|
+
/**
|
|
57
|
+
* Delivers cancellation to the in-flight operation and acknowledges the
|
|
58
|
+
* observed outcome.
|
|
59
|
+
*
|
|
60
|
+
* `requestId` is defined when the caller sent `request.cancel` as a request;
|
|
61
|
+
* in that case the ack is that request's result. Otherwise the ack is the
|
|
62
|
+
* additive `request.cancelled` notification.
|
|
63
|
+
*/
|
|
64
|
+
private handleCancel;
|
|
65
|
+
private waitForSettlement;
|
|
66
|
+
private deliverCancelAck;
|
|
67
|
+
private send;
|
|
68
|
+
private handleFatalError;
|
|
69
|
+
private notifyClose;
|
|
70
|
+
}
|