Mongoose: the options [user] and [pass] is not supported when using with useMongoClient: true

Created on 30 Jun 2017  路  15Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
When using user and pass as a connection option with useMongoClient: true, it'll report following warning:

the options [user] is not supported
the options [pass] is not supported

and failed to authenticate.

If the current behavior is a bug, please provide the steps to reproduce.

var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/github_issue?authSource=admin', { 
    user: 'username',
    pass: 'password',
    useMongoClient: true 
});
let issueSchema = new mongoose.Schema({
    'str' : { type: String }
});
issueModel = mongoose.model('issues', issueSchema );
issueModel.find({}, function(err, issueList){
    if(err)
         throw err;
    console.log(issueList);
});

Log:

the options [user] is not supported
the options [pass] is not supported
{ MongoError: not authorized on users to execute command { find: "customers", filter: {}, projection: {} }
    at Function.MongoError.create (<PROJECT_PATH>\node_modules\mongodb-core\lib\error.js:31:11)
    at queryCallback (<PROJECT_PATH>\node_modules\mongodb-core\lib\cursor.js:212:36)
    at <PROJECT_PATH>\node_modules\mongodb-core\lib\connection\pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'MongoError',
  message: 'not authorized on users to execute command { find: "customers", filter: {}, projection: {} }',
  ok: 0,
  errmsg: 'not authorized on users to execute command { find: "customers", filter: {}, projection: {} }',
  code: 13,
  codeName: 'Unauthorized' }

What is the expected behavior?
It should just report the result in the collection to the console.

[{
    str: 'Test'
}]

Please mention your node.js, mongoose and MongoDB version.
__Node__: v6.10.0
__MongoDB__: 3.4.5 on the offcial Mongo docker container
__Mongoose__: 4.11.0

underlying library issue

Most helpful comment

Working on getting support for this in the mongodb driver with the above PR :point_up: . For now, as a workaround, put username and password into the URI:

mongoose.connect(`mongodb://${encodeURIComponent(username)}:${encodeURIComponent(password)}@localhost:27017/github_issue?authSource=admin`, {
  useMongoClient: true
});

All 15 comments

The quick solution is to use node.js url module, construct your desired database connection string from it, and pass the generated url string to mongoose.connect().

The error messages "the options [user] is not supported", "the options [pass] is not supported" are most likely from the underlying node.js mongodb driver. It looks like openUri() method calls MongoClient.connect() which does not have user, pass options.

Refer:
http://mongodb.github.io/node-mongodb-native/2.1/api/MongoClient.html#.connect
https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L726

@raghuveer-s Thank you for your help. But, yeah, I already done it before I report. I just report to make sure that the issue will be resolved in the next version.

Working on getting support for this in the mongodb driver with the above PR :point_up: . For now, as a workaround, put username and password into the URI:

mongoose.connect(`mongodb://${encodeURIComponent(username)}:${encodeURIComponent(password)}@localhost:27017/github_issue?authSource=admin`, {
  useMongoClient: true
});

When I do the "work-around" I get authentication failed, but if I leave the user/pass options in the authentication works.

-edit: nevermind, I didn't know I had to specify db also

@vkarpov15 huh? what is encodeURIComponent() is that a base64 string?

Nope it encodes the string for use in a uri. Escaping /, :, and other special characters

2.2.31 is out which includes mongodb/node-mongodb-native#1529 馃帀

Upgraded Mongoose to 4.11.7 but still getting:

the options [user] is not supported
the options [pass] is not supported

With the following code:

mongoose.connect(`mongodb://${config.db_host}:${config.db_port}/${config.db_name}`, {
  user: 'username',
  pass: 'password',
  useMongoClient: true
})

Am I doing something wrong here?

@pcjmfranken try clearing out your node_modules, like rm -rf node_modules && npm install. If that doesn't work, could be a bug.

@vkarpov15 I'm using mongoose 4.12.1 and still getting this error.

I've removed/re-installed both the node_modules directory as well as package-lock.json file. Confirmed [email protected] and [email protected] are installed, yet still receiving the following

the options [user] is not supported
the options [pass] is not supported

Any thoughts?

@galenandrew can you please provide a code sample?

Details

  • Mongoose version: 4.13.7
  • Fresh package install in new project

Error

the options [user] is not supported
the options [pass] is not supported

Code

export const databaseConnect = async () => {
  try {
    mongoose.Promise = global.Promise
    await mongoose.connect(`mongodb://${config.db_host}:${config.db_port}/${config.db_name}`, {
      user: config.db_user,
      pass: config.db_pass,
      useMongoClient: true
    })
  } catch (error) {
    throw new Error(error)
  }
}

Looking at the code in mongo.client.js :

if(_validOptions.indexOf(name) == -1 && options.validateOptions) {
      return new MongoError(f('option %s is not supported', name));
    } else if(_validOptions.indexOf(name) == -1) {
      console.warn(f('the options [%s] is not supported', name));
    }

and _validOptions is:

var validOptionNames = ['poolSize', 'ssl', 'sslValidate', 'sslCA', 'sslCert',
  'sslKey', 'sslPass', 'sslCRL', 'autoReconnect', 'noDelay', 'keepAlive', 'connectTimeoutMS', 'family',
  'socketTimeoutMS', 'reconnectTries', 'reconnectInterval', 'ha', 'haInterval',
  'replicaSet', 'secondaryAcceptableLatencyMS', 'acceptableLatencyMS',
  'connectWithNoPrimary', 'authSource', 'w', 'wtimeout', 'j', 'forceServerObjectId',
  'serializeFunctions', 'ignoreUndefined', 'raw', 'bufferMaxEntries',
  'readPreference', 'pkFactory', 'promiseLibrary', 'readConcern', 'maxStalenessSeconds',
  'loggerLevel', 'logger', 'promoteValues', 'promoteBuffers', 'promoteLongs',
  'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity', 'validateOptions', 'appname', 'auth'];

it doesn't include 'user' or 'pass'

[email protected]

this is because mongoose's mongodb dependency is set to [email protected]

seems like they added it in the [email protected] :

var validOptionNames = [
  'poolSize',
  'ssl',
  'sslValidate',
  'sslCA',
  'sslCert',
  'sslKey',
  'sslPass',
  'sslCRL',
  'autoReconnect',
  'noDelay',
  'keepAlive',
  'keepAliveInitialDelay',
  'connectTimeoutMS',
  'family',
  'socketTimeoutMS',
  'reconnectTries',
  'reconnectInterval',
  'ha',
  'haInterval',
  'replicaSet',
  'secondaryAcceptableLatencyMS',
  'acceptableLatencyMS',
  'connectWithNoPrimary',
  'authSource',
  'w',
  'wtimeout',
  'j',
  'forceServerObjectId',
  'serializeFunctions',
  'ignoreUndefined',
  'raw',
  'bufferMaxEntries',
  'readPreference',
  'pkFactory',
  'promiseLibrary',
  'readConcern',
  'maxStalenessSeconds',
  'loggerLevel',
  'logger',
  'promoteValues',
  'promoteBuffers',
  'promoteLongs',
  'domainsEnabled',
  'checkServerIdentity',
  'validateOptions',
  'appname',
  'auth',
  'user',
  'password',
  'authMechanism',
  'compression',
  'fsync',
  'readPreferenceTags',
  'numberOfRetries',
  'auto_reconnect',
  'minSize'
];

but it's not even 'pass' that is valid it is 'password'

@vkarpov15 maybe reopen this?

@maloguertin mongoose supports user and pass with some limited massaging of the options object passed down to the mongodb driver. We avoid doing anything too sophisticated, but we added some logic to transform user -> auth.user and pass -> auth.password for backwards compatibility.

Was this page helpful?
0 / 5 - 0 ratings