mcp-ssh-manager 4.0.3 → 4.2.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 +199 -0
- package/CLAUDE.md +17 -5
- package/Formula/ssh-manager.rb +2 -2
- package/INSTALLATION.md +4 -0
- package/README.md +41 -5
- package/brand/README.md +1 -0
- 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 +3 -0
- package/desktop/electron/locale.mjs +34 -0
- package/desktop/electron/main.js +52 -65
- package/desktop/electron/native-menu.mjs +44 -0
- package/desktop/electron/native.zh-CN.json +58 -0
- package/desktop/electron/package-lock.json +15 -40
- package/desktop/electron/package.json +4 -1
- package/desktop/electron/smoke-test.mjs +11 -1
- package/dist/ui/app.css +1 -1
- package/dist/ui/app.js +123 -115
- package/docs/MIGRATION.md +1 -1
- package/docs/SECURITY_MODES.md +15 -8
- package/docs/SEO.md +1 -1
- package/llms-install.md +97 -0
- package/package.json +17 -3
- package/scripts/check-action-pins.mjs +57 -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 +10 -1
- package/src/database-manager.js +13 -31
- package/src/dump-guard.js +79 -0
- package/src/filtered-agent.js +53 -0
- package/src/health-monitor.js +8 -4
- package/src/index.js +216 -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 +71 -2
- 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',
|
|
@@ -887,6 +992,8 @@ registerToolConditional(
|
|
|
887
992
|
duration: `${Date.now() - startTime}ms`
|
|
888
993
|
});
|
|
889
994
|
|
|
995
|
+
await auditOk(serverName, 'ssh_download', { remotePath, localPath }, { success: true });
|
|
996
|
+
|
|
890
997
|
return {
|
|
891
998
|
content: [
|
|
892
999
|
{
|
|
@@ -896,11 +1003,16 @@ registerToolConditional(
|
|
|
896
1003
|
],
|
|
897
1004
|
};
|
|
898
1005
|
} catch (error) {
|
|
1006
|
+
await auditOk(serverName, 'ssh_download', { remotePath, localPath }, {
|
|
1007
|
+
success: false,
|
|
1008
|
+
error: error.message,
|
|
1009
|
+
});
|
|
899
1010
|
logger.logTransfer('download', serverName, remotePath, localPath, {
|
|
900
1011
|
success: false,
|
|
901
1012
|
error: error.message
|
|
902
1013
|
});
|
|
903
1014
|
return {
|
|
1015
|
+
isError: true,
|
|
904
1016
|
content: [
|
|
905
1017
|
{
|
|
906
1018
|
type: 'text',
|
|
@@ -944,6 +1056,7 @@ registerToolConditional(
|
|
|
944
1056
|
execSync('which sshpass', { stdio: 'ignore' });
|
|
945
1057
|
} catch (error) {
|
|
946
1058
|
return {
|
|
1059
|
+
isError: true,
|
|
947
1060
|
content: [
|
|
948
1061
|
{
|
|
949
1062
|
type: 'text',
|
|
@@ -1242,6 +1355,7 @@ registerToolConditional(
|
|
|
1242
1355
|
});
|
|
1243
1356
|
} catch (error) {
|
|
1244
1357
|
return {
|
|
1358
|
+
isError: true,
|
|
1245
1359
|
content: [
|
|
1246
1360
|
{
|
|
1247
1361
|
type: 'text',
|
|
@@ -1350,6 +1464,7 @@ registerToolConditional(
|
|
|
1350
1464
|
});
|
|
1351
1465
|
|
|
1352
1466
|
return {
|
|
1467
|
+
isError: true,
|
|
1353
1468
|
content: [
|
|
1354
1469
|
{
|
|
1355
1470
|
type: 'text',
|
|
@@ -1523,6 +1638,7 @@ registerToolConditional(
|
|
|
1523
1638
|
});
|
|
1524
1639
|
|
|
1525
1640
|
return {
|
|
1641
|
+
isError: true,
|
|
1526
1642
|
content: [
|
|
1527
1643
|
{
|
|
1528
1644
|
type: 'text',
|
|
@@ -1625,6 +1741,7 @@ registerToolConditional(
|
|
|
1625
1741
|
};
|
|
1626
1742
|
} catch (error) {
|
|
1627
1743
|
return {
|
|
1744
|
+
isError: true,
|
|
1628
1745
|
content: [
|
|
1629
1746
|
{
|
|
1630
1747
|
type: 'text',
|
|
@@ -1679,6 +1796,7 @@ registerToolConditional(
|
|
|
1679
1796
|
});
|
|
1680
1797
|
|
|
1681
1798
|
return {
|
|
1799
|
+
isError: true,
|
|
1682
1800
|
content: [
|
|
1683
1801
|
{
|
|
1684
1802
|
type: 'text',
|
|
@@ -1701,6 +1819,9 @@ registerToolConditional(
|
|
|
1701
1819
|
}
|
|
1702
1820
|
},
|
|
1703
1821
|
async ({ session: sessionId, command, timeout = 30000 }) => {
|
|
1822
|
+
// Set once the policy allowed the command, so a failure after that point is
|
|
1823
|
+
// audited as well.
|
|
1824
|
+
let auditServer = null;
|
|
1704
1825
|
try {
|
|
1705
1826
|
const session = getSession(sessionId);
|
|
1706
1827
|
const currentConfig = await getServerConfig(session.serverName);
|
|
@@ -1712,11 +1833,16 @@ registerToolConditional(
|
|
|
1712
1833
|
// Resolve the session's underlying server to its policy.
|
|
1713
1834
|
const denied = await applyServerPolicy(session.serverName, 'ssh_session_send', { session: sessionId, command }, command);
|
|
1714
1835
|
if (denied) return denied;
|
|
1836
|
+
auditServer = session.serverName;
|
|
1715
1837
|
|
|
1716
1838
|
const startTime = Date.now();
|
|
1717
1839
|
const result = await session.execute(command, { timeout });
|
|
1718
1840
|
const duration = Date.now() - startTime;
|
|
1719
1841
|
|
|
1842
|
+
await auditOk(session.serverName, 'ssh_session_send', { session: sessionId, command }, {
|
|
1843
|
+
success: result.success,
|
|
1844
|
+
});
|
|
1845
|
+
|
|
1720
1846
|
logger.info('Session command executed', {
|
|
1721
1847
|
session: sessionId,
|
|
1722
1848
|
command: command.substring(0, 50),
|
|
@@ -1751,6 +1877,12 @@ registerToolConditional(
|
|
|
1751
1877
|
]
|
|
1752
1878
|
};
|
|
1753
1879
|
} catch (error) {
|
|
1880
|
+
if (auditServer) {
|
|
1881
|
+
await auditOk(auditServer, 'ssh_session_send', { session: sessionId, command }, {
|
|
1882
|
+
success: false,
|
|
1883
|
+
error: error.message,
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1754
1886
|
logger.error('Failed to send command to session', {
|
|
1755
1887
|
session: sessionId,
|
|
1756
1888
|
command,
|
|
@@ -1758,6 +1890,7 @@ registerToolConditional(
|
|
|
1758
1890
|
});
|
|
1759
1891
|
|
|
1760
1892
|
return {
|
|
1893
|
+
isError: true,
|
|
1761
1894
|
content: [
|
|
1762
1895
|
{
|
|
1763
1896
|
type: 'text',
|
|
@@ -1836,6 +1969,7 @@ registerToolConditional(
|
|
|
1836
1969
|
};
|
|
1837
1970
|
} catch (error) {
|
|
1838
1971
|
return {
|
|
1972
|
+
isError: true,
|
|
1839
1973
|
content: [
|
|
1840
1974
|
{
|
|
1841
1975
|
type: 'text',
|
|
@@ -1900,6 +2034,7 @@ registerToolConditional(
|
|
|
1900
2034
|
});
|
|
1901
2035
|
|
|
1902
2036
|
return {
|
|
2037
|
+
isError: true,
|
|
1903
2038
|
content: [
|
|
1904
2039
|
{
|
|
1905
2040
|
type: 'text',
|
|
@@ -1981,6 +2116,10 @@ registerToolConditional(
|
|
|
1981
2116
|
}
|
|
1982
2117
|
|
|
1983
2118
|
const execResult = await execCommandWithTimeout(ssh, fullCommand, { platform }, 30000);
|
|
2119
|
+
await auditOk(serverName, 'ssh_execute_group', { group: groupName, command, cwd }, {
|
|
2120
|
+
code: execResult.code,
|
|
2121
|
+
success: execResult.code === 0,
|
|
2122
|
+
});
|
|
1984
2123
|
|
|
1985
2124
|
return {
|
|
1986
2125
|
stdout: execResult.stdout,
|
|
@@ -2046,6 +2185,7 @@ registerToolConditional(
|
|
|
2046
2185
|
});
|
|
2047
2186
|
|
|
2048
2187
|
return {
|
|
2188
|
+
isError: true,
|
|
2049
2189
|
content: [
|
|
2050
2190
|
{
|
|
2051
2191
|
type: 'text',
|
|
@@ -2206,6 +2346,7 @@ registerToolConditional(
|
|
|
2206
2346
|
});
|
|
2207
2347
|
|
|
2208
2348
|
return {
|
|
2349
|
+
isError: true,
|
|
2209
2350
|
content: [
|
|
2210
2351
|
{
|
|
2211
2352
|
type: 'text',
|
|
@@ -2348,6 +2489,7 @@ registerToolConditional(
|
|
|
2348
2489
|
};
|
|
2349
2490
|
} catch (error) {
|
|
2350
2491
|
return {
|
|
2492
|
+
isError: true,
|
|
2351
2493
|
content: [
|
|
2352
2494
|
{
|
|
2353
2495
|
type: 'text',
|
|
@@ -2433,6 +2575,11 @@ registerToolConditional(
|
|
|
2433
2575
|
// response format is unchanged for existing callers.
|
|
2434
2576
|
const maskedCommand = fullCommand;
|
|
2435
2577
|
|
|
2578
|
+
await auditOk(server, 'ssh_execute_sudo', { command, cwd }, {
|
|
2579
|
+
code: result.code,
|
|
2580
|
+
success: result.code === 0,
|
|
2581
|
+
});
|
|
2582
|
+
|
|
2436
2583
|
return {
|
|
2437
2584
|
content: [
|
|
2438
2585
|
{
|
|
@@ -2442,7 +2589,12 @@ registerToolConditional(
|
|
|
2442
2589
|
],
|
|
2443
2590
|
};
|
|
2444
2591
|
} catch (error) {
|
|
2592
|
+
await auditOk(server, 'ssh_execute_sudo', { command, cwd }, {
|
|
2593
|
+
success: false,
|
|
2594
|
+
error: error.message,
|
|
2595
|
+
});
|
|
2445
2596
|
return {
|
|
2597
|
+
isError: true,
|
|
2446
2598
|
content: [
|
|
2447
2599
|
{
|
|
2448
2600
|
type: 'text',
|
|
@@ -2544,6 +2696,7 @@ registerToolConditional(
|
|
|
2544
2696
|
}
|
|
2545
2697
|
} catch (error) {
|
|
2546
2698
|
return {
|
|
2699
|
+
isError: true,
|
|
2547
2700
|
content: [
|
|
2548
2701
|
{
|
|
2549
2702
|
type: 'text',
|
|
@@ -2636,6 +2789,7 @@ registerToolConditional(
|
|
|
2636
2789
|
}
|
|
2637
2790
|
} catch (error) {
|
|
2638
2791
|
return {
|
|
2792
|
+
isError: true,
|
|
2639
2793
|
content: [
|
|
2640
2794
|
{
|
|
2641
2795
|
type: 'text',
|
|
@@ -2716,6 +2870,7 @@ registerToolConditional(
|
|
|
2716
2870
|
}
|
|
2717
2871
|
} catch (error) {
|
|
2718
2872
|
return {
|
|
2873
|
+
isError: true,
|
|
2719
2874
|
content: [
|
|
2720
2875
|
{
|
|
2721
2876
|
type: 'text',
|
|
@@ -2834,6 +2989,7 @@ registerToolConditional(
|
|
|
2834
2989
|
}
|
|
2835
2990
|
} catch (error) {
|
|
2836
2991
|
return {
|
|
2992
|
+
isError: true,
|
|
2837
2993
|
content: [
|
|
2838
2994
|
{
|
|
2839
2995
|
type: 'text',
|
|
@@ -2922,6 +3078,7 @@ registerToolConditional(
|
|
|
2922
3078
|
} catch (error) {
|
|
2923
3079
|
logger.error('Failed to create tunnel', { error: error.message });
|
|
2924
3080
|
return {
|
|
3081
|
+
isError: true,
|
|
2925
3082
|
content: [
|
|
2926
3083
|
{
|
|
2927
3084
|
type: 'text',
|
|
@@ -3003,6 +3160,7 @@ registerToolConditional(
|
|
|
3003
3160
|
} catch (error) {
|
|
3004
3161
|
logger.error('Failed to list tunnels', { error: error.message });
|
|
3005
3162
|
return {
|
|
3163
|
+
isError: true,
|
|
3006
3164
|
content: [
|
|
3007
3165
|
{
|
|
3008
3166
|
type: 'text',
|
|
@@ -3069,6 +3227,7 @@ registerToolConditional(
|
|
|
3069
3227
|
} catch (error) {
|
|
3070
3228
|
logger.error('Failed to close tunnel', { error: error.message });
|
|
3071
3229
|
return {
|
|
3230
|
+
isError: true,
|
|
3072
3231
|
content: [
|
|
3073
3232
|
{
|
|
3074
3233
|
type: 'text',
|
|
@@ -3317,6 +3476,7 @@ registerToolConditional(
|
|
|
3317
3476
|
logger.error('SSH key management failed', { action, server, error: error.message });
|
|
3318
3477
|
|
|
3319
3478
|
return {
|
|
3479
|
+
isError: true,
|
|
3320
3480
|
content: [
|
|
3321
3481
|
{
|
|
3322
3482
|
type: 'text',
|
|
@@ -3404,6 +3564,7 @@ registerToolConditional(
|
|
|
3404
3564
|
}
|
|
3405
3565
|
} catch (error) {
|
|
3406
3566
|
return {
|
|
3567
|
+
isError: true,
|
|
3407
3568
|
content: [
|
|
3408
3569
|
{
|
|
3409
3570
|
type: 'text',
|
|
@@ -3465,7 +3626,7 @@ registerToolConditional(
|
|
|
3465
3626
|
|
|
3466
3627
|
const backupDirectory = backupDir || DEFAULT_BACKUP_DIR;
|
|
3467
3628
|
const backupId = generateBackupId(type, name);
|
|
3468
|
-
const backupFile = getBackupFilePath(backupId, backupDirectory);
|
|
3629
|
+
const backupFile = getBackupFilePath(backupId, backupDirectory, backupExtension(type, compress));
|
|
3469
3630
|
const metadataPath = getBackupMetadataPath(backupId, backupDirectory);
|
|
3470
3631
|
|
|
3471
3632
|
// Ensure backup directory exists with proper error handling
|
|
@@ -3519,7 +3680,9 @@ registerToolConditional(
|
|
|
3519
3680
|
if (!database) {
|
|
3520
3681
|
throw new Error('database parameter required for MongoDB backup');
|
|
3521
3682
|
}
|
|
3522
|
-
|
|
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$/, '');
|
|
3523
3686
|
backupCommand = buildMongoDBDumpCommand({
|
|
3524
3687
|
database,
|
|
3525
3688
|
user: dbUser,
|
|
@@ -3629,6 +3792,7 @@ registerToolConditional(
|
|
|
3629
3792
|
});
|
|
3630
3793
|
|
|
3631
3794
|
return {
|
|
3795
|
+
isError: true,
|
|
3632
3796
|
content: [
|
|
3633
3797
|
{
|
|
3634
3798
|
type: 'text',
|
|
@@ -3701,6 +3865,7 @@ registerToolConditional(
|
|
|
3701
3865
|
});
|
|
3702
3866
|
|
|
3703
3867
|
return {
|
|
3868
|
+
isError: true,
|
|
3704
3869
|
content: [
|
|
3705
3870
|
{
|
|
3706
3871
|
type: 'text',
|
|
@@ -3751,7 +3916,13 @@ registerToolConditional(
|
|
|
3751
3916
|
}
|
|
3752
3917
|
|
|
3753
3918
|
const metadata = JSON.parse(metadataResult.stdout);
|
|
3754
|
-
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
|
+
}
|
|
3755
3926
|
|
|
3756
3927
|
// Execute pre-restore hook
|
|
3757
3928
|
await executeHook('pre-restore', {
|
|
@@ -3825,6 +3996,7 @@ registerToolConditional(
|
|
|
3825
3996
|
});
|
|
3826
3997
|
|
|
3827
3998
|
return {
|
|
3999
|
+
isError: true,
|
|
3828
4000
|
content: [
|
|
3829
4001
|
{
|
|
3830
4002
|
type: 'text',
|
|
@@ -3880,28 +4052,36 @@ registerToolConditional(
|
|
|
3880
4052
|
scriptContent += `# Created: ${new Date().toISOString()}\n\n`;
|
|
3881
4053
|
|
|
3882
4054
|
const backupId = `\${BACKUP_TYPE}_${name}_$(date +%Y%m%d_%H%M%S)_\${RANDOM}`;
|
|
3883
|
-
const backupFile = `${backupDirectory}/${backupId}
|
|
4055
|
+
const backupFile = `${backupDirectory}/${backupId}${backupExtension(type)}`;
|
|
3884
4056
|
|
|
3885
4057
|
scriptContent += `BACKUP_DIR="${backupDirectory}"\n`;
|
|
3886
4058
|
scriptContent += `BACKUP_TYPE="${type}"\n`;
|
|
3887
4059
|
scriptContent += `BACKUP_ID="${backupId}"\n`;
|
|
3888
4060
|
scriptContent += `BACKUP_FILE="${backupFile}"\n\n`;
|
|
3889
|
-
|
|
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';
|
|
3890
4069
|
|
|
3891
4070
|
// Add backup command based on type. The script runs later from cron, so
|
|
3892
4071
|
// a value spliced in raw here would run every night, not once.
|
|
3893
4072
|
switch (type) {
|
|
3894
4073
|
case BACKUP_TYPES.MYSQL:
|
|
3895
|
-
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`;
|
|
3896
4075
|
break;
|
|
3897
4076
|
case BACKUP_TYPES.POSTGRESQL:
|
|
3898
|
-
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`;
|
|
3899
4078
|
break;
|
|
3900
4079
|
case BACKUP_TYPES.MONGODB:
|
|
3901
|
-
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`;
|
|
3902
4082
|
break;
|
|
3903
4083
|
case BACKUP_TYPES.FILES:
|
|
3904
|
-
scriptContent += `tar -czf "$BACKUP_FILE" ${(paths || []).map(shellQuote).join(' ')}\n`;
|
|
4084
|
+
scriptContent += `tar -czf "$BACKUP_FILE" ${(paths || []).map(shellQuote).join(' ')} || fail\n`;
|
|
3905
4085
|
break;
|
|
3906
4086
|
}
|
|
3907
4087
|
|
|
@@ -3957,6 +4137,7 @@ registerToolConditional(
|
|
|
3957
4137
|
});
|
|
3958
4138
|
|
|
3959
4139
|
return {
|
|
4140
|
+
isError: true,
|
|
3960
4141
|
content: [
|
|
3961
4142
|
{
|
|
3962
4143
|
type: 'text',
|
|
@@ -4056,6 +4237,7 @@ registerToolConditional(
|
|
|
4056
4237
|
});
|
|
4057
4238
|
|
|
4058
4239
|
return {
|
|
4240
|
+
isError: true,
|
|
4059
4241
|
content: [
|
|
4060
4242
|
{
|
|
4061
4243
|
type: 'text',
|
|
@@ -4138,6 +4320,7 @@ registerToolConditional(
|
|
|
4138
4320
|
});
|
|
4139
4321
|
|
|
4140
4322
|
return {
|
|
4323
|
+
isError: true,
|
|
4141
4324
|
content: [
|
|
4142
4325
|
{
|
|
4143
4326
|
type: 'text',
|
|
@@ -4287,6 +4470,7 @@ registerToolConditional(
|
|
|
4287
4470
|
});
|
|
4288
4471
|
|
|
4289
4472
|
return {
|
|
4473
|
+
isError: true,
|
|
4290
4474
|
content: [
|
|
4291
4475
|
{
|
|
4292
4476
|
type: 'text',
|
|
@@ -4467,6 +4651,7 @@ registerToolConditional(
|
|
|
4467
4651
|
});
|
|
4468
4652
|
|
|
4469
4653
|
return {
|
|
4654
|
+
isError: true,
|
|
4470
4655
|
content: [
|
|
4471
4656
|
{
|
|
4472
4657
|
type: 'text',
|
|
@@ -4545,8 +4730,14 @@ registerToolConditional(
|
|
|
4545
4730
|
throw new Error(`Dump failed: ${result.stderr || result.stdout}`);
|
|
4546
4731
|
}
|
|
4547
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
|
+
|
|
4548
4739
|
// Get file size
|
|
4549
|
-
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`;
|
|
4550
4741
|
const sizeResult = await ssh.execCommand(sizeCommand);
|
|
4551
4742
|
const size = parseSize(sizeResult.stdout);
|
|
4552
4743
|
|
|
@@ -4565,7 +4756,7 @@ registerToolConditional(
|
|
|
4565
4756
|
server: serverName,
|
|
4566
4757
|
type,
|
|
4567
4758
|
database,
|
|
4568
|
-
output_file:
|
|
4759
|
+
output_file: dumpedFile,
|
|
4569
4760
|
size_bytes: size,
|
|
4570
4761
|
size_human: formatBytes(size),
|
|
4571
4762
|
compressed: compress,
|
|
@@ -4584,6 +4775,7 @@ registerToolConditional(
|
|
|
4584
4775
|
});
|
|
4585
4776
|
|
|
4586
4777
|
return {
|
|
4778
|
+
isError: true,
|
|
4587
4779
|
content: [
|
|
4588
4780
|
{
|
|
4589
4781
|
type: 'text',
|
|
@@ -4687,6 +4879,7 @@ registerToolConditional(
|
|
|
4687
4879
|
});
|
|
4688
4880
|
|
|
4689
4881
|
return {
|
|
4882
|
+
isError: true,
|
|
4690
4883
|
content: [
|
|
4691
4884
|
{
|
|
4692
4885
|
type: 'text',
|
|
@@ -4812,6 +5005,7 @@ registerToolConditional(
|
|
|
4812
5005
|
});
|
|
4813
5006
|
|
|
4814
5007
|
return {
|
|
5008
|
+
isError: true,
|
|
4815
5009
|
content: [
|
|
4816
5010
|
{
|
|
4817
5011
|
type: 'text',
|
|
@@ -4936,6 +5130,7 @@ registerToolConditional(
|
|
|
4936
5130
|
});
|
|
4937
5131
|
|
|
4938
5132
|
return {
|
|
5133
|
+
isError: true,
|
|
4939
5134
|
content: [
|
|
4940
5135
|
{
|
|
4941
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
|
/**
|