I’m using an express API in node for mongodb access. My auth route works fine when the user/pass is correct. But when the user/pass is incorrect, the mongod instance issues this message 60 times:
2020-11-12T14:52:31.769-0500 I NETWORK [conn181] received client metadata from 127.0.0.1:49487 conn181: { driver: { name: "nodejs", version: "3.5.7" }, os: { type: "Darwin", name: "darwin", architecture: "x64", version: "19.6.0" }, platform: "'Node.js v11.0.0, LE (unified)" }
2020-11-12T14:52:31.770-0500 I ACCESS [conn181] SASL SCRAM-SHA-1 authentication failed for badUser on test_db from client 127.0.0.1:49487 ; UserNotFound: Could not find user badUser@test_db
2020-11-12T14:52:31.770-0500 I NETWORK [conn181] end connection 127.0.0.1:49487 (0 connections now open)
…then my node/express API finally returns
Authentication failed MongoServerSelectionError: Authentication failed.
This is correct, but takes about 20 seconds to complete. Therefore the user’s time is wasted waiting for the ‘incorrect user/pass’ message.
Here is the connect code in node:
var auth = { user:encodeURIComponent(req.body.user), password:encodeURIComponent(req.body.pass) }
var clientOptions = { authSource:dbName, auth:auth, authMechanism:'DEFAULT', useUnifiedTopology:true}
client = new MongoClient(process.env.MONGO_SERVER_URI, clientOptions)
client.connect((err, client) => {
if (err) {
res.status(401).send(JSON.stringify({err:`Authentication failed`}))
return
}
}
Why is mongodb (or MongoClient) taking so long to reject the login?