pulse-hub 3.5.0 → 3.6.0

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/backend/.env.example +52 -52
  3. package/backend/agent.js +372 -372
  4. package/backend/cli.js +23 -1
  5. package/backend/emails/env-backup.html +52 -52
  6. package/backend/emails/invite.html +53 -53
  7. package/backend/lib/agent-rpc.js +72 -72
  8. package/backend/lib/async-route.js +17 -17
  9. package/backend/lib/authz/bootstrap.js +35 -35
  10. package/backend/lib/authz/catalog.js +291 -291
  11. package/backend/lib/authz/delegation.js +104 -104
  12. package/backend/lib/authz/effective.js +104 -104
  13. package/backend/lib/authz/engine.js +251 -251
  14. package/backend/lib/authz/index.js +134 -134
  15. package/backend/lib/authz/migrate.js +871 -871
  16. package/backend/lib/authz/mutate.js +87 -87
  17. package/backend/lib/authz/preview.js +327 -327
  18. package/backend/lib/authz/scope.js +282 -282
  19. package/backend/lib/env-mask.js +131 -131
  20. package/backend/lib/invite-helpers.js +83 -83
  21. package/backend/lib/invites.js +160 -160
  22. package/backend/lib/logs.js +244 -244
  23. package/backend/lib/metrics.js +249 -249
  24. package/backend/lib/relay-state.js +564 -564
  25. package/backend/lib/session.js +48 -48
  26. package/backend/middleware/authz.js +65 -65
  27. package/backend/public/assets/favicon.svg +6 -6
  28. package/backend/public/assets/index.js +1 -1
  29. package/backend/public/index.html +23 -24
  30. package/backend/routes/access.js +123 -123
  31. package/backend/routes/audit.js +41 -41
  32. package/backend/routes/auth.js +207 -207
  33. package/backend/routes/invites-public.js +91 -91
  34. package/backend/routes/invites.js +121 -121
  35. package/backend/routes/logs.js +68 -68
  36. package/backend/routes/metrics.js +76 -76
  37. package/backend/routes/policies.js +240 -240
  38. package/backend/routes/teams.js +221 -221
  39. package/backend/routes/users.js +100 -100
  40. package/backend/seed-admins.js +68 -68
  41. package/backend/server.js +106 -106
  42. package/backend/ws/agent-relay.js +159 -159
  43. package/package.json +1 -1
@@ -1,244 +1,244 @@
1
- const fs = require('fs');
2
- const { spawn, spawnSync } = require('child_process');
3
- const readline = require('readline');
4
-
5
- function parseLine(line) {
6
- let record;
7
- try {
8
- record = JSON.parse(line);
9
- } catch (e) {
10
- return { raw: line, _epoch: null };
11
- }
12
- if (typeof record.message === 'string') {
13
- try {
14
- const inner = JSON.parse(record.message);
15
- record = { ...record, ...inner };
16
- delete record.message;
17
- } catch (e) {}
18
- }
19
- let epoch = null;
20
- if (record.time) epoch = Number(record.time);
21
- else if (record.timestamp) {
22
- const p = Date.parse(record.timestamp);
23
- if (!isNaN(p)) epoch = p;
24
- }
25
- record._epoch = epoch;
26
- return record;
27
- }
28
-
29
- // `docker logs --timestamps` prefixes every line with an RFC3339Nano timestamp, so
30
- // (unlike PM2's mixed JSON/non-JSON files) every line already has a real epoch — no
31
- // need to inherit a preceding line's timestamp.
32
- function parseDockerLine(line) {
33
- const m = line.match(/^(\S+) (.*)$/s);
34
- if (!m) return { raw: line, _epoch: Date.now() };
35
- const [, tsPart, rest] = m;
36
- const epoch = Date.parse(tsPart);
37
- const record = parseLine(rest);
38
- record._epoch = isNaN(epoch) ? Date.now() : epoch;
39
- return record;
40
- }
41
-
42
- // syslog PRIORITY (0 emerg .. 7 debug) -> the pino-style numbers the Logs UI badges.
43
- function journalLevel(priority) {
44
- const p = Number(priority);
45
- if (isNaN(p)) return 30;
46
- if (p <= 3) return 50;
47
- if (p === 4) return 40;
48
- return 30;
49
- }
50
-
51
- // `journalctl -o json` emits one JSON object per entry with a microsecond
52
- // __REALTIME_TIMESTAMP, so like Docker lines every entry has a real epoch. MESSAGE is a
53
- // byte array (not a string) when the entry contains non-UTF8/binary data.
54
- function parseJournalLine(line) {
55
- let e;
56
- try {
57
- e = JSON.parse(line);
58
- } catch (err) {
59
- return { raw: line, _epoch: Date.now() };
60
- }
61
- const msg = Array.isArray(e.MESSAGE)
62
- ? Buffer.from(e.MESSAGE).toString('utf-8')
63
- : String(e.MESSAGE ?? '');
64
- const epoch = Math.floor(Number(e.__REALTIME_TIMESTAMP) / 1000);
65
- return {
66
- msg,
67
- level: journalLevel(e.PRIORITY),
68
- priority: e.PRIORITY,
69
- unit: e._SYSTEMD_UNIT,
70
- identifier: e.SYSLOG_IDENTIFIER,
71
- pid: e._PID,
72
- _epoch: isNaN(epoch) ? Date.now() : epoch,
73
- };
74
- }
75
-
76
- function filterAndSort(records, search, level) {
77
- let lastEpoch = 0,
78
- tie = 0;
79
- for (const r of records) {
80
- if (r._epoch !== null) {
81
- lastEpoch = r._epoch;
82
- tie = 0;
83
- } else {
84
- tie += 1;
85
- r._epoch = lastEpoch + tie;
86
- }
87
- }
88
- records.sort((a, b) => b._epoch - a._epoch);
89
- let filtered = records;
90
- if (level) filtered = filtered.filter((r) => String(r.level) === String(level));
91
- if (search) {
92
- const s = search.toLowerCase();
93
- filtered = filtered.filter((r) => JSON.stringify(r).toLowerCase().includes(s));
94
- }
95
- return filtered;
96
- }
97
-
98
- function getFilteredRecords(filePath, search, level) {
99
- if (!filePath || !fs.existsSync(filePath)) return [];
100
- const content = fs.readFileSync(filePath, 'utf-8');
101
- const lines = content
102
- .split('\n')
103
- .filter((l) => l.trim().length > 0)
104
- .slice(-4000);
105
- return filterAndSort(lines.map(parseLine), search, level);
106
- }
107
-
108
- function getFilteredDockerRecords(containerId, stream, search, level) {
109
- if (!containerId) return [];
110
- let result;
111
- try {
112
- result = spawnSync('docker', ['logs', '--timestamps', '--tail', '4000', containerId], {
113
- encoding: 'utf-8',
114
- maxBuffer: 20 * 1024 * 1024,
115
- timeout: 15 * 1000, // sync call on the agent's only thread — never let a hung daemon freeze it
116
- });
117
- } catch (e) {
118
- return [];
119
- }
120
- const out = stream === 'err' ? result.stderr : result.stdout;
121
- if (!out) return [];
122
- const lines = out.split('\n').filter((l) => l.trim().length > 0);
123
- return filterAndSort(lines.map(parseDockerLine), search, level);
124
- }
125
-
126
- // `unit` is passed as an argv element, never interpolated into a shell string, and the
127
- // agent only ever calls this with a name it resolved from its own live unit list.
128
- function getFilteredJournalRecords(unit, search, level) {
129
- if (!unit) return [];
130
- let result;
131
- try {
132
- result = spawnSync('journalctl', ['-u', unit, '-o', 'json', '-n', '4000', '--no-pager'], {
133
- encoding: 'utf-8',
134
- maxBuffer: 20 * 1024 * 1024,
135
- timeout: 15 * 1000, // sync call on the agent's only thread — never let a hung daemon freeze it
136
- });
137
- } catch (e) {
138
- return [];
139
- }
140
- if (!result.stdout) return [];
141
- const lines = result.stdout.split('\n').filter((l) => l.trim().length > 0);
142
- return filterAndSort(lines.map(parseJournalLine), search, level);
143
- }
144
-
145
- // Polls a growing log file for new lines and reports parsed records as they appear.
146
- // Mirrors the /api/logs/stream poll loop that used to live directly in server.js.
147
- function createTailer(filePath, matches, onRecord) {
148
- if (!filePath || !fs.existsSync(filePath)) return { stop() {} };
149
- let position = fs.statSync(filePath).size,
150
- leftover = '';
151
- function poll() {
152
- fs.stat(filePath, (err, stats) => {
153
- if (err) return;
154
- if (stats.size < position) position = 0;
155
- if (stats.size > position) {
156
- const rs = fs.createReadStream(filePath, {
157
- start: position,
158
- end: stats.size - 1,
159
- encoding: 'utf-8',
160
- });
161
- let chunk = '';
162
- rs.on('data', (d) => (chunk += d));
163
- rs.on('end', () => {
164
- position = stats.size;
165
- const parts = (leftover + chunk).split('\n');
166
- leftover = parts.pop();
167
- parts
168
- .filter((l) => l.trim().length > 0)
169
- .forEach((line) => {
170
- const record = parseLine(line);
171
- if (record._epoch === null) record._epoch = Date.now();
172
- if (matches(record)) onRecord(record);
173
- });
174
- });
175
- }
176
- });
177
- }
178
- const timer = setInterval(poll, 1000);
179
- return {
180
- stop() {
181
- clearInterval(timer);
182
- },
183
- };
184
- }
185
-
186
- // Follows a container's live log output via `docker logs -f`, mirroring createTailer's
187
- // shape/contract so agent.js can treat both the same way.
188
- function createDockerTailer(containerId, stream, matches, onRecord) {
189
- if (!containerId) return { stop() {} };
190
- const child = spawn('docker', ['logs', '-f', '--since', new Date().toISOString(), containerId]);
191
- const rl = readline.createInterface({ input: stream === 'err' ? child.stderr : child.stdout });
192
- rl.on('line', (line) => {
193
- if (!line.trim()) return;
194
- const record = parseDockerLine(line);
195
- if (matches(record)) onRecord(record);
196
- });
197
- return {
198
- stop() {
199
- rl.close();
200
- child.kill();
201
- },
202
- };
203
- }
204
-
205
- // Follows a unit's live journal output via `journalctl -f`, same contract as
206
- // createDockerTailer.
207
- function createJournalTailer(unit, matches, onRecord) {
208
- if (!unit) return { stop() {} };
209
- const child = spawn('journalctl', [
210
- '-u',
211
- unit,
212
- '-o',
213
- 'json',
214
- '-f',
215
- '--since',
216
- 'now',
217
- '--no-pager',
218
- ]);
219
- child.on('error', () => {}); // journalctl missing — nothing to tail
220
- const rl = readline.createInterface({ input: child.stdout });
221
- rl.on('line', (line) => {
222
- if (!line.trim()) return;
223
- const record = parseJournalLine(line);
224
- if (matches(record)) onRecord(record);
225
- });
226
- return {
227
- stop() {
228
- rl.close();
229
- child.kill();
230
- },
231
- };
232
- }
233
-
234
- module.exports = {
235
- parseLine,
236
- parseDockerLine,
237
- parseJournalLine,
238
- getFilteredRecords,
239
- getFilteredDockerRecords,
240
- getFilteredJournalRecords,
241
- createTailer,
242
- createDockerTailer,
243
- createJournalTailer,
244
- };
1
+ const fs = require('fs');
2
+ const { spawn, spawnSync } = require('child_process');
3
+ const readline = require('readline');
4
+
5
+ function parseLine(line) {
6
+ let record;
7
+ try {
8
+ record = JSON.parse(line);
9
+ } catch (e) {
10
+ return { raw: line, _epoch: null };
11
+ }
12
+ if (typeof record.message === 'string') {
13
+ try {
14
+ const inner = JSON.parse(record.message);
15
+ record = { ...record, ...inner };
16
+ delete record.message;
17
+ } catch (e) {}
18
+ }
19
+ let epoch = null;
20
+ if (record.time) epoch = Number(record.time);
21
+ else if (record.timestamp) {
22
+ const p = Date.parse(record.timestamp);
23
+ if (!isNaN(p)) epoch = p;
24
+ }
25
+ record._epoch = epoch;
26
+ return record;
27
+ }
28
+
29
+ // `docker logs --timestamps` prefixes every line with an RFC3339Nano timestamp, so
30
+ // (unlike PM2's mixed JSON/non-JSON files) every line already has a real epoch — no
31
+ // need to inherit a preceding line's timestamp.
32
+ function parseDockerLine(line) {
33
+ const m = line.match(/^(\S+) (.*)$/s);
34
+ if (!m) return { raw: line, _epoch: Date.now() };
35
+ const [, tsPart, rest] = m;
36
+ const epoch = Date.parse(tsPart);
37
+ const record = parseLine(rest);
38
+ record._epoch = isNaN(epoch) ? Date.now() : epoch;
39
+ return record;
40
+ }
41
+
42
+ // syslog PRIORITY (0 emerg .. 7 debug) -> the pino-style numbers the Logs UI badges.
43
+ function journalLevel(priority) {
44
+ const p = Number(priority);
45
+ if (isNaN(p)) return 30;
46
+ if (p <= 3) return 50;
47
+ if (p === 4) return 40;
48
+ return 30;
49
+ }
50
+
51
+ // `journalctl -o json` emits one JSON object per entry with a microsecond
52
+ // __REALTIME_TIMESTAMP, so like Docker lines every entry has a real epoch. MESSAGE is a
53
+ // byte array (not a string) when the entry contains non-UTF8/binary data.
54
+ function parseJournalLine(line) {
55
+ let e;
56
+ try {
57
+ e = JSON.parse(line);
58
+ } catch (err) {
59
+ return { raw: line, _epoch: Date.now() };
60
+ }
61
+ const msg = Array.isArray(e.MESSAGE)
62
+ ? Buffer.from(e.MESSAGE).toString('utf-8')
63
+ : String(e.MESSAGE ?? '');
64
+ const epoch = Math.floor(Number(e.__REALTIME_TIMESTAMP) / 1000);
65
+ return {
66
+ msg,
67
+ level: journalLevel(e.PRIORITY),
68
+ priority: e.PRIORITY,
69
+ unit: e._SYSTEMD_UNIT,
70
+ identifier: e.SYSLOG_IDENTIFIER,
71
+ pid: e._PID,
72
+ _epoch: isNaN(epoch) ? Date.now() : epoch,
73
+ };
74
+ }
75
+
76
+ function filterAndSort(records, search, level) {
77
+ let lastEpoch = 0,
78
+ tie = 0;
79
+ for (const r of records) {
80
+ if (r._epoch !== null) {
81
+ lastEpoch = r._epoch;
82
+ tie = 0;
83
+ } else {
84
+ tie += 1;
85
+ r._epoch = lastEpoch + tie;
86
+ }
87
+ }
88
+ records.sort((a, b) => b._epoch - a._epoch);
89
+ let filtered = records;
90
+ if (level) filtered = filtered.filter((r) => String(r.level) === String(level));
91
+ if (search) {
92
+ const s = search.toLowerCase();
93
+ filtered = filtered.filter((r) => JSON.stringify(r).toLowerCase().includes(s));
94
+ }
95
+ return filtered;
96
+ }
97
+
98
+ function getFilteredRecords(filePath, search, level) {
99
+ if (!filePath || !fs.existsSync(filePath)) return [];
100
+ const content = fs.readFileSync(filePath, 'utf-8');
101
+ const lines = content
102
+ .split('\n')
103
+ .filter((l) => l.trim().length > 0)
104
+ .slice(-4000);
105
+ return filterAndSort(lines.map(parseLine), search, level);
106
+ }
107
+
108
+ function getFilteredDockerRecords(containerId, stream, search, level) {
109
+ if (!containerId) return [];
110
+ let result;
111
+ try {
112
+ result = spawnSync('docker', ['logs', '--timestamps', '--tail', '4000', containerId], {
113
+ encoding: 'utf-8',
114
+ maxBuffer: 20 * 1024 * 1024,
115
+ timeout: 15 * 1000, // sync call on the agent's only thread — never let a hung daemon freeze it
116
+ });
117
+ } catch (e) {
118
+ return [];
119
+ }
120
+ const out = stream === 'err' ? result.stderr : result.stdout;
121
+ if (!out) return [];
122
+ const lines = out.split('\n').filter((l) => l.trim().length > 0);
123
+ return filterAndSort(lines.map(parseDockerLine), search, level);
124
+ }
125
+
126
+ // `unit` is passed as an argv element, never interpolated into a shell string, and the
127
+ // agent only ever calls this with a name it resolved from its own live unit list.
128
+ function getFilteredJournalRecords(unit, search, level) {
129
+ if (!unit) return [];
130
+ let result;
131
+ try {
132
+ result = spawnSync('journalctl', ['-u', unit, '-o', 'json', '-n', '4000', '--no-pager'], {
133
+ encoding: 'utf-8',
134
+ maxBuffer: 20 * 1024 * 1024,
135
+ timeout: 15 * 1000, // sync call on the agent's only thread — never let a hung daemon freeze it
136
+ });
137
+ } catch (e) {
138
+ return [];
139
+ }
140
+ if (!result.stdout) return [];
141
+ const lines = result.stdout.split('\n').filter((l) => l.trim().length > 0);
142
+ return filterAndSort(lines.map(parseJournalLine), search, level);
143
+ }
144
+
145
+ // Polls a growing log file for new lines and reports parsed records as they appear.
146
+ // Mirrors the /api/logs/stream poll loop that used to live directly in server.js.
147
+ function createTailer(filePath, matches, onRecord) {
148
+ if (!filePath || !fs.existsSync(filePath)) return { stop() {} };
149
+ let position = fs.statSync(filePath).size,
150
+ leftover = '';
151
+ function poll() {
152
+ fs.stat(filePath, (err, stats) => {
153
+ if (err) return;
154
+ if (stats.size < position) position = 0;
155
+ if (stats.size > position) {
156
+ const rs = fs.createReadStream(filePath, {
157
+ start: position,
158
+ end: stats.size - 1,
159
+ encoding: 'utf-8',
160
+ });
161
+ let chunk = '';
162
+ rs.on('data', (d) => (chunk += d));
163
+ rs.on('end', () => {
164
+ position = stats.size;
165
+ const parts = (leftover + chunk).split('\n');
166
+ leftover = parts.pop();
167
+ parts
168
+ .filter((l) => l.trim().length > 0)
169
+ .forEach((line) => {
170
+ const record = parseLine(line);
171
+ if (record._epoch === null) record._epoch = Date.now();
172
+ if (matches(record)) onRecord(record);
173
+ });
174
+ });
175
+ }
176
+ });
177
+ }
178
+ const timer = setInterval(poll, 1000);
179
+ return {
180
+ stop() {
181
+ clearInterval(timer);
182
+ },
183
+ };
184
+ }
185
+
186
+ // Follows a container's live log output via `docker logs -f`, mirroring createTailer's
187
+ // shape/contract so agent.js can treat both the same way.
188
+ function createDockerTailer(containerId, stream, matches, onRecord) {
189
+ if (!containerId) return { stop() {} };
190
+ const child = spawn('docker', ['logs', '-f', '--since', new Date().toISOString(), containerId]);
191
+ const rl = readline.createInterface({ input: stream === 'err' ? child.stderr : child.stdout });
192
+ rl.on('line', (line) => {
193
+ if (!line.trim()) return;
194
+ const record = parseDockerLine(line);
195
+ if (matches(record)) onRecord(record);
196
+ });
197
+ return {
198
+ stop() {
199
+ rl.close();
200
+ child.kill();
201
+ },
202
+ };
203
+ }
204
+
205
+ // Follows a unit's live journal output via `journalctl -f`, same contract as
206
+ // createDockerTailer.
207
+ function createJournalTailer(unit, matches, onRecord) {
208
+ if (!unit) return { stop() {} };
209
+ const child = spawn('journalctl', [
210
+ '-u',
211
+ unit,
212
+ '-o',
213
+ 'json',
214
+ '-f',
215
+ '--since',
216
+ 'now',
217
+ '--no-pager',
218
+ ]);
219
+ child.on('error', () => {}); // journalctl missing — nothing to tail
220
+ const rl = readline.createInterface({ input: child.stdout });
221
+ rl.on('line', (line) => {
222
+ if (!line.trim()) return;
223
+ const record = parseJournalLine(line);
224
+ if (matches(record)) onRecord(record);
225
+ });
226
+ return {
227
+ stop() {
228
+ rl.close();
229
+ child.kill();
230
+ },
231
+ };
232
+ }
233
+
234
+ module.exports = {
235
+ parseLine,
236
+ parseDockerLine,
237
+ parseJournalLine,
238
+ getFilteredRecords,
239
+ getFilteredDockerRecords,
240
+ getFilteredJournalRecords,
241
+ createTailer,
242
+ createDockerTailer,
243
+ createJournalTailer,
244
+ };