Skip to content

Commit

Permalink
Merge pull request #1527 from cblanco-libelium/master
Browse files Browse the repository at this point in the history
[Fix] Parse authSource variable into mongo url string
  • Loading branch information
AlvaroVega authored Nov 14, 2023
2 parents 95435fd + 1557dde commit 63d9bbd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
- Fix: null values arithmetics in JEXL expressions (#1440)
- Fix: remove mongo `DeprecationWarning: current Server Discovery and Monitoring engine is deprecated` by setting `useUnifiedTopology = true`
- Upgrade mongodb dev dep from 4.17.0 to 4.17.1
- Fix: mongodb.authSource / IOTA_MONGO_AUTH_SOURCE was not correctly supported (#1526)
24 changes: 14 additions & 10 deletions lib/model/dbConn.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,6 @@ function configureDb(callback) {
options.replicaSet = currentConfig.mongodb.replicaSet;
}

if (currentConfig.mongodb.user && currentConfig.mongodb.password) {
options.auth = {};
options.auth.user = currentConfig.mongodb.user;
options.auth.password = currentConfig.mongodb.password;
}

if (currentConfig.mongodb.authSource) {
options.authSource = currentConfig.mongodb.authSource;
}

if (currentConfig.mongodb.ssl) {
options.ssl = currentConfig.mongodb.ssl;
}
Expand All @@ -228,6 +218,20 @@ function configureDb(callback) {
options.extraArgs = currentConfig.mongodb.extraArgs;
}

if (currentConfig.mongodb.user && currentConfig.mongodb.password) {
options.auth = {};
options.auth.user = currentConfig.mongodb.user;
options.auth.password = currentConfig.mongodb.password;
// authSource only applies if auth is set
if (currentConfig.mongodb.authSource) {
// Overload extraArgs if it was set
options.extraArgs = {
...options.extraArgs,
authSource: currentConfig.mongodb.authSource
};
}
}

init(config.getConfig().mongodb.host, dbName, port, options, callback);
}
} else {
Expand Down
7 changes: 2 additions & 5 deletions test/unit/mongodb/mongodb-connectionoptions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ describe('dbConn.configureDb', function () {
expected: {
url: 'mongodb://example.com:27017/' + dbConn.DEFAULT_DB_NAME,
options: {
authSource: 'admin',
useNewUrlParser: true,
useUnifiedTopology: true
}
Expand All @@ -190,14 +189,13 @@ describe('dbConn.configureDb', function () {
authSource: 'admin'
},
expected: {
url: 'mongodb://user01:[email protected]:98765/examples',
url: 'mongodb://user01:[email protected]:98765/examples?authSource=admin',
options: {
replicaSet: 'rs0',
auth: {
user: 'user01',
password: 'pass01'
},
authSource: 'admin',
useNewUrlParser: true,
useUnifiedTopology: true
}
Expand Down Expand Up @@ -308,14 +306,13 @@ describe('dbConn.configureDb', function () {
unknownparam: 'unknown'
},
expected: {
url: 'mongodb://user01:[email protected]:98765/examples?retryWrites=true&readPreference=nearest&w=majority',
url: 'mongodb://user01:[email protected]:98765/examples?retryWrites=true&readPreference=nearest&w=majority&authSource=admin',
options: {
replicaSet: 'rs0',
auth: {
user: 'user01',
password: 'pass01'
},
authSource: 'admin',
ssl: true,
useNewUrlParser: true,
useUnifiedTopology: true
Expand Down

0 comments on commit 63d9bbd

Please sign in to comment.