Skip to content
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# bedrock-mongodb ChangeLog

## 11.0.0 -

### Removed
- **BREAKING**: Remove roles check from authn.

### Added
- **BREAKING**: Throw if server version is less than 3.6.

## 10.1.1 -

### Changed
Expand Down
29 changes: 13 additions & 16 deletions lib/authn.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ export async function openDatabase(options) {
const serverInfo = await admin.serverInfo(null);
_checkServerVersion({serverInfo, config});

// check if server supports roles; if not, can't authenticate
if(!_usesRoles(serverInfo)) {
const stringVersion = serverInfo.versionArray.join('.');
throw new BedrockError(
`MongoDB server version "${stringVersion}" is unsupported.`,
'NotSupportedError');
}
// makes an unauthenticated call to the server
// to see if auth is required
const authRequired = await _isAuthnRequired({config, admin});
Expand Down Expand Up @@ -89,13 +82,6 @@ async function _connect(options) {
return {client, db};
}

function _usesRoles(serverInfo) {
// >= Mongo 2.6 uses user roles
return (
(serverInfo.versionArray[0] == 2 && serverInfo.versionArray[1] >= 6) ||
(serverInfo.versionArray[0] > 2));
}

/**
* Determines if authn is required.
*
Expand Down Expand Up @@ -128,7 +114,7 @@ async function _isAuthnRequired({config, admin}) {

function _addAuthOptions({options, config}) {
options.connectOptions.auth = {
user: config.username,
username: config.username,
password: config.password
};
// authSource is the database to authenticate against
Expand Down Expand Up @@ -170,11 +156,22 @@ function _checkServerVersion({serverInfo, config}) {
url: urls.sanitize(config.url),
version,
});
// if the server is not at least 3.6.0 throw
// as mongo node driver 4 only supports 3.6.0 and higher
if(!semver.gte(version, '3.6.0')) {
throw new BedrockError(
'Unsupported database version.',
'NotSupportedError', {
url: urls.sanitize(config.url),
version,
required: '>= 3.6.0'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't a different version being required by code changes elsewhere in this PR?

Copy link
Contributor Author

@aljones15 aljones15 Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The driver only goes down to 3.6 now: https://www.mongodb.com/docs/drivers/node/current/compatibility/#mongodb-compatibility

So 3.6 is the hard limit on server version.

You can see the 2 checks are here:

function _checkServerVersion({serverInfo, config}) {
// check that server version is supported
const {version} = serverInfo;
logger.info('connected to database', {
url: urls.sanitize(config.url),
version,
});
// if the server is not at least 3.6.0 throw
// as mongo node driver 4 only supports 3.6.0 and higher
if(!semver.gte(version, '3.6.0')) {
throw new BedrockError(
'Unsupported database version.',
'NotSupportedError', {
url: urls.sanitize(config.url),
version,
required: '>= 3.6.0'
});
}
if(config.requirements.serverVersion &&
!semver.satisfies(version, config.requirements.serverVersion)) {
throw new BedrockError(
'Unsupported database version.',
'NotSupportedError', {
url: urls.sanitize(config.url),
version,
required: config.requirements.serverVersion
});
}
}

});
}
if(config.requirements.serverVersion &&
!semver.satisfies(version, config.requirements.serverVersion)) {
throw new BedrockError(
'Unsupported database version.',
'DatabaseError', {
'NotSupportedError', {
url: urls.sanitize(config.url),
version,
required: config.requirements.serverVersion
Expand Down
4 changes: 2 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ config.mongodb.connectOptions = {
serverSelectionTimeoutMS: 30000,
// promotes binary BSON values to native Node.js buffers
promoteBuffers: true,
forceServerObjectId: true,
// it is recommended to set either ssl or tls to true in production
// ssl: true
// tls: true
Expand All @@ -56,12 +57,11 @@ config.mongodb.writeOptions = {
w: 'majority',
j: true,
},
forceServerObjectId: true,
};

config.mongodb.requirements = {};
// server version requirement with server-style string
config.mongodb.requirements.serverVersion = '>=4.2';
config.mongodb.requirements.serverVersion = '>=4.4';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 4.4 a requirement? We do still have some clusters on Atlas that are 4.2.x.

Copy link
Contributor Author

@aljones15 aljones15 Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so 4.4 is the recommended min version, you can set the serverVersion as low as 3.6. See the compatibility matrix here: https://www.mongodb.com/docs/drivers/node/current/compatibility/#mongodb-compatibility

You can see the two checks here:

function _checkServerVersion({serverInfo, config}) {
// check that server version is supported
const {version} = serverInfo;
logger.info('connected to database', {
url: urls.sanitize(config.url),
version,
});
// if the server is not at least 3.6.0 throw
// as mongo node driver 4 only supports 3.6.0 and higher
if(!semver.gte(version, '3.6.0')) {
throw new BedrockError(
'Unsupported database version.',
'NotSupportedError', {
url: urls.sanitize(config.url),
version,
required: '>= 3.6.0'
});
}
if(config.requirements.serverVersion &&
!semver.satisfies(version, config.requirements.serverVersion)) {
throw new BedrockError(
'Unsupported database version.',
'NotSupportedError', {
url: urls.sanitize(config.url),
version,
required: config.requirements.serverVersion
});
}
}


// this is used by _createUser to add a user as an admin to a collection
// config.mongodb.collection = 'admin-collection';
5 changes: 5 additions & 0 deletions test/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ if(process.env.MONGODB_PASSWORD) {
config.mongodb.password = process.env.MONGODB_PASSWORD;
}

if(process.env.MONGODB_SERVER_VERSION) {
config.mongodb.requirements.serverVersion =
process.env.MONGODB_SERVER_VERSION;
}

//config.mongodb.connectOptions.loggerLevel = 'debug';
config.mongodb.dropCollections.onInit = true;
config.mongodb.dropCollections.collections = [];
Expand Down