Skip to content

Commit

Permalink
Merge pull request #120 from steverice/full-ssh-options
Browse files Browse the repository at this point in the history
Allow arbitrary sshOptions to be passed to ssh2
  • Loading branch information
apocas authored Dec 1, 2020
2 parents 000b873 + 243c7e9 commit 3fe6f38
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.host = opts.host;

Expand All @@ -90,7 +93,7 @@ var Modem = function (options) {
this.agent = opts.agent;
this.agentForward = opts.agentForward;
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 @@ -212,8 +215,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, 'agentForward': self.agentForward }),
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 @@ -172,4 +172,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');
});
});

0 comments on commit 3fe6f38

Please sign in to comment.