Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit debe1a4

Browse files
fix: use a real password for driver url with ssh (#281)
1 parent c06b4ef commit debe1a4

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/model.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const URL = require('url');
33
const toURL = URL.format;
44
const { format } = require('util');
55
const fs = require('fs');
6-
const { assign, defaults, clone, includes, unescape } = require('lodash');
6+
const { assign, defaults, clone, cloneDeep, includes, unescape } = require('lodash');
77
const AmpersandModel = require('ampersand-model');
88
const AmpersandCollection = require('ampersand-rest-collection');
99
const { ReadPreference } = require('mongodb');
@@ -595,15 +595,15 @@ assign(derived, {
595595
driverUrlWithSsh: {
596596
cache: false,
597597
fn() {
598-
const req = clone(prepareRequest(this));
598+
const req = cloneDeep(prepareRequest(this));
599599

600600
if (this.sshTunnel !== 'NONE') {
601601
// Populate the SSH Tunnel options correctly.
602602
req.hostname = this.sshTunnelOptions.localAddr;
603603
req.port = this.sshTunnelOptions.localPort;
604604
}
605605

606-
return addAuthToUrl.call(this, { url: toURL(req), isPasswordProtected: true });
606+
return addAuthToUrl.call(this, { url: toURL(req), isPasswordProtected: false });
607607
}
608608
},
609609
/**

test/build-uri.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,18 @@ describe('Connection model builder', () => {
632632
});
633633
});
634634

635+
it('should add a real password to driver url', () => {
636+
const c = new Connection({
637+
mongodbUsername: 'arlo',
638+
mongodbPassword: 'woof'
639+
});
640+
641+
expect(c.driverUrl).to.be.equal(
642+
'mongodb://arlo:woof@localhost:27017/?authSource=admin&readPreference=primary&ssl=false'
643+
);
644+
expect(c.driverUrl).to.be.equal(c.driverUrlWithSsh);
645+
});
646+
635647
it('should set authStrategy to LDAP', (done) => {
636648
const c = new Connection({ ldapUsername: 'arlo', ldapPassword: 'w@of'});
637649

test/ssh-tunnel.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,26 @@ describe('sshTunnel', function() {
298298
assert(c.isValid());
299299
});
300300

301+
it('should inject ssh tunnel port', (done) => {
302+
assert.equal(
303+
c.driverUrl,
304+
'mongodb://mongodb.my-internal-host.com:27000/?readPreference=primary&ssl=false'
305+
);
306+
307+
Connection.from(c.driverUrlWithSsh, (error, sshModel) => {
308+
assert(!error);
309+
assert.equal(
310+
sshModel.hostname,
311+
'127.0.0.1'
312+
);
313+
assert.notEqual(
314+
c.port,
315+
sshModel.port
316+
);
317+
done();
318+
});
319+
});
320+
301321
describe('sshTunnelOptions', () => {
302322
const options = c.sshTunnelOptions;
303323

0 commit comments

Comments
 (0)