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

Commit be6377b

Browse files
alenakhineikadurran
authored andcommitted
Consistency with compass connection
1 parent ba2213c commit be6377b

File tree

6 files changed

+64
-46
lines changed

6 files changed

+64
-46
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Connection.from(
5050
connectionType: 'NODE_DRIVER',
5151
readPreference: 'primary',
5252
kerberosCanonicalizeHostname: false,
53-
sslType: 'NONE',
53+
sslMethod: 'NONE',
5454
sshTunnel: 'NONE',
5555
sshTunnelPort: 22
5656
}`
@@ -300,13 +300,13 @@ console.log(c.driverOptions)
300300
| Property | Type | Description | Default |
301301
| ----- | ---- | ---------- | ---- |
302302
| `ssl` | Number/String | A boolean to enable or disables TLS/SSL for the connection | `undefined` |
303-
| `sslType` | String | The desired ssl strategy. Possible values: `NONE`, `SYSTEMCA`, `IFAVAILABLE`, `UNVALIDATED`, `SERVER`, `ALL` | `NONE` |
303+
| `sslMethod` | String | The desired ssl method. Possible values: `NONE`, `SYSTEMCA`, `IFAVAILABLE`, `UNVALIDATED`, `SERVER`, `ALL` | `NONE` |
304304
| `sslCA` | Buffer/String | Array of valid certificates | `undefined` |
305305
| `sslCert` | Buffer/String | The certificate | `undefined` |
306306
| `sslKey` | Buffer/String | The certificate private key | `undefined` |
307307
| `sslPass` | Buffer/String | The certificate password | `undefined` |
308308

309-
Description of `sslType` values:
309+
Description of `sslMethod` values:
310310

311311
- `SYSTEMCA` - SSL required, validate using System CA, with host verification.
312312
- `IFAVAILABLE` - The driver should try SSL first, fall back to no SSL if unavailable, and use the system's Certificate Authority.

constants/ssl-type-values.js renamed to constants/ssl-method-values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Allowed values for the `sslType` field
1+
// Allowed values for the `sslMethod` field
22
module.exports = [
33
/**
44
* Do not use SSL for anything.

lib/extended-model.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ try {
1616
/**
1717
* Configuration for connecting to a MongoDB Deployment.
1818
*/
19-
module.exports = Connection.extend(storageMixin, {
19+
const ExtendedConnection = Connection.extend(storageMixin, {
2020
idAttribute: '_id',
2121
namespace: 'Connections',
2222
storage: {
@@ -33,7 +33,8 @@ module.exports = Connection.extend(storageMixin, {
3333
isFavorite: { type: 'boolean', default: false },
3434
name: { type: 'string', default: 'Local' },
3535
ns: { type: 'string', default: undefined },
36-
isSrvRecord: { type: 'boolean', default: false }
36+
isSrvRecord: { type: 'boolean', default: false },
37+
appname: { type: 'string', default: undefined }
3738
},
3839
session: { selected: { type: 'boolean', default: false } },
3940
derived: {
@@ -63,3 +64,20 @@ module.exports = Connection.extend(storageMixin, {
6364
return Connection.prototype.serialize.call(this, { all: true });
6465
}
6566
});
67+
68+
/**
69+
* Create a connection from a URI. This needs to ensure we create our subclass
70+
* or we won't have the storage mixin available.
71+
*
72+
* @param {String} url - The mongodb url to create from.
73+
* @param {Function} callback - The callback function.
74+
*/
75+
ExtendedConnection.from = (url, callback) => Connection.from(url, (error, c) => {
76+
if (error) {
77+
return callback(error);
78+
}
79+
80+
callback(null, new ExtendedConnection(c.getAttributes({ props: true })));
81+
});
82+
83+
module.exports = ExtendedConnection;

lib/model.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const AUTH_MECHANISM_TO_AUTH_STRATEGY = require('../constants/auth-mechanism-to-
2121
const AUTHENICATION_TO_AUTH_MECHANISM = require('../constants/auth-strategy-to-auth-mechanism');
2222
const AUTH_STRATEGY_VALUES = require('../constants/auth-strategy-values');
2323
const AUTH_STRATEGY_TO_FIELD_NAMES = require('../constants/auth-strategy-to-field-names');
24-
const SSL_TYPE_VALUES = require('../constants/ssl-type-values');
24+
const SSL_METHOD_VALUES = require('../constants/ssl-method-values');
2525
const SSH_TUNNEL_VALUES = require('../constants/ssh-tunnel-values');
2626
const READ_PREFERENCE_VALUES = [
2727
ReadPreference.PRIMARY,
@@ -298,7 +298,7 @@ assign(props, {
298298
*/
299299
assign(props, {
300300
ssl: { type: 'any', default: undefined },
301-
sslType: { type: 'string', values: SSL_TYPE_VALUES, default: SSL_DEFAULT },
301+
sslMethod: { type: 'string', values: SSL_METHOD_VALUES, default: SSL_DEFAULT },
302302
/**
303303
* Array of valid certificates either as Buffers or Strings
304304
* (needs to have a mongod server with ssl support, 2.4 or higher).
@@ -478,11 +478,11 @@ assign(derived, {
478478

479479
if (this.ssl) {
480480
req.query.ssl = this.ssl;
481-
} else if (includes(['UNVALIDATED', 'SYSTEMCA', 'SERVER', 'ALL'], this.sslType)) {
481+
} else if (includes(['UNVALIDATED', 'SYSTEMCA', 'SERVER', 'ALL'], this.sslMethod)) {
482482
req.query.ssl = 'true';
483-
} else if (this.sslType === 'IFAVAILABLE') {
483+
} else if (this.sslMethod === 'IFAVAILABLE') {
484484
req.query.ssl = 'prefer';
485-
} else if (this.sslType === 'NONE') {
485+
} else if (this.sslMethod === 'NONE') {
486486
req.query.ssl = 'false';
487487
}
488488

@@ -558,9 +558,9 @@ assign(derived, {
558558
fn() {
559559
const opts = clone(DRIVER_OPTIONS_DEFAULT, true);
560560

561-
if (this.sslType === 'SERVER') {
561+
if (this.sslMethod === 'SERVER') {
562562
assign(opts, { sslValidate: true, sslCA: this.sslCA });
563-
} else if (this.sslType === 'ALL') {
563+
} else if (this.sslMethod === 'ALL') {
564564
assign(opts, {
565565
sslValidate: true,
566566
sslCA: this.sslCA,
@@ -576,11 +576,11 @@ assign(derived, {
576576
opts.checkServerIdentity = false;
577577
opts.sslValidate = false;
578578
}
579-
} else if (this.sslType === 'UNVALIDATED') {
579+
} else if (this.sslMethod === 'UNVALIDATED') {
580580
assign(opts, { checkServerIdentity: false, sslValidate: false });
581-
} else if (this.sslType === 'SYSTEMCA') {
581+
} else if (this.sslMethod === 'SYSTEMCA') {
582582
assign(opts, { checkServerIdentity: true, sslValidate: true });
583-
} else if (this.sslType === 'IFAVAILABLE') {
583+
} else if (this.sslMethod === 'IFAVAILABLE') {
584584
assign(opts, { checkServerIdentity: false, sslValidate: true });
585585
}
586586

@@ -744,15 +744,15 @@ Connection = AmpersandModel.extend({
744744
*/
745745
validateSsl(attrs) {
746746
if (
747-
!attrs.sslType ||
748-
includes(['NONE', 'UNVALIDATED', 'IFAVAILABLE', 'SYSTEMCA'], attrs.sslType)
747+
!attrs.sslMethod ||
748+
includes(['NONE', 'UNVALIDATED', 'IFAVAILABLE', 'SYSTEMCA'], attrs.sslMethod)
749749
) {
750750
return;
751751
}
752752

753-
if (attrs.sslType === 'SERVER' && !attrs.sslCA) {
753+
if (attrs.sslMethod === 'SERVER' && !attrs.sslCA) {
754754
throw new TypeError('sslCA is required when ssl is SERVER.');
755-
} else if (attrs.sslType === 'ALL') {
755+
} else if (attrs.sslMethod === 'ALL') {
756756
if (!attrs.sslCA) {
757757
throw new TypeError('sslCA is required when ssl is ALL.');
758758
}
@@ -1028,7 +1028,7 @@ Connection._improveAtlasDefaults = (url, mongodbPassword, ns) => {
10281028
const atlasConnectionAttrs = {};
10291029

10301030
if (Connection.isAtlas(url)) {
1031-
atlasConnectionAttrs.sslType = 'SYSTEMCA';
1031+
atlasConnectionAttrs.sslMethod = 'SYSTEMCA';
10321032

10331033
if (mongodbPassword.match(/^.?PASSWORD.?$/i)) {
10341034
atlasConnectionAttrs.mongodbPassword = '';
@@ -1057,7 +1057,7 @@ Connection.isURI = (str) => (str.startsWith('mongodb://')) || (str.startsWith('m
10571057

10581058
Connection.AUTH_STRATEGY_VALUES = AUTH_STRATEGY_VALUES;
10591059
Connection.AUTH_STRATEGY_DEFAULT = AUTH_STRATEGY_DEFAULT;
1060-
Connection.SSL_TYPE_VALUES = SSL_TYPE_VALUES;
1060+
Connection.SSL_METHOD_VALUES = SSL_METHOD_VALUES;
10611061
Connection.SSL_DEFAULT = SSL_DEFAULT;
10621062
Connection.SSH_TUNNEL_VALUES = SSH_TUNNEL_VALUES;
10631063
Connection.SSH_TUNNEL_DEFAULT = SSH_TUNNEL_DEFAULT;

test/build-uri.test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ describe('connection model builder', () => {
3838
});
3939
});
4040

41-
it('when the connection is a srv record and sslType is NONE', () => {
42-
const c = new Connection({ isSrvRecord: true, sslType: 'NONE' });
41+
it('when the connection is a srv record and sslMethod is NONE', () => {
42+
const c = new Connection({ isSrvRecord: true, sslMethod: 'NONE' });
4343

4444
expect(c.driverUrl).to.be.equal('mongodb+srv://localhost/?readPreference=primary&ssl=false');
4545
});
4646

47-
it('when sslType is NONE', (done) => {
48-
const c = new Connection({ sslType: 'NONE' });
47+
it('when sslMethod is NONE', (done) => {
48+
const c = new Connection({ sslMethod: 'NONE' });
4949

5050
expect(c.driverUrl).to.be.equal('mongodb://localhost:27017/?readPreference=primary&ssl=false');
5151

@@ -55,8 +55,8 @@ describe('connection model builder', () => {
5555
});
5656
});
5757

58-
it('when sslType is UNVALIDATED', (done) => {
59-
const c = new Connection({ sslType: 'UNVALIDATED' });
58+
it('when sslMethod is UNVALIDATED', (done) => {
59+
const c = new Connection({ sslMethod: 'UNVALIDATED' });
6060
const options = Object.assign(
6161
{},
6262
Connection.DRIVER_OPTIONS_DEFAULT,
@@ -77,8 +77,8 @@ describe('connection model builder', () => {
7777
});
7878
});
7979

80-
it('when sslType is SYSTEMCA', (done) => {
81-
const c = new Connection({ sslType: 'SYSTEMCA' });
80+
it('when sslMethod is SYSTEMCA', (done) => {
81+
const c = new Connection({ sslMethod: 'SYSTEMCA' });
8282
const options = Object.assign(
8383
{},
8484
Connection.DRIVER_OPTIONS_DEFAULT,
@@ -99,8 +99,8 @@ describe('connection model builder', () => {
9999
});
100100
});
101101

102-
it('when sslType is IFAVAILABLE', (done) => {
103-
const c = new Connection({ sslType: 'IFAVAILABLE' });
102+
it('when sslMethod is IFAVAILABLE', (done) => {
103+
const c = new Connection({ sslMethod: 'IFAVAILABLE' });
104104
const options = Object.assign(
105105
{},
106106
Connection.DRIVER_OPTIONS_DEFAULT,
@@ -121,8 +121,8 @@ describe('connection model builder', () => {
121121
});
122122
});
123123

124-
it('when sslType is SERVER', (done) => {
125-
const c = new Connection({ sslType: 'SERVER', sslCA: fixture.ssl.ca });
124+
it('when sslMethod is SERVER', (done) => {
125+
const c = new Connection({ sslMethod: 'SERVER', sslCA: fixture.ssl.ca });
126126
const options = Object.assign(
127127
{},
128128
Connection.DRIVER_OPTIONS_DEFAULT,
@@ -143,9 +143,9 @@ describe('connection model builder', () => {
143143
});
144144
});
145145

146-
it('when sslType is ALL and using X509 auth', (done) => {
146+
it('when sslMethod is ALL and using X509 auth', (done) => {
147147
const c = new Connection({
148-
sslType: 'ALL',
148+
sslMethod: 'ALL',
149149
sslCA: fixture.ssl.ca,
150150
sslCert: fixture.ssl.server,
151151
sslKey: fixture.ssl.server,
@@ -175,9 +175,9 @@ describe('connection model builder', () => {
175175
});
176176
});
177177

178-
it('when sslType is ALL and passwordless private keys', (done) => {
178+
it('when sslMethod is ALL and passwordless private keys', (done) => {
179179
const c = new Connection({
180-
sslType: 'ALL',
180+
sslMethod: 'ALL',
181181
sslCA: fixture.ssl.ca,
182182
sslCert: fixture.ssl.server,
183183
sslKey: fixture.ssl.server
@@ -217,9 +217,9 @@ describe('connection model builder', () => {
217217
});
218218
});
219219

220-
it('when sslType is ALL and password protected private keys', (done) => {
220+
it('when sslMethod is ALL and password protected private keys', (done) => {
221221
const c = new Connection({
222-
sslType: 'ALL',
222+
sslMethod: 'ALL',
223223
sslCA: fixture.ssl.ca,
224224
sslCert: fixture.ssl.server,
225225
sslKey: fixture.ssl.server,
@@ -724,9 +724,9 @@ describe('connection model builder', () => {
724724
expect(c.sshTunnelBindToLocalPort).to.exist;
725725
});
726726

727-
it('when sslType ia ALL should load all of the files from the filesystem', (done) => {
727+
it('when sslMethod ia ALL should load all of the files from the filesystem', (done) => {
728728
const c = new Connection({
729-
sslType: 'ALL',
729+
sslMethod: 'ALL',
730730
sslCA: [fixture.ssl.ca],
731731
sslCert: fixture.ssl.server,
732732
sslKey: fixture.ssl.server

test/parse-uri-common-targets.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('connection model parser should parse URI strings for common connection
1919
expect(error).to.not.exist;
2020
expect(result.replicaSet).to.be.equal('a-compass-atlas-test-shard-0');
2121
expect(result.readPreference).to.be.equal('secondary');
22-
expect(result.sslType).to.be.equal('SYSTEMCA');
22+
expect(result.sslMethod).to.be.equal('SYSTEMCA');
2323
expect(result.mongodbPassword).to.be.equal('');
2424
expect(result.ns).to.be.equal('admin');
2525
expect(result.driverUrl).to.include('authSource=admin');
@@ -41,7 +41,7 @@ describe('connection model parser should parse URI strings for common connection
4141

4242
Connection.from(modifiedAtlasConnection, (error, result) => {
4343
expect(error).to.not.exist;
44-
expect(result.sslType).to.be.equal('SYSTEMCA');
44+
expect(result.sslMethod).to.be.equal('SYSTEMCA');
4545
expect(result.mongodbPassword).to.be.equal(userPass);
4646
done();
4747
});
@@ -55,7 +55,7 @@ describe('connection model parser should parse URI strings for common connection
5555

5656
Connection.from(modifiedAtlasConnection, (error, result) => {
5757
expect(error).to.not.exist;
58-
expect(result.sslType).to.be.equal('NONE');
58+
expect(result.sslMethod).to.be.equal('NONE');
5959
done();
6060
});
6161
});
@@ -65,7 +65,7 @@ describe('connection model parser should parse URI strings for common connection
6565

6666
Connection.from(modifiedAtlasConnection, (error, result) => {
6767
expect(error).to.not.exist;
68-
expect(result.sslType).to.be.equal('SYSTEMCA');
68+
expect(result.sslMethod).to.be.equal('SYSTEMCA');
6969
done();
7070
});
7171
});

0 commit comments

Comments
 (0)