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
package/backend/cli.js CHANGED
@@ -525,6 +525,8 @@ async function fillConfig(args, interactive) {
525
525
  googleClientId: 'PULSE_GOOGLE_CLIENT_ID',
526
526
  googleClientSecret: 'PULSE_GOOGLE_CLIENT_SECRET',
527
527
  adminEmail: 'PULSE_SUPER_ADMIN_EMAILS',
528
+ gmailAddress: 'PULSE_GMAIL_ADDRESS',
529
+ gmailAppPassword: 'PULSE_GMAIL_APP_PASSWORD',
528
530
  port: 'PULSE_PORT',
529
531
  };
530
532
  for (const [flag, key] of Object.entries(flagMap)) {
@@ -573,6 +575,25 @@ free OAuth client from Google Cloud (about 3 minutes):
573
575
  );
574
576
  updates.PULSE_SUPER_ADMIN_EMAILS = await askUntil(prompter, 'Admin email', validEmails);
575
577
  }
578
+ // Optional: only offered while email is unconfigured, and skipped if declined (default).
579
+ if (!current('PULSE_GMAIL_ADDRESS') && !current('PULSE_GMAIL_APP_PASSWORD')) {
580
+ console.log(`
581
+ ${bold('Email (optional)')} — lets Pulse email invites to new users and send env-file backups.
582
+ Without it, new users can only be added with ${bold('pulse-hub seed-admins')}. It uses a Gmail account with
583
+ 2-Step Verification and an App Password (${bold('https://myaccount.google.com/apppasswords')}),
584
+ not the account's real password. You can also set it up later in ${ENV_FILE}.`);
585
+ const answer = await prompter.ask('Set up email now? (y/N)');
586
+ if (/^y/i.test(answer)) {
587
+ updates.PULSE_GMAIL_ADDRESS = await askUntil(
588
+ prompter,
589
+ 'Gmail address',
590
+ (v) => (validEmails(v) || v.includes(',') ? 'Enter a single email address.' : null),
591
+ );
592
+ updates.PULSE_GMAIL_APP_PASSWORD = (
593
+ await askUntil(prompter, 'App password (hidden)', notEmpty, { secret: true })
594
+ ).replace(/\s+/g, '');
595
+ }
596
+ }
576
597
  } finally {
577
598
  prompter.close();
578
599
  }
@@ -933,7 +954,8 @@ Usage: pulse-hub <command> [options]
933
954
 
934
955
  setup Install/repair the hub service on this box (asks for anything missing)
935
956
  --public-url URL --google-client-id ID --google-client-secret S
936
- --admin-email a@b.c --port N --import-from DIR --fresh --non-interactive
957
+ --admin-email a@b.c --gmail-address a@gmail.com --gmail-app-password P
958
+ --port N --import-from DIR --fresh --non-interactive
937
959
  upgrade Upgrade to the latest version (DB backup, automatic rollback)
938
960
  --version X.Y.Z --from file.tgz --force
939
961
  status Service state and installed/running/latest versions
@@ -1,52 +1,52 @@
1
- <!doctype html>
2
- <html>
3
- <body style="margin:0;padding:0;background:#f4f4f5;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;">
4
- <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f5;padding:40px 16px;">
5
- <tr><td align="center">
6
- <table role="presentation" width="480" cellpadding="0" cellspacing="0" style="max-width:480px;width:100%;background:#ffffff;border:1px solid #e4e4e7;border-radius:16px;overflow:hidden;">
7
- <tr><td style="padding:32px 32px 0 32px;">
8
- <span style="display:inline-block;background:#059669;color:#ffffff;font-size:12px;font-weight:700;letter-spacing:0.04em;text-transform:uppercase;padding:4px 10px;border-radius:999px;">Pulse</span>
9
- </td></tr>
10
- <tr><td style="padding:20px 32px 0 32px;">
11
- <h1 style="margin:0;font-size:21px;line-height:1.4;color:#18181b;font-weight:700;">
12
- {{headline}}
13
- </h1>
14
- </td></tr>
15
- <tr><td style="padding:16px 32px 0 32px;">
16
- <table role="presentation" cellpadding="0" cellspacing="0" style="background:#f4f4f5;border-radius:10px;width:100%;">
17
- <tr>
18
- <td style="padding:12px 16px;">
19
- <div style="font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">Server</div>
20
- <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{instanceLabel}}</div>
21
- <div style="margin-top:10px;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">File</div>
22
- <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{fileLabel}}</div>
23
- <div style="margin-top:2px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:#71717a;">{{filePath}}</div>
24
- <div style="margin-top:10px;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">{{actorLabel}}</div>
25
- <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{actor}}</div>
26
- <div style="margin-top:2px;font-size:12px;color:#71717a;">{{changeSummary}}</div>
27
- </td>
28
- </tr>
29
- </table>
30
- </td></tr>
31
- <tr><td style="padding:16px 32px 0 32px;">
32
- <p style="margin:0;font-size:14px;line-height:1.6;color:#52525b;">
33
- {{introText}}
34
- </p>
35
- </td></tr>
36
- <tr><td style="padding:16px 32px 0 32px;">
37
- <p style="margin:0;font-size:13px;line-height:1.6;color:#71717a;background:#fef9c3;border-radius:10px;padding:12px 14px;">
38
- The PDF is password-protected. The password is deliberately not in this email
39
- &mdash; ask whoever manages this server's env files for it.
40
- </p>
41
- </td></tr>
42
- <tr><td style="padding:24px 32px 28px 32px;border-top:1px solid #e4e4e7;margin-top:24px;">
43
- <p style="margin:16px 0 0 0;font-size:12px;line-height:1.6;color:#a1a1aa;">
44
- This file contains secrets. Treat the attachment the same way you'd treat the
45
- original env file.
46
- </p>
47
- </td></tr>
48
- </table>
49
- </td></tr>
50
- </table>
51
- </body>
52
- </html>
1
+ <!doctype html>
2
+ <html>
3
+ <body style="margin:0;padding:0;background:#f4f4f5;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;">
4
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f5;padding:40px 16px;">
5
+ <tr><td align="center">
6
+ <table role="presentation" width="480" cellpadding="0" cellspacing="0" style="max-width:480px;width:100%;background:#ffffff;border:1px solid #e4e4e7;border-radius:16px;overflow:hidden;">
7
+ <tr><td style="padding:32px 32px 0 32px;">
8
+ <span style="display:inline-block;background:#059669;color:#ffffff;font-size:12px;font-weight:700;letter-spacing:0.04em;text-transform:uppercase;padding:4px 10px;border-radius:999px;">Pulse</span>
9
+ </td></tr>
10
+ <tr><td style="padding:20px 32px 0 32px;">
11
+ <h1 style="margin:0;font-size:21px;line-height:1.4;color:#18181b;font-weight:700;">
12
+ {{headline}}
13
+ </h1>
14
+ </td></tr>
15
+ <tr><td style="padding:16px 32px 0 32px;">
16
+ <table role="presentation" cellpadding="0" cellspacing="0" style="background:#f4f4f5;border-radius:10px;width:100%;">
17
+ <tr>
18
+ <td style="padding:12px 16px;">
19
+ <div style="font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">Server</div>
20
+ <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{instanceLabel}}</div>
21
+ <div style="margin-top:10px;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">File</div>
22
+ <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{fileLabel}}</div>
23
+ <div style="margin-top:2px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:#71717a;">{{filePath}}</div>
24
+ <div style="margin-top:10px;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">{{actorLabel}}</div>
25
+ <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{actor}}</div>
26
+ <div style="margin-top:2px;font-size:12px;color:#71717a;">{{changeSummary}}</div>
27
+ </td>
28
+ </tr>
29
+ </table>
30
+ </td></tr>
31
+ <tr><td style="padding:16px 32px 0 32px;">
32
+ <p style="margin:0;font-size:14px;line-height:1.6;color:#52525b;">
33
+ {{introText}}
34
+ </p>
35
+ </td></tr>
36
+ <tr><td style="padding:16px 32px 0 32px;">
37
+ <p style="margin:0;font-size:13px;line-height:1.6;color:#71717a;background:#fef9c3;border-radius:10px;padding:12px 14px;">
38
+ The PDF is password-protected. The password is deliberately not in this email
39
+ &mdash; ask whoever manages this server's env files for it.
40
+ </p>
41
+ </td></tr>
42
+ <tr><td style="padding:24px 32px 28px 32px;border-top:1px solid #e4e4e7;margin-top:24px;">
43
+ <p style="margin:16px 0 0 0;font-size:12px;line-height:1.6;color:#a1a1aa;">
44
+ This file contains secrets. Treat the attachment the same way you'd treat the
45
+ original env file.
46
+ </p>
47
+ </td></tr>
48
+ </table>
49
+ </td></tr>
50
+ </table>
51
+ </body>
52
+ </html>
@@ -1,53 +1,53 @@
1
- <!doctype html>
2
- <html>
3
- <body style="margin:0;padding:0;background:#f4f4f5;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;">
4
- <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f5;padding:40px 16px;">
5
- <tr><td align="center">
6
- <table role="presentation" width="480" cellpadding="0" cellspacing="0" style="max-width:480px;width:100%;background:#ffffff;border:1px solid #e4e4e7;border-radius:16px;overflow:hidden;">
7
- <tr><td style="padding:32px 32px 0 32px;">
8
- <span style="display:inline-block;background:#059669;color:#ffffff;font-size:12px;font-weight:700;letter-spacing:0.04em;text-transform:uppercase;padding:4px 10px;border-radius:999px;">Pulse</span>
9
- </td></tr>
10
- <tr><td style="padding:20px 32px 0 32px;">
11
- <h1 style="margin:0;font-size:21px;line-height:1.4;color:#18181b;font-weight:700;">
12
- You've been invited to a Pulse monitoring hub
13
- </h1>
14
- </td></tr>
15
- <tr><td style="padding:16px 32px 0 32px;">
16
- <table role="presentation" cellpadding="0" cellspacing="0" style="background:#f4f4f5;border-radius:10px;width:100%;">
17
- <tr>
18
- <td style="padding:12px 16px;">
19
- <div style="font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">Invited by</div>
20
- <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{invitedByLabel}}</div>
21
- </td>
22
- </tr>
23
- </table>
24
- </td></tr>
25
- <tr><td style="padding:16px 32px 0 32px;">
26
- <p style="margin:0;font-size:14px;line-height:1.6;color:#52525b;">
27
- Pulse is a self-hosted server monitoring dashboard. Accept the invite below to get access.
28
- </p>
29
- </td></tr>
30
- <tr><td style="padding:24px 32px 0 32px;" align="center">
31
- <a href="{{inviteLink}}" style="display:inline-block;background:#059669;color:#ffffff;font-size:14px;font-weight:600;text-decoration:none;padding:12px 28px;border-radius:999px;">
32
- Accept invite
33
- </a>
34
- </td></tr>
35
- <tr><td style="padding:20px 32px 0 32px;">
36
- <p style="margin:0;font-size:12px;line-height:1.6;color:#a1a1aa;">
37
- This link expires in {{expiryLabel}}. If the button above doesn't work, copy and paste this URL:<br>
38
- <a href="{{inviteLink}}" style="color:#71717a;word-break:break-all;">{{inviteLink}}</a>
39
- </p>
40
- </td></tr>
41
- <tr><td style="padding:24px 32px 28px 32px;border-top:1px solid #e4e4e7;margin-top:24px;">
42
- <p style="margin:16px 0 8px 0;font-size:12px;line-height:1.6;color:#a1a1aa;">
43
- Having trouble with this invite, or didn't expect it?
44
- </p>
45
- <a href="{{resendLink}}" style="display:inline-block;font-size:12px;font-weight:600;color:#059669;text-decoration:underline;">
46
- Let {{invitedByLabel}} know
47
- </a>
48
- </td></tr>
49
- </table>
50
- </td></tr>
51
- </table>
52
- </body>
53
- </html>
1
+ <!doctype html>
2
+ <html>
3
+ <body style="margin:0;padding:0;background:#f4f4f5;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;">
4
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f5;padding:40px 16px;">
5
+ <tr><td align="center">
6
+ <table role="presentation" width="480" cellpadding="0" cellspacing="0" style="max-width:480px;width:100%;background:#ffffff;border:1px solid #e4e4e7;border-radius:16px;overflow:hidden;">
7
+ <tr><td style="padding:32px 32px 0 32px;">
8
+ <span style="display:inline-block;background:#059669;color:#ffffff;font-size:12px;font-weight:700;letter-spacing:0.04em;text-transform:uppercase;padding:4px 10px;border-radius:999px;">Pulse</span>
9
+ </td></tr>
10
+ <tr><td style="padding:20px 32px 0 32px;">
11
+ <h1 style="margin:0;font-size:21px;line-height:1.4;color:#18181b;font-weight:700;">
12
+ You've been invited to a Pulse monitoring hub
13
+ </h1>
14
+ </td></tr>
15
+ <tr><td style="padding:16px 32px 0 32px;">
16
+ <table role="presentation" cellpadding="0" cellspacing="0" style="background:#f4f4f5;border-radius:10px;width:100%;">
17
+ <tr>
18
+ <td style="padding:12px 16px;">
19
+ <div style="font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;">Invited by</div>
20
+ <div style="margin-top:2px;font-size:14px;font-weight:600;color:#18181b;">{{invitedByLabel}}</div>
21
+ </td>
22
+ </tr>
23
+ </table>
24
+ </td></tr>
25
+ <tr><td style="padding:16px 32px 0 32px;">
26
+ <p style="margin:0;font-size:14px;line-height:1.6;color:#52525b;">
27
+ Pulse is a self-hosted server monitoring dashboard. Accept the invite below to get access.
28
+ </p>
29
+ </td></tr>
30
+ <tr><td style="padding:24px 32px 0 32px;" align="center">
31
+ <a href="{{inviteLink}}" style="display:inline-block;background:#059669;color:#ffffff;font-size:14px;font-weight:600;text-decoration:none;padding:12px 28px;border-radius:999px;">
32
+ Accept invite
33
+ </a>
34
+ </td></tr>
35
+ <tr><td style="padding:20px 32px 0 32px;">
36
+ <p style="margin:0;font-size:12px;line-height:1.6;color:#a1a1aa;">
37
+ This link expires in {{expiryLabel}}. If the button above doesn't work, copy and paste this URL:<br>
38
+ <a href="{{inviteLink}}" style="color:#71717a;word-break:break-all;">{{inviteLink}}</a>
39
+ </p>
40
+ </td></tr>
41
+ <tr><td style="padding:24px 32px 28px 32px;border-top:1px solid #e4e4e7;margin-top:24px;">
42
+ <p style="margin:16px 0 8px 0;font-size:12px;line-height:1.6;color:#a1a1aa;">
43
+ Having trouble with this invite, or didn't expect it?
44
+ </p>
45
+ <a href="{{resendLink}}" style="display:inline-block;font-size:12px;font-weight:600;color:#059669;text-decoration:underline;">
46
+ Let {{invitedByLabel}} know
47
+ </a>
48
+ </td></tr>
49
+ </table>
50
+ </td></tr>
51
+ </table>
52
+ </body>
53
+ </html>
@@ -1,72 +1,72 @@
1
- // Shared plumbing for hub -> agent RPC routes (env-file editor, service restarts): the
2
- // "is the agent ready" checks, the uniform { ok:false, code, error } failure shape, and the
3
- // write-ahead-audited service restart used by both restart endpoints.
4
- //
5
- // Agent-side failures (offline, permission denied, conflict, ...) come back as HTTP 200 with
6
- // `{ ok:false, code, error }` — the same convention as POST .../update-agent — so the
7
- // frontend's useApiAction toasts them uniformly.
8
-
9
- const config = require('../config');
10
- const audit = require('./audit');
11
- const { sendJson } = require('./http-helpers');
12
- const { requestAgent, isAgentConnected, agentHasCapability } = require('./relay-state');
13
-
14
- // Checks the agent can be asked at all. Returns true to proceed, or false after having
15
- // written the response.
16
- function requireReadyAgent(res, instanceId) {
17
- if (!isAgentConnected(instanceId)) {
18
- sendJson(res, 200, { ok: false, code: 'OFFLINE', error: 'Instance is offline.' });
19
- return false;
20
- }
21
- if (!agentHasCapability(instanceId, 'env-files')) {
22
- sendJson(res, 200, {
23
- ok: false,
24
- code: 'AGENT_OUTDATED',
25
- error:
26
- 'This instance\'s agent is too old for env files — use "Update agent" on the Servers page, then try again.',
27
- });
28
- return false;
29
- }
30
- return true;
31
- }
32
-
33
- function sendAgentFailure(res, r) {
34
- sendJson(res, 200, {
35
- ok: false,
36
- code: r.code || 'ERROR',
37
- error: r.error || 'The agent reported an error.',
38
- });
39
- }
40
-
41
- // Restarts a unit on an instance. The caller has already authorized service.restart for this
42
- // exact (instance, service) and picked the unit from a registered env entry — this never
43
- // takes a unit name straight from a request. Write-ahead audit: an 'attempted' row before
44
- // the agent is asked, a second row with the outcome after.
45
- async function restartService(req, res, { instanceId, kind, name, envFileId }) {
46
- if (!requireReadyAgent(res, instanceId)) return;
47
- const token = audit.attempt(
48
- {
49
- action: 'service.restart',
50
- targetType: 'service',
51
- targetId: name,
52
- instanceId,
53
- summary: `Restart ${name}`,
54
- metadata: { kind, envFileId: envFileId || null },
55
- },
56
- req,
57
- );
58
- const r = await requestAgent(
59
- instanceId,
60
- { type: 'service_restart', kind, name },
61
- config.SERVICE_RESTART_TIMEOUT_MS,
62
- );
63
- if (!r.ok) {
64
- audit.finish(token, 'failed', `Restart ${name} failed: ${r.code || 'ERROR'}`);
65
- sendAgentFailure(res, r);
66
- return;
67
- }
68
- audit.finish(token, 'succeeded', `Restarted ${name}`, { active: !!r.active });
69
- sendJson(res, 200, { ok: true, active: !!r.active, state: r.state || 'unknown' });
70
- }
71
-
72
- module.exports = { requireReadyAgent, sendAgentFailure, restartService };
1
+ // Shared plumbing for hub -> agent RPC routes (env-file editor, service restarts): the
2
+ // "is the agent ready" checks, the uniform { ok:false, code, error } failure shape, and the
3
+ // write-ahead-audited service restart used by both restart endpoints.
4
+ //
5
+ // Agent-side failures (offline, permission denied, conflict, ...) come back as HTTP 200 with
6
+ // `{ ok:false, code, error }` — the same convention as POST .../update-agent — so the
7
+ // frontend's useApiAction toasts them uniformly.
8
+
9
+ const config = require('../config');
10
+ const audit = require('./audit');
11
+ const { sendJson } = require('./http-helpers');
12
+ const { requestAgent, isAgentConnected, agentHasCapability } = require('./relay-state');
13
+
14
+ // Checks the agent can be asked at all. Returns true to proceed, or false after having
15
+ // written the response.
16
+ function requireReadyAgent(res, instanceId) {
17
+ if (!isAgentConnected(instanceId)) {
18
+ sendJson(res, 200, { ok: false, code: 'OFFLINE', error: 'Instance is offline.' });
19
+ return false;
20
+ }
21
+ if (!agentHasCapability(instanceId, 'env-files')) {
22
+ sendJson(res, 200, {
23
+ ok: false,
24
+ code: 'AGENT_OUTDATED',
25
+ error:
26
+ 'This instance\'s agent is too old for env files — use "Update agent" on the Servers page, then try again.',
27
+ });
28
+ return false;
29
+ }
30
+ return true;
31
+ }
32
+
33
+ function sendAgentFailure(res, r) {
34
+ sendJson(res, 200, {
35
+ ok: false,
36
+ code: r.code || 'ERROR',
37
+ error: r.error || 'The agent reported an error.',
38
+ });
39
+ }
40
+
41
+ // Restarts a unit on an instance. The caller has already authorized service.restart for this
42
+ // exact (instance, service) and picked the unit from a registered env entry — this never
43
+ // takes a unit name straight from a request. Write-ahead audit: an 'attempted' row before
44
+ // the agent is asked, a second row with the outcome after.
45
+ async function restartService(req, res, { instanceId, kind, name, envFileId }) {
46
+ if (!requireReadyAgent(res, instanceId)) return;
47
+ const token = audit.attempt(
48
+ {
49
+ action: 'service.restart',
50
+ targetType: 'service',
51
+ targetId: name,
52
+ instanceId,
53
+ summary: `Restart ${name}`,
54
+ metadata: { kind, envFileId: envFileId || null },
55
+ },
56
+ req,
57
+ );
58
+ const r = await requestAgent(
59
+ instanceId,
60
+ { type: 'service_restart', kind, name },
61
+ config.SERVICE_RESTART_TIMEOUT_MS,
62
+ );
63
+ if (!r.ok) {
64
+ audit.finish(token, 'failed', `Restart ${name} failed: ${r.code || 'ERROR'}`);
65
+ sendAgentFailure(res, r);
66
+ return;
67
+ }
68
+ audit.finish(token, 'succeeded', `Restarted ${name}`, { active: !!r.active });
69
+ sendJson(res, 200, { ok: true, active: !!r.active, state: r.state || 'unknown' });
70
+ }
71
+
72
+ module.exports = { requireReadyAgent, sendAgentFailure, restartService };
@@ -1,17 +1,17 @@
1
- // Express 4 does not catch a rejected promise from an async handler: the request would hang
2
- // and, on current Node, the unhandled rejection would take the whole hub down. Wrap every
3
- // async route so a thrown error (an audit write that failed, a bad row, ...) becomes a 500.
4
- const { sendJson } = require('./http-helpers');
5
-
6
- function asyncRoute(handler) {
7
- return (req, res, next) => {
8
- // new Promise(...) so a synchronous throw is caught the same way as a rejection.
9
- new Promise((resolve) => resolve(handler(req, res, next))).catch((e) => {
10
- console.error(`[${req.method} ${req.path}]`, e);
11
- if (!res.headersSent) sendJson(res, 500, { error: 'Internal error.' });
12
- else res.end();
13
- });
14
- };
15
- }
16
-
17
- module.exports = { asyncRoute };
1
+ // Express 4 does not catch a rejected promise from an async handler: the request would hang
2
+ // and, on current Node, the unhandled rejection would take the whole hub down. Wrap every
3
+ // async route so a thrown error (an audit write that failed, a bad row, ...) becomes a 500.
4
+ const { sendJson } = require('./http-helpers');
5
+
6
+ function asyncRoute(handler) {
7
+ return (req, res, next) => {
8
+ // new Promise(...) so a synchronous throw is caught the same way as a rejection.
9
+ new Promise((resolve) => resolve(handler(req, res, next))).catch((e) => {
10
+ console.error(`[${req.method} ${req.path}]`, e);
11
+ if (!res.headersSent) sendJson(res, 500, { error: 'Internal error.' });
12
+ else res.end();
13
+ });
14
+ };
15
+ }
16
+
17
+ module.exports = { asyncRoute };
@@ -1,35 +1,35 @@
1
- // Startup hook for the permissions-v2 data migration (see authz/migrate.js). Required by
2
- // authz/index.js, so it runs the first time anything loads the authorization module — i.e.
3
- // before the hub serves a request. Kept separate from migrate.js so the migration itself
4
- // never calls process.exit (tests and the CLI call runMigration directly and catch the
5
- // typed MigrationParityError); only this wrapper turns a parity failure into exit(1).
6
-
7
- const { runMigration, formatReport, formatDiffs, MigrationParityError } = require('./migrate');
8
-
9
- function runStartupMigration() {
10
- try {
11
- const report = runMigration();
12
- if (report.status === 'migrated') {
13
- console.log('[authz] permissions migration applied');
14
- console.log(
15
- formatReport(report)
16
- .split('\n')
17
- .map((l) => `[authz] ${l}`)
18
- .join('\n'),
19
- );
20
- }
21
- return report;
22
- } catch (err) {
23
- if (err instanceof MigrationParityError) {
24
- console.error('[authz] FATAL: permissions migration parity check failed.');
25
- console.error(formatDiffs(err.diffs));
26
- console.error(
27
- '[authz] The migration was rolled back and the database is unchanged; a pre-migration backup was kept. Startup aborted — redeploy the previous version or run `npm run authz-migrate -- --dry-run`.',
28
- );
29
- process.exit(1);
30
- }
31
- throw err;
32
- }
33
- }
34
-
35
- module.exports = { runStartupMigration };
1
+ // Startup hook for the permissions-v2 data migration (see authz/migrate.js). Required by
2
+ // authz/index.js, so it runs the first time anything loads the authorization module — i.e.
3
+ // before the hub serves a request. Kept separate from migrate.js so the migration itself
4
+ // never calls process.exit (tests and the CLI call runMigration directly and catch the
5
+ // typed MigrationParityError); only this wrapper turns a parity failure into exit(1).
6
+
7
+ const { runMigration, formatReport, formatDiffs, MigrationParityError } = require('./migrate');
8
+
9
+ function runStartupMigration() {
10
+ try {
11
+ const report = runMigration();
12
+ if (report.status === 'migrated') {
13
+ console.log('[authz] permissions migration applied');
14
+ console.log(
15
+ formatReport(report)
16
+ .split('\n')
17
+ .map((l) => `[authz] ${l}`)
18
+ .join('\n'),
19
+ );
20
+ }
21
+ return report;
22
+ } catch (err) {
23
+ if (err instanceof MigrationParityError) {
24
+ console.error('[authz] FATAL: permissions migration parity check failed.');
25
+ console.error(formatDiffs(err.diffs));
26
+ console.error(
27
+ '[authz] The migration was rolled back and the database is unchanged; a pre-migration backup was kept. Startup aborted — redeploy the previous version or run `npm run authz-migrate -- --dry-run`.',
28
+ );
29
+ process.exit(1);
30
+ }
31
+ throw err;
32
+ }
33
+ }
34
+
35
+ module.exports = { runStartupMigration };