ssh-client-wrapper 3.2.0 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/lib/agent.js +2 -2
- package/lib/fork.js +2 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/sshExec.js +58 -1
- package/lib/util.js +49 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,6 +64,7 @@ Creates a new SSH client instance.
|
|
|
64
64
|
* `retryDuration` (number, optional): Time (msec) to wait between each retry. Defaults to `1000`.
|
|
65
65
|
* `retryableExitCodes` (number[], optional): Additional rsync exit codes to treat as retryable for `.send()`/`.recv()`. See [Retry behavior for `.send()` / `.recv()`](#retry-behavior-for-send--recv) below.
|
|
66
66
|
* `replaceRetryableExitCodes` (boolean, optional): If `true`, `retryableExitCodes` replaces the built-in retryable exit code list instead of adding to it.
|
|
67
|
+
* `useControlMaster` (boolean, optional): Keep an ssh master connection (`ControlMaster`) and share it between the ssh/rsync calls. Defaults to `true`, but to `false` on Windows because its ssh clients can not do it. If `false`, every call logs in by itself (use the ssh-agent to avoid answering prompts every time), and `ControlPersist` and `ControlPersistDir` are ignored.
|
|
67
68
|
* `useAgent` (boolean, optional): Authenticate key-based hosts through an ssh-agent instead of replaying the passphrase on every connection. Defaults to `true` when `keyFile` is set. Set `false` to force the legacy behavior. See [SSH agent authentication](#ssh-agent-authentication) below.
|
|
68
69
|
* `identityAgent` (string, optional): Path to an existing ssh-agent socket to authenticate through (emitted as `-oIdentityAgent`). When set, the wrapper uses that agent as-is and manages nothing.
|
|
69
70
|
* `agentKeyTTL` (number, optional): Lifetime in seconds applied to keys the wrapper adds to an agent (`ssh-add -t`). Defaults to `3600`.
|
package/lib/agent.js
CHANGED
|
@@ -6,7 +6,7 @@ import { promisify } from "node:util";
|
|
|
6
6
|
import { setTimeout as setTimeoutPromise } from "node:timers/promises";
|
|
7
7
|
import Debug from "debug";
|
|
8
8
|
import { spawn } from "node-pty";
|
|
9
|
-
import { rePhPrompt } from "./util.js";
|
|
9
|
+
import { rePhPrompt, resolveExecutable } from "./util.js";
|
|
10
10
|
|
|
11
11
|
const debug = Debug("sshClientWrapper:debug:agent");
|
|
12
12
|
const debugVerbose = Debug("sshClientWrapper:verbose:agent");
|
|
@@ -244,7 +244,7 @@ async function resolvePassphrase(ph) {
|
|
|
244
244
|
*/
|
|
245
245
|
function addKey(sock, keyFile, phCallback, ttl) {
|
|
246
246
|
return new Promise((resolve, reject)=>{
|
|
247
|
-
const pty = _internal.spawn("ssh-add", ["-t", String(ttl), keyFile], {
|
|
247
|
+
const pty = _internal.spawn(resolveExecutable("ssh-add"), ["-t", String(ttl), keyFile], {
|
|
248
248
|
windowsHide: true,
|
|
249
249
|
env: sockEnv(sock)
|
|
250
250
|
});
|
package/lib/fork.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Debug from "debug";
|
|
2
2
|
import { spawn } from "node-pty";
|
|
3
3
|
|
|
4
|
-
import { rePwPrompt, rePhPrompt, reNewHostPrompt, reRemoteHostIdentificationHasChanged, buff2String, sendPty, watchDogTimer } from "./util.js";
|
|
4
|
+
import { resolveExecutable, rePwPrompt, rePhPrompt, reNewHostPrompt, reRemoteHostIdentificationHasChanged, buff2String, sendPty, watchDogTimer } from "./util.js";
|
|
5
5
|
|
|
6
6
|
const debug = Debug("sshClientWrapper:debug:fork");
|
|
7
7
|
const debugVerbose = Debug("sshClientWrapper:verbose:fork");
|
|
@@ -86,7 +86,7 @@ export async function fork(hostInfo, cmd, args, timeout, outputCallback, retryab
|
|
|
86
86
|
return new Promise((resolve, reject)=>{
|
|
87
87
|
debug(`cmd=${cmd}: args=${args}`);
|
|
88
88
|
//node-pty accepts only string arguments (e.g. the port number is a number)
|
|
89
|
-
const ptyProcess = spawn(cmd, args.map(String), nodePtyOpt);
|
|
89
|
+
const ptyProcess = spawn(resolveExecutable(cmd), args.map(String), nodePtyOpt);
|
|
90
90
|
let ac = null;
|
|
91
91
|
if (timeout > 0) {
|
|
92
92
|
ac = watchDogTimer(timeout, cmd, ()=>{
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -28,6 +28,7 @@ const debugVerbose = Debug("sshClientWrapper:verbose:interface");
|
|
|
28
28
|
* @param {string} hostInfo.keyFile - private key filename
|
|
29
29
|
* @param {boolean} hostInfo.noStrictHostKeyChecking - bypass host key checking if true
|
|
30
30
|
* @param {Integer} hostInfo.ControlPersist - how long keep master connection after last client connection has been closed (sec).
|
|
31
|
+
* @param {boolean} hostInfo.useControlMaster - keep a master connection (ssh ControlMaster) and share it between ssh/rsync calls. default: true, but false on win32 because its ssh clients do not support it. If false, every call logs in by itself and ControlPersist and ControlPersistDir are ignored.
|
|
31
32
|
* @param {Integer} hostInfo.ConnectTimeout - timeout used when connectiong to ths SSH server
|
|
32
33
|
* @param {Integer} hostInfo.maxRetry - max number of retry
|
|
33
34
|
* @param {Integer} hostInfo.retryDuration - duration time (msec) between each retry
|
package/lib/sshExec.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import crypto from "crypto";
|
|
2
2
|
import { setTimeout as setTimeoutPromise } from "timers/promises";
|
|
3
3
|
import Debug from "debug";
|
|
4
|
-
import { sendPty, sshCmd, getSshOption, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
4
|
+
import { sendPty, sshCmd, getSshOption, isControlMasterEnabled, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
5
5
|
import { fork, createMasterPty, sshLoginCallback } from "./fork.js";
|
|
6
6
|
import { shouldUseAgent, ensureAgentForHost } from "./agent.js";
|
|
7
7
|
|
|
@@ -142,8 +142,55 @@ async function existsMaster(hostInfo, argTimeout) {
|
|
|
142
142
|
return !/Control socket connect.*: (No such file or directory|Connection refused)/.test(output);
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
/**
|
|
146
|
+
* check once that the host can be logged in to, for the case that no master connection is used
|
|
147
|
+
* @param {object} hostInfo - hostinfo object
|
|
148
|
+
* @param {number} timeout - timeout (in sec.)
|
|
149
|
+
*/
|
|
150
|
+
async function verifyConnection(hostInfo, timeout) {
|
|
151
|
+
if (hostInfo._connectionVerified) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const args = getSshOption(hostInfo);
|
|
155
|
+
args.push(`echo ${crypto.randomUUID()}`);
|
|
156
|
+
|
|
157
|
+
let output = "";
|
|
158
|
+
const cb = (e)=>{
|
|
159
|
+
output += e;
|
|
160
|
+
};
|
|
161
|
+
debug(`verify connection: ${sshCmd} ${args.join(" ")}`);
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
await fork(hostInfo, sshCmd, args, timeout, cb);
|
|
165
|
+
} catch (e) {
|
|
166
|
+
const err = new Error(e.message);
|
|
167
|
+
err.host = hostInfo.host;
|
|
168
|
+
err.user = hostInfo.user;
|
|
169
|
+
err.port = hostInfo.port;
|
|
170
|
+
err.output = output;
|
|
171
|
+
if (e.code === "TIMEEXPIRE") {
|
|
172
|
+
err.message = `can not connect within ${timeout} sec`;
|
|
173
|
+
} else if (/Permission denied \(/.test(output)) {
|
|
174
|
+
err.message = "Permission denied";
|
|
175
|
+
err.code = "AUTH_FAILED";
|
|
176
|
+
} else if (/Could not resolve hostname/.test(output)) {
|
|
177
|
+
err.message = "Could not resolve hostname";
|
|
178
|
+
} else if (/Bad port/.test(output)) {
|
|
179
|
+
err.message = "Bad port";
|
|
180
|
+
} else if (/Operation|Connection timed out/.test(output)) {
|
|
181
|
+
err.message = "Operation timed out";
|
|
182
|
+
} else {
|
|
183
|
+
throw e;
|
|
184
|
+
}
|
|
185
|
+
throw err;
|
|
186
|
+
}
|
|
187
|
+
debug(`connected to ${hostInfo.host}`);
|
|
188
|
+
hostInfo._connectionVerified = true;
|
|
189
|
+
}
|
|
190
|
+
|
|
145
191
|
/**
|
|
146
192
|
* create master ssh connection
|
|
193
|
+
* if the master connection is not used, only check that the host can be logged in to
|
|
147
194
|
* @param {object} hostInfo - hostinfo object
|
|
148
195
|
*/
|
|
149
196
|
export async function connect(hostInfo, timeout = 60) {
|
|
@@ -165,6 +212,10 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
165
212
|
delete hostInfo._agentEnsuredAt;
|
|
166
213
|
}
|
|
167
214
|
|
|
215
|
+
if (!isControlMasterEnabled(hostInfo)) {
|
|
216
|
+
return verifyConnection(hostInfo, timeout);
|
|
217
|
+
}
|
|
218
|
+
|
|
168
219
|
if (await existsMaster(hostInfo, timeout)) {
|
|
169
220
|
debugVerbose("master connection exists");
|
|
170
221
|
return;
|
|
@@ -279,6 +330,12 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
279
330
|
export async function disconnect(hostInfo) {
|
|
280
331
|
debug("disconnect called");
|
|
281
332
|
|
|
333
|
+
if (!isControlMasterEnabled(hostInfo)) {
|
|
334
|
+
debug(`disconnect: master connection is not used for ${hostInfo.host}`);
|
|
335
|
+
delete hostInfo._connectionVerified;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
282
339
|
try {
|
|
283
340
|
if (await existsMaster(hostInfo)) {
|
|
284
341
|
const args = getSshOption(hostInfo);
|
package/lib/util.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EOL } from "os";
|
|
2
2
|
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
3
4
|
import Debug from "debug";
|
|
4
5
|
import { setTimeout as setTimeoutPromise } from "timers/promises";
|
|
5
6
|
import Ajv from "ajv";
|
|
@@ -11,6 +12,34 @@ export const sshCmd = "ssh";
|
|
|
11
12
|
export const rsyncCmd = "rsync";
|
|
12
13
|
export const acceptableRsyncRetrunCodes = [10, 11, 12, 13, 14];
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* resolve a command name to the path of the executable file to be spawned in a pty
|
|
17
|
+
* node-pty on Windows does not add an extension (.exe) to the command, and it does not find a command in the last directory of PATH.
|
|
18
|
+
* @param {string} cmd - command name or path
|
|
19
|
+
* @param {object} opt - options (platform, env and isFile are injectable for tests)
|
|
20
|
+
* @param {string} opt.platform - platform name
|
|
21
|
+
* @param {object} opt.env - environment variables
|
|
22
|
+
* @param {Function} opt.isFile - function which checks if given path is a file
|
|
23
|
+
* @returns {string} - full path of the executable on win32 if it is found, otherwise given cmd as it is
|
|
24
|
+
*/
|
|
25
|
+
export const resolveExecutable = (cmd, { platform = process.platform, env = process.env, isFile = (p)=>{ return fs.statSync(p, { throwIfNoEntry: false })?.isFile() === true; } } = {})=>{
|
|
26
|
+
if (platform !== "win32" || /[\\/]/.test(cmd)) {
|
|
27
|
+
return cmd;
|
|
28
|
+
}
|
|
29
|
+
const dirs = (env.Path ?? env.PATH ?? "").split(";").filter(Boolean);
|
|
30
|
+
const names = path.win32.extname(cmd) === "" ? [`${cmd}.exe`] : [cmd];
|
|
31
|
+
for (const dir of dirs) {
|
|
32
|
+
for (const name of names) {
|
|
33
|
+
const candidate = path.win32.join(dir, name);
|
|
34
|
+
if (isFile(candidate)) {
|
|
35
|
+
return candidate;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
debug(`${cmd} not found in PATH`);
|
|
40
|
+
return cmd;
|
|
41
|
+
};
|
|
42
|
+
|
|
14
43
|
export const rePwPrompt = /password:/;
|
|
15
44
|
export const rePhPrompt = /Enter passphrase for key/;
|
|
16
45
|
export const reNewHostPrompt = /Are you sure you want to continue connecting/;
|
|
@@ -40,6 +69,7 @@ const hostInfoSchema = {
|
|
|
40
69
|
ControlPersist: { type: "number", minimum: 0, default: 180 },
|
|
41
70
|
ConnectTimeout: { type: "number", minimum: 0 },
|
|
42
71
|
ControlPersistDir: { type: "string", pattern: "\\S+", transform: ["trim"] },
|
|
72
|
+
useControlMaster: { type: "boolean" },
|
|
43
73
|
maxRetry: { type: "number", minimum: 0, default: 3 },
|
|
44
74
|
retryDuration: { type: "number", minimum: 0, default: 1000 },
|
|
45
75
|
retryableExitCodes: {
|
|
@@ -181,6 +211,18 @@ function getControlPersistDir(hostInfo) {
|
|
|
181
211
|
return "~/.ssh";
|
|
182
212
|
}
|
|
183
213
|
|
|
214
|
+
/**
|
|
215
|
+
* check if the ssh master connection (ControlMaster) should be used
|
|
216
|
+
* @param {object} hostInfo - host information object defined in index.js
|
|
217
|
+
* @returns {boolean} - hostInfo.useControlMaster if it is set, otherwise false on win32 (its ssh clients can not do it) and true elsewhere
|
|
218
|
+
*/
|
|
219
|
+
export const isControlMasterEnabled = (hostInfo)=>{
|
|
220
|
+
if (typeof hostInfo.useControlMaster === "boolean") {
|
|
221
|
+
return hostInfo.useControlMaster;
|
|
222
|
+
}
|
|
223
|
+
return process.platform !== "win32";
|
|
224
|
+
};
|
|
225
|
+
|
|
184
226
|
/**
|
|
185
227
|
* return ssh option with some hard corded options
|
|
186
228
|
* @param {object} hostInfo - host information object defined in index.js
|
|
@@ -242,11 +284,13 @@ export const getSshOption = (hostInfo, withoutDestination)=>{
|
|
|
242
284
|
args.push(`-oIdentityAgent=${agentSock}`);
|
|
243
285
|
}
|
|
244
286
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
287
|
+
if (isControlMasterEnabled(hostInfo)) {
|
|
288
|
+
const controlPersist = Number.isInteger(hostInfo.ControlPersist) && hostInfo.ControlPersist >= 0 ? hostInfo.ControlPersist : "180";
|
|
289
|
+
const controlPersistDir = getControlPersistDir(hostInfo);
|
|
290
|
+
args.push("-oControlMaster=auto");
|
|
291
|
+
args.push(`-oControlPath=${controlPersistDir}/scw-%C`);
|
|
292
|
+
args.push(`-oControlPersist=${controlPersist}`);
|
|
293
|
+
}
|
|
250
294
|
|
|
251
295
|
if (Number.isInteger(hostInfo.ConnectTimeout) && hostInfo.ConnectTimeout > 0) {
|
|
252
296
|
args.push(`-oConnectTimeout=${hostInfo.ConnectTimeout}`);
|