Skip to content

Commit

Permalink
Allow setting authentication database in the MongoClient connection v…
Browse files Browse the repository at this point in the history
…ia config (#38)

Allow setting authentication database
  • Loading branch information
samgavinio authored and emirotin committed Jan 16, 2017
1 parent 082460c commit 72329e0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.5

* Added support for setting authentication database via the `authSource` string.

## 0.8.4

* Migration files other than `.js` and `.coffee` **will be skipped**, along with dotfiles
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ from the current directory (include it as your project's dependency).

The configuration object can have the following keys:

* `url` — full MongoDB connection url (_optional_, when used the rest of the connection params (`host`, `port`, `db`, `user`, `password`, `replicaset`) are ignored),
* `url` — full MongoDB connection url (_optional_, when used the rest of the connection params (`host`, `port`, `db`, `user`, `password`, `replicaset`, `authDatabase`) are ignored),
* `host` — MongoDB host (_optional_ when using `url` or `replicaset`, **required** otherwise),
* `port` _[optional]_ — MongoDB port,
* `db` — MongoDB database name,
* `ssl` _[optional]_ - boolean, if `true`, `'?ssl=true'` is added to the MongoDB URL,
* `user` _[optional]_ — MongoDB user name when authentication is required,
* `password` _[optional]_ — MongoDB password when authentication is required,
* `authDatabase` _[optional]_ - MongoDB database to authenticate the user against,
* `collection` _[optional]_ — The name of the MongoDB collection to track already ran migrations, **defaults to `_migrations`**,
* `directory` — the directory (path relative to the current folder) to store migration files in and read them from, used when running from the command-line or when using `runFromDir`,
* `timeout` _[optional]_ — time in milliseconds after which migration should fail if `done()` is not called (use 0 to disable timeout)
Expand Down
5 changes: 4 additions & 1 deletion lib/url-builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/url-builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ module.exports =
params = []

if replicaset
params.push 'replicaSet=' + replicaset.name
params.push "replicaSet=#{replicaset.name}"

if config.ssl
params.push 'ssl=true'

if config.authDatabase
params.push "authSource=#{config.authDatabase}"

if params.length > 0
s += '?' + params.join('&')

Expand Down
3 changes: 3 additions & 0 deletions src/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ validateConnSettings = (config) ->
if config.password and not config.user
throw new Error('`password` provided but `user` is not')

if config.authDatabase and not config.user
throw new Error('`authDatabase` provided but `user` is not')

exports.normalizeConfig = (config) ->
if not (_.isObject(config) and not _.isArray(config))
throw new Error('`config` is not provided or is not an object')
Expand Down
48 changes: 28 additions & 20 deletions test/url-builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ describe 'Url Builder', ->
collection: '_migrations'

connString = urlBuilder.buildMongoConnString config
connString.should.be.equal 'mongodb://' + config.user + ':' +
config.password + '@' + config.host + ':' + config.port +
'/' + config.db
connString.should.be.equal "mongodb://#{config.user}:#{config.password}@" +
"#{config.host}:#{config.port}/#{config.db}"
done()

it 'builds a single node url with ssl', (done) ->
Expand All @@ -37,9 +36,23 @@ describe 'Url Builder', ->
ssl: true

connString = urlBuilder.buildMongoConnString config
connString.should.be.equal 'mongodb://' + config.user + ':' +
config.password + '@' + config.host + ':' + config.port +
'/' + config.db + '?ssl=true'
connString.should.be.equal "mongodb://#{config.user}:#{config.password}@" +
"#{config.host}:#{config.port}/#{config.db}?ssl=true"
done()

it 'builds a single node url with an authDatabase', (done) ->
config =
user: 'someuser'
password: 'somepass'
host: 'abcde',
port: 27111
db: '_mm'
collection: '_migrations',
authDatabase: 'admin'

connString = urlBuilder.buildMongoConnString config
connString.should.be.equal "mongodb://#{config.user}:#{config.password}@" +
"#{config.host}:#{config.port}/#{config.db}?authSource=#{config.authDatabase}"
done()

it 'builds a replicaset url with two replicas', (done) ->
Expand All @@ -62,12 +75,10 @@ describe 'Url Builder', ->
collection: '_migrations'

connString = urlBuilder.buildMongoConnString config
connString.should.be.equal 'mongodb://' + config.user + ':' +
config.password + '@' +
config.replicaset.members[0].host + ':' + config.replicaset.members[0].port +
',' +
config.replicaset.members[1].host + ':' + config.replicaset.members[1].port +
'/' + config.db + '?replicaSet=' + config.replicaset.name
connString.should.be.equal "mongodb://#{config.user}:#{config.password}@" +
"#{config.replicaset.members[0].host}:#{config.replicaset.members[0].port}," +
"#{config.replicaset.members[1].host}:#{config.replicaset.members[1].port}/" +
"#{config.db}?replicaSet=#{config.replicaset.name}"
done()

it 'builds a replicaset url with three replicas', (done) ->
Expand All @@ -94,12 +105,9 @@ describe 'Url Builder', ->
collection: '_migrations'

connString = urlBuilder.buildMongoConnString config
connString.should.be.equal 'mongodb://' + config.user + ':' +
config.password + '@' +
config.replicaset.members[0].host + ':' + config.replicaset.members[0].port +
',' +
config.replicaset.members[1].host + ':' + config.replicaset.members[1].port +
',' +
config.replicaset.members[2].host + ':' + config.replicaset.members[2].port +
'/' + config.db + '?replicaSet=' + config.replicaset.name
connString.should.be.equal "mongodb://#{config.user}:#{config.password}@" +
"#{config.replicaset.members[0].host}:#{config.replicaset.members[0].port}," +
"#{config.replicaset.members[1].host}:#{config.replicaset.members[1].port}," +
"#{config.replicaset.members[2].host}:#{config.replicaset.members[2].port}/" +
"#{config.db}?replicaSet=#{config.replicaset.name}"
done()
8 changes: 8 additions & 0 deletions test/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ describe 'Utils', ->
password: 'very secret password'
}).should.throw('`password` provided but `user` is not')
done()

it 'should throw with authDatabase but without username', (done) ->
normalizeConfig.bind(null, {
host: 'localhost',
db: '_mm',
authDatabase: 'admin'
}).should.throw('`authDatabase` provided but `user` is not')
done()

0 comments on commit 72329e0

Please sign in to comment.