mcp-ssh-manager 4.1.0 → 4.3.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.
- package/CHANGELOG.md +201 -0
- package/CLAUDE.md +3 -5
- package/Formula/ssh-manager.rb +2 -2
- package/INSTALLATION.md +4 -0
- package/README.md +31 -5
- package/cli/codex.js +67 -0
- package/cli/guard.js +69 -0
- package/cli/lib/config.sh +102 -37
- package/cli/ssh-manager +13 -0
- package/cli/ssh-manager.js +1 -1
- package/desktop/electron/electron-builder.yml +5 -0
- package/desktop/electron/locale.mjs +55 -0
- package/desktop/electron/main.js +52 -65
- package/desktop/electron/native-menu.mjs +44 -0
- package/desktop/electron/native.fr.json +58 -0
- package/desktop/electron/native.ja.json +58 -0
- package/desktop/electron/native.zh-CN.json +58 -0
- package/desktop/electron/package-lock.json +2 -2
- package/desktop/electron/package.json +1 -1
- package/desktop/electron/smoke-test.mjs +15 -1
- package/dist/ui/app.css +1 -1
- package/dist/ui/app.js +135 -119
- package/docs/MIGRATION.md +1 -1
- package/docs/SECURITY_MODES.md +15 -8
- package/docs/SEO.md +1 -1
- package/package.json +17 -3
- package/scripts/language.mjs +172 -0
- package/scripts/test-upgrade-from-published.mjs +24 -2
- package/server.json +2 -2
- package/src/audit.js +6 -15
- package/src/background-job.js +44 -0
- package/src/backup-manager.js +24 -29
- package/src/config-loader.js +5 -1
- package/src/control-plane.js +5 -0
- package/src/database-manager.js +13 -31
- package/src/dump-guard.js +79 -0
- package/src/health-monitor.js +8 -4
- package/src/index.js +183 -21
- package/src/logger.js +3 -1
- package/src/quiet-stderr.js +15 -0
- package/src/ssh-guard.js +344 -0
- package/src/ssh-manager.js +51 -0
- package/src/tunnel-manager.js +14 -4
package/src/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
// First, so that SSH_LOG_STDERR=false is honoured before any other module prints.
|
|
4
|
+
import './quiet-stderr.js';
|
|
3
5
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
4
6
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
5
7
|
import { z } from 'zod';
|
|
@@ -92,6 +94,7 @@ import {
|
|
|
92
94
|
generateBackupId,
|
|
93
95
|
getBackupMetadataPath,
|
|
94
96
|
getBackupFilePath,
|
|
97
|
+
backupExtension,
|
|
95
98
|
assertSafeBackupId,
|
|
96
99
|
buildReadMetadataCommand,
|
|
97
100
|
buildEnsureBackupDirCommand,
|
|
@@ -129,6 +132,7 @@ import {
|
|
|
129
132
|
buildMySQLDumpCommand as buildDBMySQLDumpCommand,
|
|
130
133
|
buildPostgreSQLDumpCommand as buildDBPostgreSQLDumpCommand,
|
|
131
134
|
buildMongoDBDumpCommand as buildDBMongoDBDumpCommand,
|
|
135
|
+
mongoArchivePath,
|
|
132
136
|
buildMySQLImportCommand,
|
|
133
137
|
buildPostgreSQLImportCommand,
|
|
134
138
|
buildMongoDBRestoreCommand,
|
|
@@ -150,6 +154,7 @@ import {
|
|
|
150
154
|
} from './database-manager.js';
|
|
151
155
|
import { loadToolConfig, isToolEnabled } from './tool-config-manager.js';
|
|
152
156
|
import { evaluatePolicy } from './policy.js';
|
|
157
|
+
import { buildBackgroundCommand, parseBackgroundStart } from './background-job.js';
|
|
153
158
|
import { needsApproval, isControlPlaneListening, requestDecision, buildRequest, isApprovalExempt, isReadOnlyAction, HANDLER_GATED_TOOLS } from './approval.js';
|
|
154
159
|
import { shellQuote, shellPath, safeInteger, withRemoteTimeout } from './shell-quote.js';
|
|
155
160
|
import { openStream } from './live-stream.js';
|
|
@@ -319,13 +324,40 @@ async function applyServerPolicy(serverName, toolName, args, command) {
|
|
|
319
324
|
return null;
|
|
320
325
|
}
|
|
321
326
|
|
|
322
|
-
//
|
|
323
|
-
//
|
|
327
|
+
// A handler that records its own outcome (an exit code, one line per server)
|
|
328
|
+
// calls this after execution. Calling it also tells the shared guard, which
|
|
329
|
+
// audits every other tool itself, not to write a second line.
|
|
330
|
+
// No-op when AUDIT_LOG is not configured.
|
|
324
331
|
async function auditOk(serverName, toolName, args, executionResult) {
|
|
325
332
|
const serverConfig = await getServerConfig(serverName);
|
|
333
|
+
const scope = toolContext.getStore()?.audit;
|
|
334
|
+
if (scope) scope.written = true;
|
|
326
335
|
auditLog(serverConfig, toolName, args, { allowed: true }, executionResult);
|
|
327
336
|
}
|
|
328
337
|
|
|
338
|
+
/**
|
|
339
|
+
* The outcome line for a tool that passed the policy gate and did not write its
|
|
340
|
+
* own: success unless the handler threw or answered with isError. Only a denial
|
|
341
|
+
* or an approval decision used to be recorded for these tools, so most of what
|
|
342
|
+
* ran on a server left no trace. A broken audit file never changes the result.
|
|
343
|
+
* @param {any} serverConfig
|
|
344
|
+
* @param {string} toolName
|
|
345
|
+
* @param {any} args
|
|
346
|
+
* @param {any} result - The handler's reply, or null when it threw
|
|
347
|
+
* @param {Error|null} [error] - What the handler threw
|
|
348
|
+
*/
|
|
349
|
+
function auditOutcome(serverConfig, toolName, args, result, error = null) {
|
|
350
|
+
if (toolContext.getStore()?.audit?.written) return;
|
|
351
|
+
try {
|
|
352
|
+
const failed = !!error || result?.isError === true;
|
|
353
|
+
const text = error ? error.message
|
|
354
|
+
: failed ? String(result?.content?.find?.(item => item?.type === 'text')?.text ?? '') : '';
|
|
355
|
+
auditLog(serverConfig, toolName, args, { allowed: true }, failed ? { success: false, error: text } : { success: true });
|
|
356
|
+
} catch (auditError) {
|
|
357
|
+
logger.warn('Could not write the audit outcome', { tool: toolName, error: auditError.message });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
329
361
|
// Execute command with timeout - using child_process timeout for real kill
|
|
330
362
|
/**
|
|
331
363
|
* @param {any} ssh
|
|
@@ -522,6 +554,13 @@ function connectionMatchesConfig(name, servers, seen = new Set()) {
|
|
|
522
554
|
return !jump || connectionMatchesConfig(jump, servers, seen);
|
|
523
555
|
}
|
|
524
556
|
|
|
557
|
+
// Connections being established, by server. Six simultaneous calls on a cold
|
|
558
|
+
// engine used to open six SSH connections: every one was kept open, five of them
|
|
559
|
+
// unreachable once the last was stored, and the burst of logins is what a
|
|
560
|
+
// server's MaxStartups or fail2ban refuses.
|
|
561
|
+
/** @type {Map<string, Promise<any>>} */
|
|
562
|
+
const pendingConnections = new Map();
|
|
563
|
+
|
|
525
564
|
// Get or create SSH connection with reconnection support
|
|
526
565
|
async function getConnection(serverName) {
|
|
527
566
|
const servers = await loadServerConfig();
|
|
@@ -569,7 +608,27 @@ async function getConnection(serverName) {
|
|
|
569
608
|
}
|
|
570
609
|
}
|
|
571
610
|
|
|
572
|
-
//
|
|
611
|
+
// Callers that arrive while a connection to this server is being established
|
|
612
|
+
// wait for it instead of opening their own.
|
|
613
|
+
const inProgress = pendingConnections.get(normalizedName);
|
|
614
|
+
if (inProgress) return inProgress;
|
|
615
|
+
const establishing = openConnection(serverName, normalizedName, serverConfig, servers);
|
|
616
|
+
pendingConnections.set(normalizedName, establishing);
|
|
617
|
+
try {
|
|
618
|
+
return await establishing;
|
|
619
|
+
} finally {
|
|
620
|
+
pendingConnections.delete(normalizedName);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Open, register and return a new connection to a configured server.
|
|
626
|
+
* @param {string} serverName - The name the caller used (for logs and hooks)
|
|
627
|
+
* @param {string} normalizedName - The configured server it resolves to
|
|
628
|
+
* @param {any} serverConfig
|
|
629
|
+
* @param {Record<string, any>} servers
|
|
630
|
+
*/
|
|
631
|
+
async function openConnection(serverName, normalizedName, serverConfig, servers) {
|
|
573
632
|
const ssh = new SSHManager(serverConfig);
|
|
574
633
|
|
|
575
634
|
try {
|
|
@@ -641,23 +700,35 @@ function registerToolConditional(toolName, schema, handler) {
|
|
|
641
700
|
// configuration (such as server listing) still load it explicitly.
|
|
642
701
|
if (isApprovalExempt(toolName, args)) return handler(args, extra);
|
|
643
702
|
const servers = await serverConfigManager.getServers();
|
|
644
|
-
return toolContext.run({ servers }, async () => {
|
|
703
|
+
return toolContext.run({ servers, audit: { written: false } }, async () => {
|
|
645
704
|
if (HANDLER_GATED_TOOLS.has(toolName)) {
|
|
646
705
|
return handler(args, extra);
|
|
647
706
|
}
|
|
648
707
|
// Fail closed for newly added remote tools with an unresolved target.
|
|
649
708
|
// Local tools must be explicitly exempted above.
|
|
709
|
+
let config;
|
|
710
|
+
let canonicalArgs;
|
|
650
711
|
try {
|
|
651
|
-
|
|
652
|
-
|
|
712
|
+
config = await getServerConfig(args.server);
|
|
713
|
+
canonicalArgs = { ...args, server: config.name };
|
|
653
714
|
const command = toolName === 'ssh_execute'
|
|
654
715
|
? expandCommandAlias(args.command) : args.command;
|
|
655
716
|
const denied = await applyServerPolicy(config.name, toolName, canonicalArgs, command);
|
|
656
717
|
if (denied) return denied;
|
|
657
|
-
return handler(canonicalArgs, extra);
|
|
658
718
|
} catch (error) {
|
|
659
719
|
return { isError: true, content: [{ type: 'text', text: error.message }] };
|
|
660
720
|
}
|
|
721
|
+
// The handler runs outside the try above: what it throws still reaches
|
|
722
|
+
// the SDK as before; it is only recorded on the way.
|
|
723
|
+
let result;
|
|
724
|
+
try {
|
|
725
|
+
result = await handler(canonicalArgs, extra);
|
|
726
|
+
} catch (error) {
|
|
727
|
+
auditOutcome(config, toolName, canonicalArgs, null, error);
|
|
728
|
+
throw error;
|
|
729
|
+
}
|
|
730
|
+
auditOutcome(config, toolName, canonicalArgs, result);
|
|
731
|
+
return result;
|
|
661
732
|
});
|
|
662
733
|
};
|
|
663
734
|
// The runtime zod schema validates args before this shared guard runs.
|
|
@@ -672,15 +743,16 @@ function registerToolConditional(toolName, schema, handler) {
|
|
|
672
743
|
registerToolConditional(
|
|
673
744
|
'ssh_execute',
|
|
674
745
|
{
|
|
675
|
-
description: 'Runs a shell command over SSH on a named configured server and returns stdout, stderr, and exit code. Mutates remote state depending on the command; not read-only. Expands command aliases before running. Uses the cwd parameter or, if omitted, the server configured default directory; adapts syntax for Linux versus Windows PowerShell targets. Timeout defaults to 120000 ms and is capped at 300000 ms. Under readonly mode destructive commands like rm or dd are refused; under restricted mode the command must match allow patterns. Output is truncated when very large.',
|
|
746
|
+
description: 'Runs a shell command over SSH on a named configured server and returns stdout, stderr, and exit code. Mutates remote state depending on the command; not read-only. Expands command aliases before running. Uses the cwd parameter or, if omitted, the server configured default directory; adapts syntax for Linux versus Windows PowerShell targets. Timeout defaults to 120000 ms and is capped at 300000 ms. Under readonly mode destructive commands like rm or dd are refused; under restricted mode the command must match allow patterns. Output is truncated when very large. For a job that outlives the timeout (a training run, a long build) set background to true instead of using nohup yourself: it starts the command detached and returns at once with its pid and the log file it writes to; read that file with ssh_tail (follow: false) and check or stop the job with ssh_process_manager.',
|
|
676
747
|
inputSchema: {
|
|
677
748
|
server: z.string().describe('Server name from configuration'),
|
|
678
749
|
command: z.string().describe('Command to execute'),
|
|
679
750
|
cwd: z.string().optional().describe('Working directory (optional, uses default if configured)'),
|
|
680
|
-
timeout: z.number().optional().describe('Command timeout in milliseconds (default: 120000, max: 300000)')
|
|
751
|
+
timeout: z.number().optional().describe('Command timeout in milliseconds (default: 120000, max: 300000)'),
|
|
752
|
+
background: z.boolean().optional().describe('Start the command detached and return at once with { pid, logFile } instead of waiting for it; the timeout does not apply. For long jobs; not available on Windows targets.')
|
|
681
753
|
}
|
|
682
754
|
},
|
|
683
|
-
async ({ server: serverName, command, cwd, timeout = TIMEOUTS.DEFAULT_COMMAND_TIMEOUT }) => {
|
|
755
|
+
async ({ server: serverName, command, cwd, timeout = TIMEOUTS.DEFAULT_COMMAND_TIMEOUT, background = false }) => {
|
|
684
756
|
// Cap timeout at maximum allowed
|
|
685
757
|
const cappedTimeout = Math.min(timeout, TIMEOUTS.MAX_COMMAND_TIMEOUT);
|
|
686
758
|
|
|
@@ -720,6 +792,38 @@ registerToolConditional(
|
|
|
720
792
|
fullCommand = expandedCommand;
|
|
721
793
|
}
|
|
722
794
|
|
|
795
|
+
if (background) {
|
|
796
|
+
if (platform === 'windows') {
|
|
797
|
+
throw new Error('background is not supported on Windows targets: start the job with Start-Process in the command');
|
|
798
|
+
}
|
|
799
|
+
const started = await execCommandWithTimeout(ssh, buildBackgroundCommand(fullCommand), { platform }, 15000);
|
|
800
|
+
const job = parseBackgroundStart(started.stdout);
|
|
801
|
+
await auditOk(serverName, 'ssh_execute', { command, cwd, background: true }, {
|
|
802
|
+
code: started.code,
|
|
803
|
+
success: !!job,
|
|
804
|
+
});
|
|
805
|
+
return {
|
|
806
|
+
...(job ? {} : { isError: true }),
|
|
807
|
+
content: [
|
|
808
|
+
{
|
|
809
|
+
type: 'text',
|
|
810
|
+
text: formatJSONResponse({
|
|
811
|
+
server: serverName,
|
|
812
|
+
command: fullCommand,
|
|
813
|
+
background: true,
|
|
814
|
+
success: !!job,
|
|
815
|
+
pid: job?.pid,
|
|
816
|
+
logFile: job?.logFile,
|
|
817
|
+
error: job ? undefined : truncateOutput(started.stderr || 'The job did not start', 1000),
|
|
818
|
+
note: job
|
|
819
|
+
? 'The job runs detached from this SSH session. Read its output with ssh_tail (file: logFile, follow: false); check on it or stop it with ssh_process_manager (pid).'
|
|
820
|
+
: undefined,
|
|
821
|
+
}),
|
|
822
|
+
},
|
|
823
|
+
],
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
|
|
723
827
|
// Log command execution
|
|
724
828
|
const startTime = logger.logCommand(serverName, fullCommand, workingDir);
|
|
725
829
|
|
|
@@ -850,6 +954,7 @@ registerToolConditional(
|
|
|
850
954
|
error: error.message
|
|
851
955
|
});
|
|
852
956
|
return {
|
|
957
|
+
isError: true,
|
|
853
958
|
content: [
|
|
854
959
|
{
|
|
855
960
|
type: 'text',
|
|
@@ -907,6 +1012,7 @@ registerToolConditional(
|
|
|
907
1012
|
error: error.message
|
|
908
1013
|
});
|
|
909
1014
|
return {
|
|
1015
|
+
isError: true,
|
|
910
1016
|
content: [
|
|
911
1017
|
{
|
|
912
1018
|
type: 'text',
|
|
@@ -950,6 +1056,7 @@ registerToolConditional(
|
|
|
950
1056
|
execSync('which sshpass', { stdio: 'ignore' });
|
|
951
1057
|
} catch (error) {
|
|
952
1058
|
return {
|
|
1059
|
+
isError: true,
|
|
953
1060
|
content: [
|
|
954
1061
|
{
|
|
955
1062
|
type: 'text',
|
|
@@ -1248,6 +1355,7 @@ registerToolConditional(
|
|
|
1248
1355
|
});
|
|
1249
1356
|
} catch (error) {
|
|
1250
1357
|
return {
|
|
1358
|
+
isError: true,
|
|
1251
1359
|
content: [
|
|
1252
1360
|
{
|
|
1253
1361
|
type: 'text',
|
|
@@ -1356,6 +1464,7 @@ registerToolConditional(
|
|
|
1356
1464
|
});
|
|
1357
1465
|
|
|
1358
1466
|
return {
|
|
1467
|
+
isError: true,
|
|
1359
1468
|
content: [
|
|
1360
1469
|
{
|
|
1361
1470
|
type: 'text',
|
|
@@ -1529,6 +1638,7 @@ registerToolConditional(
|
|
|
1529
1638
|
});
|
|
1530
1639
|
|
|
1531
1640
|
return {
|
|
1641
|
+
isError: true,
|
|
1532
1642
|
content: [
|
|
1533
1643
|
{
|
|
1534
1644
|
type: 'text',
|
|
@@ -1631,6 +1741,7 @@ registerToolConditional(
|
|
|
1631
1741
|
};
|
|
1632
1742
|
} catch (error) {
|
|
1633
1743
|
return {
|
|
1744
|
+
isError: true,
|
|
1634
1745
|
content: [
|
|
1635
1746
|
{
|
|
1636
1747
|
type: 'text',
|
|
@@ -1685,6 +1796,7 @@ registerToolConditional(
|
|
|
1685
1796
|
});
|
|
1686
1797
|
|
|
1687
1798
|
return {
|
|
1799
|
+
isError: true,
|
|
1688
1800
|
content: [
|
|
1689
1801
|
{
|
|
1690
1802
|
type: 'text',
|
|
@@ -1778,6 +1890,7 @@ registerToolConditional(
|
|
|
1778
1890
|
});
|
|
1779
1891
|
|
|
1780
1892
|
return {
|
|
1893
|
+
isError: true,
|
|
1781
1894
|
content: [
|
|
1782
1895
|
{
|
|
1783
1896
|
type: 'text',
|
|
@@ -1856,6 +1969,7 @@ registerToolConditional(
|
|
|
1856
1969
|
};
|
|
1857
1970
|
} catch (error) {
|
|
1858
1971
|
return {
|
|
1972
|
+
isError: true,
|
|
1859
1973
|
content: [
|
|
1860
1974
|
{
|
|
1861
1975
|
type: 'text',
|
|
@@ -1920,6 +2034,7 @@ registerToolConditional(
|
|
|
1920
2034
|
});
|
|
1921
2035
|
|
|
1922
2036
|
return {
|
|
2037
|
+
isError: true,
|
|
1923
2038
|
content: [
|
|
1924
2039
|
{
|
|
1925
2040
|
type: 'text',
|
|
@@ -2070,6 +2185,7 @@ registerToolConditional(
|
|
|
2070
2185
|
});
|
|
2071
2186
|
|
|
2072
2187
|
return {
|
|
2188
|
+
isError: true,
|
|
2073
2189
|
content: [
|
|
2074
2190
|
{
|
|
2075
2191
|
type: 'text',
|
|
@@ -2230,6 +2346,7 @@ registerToolConditional(
|
|
|
2230
2346
|
});
|
|
2231
2347
|
|
|
2232
2348
|
return {
|
|
2349
|
+
isError: true,
|
|
2233
2350
|
content: [
|
|
2234
2351
|
{
|
|
2235
2352
|
type: 'text',
|
|
@@ -2372,6 +2489,7 @@ registerToolConditional(
|
|
|
2372
2489
|
};
|
|
2373
2490
|
} catch (error) {
|
|
2374
2491
|
return {
|
|
2492
|
+
isError: true,
|
|
2375
2493
|
content: [
|
|
2376
2494
|
{
|
|
2377
2495
|
type: 'text',
|
|
@@ -2476,6 +2594,7 @@ registerToolConditional(
|
|
|
2476
2594
|
error: error.message,
|
|
2477
2595
|
});
|
|
2478
2596
|
return {
|
|
2597
|
+
isError: true,
|
|
2479
2598
|
content: [
|
|
2480
2599
|
{
|
|
2481
2600
|
type: 'text',
|
|
@@ -2577,6 +2696,7 @@ registerToolConditional(
|
|
|
2577
2696
|
}
|
|
2578
2697
|
} catch (error) {
|
|
2579
2698
|
return {
|
|
2699
|
+
isError: true,
|
|
2580
2700
|
content: [
|
|
2581
2701
|
{
|
|
2582
2702
|
type: 'text',
|
|
@@ -2669,6 +2789,7 @@ registerToolConditional(
|
|
|
2669
2789
|
}
|
|
2670
2790
|
} catch (error) {
|
|
2671
2791
|
return {
|
|
2792
|
+
isError: true,
|
|
2672
2793
|
content: [
|
|
2673
2794
|
{
|
|
2674
2795
|
type: 'text',
|
|
@@ -2749,6 +2870,7 @@ registerToolConditional(
|
|
|
2749
2870
|
}
|
|
2750
2871
|
} catch (error) {
|
|
2751
2872
|
return {
|
|
2873
|
+
isError: true,
|
|
2752
2874
|
content: [
|
|
2753
2875
|
{
|
|
2754
2876
|
type: 'text',
|
|
@@ -2867,6 +2989,7 @@ registerToolConditional(
|
|
|
2867
2989
|
}
|
|
2868
2990
|
} catch (error) {
|
|
2869
2991
|
return {
|
|
2992
|
+
isError: true,
|
|
2870
2993
|
content: [
|
|
2871
2994
|
{
|
|
2872
2995
|
type: 'text',
|
|
@@ -2955,6 +3078,7 @@ registerToolConditional(
|
|
|
2955
3078
|
} catch (error) {
|
|
2956
3079
|
logger.error('Failed to create tunnel', { error: error.message });
|
|
2957
3080
|
return {
|
|
3081
|
+
isError: true,
|
|
2958
3082
|
content: [
|
|
2959
3083
|
{
|
|
2960
3084
|
type: 'text',
|
|
@@ -3036,6 +3160,7 @@ registerToolConditional(
|
|
|
3036
3160
|
} catch (error) {
|
|
3037
3161
|
logger.error('Failed to list tunnels', { error: error.message });
|
|
3038
3162
|
return {
|
|
3163
|
+
isError: true,
|
|
3039
3164
|
content: [
|
|
3040
3165
|
{
|
|
3041
3166
|
type: 'text',
|
|
@@ -3102,6 +3227,7 @@ registerToolConditional(
|
|
|
3102
3227
|
} catch (error) {
|
|
3103
3228
|
logger.error('Failed to close tunnel', { error: error.message });
|
|
3104
3229
|
return {
|
|
3230
|
+
isError: true,
|
|
3105
3231
|
content: [
|
|
3106
3232
|
{
|
|
3107
3233
|
type: 'text',
|
|
@@ -3350,6 +3476,7 @@ registerToolConditional(
|
|
|
3350
3476
|
logger.error('SSH key management failed', { action, server, error: error.message });
|
|
3351
3477
|
|
|
3352
3478
|
return {
|
|
3479
|
+
isError: true,
|
|
3353
3480
|
content: [
|
|
3354
3481
|
{
|
|
3355
3482
|
type: 'text',
|
|
@@ -3437,6 +3564,7 @@ registerToolConditional(
|
|
|
3437
3564
|
}
|
|
3438
3565
|
} catch (error) {
|
|
3439
3566
|
return {
|
|
3567
|
+
isError: true,
|
|
3440
3568
|
content: [
|
|
3441
3569
|
{
|
|
3442
3570
|
type: 'text',
|
|
@@ -3498,7 +3626,7 @@ registerToolConditional(
|
|
|
3498
3626
|
|
|
3499
3627
|
const backupDirectory = backupDir || DEFAULT_BACKUP_DIR;
|
|
3500
3628
|
const backupId = generateBackupId(type, name);
|
|
3501
|
-
const backupFile = getBackupFilePath(backupId, backupDirectory);
|
|
3629
|
+
const backupFile = getBackupFilePath(backupId, backupDirectory, backupExtension(type, compress));
|
|
3502
3630
|
const metadataPath = getBackupMetadataPath(backupId, backupDirectory);
|
|
3503
3631
|
|
|
3504
3632
|
// Ensure backup directory exists with proper error handling
|
|
@@ -3552,7 +3680,9 @@ registerToolConditional(
|
|
|
3552
3680
|
if (!database) {
|
|
3553
3681
|
throw new Error('database parameter required for MongoDB backup');
|
|
3554
3682
|
}
|
|
3555
|
-
|
|
3683
|
+
// The dump directory is the backup file without its extension (a path can
|
|
3684
|
+
// contain ".gz" elsewhere: only the end is removed).
|
|
3685
|
+
const mongoOutputDir = backupFile.replace(/\.tar\.gz$/, '');
|
|
3556
3686
|
backupCommand = buildMongoDBDumpCommand({
|
|
3557
3687
|
database,
|
|
3558
3688
|
user: dbUser,
|
|
@@ -3662,6 +3792,7 @@ registerToolConditional(
|
|
|
3662
3792
|
});
|
|
3663
3793
|
|
|
3664
3794
|
return {
|
|
3795
|
+
isError: true,
|
|
3665
3796
|
content: [
|
|
3666
3797
|
{
|
|
3667
3798
|
type: 'text',
|
|
@@ -3734,6 +3865,7 @@ registerToolConditional(
|
|
|
3734
3865
|
});
|
|
3735
3866
|
|
|
3736
3867
|
return {
|
|
3868
|
+
isError: true,
|
|
3737
3869
|
content: [
|
|
3738
3870
|
{
|
|
3739
3871
|
type: 'text',
|
|
@@ -3784,7 +3916,13 @@ registerToolConditional(
|
|
|
3784
3916
|
}
|
|
3785
3917
|
|
|
3786
3918
|
const metadata = JSON.parse(metadataResult.stdout);
|
|
3787
|
-
const backupFile = getBackupFilePath(backupId, backupDirectory);
|
|
3919
|
+
const backupFile = getBackupFilePath(backupId, backupDirectory, backupExtension(metadata.type, metadata.compressed !== false));
|
|
3920
|
+
|
|
3921
|
+
// Said before anything is restored: a backup that is not there is not a restore failure.
|
|
3922
|
+
const present = await ssh.execCommand(`test -e ${shellQuote(backupFile)}`);
|
|
3923
|
+
if (present.code !== 0) {
|
|
3924
|
+
throw new Error(`Backup file not found: ${backupFile}`);
|
|
3925
|
+
}
|
|
3788
3926
|
|
|
3789
3927
|
// Execute pre-restore hook
|
|
3790
3928
|
await executeHook('pre-restore', {
|
|
@@ -3858,6 +3996,7 @@ registerToolConditional(
|
|
|
3858
3996
|
});
|
|
3859
3997
|
|
|
3860
3998
|
return {
|
|
3999
|
+
isError: true,
|
|
3861
4000
|
content: [
|
|
3862
4001
|
{
|
|
3863
4002
|
type: 'text',
|
|
@@ -3913,28 +4052,36 @@ registerToolConditional(
|
|
|
3913
4052
|
scriptContent += `# Created: ${new Date().toISOString()}\n\n`;
|
|
3914
4053
|
|
|
3915
4054
|
const backupId = `\${BACKUP_TYPE}_${name}_$(date +%Y%m%d_%H%M%S)_\${RANDOM}`;
|
|
3916
|
-
const backupFile = `${backupDirectory}/${backupId}
|
|
4055
|
+
const backupFile = `${backupDirectory}/${backupId}${backupExtension(type)}`;
|
|
3917
4056
|
|
|
3918
4057
|
scriptContent += `BACKUP_DIR="${backupDirectory}"\n`;
|
|
3919
4058
|
scriptContent += `BACKUP_TYPE="${type}"\n`;
|
|
3920
4059
|
scriptContent += `BACKUP_ID="${backupId}"\n`;
|
|
3921
4060
|
scriptContent += `BACKUP_FILE="${backupFile}"\n\n`;
|
|
3922
|
-
|
|
4061
|
+
// A pipeline is as failed as its first failing command, and a failed backup
|
|
4062
|
+
// leaves no file, says so, and stops before the cleanup below: otherwise a
|
|
4063
|
+
// dump that fails every night leaves an empty archive each time while the
|
|
4064
|
+
// cleanup deletes the good ones, until none is left.
|
|
4065
|
+
scriptContent += 'set -o pipefail\n';
|
|
4066
|
+
scriptContent += 'DUMP_DIR=""\n';
|
|
4067
|
+
scriptContent += 'fail() { echo "ssh-manager backup failed: $BACKUP_ID" >&2; rm -rf "$BACKUP_FILE"; [ -n "$DUMP_DIR" ] && rm -rf "$DUMP_DIR"; exit 1; }\n\n';
|
|
4068
|
+
scriptContent += 'mkdir -p "$BACKUP_DIR" || fail\n\n';
|
|
3923
4069
|
|
|
3924
4070
|
// Add backup command based on type. The script runs later from cron, so
|
|
3925
4071
|
// a value spliced in raw here would run every night, not once.
|
|
3926
4072
|
switch (type) {
|
|
3927
4073
|
case BACKUP_TYPES.MYSQL:
|
|
3928
|
-
scriptContent += `mysqldump --single-transaction --routines --triggers ${shellQuote(database)} | gzip > "$BACKUP_FILE"\n`;
|
|
4074
|
+
scriptContent += `mysqldump --single-transaction --routines --triggers ${shellQuote(database)} | gzip > "$BACKUP_FILE" || fail\n`;
|
|
3929
4075
|
break;
|
|
3930
4076
|
case BACKUP_TYPES.POSTGRESQL:
|
|
3931
|
-
scriptContent += `pg_dump --format=custom --clean --if-exists ${shellQuote(database)} | gzip > "$BACKUP_FILE"\n`;
|
|
4077
|
+
scriptContent += `pg_dump --format=custom --clean --if-exists ${shellQuote(database)} | gzip > "$BACKUP_FILE" || fail\n`;
|
|
3932
4078
|
break;
|
|
3933
4079
|
case BACKUP_TYPES.MONGODB:
|
|
3934
|
-
scriptContent +=
|
|
4080
|
+
scriptContent += 'DUMP_DIR="/tmp/mongo_${RANDOM}"\n';
|
|
4081
|
+
scriptContent += `mongodump --db ${shellQuote(database)} --out "$DUMP_DIR" && tar -czf "$BACKUP_FILE" -C /tmp "$(basename "$DUMP_DIR")" && rm -rf "$DUMP_DIR" || fail\n`;
|
|
3935
4082
|
break;
|
|
3936
4083
|
case BACKUP_TYPES.FILES:
|
|
3937
|
-
scriptContent += `tar -czf "$BACKUP_FILE" ${(paths || []).map(shellQuote).join(' ')}\n`;
|
|
4084
|
+
scriptContent += `tar -czf "$BACKUP_FILE" ${(paths || []).map(shellQuote).join(' ')} || fail\n`;
|
|
3938
4085
|
break;
|
|
3939
4086
|
}
|
|
3940
4087
|
|
|
@@ -3990,6 +4137,7 @@ registerToolConditional(
|
|
|
3990
4137
|
});
|
|
3991
4138
|
|
|
3992
4139
|
return {
|
|
4140
|
+
isError: true,
|
|
3993
4141
|
content: [
|
|
3994
4142
|
{
|
|
3995
4143
|
type: 'text',
|
|
@@ -4089,6 +4237,7 @@ registerToolConditional(
|
|
|
4089
4237
|
});
|
|
4090
4238
|
|
|
4091
4239
|
return {
|
|
4240
|
+
isError: true,
|
|
4092
4241
|
content: [
|
|
4093
4242
|
{
|
|
4094
4243
|
type: 'text',
|
|
@@ -4171,6 +4320,7 @@ registerToolConditional(
|
|
|
4171
4320
|
});
|
|
4172
4321
|
|
|
4173
4322
|
return {
|
|
4323
|
+
isError: true,
|
|
4174
4324
|
content: [
|
|
4175
4325
|
{
|
|
4176
4326
|
type: 'text',
|
|
@@ -4320,6 +4470,7 @@ registerToolConditional(
|
|
|
4320
4470
|
});
|
|
4321
4471
|
|
|
4322
4472
|
return {
|
|
4473
|
+
isError: true,
|
|
4323
4474
|
content: [
|
|
4324
4475
|
{
|
|
4325
4476
|
type: 'text',
|
|
@@ -4500,6 +4651,7 @@ registerToolConditional(
|
|
|
4500
4651
|
});
|
|
4501
4652
|
|
|
4502
4653
|
return {
|
|
4654
|
+
isError: true,
|
|
4503
4655
|
content: [
|
|
4504
4656
|
{
|
|
4505
4657
|
type: 'text',
|
|
@@ -4578,8 +4730,14 @@ registerToolConditional(
|
|
|
4578
4730
|
throw new Error(`Dump failed: ${result.stderr || result.stdout}`);
|
|
4579
4731
|
}
|
|
4580
4732
|
|
|
4733
|
+
// What the dump really wrote: a MongoDB dump is a directory, or an archive
|
|
4734
|
+
// named after it, not necessarily the path the caller gave.
|
|
4735
|
+
const dumpedFile = type === DB_TYPES.MONGODB
|
|
4736
|
+
? (compress ? mongoArchivePath(options.outputDir) : options.outputDir)
|
|
4737
|
+
: outputFile;
|
|
4738
|
+
|
|
4581
4739
|
// Get file size
|
|
4582
|
-
const sizeCommand = `stat -f%z ${shellQuote(
|
|
4740
|
+
const sizeCommand = `stat -f%z ${shellQuote(dumpedFile)} 2>/dev/null || stat -c%s ${shellQuote(dumpedFile)} 2>/dev/null`;
|
|
4583
4741
|
const sizeResult = await ssh.execCommand(sizeCommand);
|
|
4584
4742
|
const size = parseSize(sizeResult.stdout);
|
|
4585
4743
|
|
|
@@ -4598,7 +4756,7 @@ registerToolConditional(
|
|
|
4598
4756
|
server: serverName,
|
|
4599
4757
|
type,
|
|
4600
4758
|
database,
|
|
4601
|
-
output_file:
|
|
4759
|
+
output_file: dumpedFile,
|
|
4602
4760
|
size_bytes: size,
|
|
4603
4761
|
size_human: formatBytes(size),
|
|
4604
4762
|
compressed: compress,
|
|
@@ -4617,6 +4775,7 @@ registerToolConditional(
|
|
|
4617
4775
|
});
|
|
4618
4776
|
|
|
4619
4777
|
return {
|
|
4778
|
+
isError: true,
|
|
4620
4779
|
content: [
|
|
4621
4780
|
{
|
|
4622
4781
|
type: 'text',
|
|
@@ -4720,6 +4879,7 @@ registerToolConditional(
|
|
|
4720
4879
|
});
|
|
4721
4880
|
|
|
4722
4881
|
return {
|
|
4882
|
+
isError: true,
|
|
4723
4883
|
content: [
|
|
4724
4884
|
{
|
|
4725
4885
|
type: 'text',
|
|
@@ -4845,6 +5005,7 @@ registerToolConditional(
|
|
|
4845
5005
|
});
|
|
4846
5006
|
|
|
4847
5007
|
return {
|
|
5008
|
+
isError: true,
|
|
4848
5009
|
content: [
|
|
4849
5010
|
{
|
|
4850
5011
|
type: 'text',
|
|
@@ -4969,6 +5130,7 @@ registerToolConditional(
|
|
|
4969
5130
|
});
|
|
4970
5131
|
|
|
4971
5132
|
return {
|
|
5133
|
+
isError: true,
|
|
4972
5134
|
content: [
|
|
4973
5135
|
{
|
|
4974
5136
|
type: 'text',
|
package/src/logger.js
CHANGED
|
@@ -41,7 +41,9 @@ const ICONS = {
|
|
|
41
41
|
// Field names whose values must never be written to a log, matched
|
|
42
42
|
// case-insensitively against the whole key (so `sudoPassword`, `SSH_PASSWORD`
|
|
43
43
|
// and `passphrase` are all caught).
|
|
44
|
-
|
|
44
|
+
// Exported so the audit log and the approval request redact with the same rule:
|
|
45
|
+
// a list of its own went stale (dbPassword reached the audit log in clear text).
|
|
46
|
+
export const SECRET_KEY_PATTERN = /(pass(word|phrase)?|secret|token|credential|privatekey|private_key|apikey|api_key|auth)/i;
|
|
45
47
|
const REDACTED = '[redacted]';
|
|
46
48
|
|
|
47
49
|
/**
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Opt-in silence on stderr: SSH_LOG_STDERR=false (also 0, off, no).
|
|
2
|
+
//
|
|
3
|
+
// The engine writes a startup banner, its logger and a few hook and profile
|
|
4
|
+
// messages to stderr. An MCP host captures that, and most hosts keep it out of
|
|
5
|
+
// sight. A client that draws a terminal interface on the same stderr (OpenCode
|
|
6
|
+
// and Kilo were the reports) gets its screen overwritten by those lines.
|
|
7
|
+
// The log file (SSH_LOG_FILE) keeps receiving the logger's lines either way.
|
|
8
|
+
//
|
|
9
|
+
// Idea from fyq163's fork, which silenced stderr for everybody; here nothing
|
|
10
|
+
// changes unless the variable is set to a false value. Imported first by
|
|
11
|
+
// index.js so it is in place before any other module can print.
|
|
12
|
+
const value = process.env.SSH_LOG_STDERR;
|
|
13
|
+
if (value !== undefined && /^(0|false|off|no)$/i.test(value.trim())) {
|
|
14
|
+
console.error = () => {};
|
|
15
|
+
}
|