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

[Fix] Parse authSource variable into mongo url string #1527

Merged
merged 2 commits into from
Nov 14, 2023
Merged
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
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
Loading