Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arbitrary sshOptions to be passed to ssh2 #119

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 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,12 @@ 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);

// 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';
Expand Down Expand Up @@ -207,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 }),
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
35 changes: 35 additions & 0 deletions test/modem_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,39 @@ 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');
});

it('supports custom sshAuthAgent for backwards-compatibility', function() {
process.env.DOCKER_HOST = 'ssh://[email protected]: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');
});
});