From 779cdc1e2db731af1e4d145aa57f28ea5ea02f00 Mon Sep 17 00:00:00 2001 From: Steve Rice Date: Tue, 21 Apr 2020 20:28:52 -0700 Subject: [PATCH 1/2] Allow arbitrary sshOptions to be passed to ssh2 This allows for an arbitrary `sshOptions` to be passed in `Modem` options and be used when passed to the `ssh` constructor to get an agent from the `ssh2` library. Tests are added to be sure we're still respecting the `SSH_AUTH_SOCK` env var. --- lib/modem.js | 18 +++++++++++++----- test/modem_test.js | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/modem.js b/lib/modem.js index 003b5a0..846d98b 100644 --- a/lib/modem.js +++ b/lib/modem.js @@ -46,7 +46,9 @@ var defaultOpts = function () { } else if (host.protocol === 'ssh:') { opts.protocol = 'ssh'; opts.username = host.username; - opts.sshAuthAgent = process.env.SSH_AUTH_SOCK; + opts.sshOptions = { + agent: process.env.SSH_AUTH_SOCK, + } } else { opts.protocol = 'http'; } @@ -69,7 +71,8 @@ var defaultOpts = function () { var Modem = function (options) { - var opts = Object.assign({}, defaultOpts(), options); + var optDefaults = defaultOpts(); + var opts = Object.assign({}, optDefaults, options); this.socketPath = opts.socketPath; this.host = opts.host; @@ -85,7 +88,7 @@ var Modem = function (options) { this.checkServerIdentity = opts.checkServerIdentity; this.agent = opts.agent; this.headers = opts.headers || {}; - this.sshAuthAgent = opts.sshAuthAgent; + this.sshOptions = Object.assign({}, options ? options.sshOptions : {}, optDefaults.sshOptions); if (this.key && this.cert && this.ca) { this.protocol = 'https'; @@ -207,8 +210,13 @@ Modem.prototype.buildRequest = function (options, context, data, callback) { var connectionTimeoutTimer; var opts = self.protocol === 'ssh' ? Object.assign(options, { - agent: ssh({ 'host': self.host, 'port': self.port, 'username': self.username, 'password': self.password, 'agent': self.sshAuthAgent }), - protocol: 'http:' + agent: ssh(Object.assign({}, self.sshOptions, { + 'host': self.host, + 'port': self.port, + 'username': self.username, + 'password': self.password, + })), + protocol: 'http:', }) : options; var req = http[self.protocol === 'ssh' ? 'http' : self.protocol].request(opts, function () { }); diff --git a/test/modem_test.js b/test/modem_test.js index 2021528..1de08e2 100644 --- a/test/modem_test.js +++ b/test/modem_test.js @@ -144,4 +144,29 @@ describe('Modem', function() { assert.ok(modem.agent instanceof http.Agent); assert.strictEqual(modem.agent, httpAgent); }); + + it('should set default ssh agent options from DOCKER_HOST', function() { + process.env.DOCKER_HOST = 'ssh://user@192.168.59.105:5555'; + process.env.SSH_AUTH_SOCK = '/var/lib/sock'; + + var modem = new Modem(); + assert.strictEqual(modem.protocol, 'ssh'); + assert.strictEqual(modem.username, 'user'); + assert.ok(modem.sshOptions); + assert.strictEqual(modem.sshOptions.agent, '/var/lib/sock'); + }); + + it('should combine custom ssh agent options', function() { + process.env.DOCKER_HOST = 'ssh://user@192.168.59.105:5555'; + process.env.SSH_AUTH_SOCK = '/var/lib/sock'; + + var modem = new Modem({ + sshOptions: { + foo: 'bar', // options are arbitrary, whatever ssh2 supports + }, + }); + assert.ok(modem.sshOptions); + assert.strictEqual(modem.sshOptions.agent, '/var/lib/sock'); + assert.strictEqual(modem.sshOptions.foo, 'bar'); + }); }); From bde24db0a1d74df12a84c6b716ca3771e0820c39 Mon Sep 17 00:00:00 2001 From: Steve Rice Date: Wed, 22 Apr 2020 08:41:11 -0700 Subject: [PATCH 2/2] Add backwards-compatibility with sshAuthAgent option Since d2f92b22cf284d537a57cc255ba11b4c4c5d7b61, callers may be passing this option in and expect it to be used as the `agent` option for ssh2. --- lib/modem.js | 5 +++++ test/modem_test.js | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/modem.js b/lib/modem.js index 846d98b..60ec49e 100644 --- a/lib/modem.js +++ b/lib/modem.js @@ -90,6 +90,11 @@ var Modem = function (options) { this.headers = opts.headers || {}; this.sshOptions = Object.assign({}, options ? options.sshOptions : {}, optDefaults.sshOptions); + // Support sshAuthAgent for backwards-compatibility with d2f92b22cf284d537a57cc255ba11b4c4c5d7b61 + if (options && options.sshAuthAgent) { + this.sshOptions.agent = options.sshAuthAgent; + } + if (this.key && this.cert && this.ca) { this.protocol = 'https'; } diff --git a/test/modem_test.js b/test/modem_test.js index 1de08e2..640eaea 100644 --- a/test/modem_test.js +++ b/test/modem_test.js @@ -169,4 +169,14 @@ describe('Modem', function() { assert.strictEqual(modem.sshOptions.agent, '/var/lib/sock'); assert.strictEqual(modem.sshOptions.foo, 'bar'); }); + + it('supports custom sshAuthAgent for backwards-compatibility', function() { + process.env.DOCKER_HOST = 'ssh://user@192.168.59.105:5555'; + process.env.SSH_AUTH_SOCK = '/var/lib/sock'; + + var modem = new Modem({ + sshAuthAgent: '/var/lib/custom_agent', + }); + assert.strictEqual(modem.sshOptions.agent, '/var/lib/custom_agent'); + }); });