Skip to content

Commit

Permalink
Allow arbitrary sshOptions to be passed to ssh2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
steverice committed Apr 22, 2020
1 parent 1563783 commit 779cdc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/modem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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 () { });
Expand Down
25 changes: 25 additions & 0 deletions test/modem_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]: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://[email protected]: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');
});
});

0 comments on commit 779cdc1

Please sign in to comment.