Connecting to MongoDB - Command 'createIndexes' requires authentication (Maybe Agenda.js related?)

1.8k views Asked by At

As I'm introducing authentication for my MongoDB instance in Docker, I ran into problems which are probably related to the way agenda.js tries to connect to MongoDB, as the connection string invokes successful logs for mongoose connecting to the DB, therefore I assume the string should be valid.

Everything worked until I changed the connection string to use authentication. I verified the users are properly created in the database and also tried variations of the connection string and deleting/installing node modules.

Following output I get upon running docker-compose up:

server    | /server/node_modules/agenda/node_modules/mongodb/lib/utils.js:133
server    |       throw err;
server    |       ^
server    | 
server    | MongoError: command createIndexes requires authentication
server    |     at Connection.<anonymous> (/server/node_modules/agenda/node_modules/mongodb/lib/core/connection/pool.js:466:61)
server    |     at Connection.emit (events.js:314:20)
server    |     at processMessage (/server/node_modules/agenda/node_modules/mongodb/lib/core/connection/connection.js:384:10)
server    |     at Socket.<anonymous> (/server/node_modules/agenda/node_modules/mongodb/lib/core/connection/connection.js:553:15)
server    |     at Socket.emit (events.js:314:20)
server    |     at addChunk (_stream_readable.js:297:12)
server    |     at readableAddChunk (_stream_readable.js:272:9)
server    |     at Socket.Readable.push (_stream_readable.js:213:10)
server    |     at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
server    | Emitted 'error' event on Agenda instance at:
server    |     at /server/node_modules/agenda/lib/agenda/db-init.js:23:14
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/execute_operation.js:76:14
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/execute_operation.js:63:27
server    |     at ClientSession.endSession (/server/node_modules/agenda/node_modules/mongodb/lib/core/sessions.js:135:41)
server    |     at executeCallback (/server/node_modules/agenda/node_modules/mongodb/lib/operations/execute_operation.js:59:17)
server    |     at handleCallback (/server/node_modules/agenda/node_modules/mongodb/lib/utils.js:129:55)
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/create_index.js:85:14
server    |     at handleCallback (/server/node_modules/agenda/node_modules/mongodb/lib/utils.js:129:55)
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/operations/command.js:113:23
server    |     at /server/node_modules/agenda/node_modules/mongodb/lib/core/connection/pool.js:420:18
server    |     at processTicksAndRejections (internal/process/task_queues.js:79:11) {
server    |   ok: 0,
server    |   errmsg: 'command createIndexes requires authentication',
server    |   code: 13,
server    |   codeName: 'Unauthorized',
server    |   [Symbol(mongoErrorContextSymbol)]: {}
server    | }

package.json

"dependencies": {
    "agenda": "^4.1.2",
    "axios": "^0.21.1",
    "bcryptjs": "^2.4.3",
    "body-parser": "latest",
    "express": "^4.17.1",
    "infinite-timeout": "^0.1.0",
    "jsonwebtoken": "^8.5.1",
    "method-override": "^3.0.0",
    "mongoose": "^5.12.4",
    "mosca": "^2.8.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "websocket": "^1.0.34",
    "webstorm": "^1.0.0",
    "ws": "latest"
  },
  "devDependencies": {
    "@shelf/jest-mongodb": "^1.1.3",
    "@types/express": "^4.17.1",
    "cors": "^2.8.5",
    "jest": "^24.9.0"
  },
  "engines": {
    "node": "12.x"
  }

docker-compose.yml

version: "3.8"
services:
  server:
    container_name: server
    build: .
    restart: unless-stopped
    ports:
      - "9000:9000"
      - "1883:1883"
    links:
      - mongo
    networks:
      - app-network
    command: npm start
  mongo:
    container_name: mongo
    image: mongo:4.4.4
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=superSafePassword
    ports:
      - "27317:27017"
    volumes:
      - dbdata:/data/db
      - "./init.js/:/docker-entrypoint-initdb.d/init.js:ro"
    restart: unless-stopped
    networks:
      - app-network
    command: mongod
networks:
  app-network:
    driver: bridge
volumes:
  dbdata:
  node_modules:

Script to create users init.js

db = db.getSiblingDB("dbName");
db.createUser(
    {
        user: "dbUser",
        pwd: "dbPassword",
        roles: [
            {
                role: "readWrite",
                db: "dbName"
            }
        ]
    }
);

Connection string

// mongo refers to the docker container
    connectionString : 'mongodb://dbUser:dbPassword@mongo:27317/dbName',  

How I initialize agenda:

class mScheduler {

    constructor() {
        if(typeof mScheduler.instance === 'object')
            return mScheduler.instance;

        this.agenda = new Agenda({ db: {address: config.connectionString, collection: 'agendaJobs', options: { useNewUrlParser: true }}, processEvery: '2 seconds', maxConcurrency : MAX_CONC});
        process.on('SIGTERM', this.endJobs);
        process.on('SIGINT', this.endJobs);
        setImmediate(async ()=> await this.agenda.start());
        mScheduler.instance = this;
        return this;
    }

someOtherMethods()
}

module.exports = new mScheduler();
1

There are 1 answers

5
Abeeb Ridwan Olumide On BEST ANSWER

try this

workaround these deprecation warnings with Mongoose provided options

const connectionOptions = {
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false,
  useUnifiedTopology: true,
  autoIndex: false
};
this.agenda = new Agenda({ db: {address: config.connectionString, collection: 'agendaJobs', options: connectionOptions}, processEvery: '2 seconds', maxConcurrency : MAX_CONC});