ssh-client-wrapper 2.0.2 → 2.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/.dockerignore +1 -0
- package/Dockerfile +10 -0
- package/docker-compose.yml +17 -0
- package/lib/fork.js +55 -48
- package/lib/index.js +13 -0
- package/lib/rsyncExec.js +32 -1
- package/lib/sshExec.js +7 -2
- package/package.json +2 -2
- package/test/interface.js +1 -1
- package/test/rsyncExec.js +29 -1
- package/test/sshExec.js +1 -1
package/.dockerignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
node_modules
|
package/Dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# syntax=docker/dockerfile:1
|
|
2
|
+
#build WHEEL client code
|
|
3
|
+
FROM --platform=linux/amd64 node:hydrogen-bookworm-slim
|
|
4
|
+
#FROM --platform=linux/amd64 node:hydrogen-bullseye-slim
|
|
5
|
+
#FROM --platform=linux/amd64 node:hydrogen-buster-slim
|
|
6
|
+
|
|
7
|
+
WORKDIR /work
|
|
8
|
+
RUN apt-get update && apt -y install ssh rsync bzip2 python3 g++ build-essential
|
|
9
|
+
COPY ./ ssh-client-wrapper
|
|
10
|
+
RUN cd ssh-client-wrapper && npm install
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
version: "3.8"
|
|
2
|
+
services:
|
|
3
|
+
test-bed:
|
|
4
|
+
image: naoso5/test-sshd
|
|
5
|
+
ports:
|
|
6
|
+
- 4000:22
|
|
7
|
+
container_name: testbed
|
|
8
|
+
tester:
|
|
9
|
+
build:
|
|
10
|
+
context: .
|
|
11
|
+
dockerfile: Dockerfile
|
|
12
|
+
environment:
|
|
13
|
+
TEST_USER: testuser
|
|
14
|
+
TEST_PW: passw0rd
|
|
15
|
+
TEST_HOST: testbed
|
|
16
|
+
working_dir: /work/ssh-client-wrapper
|
|
17
|
+
command: bash -c "mkdir /root/.ssh 2>/dev/null; npm run test"
|
package/lib/fork.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const debug = require("debug")("sshClientWrapper:debug:fork");
|
|
3
3
|
const debugVerbose = require("debug")("sshClientWrapper:verbose:fork");
|
|
4
|
-
const debugMasterVerbose = require("debug")("sshClientWrapper:insecure:fork_masterPty");
|
|
5
4
|
const debugSendPty = require("debug")("sshClientWrapper:insecure:fork_sendpty");
|
|
6
5
|
const { spawn } = require("node-pty");
|
|
7
6
|
|
|
@@ -13,45 +12,53 @@ const nodePtyOpt = {
|
|
|
13
12
|
};
|
|
14
13
|
|
|
15
14
|
function sshLoginCallback(output, ptyProcess, pw, ph, logger = debugSendPty) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (rePwPrompt.test(output)) {
|
|
21
|
-
if (typeof pw === "string") {
|
|
22
|
-
debug("use given password");
|
|
23
|
-
sendPty(ptyProcess, `${pw}\n`, logger, 23);
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (typeof pw === "function") {
|
|
27
|
-
debug("call password callback function");
|
|
28
|
-
pw()
|
|
29
|
-
.then((v)=>{
|
|
30
|
-
sendPty(ptyProcess, `${v}\n`, logger, 30);
|
|
31
|
-
});
|
|
32
|
-
return;
|
|
15
|
+
return new Promise((resolve, reject)=>{
|
|
16
|
+
if (reNewHostPrompt.test(output)) {
|
|
17
|
+
sendPty(ptyProcess, "yes\n", logger, 17);
|
|
18
|
+
resolve();
|
|
33
19
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
20
|
+
if (rePwPrompt.test(output)) {
|
|
21
|
+
if (typeof pw === "string") {
|
|
22
|
+
debug("use given password");
|
|
23
|
+
sendPty(ptyProcess, `${pw}\n`, logger, 23);
|
|
24
|
+
resolve();
|
|
25
|
+
}
|
|
26
|
+
if (typeof pw === "function") {
|
|
27
|
+
debug("call password callback function");
|
|
28
|
+
pw()
|
|
29
|
+
.then((v)=>{
|
|
30
|
+
sendPty(ptyProcess, `${v}\n`, logger, 30);
|
|
31
|
+
})
|
|
32
|
+
.catch((e)=>{
|
|
33
|
+
reject(e);
|
|
34
|
+
});
|
|
35
|
+
resolve();
|
|
36
|
+
}
|
|
40
37
|
}
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
if (rePhPrompt.test(output)) {
|
|
39
|
+
if (typeof ph === "string") {
|
|
40
|
+
debug("use given passphrase");
|
|
41
|
+
sendPty(ptyProcess, `${ph}\n`, logger, 38);
|
|
42
|
+
resolve();
|
|
43
|
+
}
|
|
44
|
+
if (typeof ph === "function") {
|
|
45
|
+
debug("call passphrase callback function");
|
|
46
|
+
ph()
|
|
47
|
+
.then((v)=>{
|
|
48
|
+
sendPty(ptyProcess, `${v}\n`, logger, 45);
|
|
49
|
+
})
|
|
50
|
+
.catch((e)=>{
|
|
51
|
+
reject(e);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
|
-
}
|
|
55
|
+
});
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
/**
|
|
52
59
|
* create pty for master ssh session and store to hostInfo.masterPty
|
|
53
60
|
*/
|
|
54
|
-
function
|
|
61
|
+
function createMastePty(hostInfo) {
|
|
55
62
|
debug("fork pty for master connection");
|
|
56
63
|
|
|
57
64
|
if (hostInfo.masterPty) {
|
|
@@ -59,11 +66,6 @@ function getMasterPty(hostInfo, logger) {
|
|
|
59
66
|
hostInfo.masterPty.kill();
|
|
60
67
|
}
|
|
61
68
|
const ptyProcess = spawn(nodePtyOpt.shell, [], nodePtyOpt);
|
|
62
|
-
ptyProcess.onData(buff2String.bind(null, debugMasterVerbose));
|
|
63
|
-
ptyProcess.onData((data)=>{
|
|
64
|
-
const output = data.toString();
|
|
65
|
-
sshLoginCallback(output, ptyProcess, hostInfo.password, hostInfo.passphrase, logger);
|
|
66
|
-
});
|
|
67
69
|
hostInfo.masterPty = ptyProcess;
|
|
68
70
|
}
|
|
69
71
|
|
|
@@ -81,17 +83,21 @@ async function fork(hostInfo, cmd, args, timeout, outputCallback) {
|
|
|
81
83
|
});
|
|
82
84
|
}
|
|
83
85
|
ptyProcess.onData(buff2String.bind(null, debugVerbose));
|
|
84
|
-
ptyProcess.onData((data)=>{
|
|
85
|
-
const output = data.toString();
|
|
86
|
-
sshLoginCallback(output, ptyProcess, hostInfo.password, hostInfo.passphrase);
|
|
87
86
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
87
|
+
if (hostInfo !== null) {
|
|
88
|
+
ptyProcess.onData(async (data)=>{
|
|
89
|
+
const output = data.toString();
|
|
90
|
+
sshLoginCallback(output, ptyProcess, hostInfo.password, hostInfo.passphrase)
|
|
91
|
+
.catch(reject);
|
|
92
|
+
|
|
93
|
+
if (/kex_exchange_identification: Connection closed by remote host/.test(output)) {
|
|
94
|
+
ptyProcess.kill();
|
|
95
|
+
const err = new Error("kex_exchange_identification");
|
|
96
|
+
err.retryable = true;
|
|
97
|
+
reject(err);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
95
101
|
|
|
96
102
|
if (typeof outputCallback === "function") {
|
|
97
103
|
ptyProcess.onData(buff2String.bind(null, outputCallback));
|
|
@@ -119,5 +125,6 @@ async function fork(hostInfo, cmd, args, timeout, outputCallback) {
|
|
|
119
125
|
}
|
|
120
126
|
module.exports = {
|
|
121
127
|
fork,
|
|
122
|
-
|
|
128
|
+
createMastePty,
|
|
129
|
+
sshLoginCallback
|
|
123
130
|
};
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
2
4
|
const debug = require("debug")("sshClientWrapper:interface");
|
|
3
5
|
const debugVerbose = require("debug")("sshClientWrapper:verbose:interface");
|
|
4
6
|
const { sshExec, canConnect, disconnect, ls } = require("./sshExec.js");
|
|
@@ -46,6 +48,17 @@ class SshClientWrapper {
|
|
|
46
48
|
sanityCheck(hostInfo);
|
|
47
49
|
this.hostInfo = { ...hostInfo };
|
|
48
50
|
this.hostInfo.masterPty = null;
|
|
51
|
+
this.hostInfo.rsyncVersion = null;
|
|
52
|
+
|
|
53
|
+
if (process.env.HOME) {
|
|
54
|
+
try {
|
|
55
|
+
fs.mkdirSync(path.resolve(process.env.HOME, ".ssh"));
|
|
56
|
+
} catch (e) {
|
|
57
|
+
if (e.code !== "EEXIST") {
|
|
58
|
+
throw e;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
/**
|
package/lib/rsyncExec.js
CHANGED
|
@@ -9,6 +9,19 @@ const { fork } = require("./fork.js");
|
|
|
9
9
|
|
|
10
10
|
const { sshCmd, rsyncCmd, getSshOption } = require("./util.js");
|
|
11
11
|
|
|
12
|
+
async function checkRsyncVersion(timeout) {
|
|
13
|
+
const rt = {};
|
|
14
|
+
await fork(null, rsyncCmd, ["--version"], timeout, (output)=>{
|
|
15
|
+
const result = /^rsync *version *(\d+)\.(\d+)\.(\d+).*$/m.exec(output);
|
|
16
|
+
|
|
17
|
+
if (result) {
|
|
18
|
+
rt.major = Number(result[1]);
|
|
19
|
+
rt.minor = Number(result[2]);
|
|
20
|
+
rt.patch = Number(result[3]);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return rt;
|
|
24
|
+
}
|
|
12
25
|
|
|
13
26
|
/**
|
|
14
27
|
* Send file or directory and its child to server.
|
|
@@ -69,6 +82,21 @@ async function send(hostInfo, argSrc, dst, rsyncOpt, timeout) {
|
|
|
69
82
|
return retryWrapper(fork.bind(null, hostInfo, rsyncCmd, args, timeout), hostInfo);
|
|
70
83
|
}
|
|
71
84
|
|
|
85
|
+
/**
|
|
86
|
+
* format src array to src string for recv
|
|
87
|
+
* @param {Object} hostInfo - hostinfo object
|
|
88
|
+
* @returns {boolean} - rsync command version is higher than 3.2.4 or not
|
|
89
|
+
*/
|
|
90
|
+
async function isNewRsync(hostInfo) {
|
|
91
|
+
if (!hostInfo.rsyncVersion ||
|
|
92
|
+
!Number.isInteger(hostInfo.rsyncVersion.major) ||
|
|
93
|
+
!Number.isInteger(hostInfo.rsyncVersion.minor) ||
|
|
94
|
+
!Number.isInteger(hostInfo.rsyncVersion.patch)) {
|
|
95
|
+
hostInfo.rsyncVersion = await checkRsyncVersion(3);
|
|
96
|
+
}
|
|
97
|
+
return hostInfo.rsyncVersion.major >= 3 && hostInfo.rsyncVersion.minor >= 2 && hostInfo.rsyncVersion.patch >= 4;
|
|
98
|
+
}
|
|
99
|
+
|
|
72
100
|
/**
|
|
73
101
|
* Send file or directory and its child to server.
|
|
74
102
|
* @param {Object} hostInfo - hostinfo object
|
|
@@ -84,14 +112,17 @@ async function recv(hostInfo, argSrc, dst, rsyncOpt, timeout) {
|
|
|
84
112
|
const src = `${hostInfo.host}:${argSrc.join(" ")}`;
|
|
85
113
|
const sshArgs = getSshOption(hostInfo, true);
|
|
86
114
|
const rshOpt = ["-e", `${sshCmd} ${sshArgs.join(" ")}`];
|
|
87
|
-
|
|
88
115
|
const args = ["-avv", "--copy-unsafe-links", ...rshOpt, ...rsyncOpt, src, dst];
|
|
116
|
+
if (await isNewRsync(hostInfo)) {
|
|
117
|
+
args.unshift("--old-args");
|
|
118
|
+
}
|
|
89
119
|
|
|
90
120
|
debug(`recv ${src} to ${dst}`);
|
|
91
121
|
debugVerbose(`recv: ${rsyncCmd} ${args.join(" ")}`);
|
|
92
122
|
return retryWrapper(fork.bind(null, hostInfo, rsyncCmd, args, timeout), hostInfo);
|
|
93
123
|
}
|
|
94
124
|
module.exports = {
|
|
125
|
+
checkRsyncVersion,
|
|
95
126
|
send,
|
|
96
127
|
recv
|
|
97
128
|
};
|
package/lib/sshExec.js
CHANGED
|
@@ -5,7 +5,7 @@ const debug = require("debug")("sshClientWrapper:debug:sshExec");
|
|
|
5
5
|
const debugVerbose = require("debug")("sshClientWrapper:verbose:sshExec");
|
|
6
6
|
const debugSendMasterPty = require("debug")("sshClientWrapper:insecure:sshExec_masterPty");
|
|
7
7
|
const { sendPty, sshCmd, getSshOption, sanityCheck, watchDogTimer } = require("./util.js");
|
|
8
|
-
const { fork,
|
|
8
|
+
const { fork, createMastePty, sshLoginCallback } = require("./fork.js");
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Execute command on remote host.
|
|
@@ -103,7 +103,7 @@ async function connect(hostInfo, timeout = 60) {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
debug(`create master pty for ${hostInfo.host}`);
|
|
106
|
-
|
|
106
|
+
createMastePty(hostInfo, debugSendMasterPty);
|
|
107
107
|
|
|
108
108
|
debug(`create master ssh session to ${hostInfo.host}`);
|
|
109
109
|
|
|
@@ -146,6 +146,11 @@ async function connect(hostInfo, timeout = 60) {
|
|
|
146
146
|
reject(new Error("Operation timed out"));
|
|
147
147
|
}
|
|
148
148
|
});
|
|
149
|
+
hostInfo.masterPty.onData((data)=>{
|
|
150
|
+
const output = data.toString();
|
|
151
|
+
sshLoginCallback(output, hostInfo.masterPty, hostInfo.password, hostInfo.passphrase, debugSendMasterPty)
|
|
152
|
+
.catch(reject);
|
|
153
|
+
});
|
|
149
154
|
hostInfo.masterPty.onExit((e)=>{
|
|
150
155
|
debug("master pty exit", e);
|
|
151
156
|
resolve(e);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-client-wrapper",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "wrapper library for open ssh client and rsync",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ssh",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"debug": "^4.3.4",
|
|
38
|
-
"glob": "^10.3.
|
|
38
|
+
"glob": "^10.3.10",
|
|
39
39
|
"node-pty": "^0.10.1",
|
|
40
40
|
"nyc": "^15.1.0",
|
|
41
41
|
"promise-retry": "^2.0.1",
|
package/test/interface.js
CHANGED
|
@@ -56,7 +56,7 @@ describe("test for interface", ()=>{
|
|
|
56
56
|
describe("test for public method", ()=>{
|
|
57
57
|
let ssh;
|
|
58
58
|
const hostInfoOrg = { host: "hoge" };
|
|
59
|
-
const hostInfo = { ...hostInfoOrg, masterPty: null };
|
|
59
|
+
const hostInfo = { ...hostInfoOrg, masterPty: null, rsyncVersion: null };
|
|
60
60
|
beforeEach(()=>{
|
|
61
61
|
ssh = new SshClientWrapper(hostInfoOrg);
|
|
62
62
|
sshExec.reset();
|
package/test/rsyncExec.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const fs = require("fs-extra");
|
|
3
3
|
const path = require("path");
|
|
4
|
+
const util = require("util");
|
|
5
|
+
const exec = util.promisify(require("node:child_process").exec);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* to enable debug log dynamically
|
|
9
|
+
* (1) enable folloing line
|
|
10
|
+
* const debug = require("debug");
|
|
11
|
+
* (2) insert this line into the beginning of it()
|
|
12
|
+
* debug.enable("ssh*");
|
|
13
|
+
* (3) insert this line into the end of it()
|
|
14
|
+
* debug.disable();
|
|
15
|
+
**/
|
|
4
16
|
|
|
5
17
|
process.on("unhandledRejection", console.dir); //eslint-disable-line no-console
|
|
6
18
|
Error.traceLimit = 100000;
|
|
@@ -12,7 +24,7 @@ chai.use(require("chai-fs"));
|
|
|
12
24
|
chai.use(require("chai-as-promised"));
|
|
13
25
|
|
|
14
26
|
//testee
|
|
15
|
-
const { send, recv } = require("../lib/rsyncExec.js");
|
|
27
|
+
const { checkRsyncVersion, send, recv } = require("../lib/rsyncExec.js");
|
|
16
28
|
|
|
17
29
|
//test helpers
|
|
18
30
|
const { sshExec, disconnect } = require("../lib/sshExec.js");
|
|
@@ -42,6 +54,14 @@ const formatLsOutput = (array)=>{
|
|
|
42
54
|
|
|
43
55
|
|
|
44
56
|
//actual test start from here
|
|
57
|
+
describe("test checkRsyncVersion", async ()=>{
|
|
58
|
+
const { major, minor, patch } = await checkRsyncVersion(1);
|
|
59
|
+
const versionString = `${major}.${minor}.${patch}`;
|
|
60
|
+
console.log(`==== test with rsync version ${versionString} ====`); //eslint-disable-line no-console
|
|
61
|
+
const { stdout } = await exec("rsync --version |grep 'version'");
|
|
62
|
+
expect(stdout).to.match(new RegExp(versionString));
|
|
63
|
+
});
|
|
64
|
+
|
|
45
65
|
describe("test rsync exec", function() {
|
|
46
66
|
this.timeout(10000);//eslint-disable-line no-invalid-this
|
|
47
67
|
const rt = [];
|
|
@@ -370,6 +390,14 @@ describe("test rsync exec", function() {
|
|
|
370
390
|
expect(rt2).to.have.members(["puyo", "poyo"]);
|
|
371
391
|
});
|
|
372
392
|
});
|
|
393
|
+
describe("degradation check", ()=>{
|
|
394
|
+
it("should recieve multiple files from remote machine", async ()=>{
|
|
395
|
+
await recv(hostInfo, [path.posix.join(remoteRoot, "foo"),
|
|
396
|
+
path.posix.join(remoteRoot, "bar"), path.posix.join(remoteRoot, "baz")], localEmptyDir, [], 0);
|
|
397
|
+
const rt2 = await fs.readdir(localEmptyDir);
|
|
398
|
+
expect(rt2).to.have.members(["foo", "bar", "baz"]);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
373
401
|
describe("error case", ()=>{
|
|
374
402
|
it("should not send directory to existing file path", ()=>{
|
|
375
403
|
return expect(recv(hostInfo, [remoteRoot], localFiles[0], [], 0)).to.be.rejected;
|
package/test/sshExec.js
CHANGED
|
@@ -76,7 +76,7 @@ describe("test for ssh execution", function() {
|
|
|
76
76
|
await disconnect(hostInfo2);
|
|
77
77
|
});
|
|
78
78
|
it("should be resolved with true", async ()=>{
|
|
79
|
-
return expect(await canConnect(hostInfo2,
|
|
79
|
+
return expect(await canConnect(hostInfo2, 5)).to.be.true;
|
|
80
80
|
});
|
|
81
81
|
it("should be rejected if user does not exist", async ()=>{
|
|
82
82
|
hostInfo2.user = "xxxx";
|