Skip to content

MongoDB connection authentication failing again (previously resolved in #1510) #1736

@FukayaTakashi

Description

@FukayaTakashi

IoT Agent Node Lib version the issue has been seen with

4.9.0

Bound or port used (API interaction)

Other

NGSI version

Other

Are you running a container?

Yes, I am using a contaner (Docker, Kubernetes...)

Image type

distroless

Expected behaviour you didn't see

Perform user authentication in MongoDB with using environment variable "IOTA_MONGO_USER" and "IOTA_MONGO_PASSWORD"

Unexpected behaviour you saw

Unable to connect to MongoDB due to the following error:
"MongoParseError: credentials must be an object with 'username' and 'password' properties."

This occurs because MongoDB expects the credentials to be provided as options.auth.username and options.auth.password.
However, the current implementation in lib/model/dbConn.js uses options.auth.user, which does not conform to the expected format.

Reference: MongoDB Node.js Driver Auth Interface

To resolve this, I suggest modifying the code to use options.user and options.pass instead. Mongoose will automatically populate options.auth correctly.

Current code:

options.auth = {
    user: currentConfig.mongodb.user,
    password: currentConfig.mongodb.password
};

Suggested fix:

options.user = currentConfig.mongodb.user;
options.pass = currentConfig.mongodb.password;

Steps to reproduce the problem

docker run --name mongodb -d -e MONGO_INITDB_ROOT_USERNAME=test -e MONGO_INITDB_ROOT_PASSWORD=test -p 27017:27017 mongo:8.0.0

Configs

environment:
    - "IOTA_CB_HOST=orion"
    - "IOTA_CB_PORT=1026"
    - "IOTA_NORTH_PORT=4041"
    - "IOTA_REGISTRY_TYPE=mongodb"
    - "IOTA_MONGO_HOST=mongodb"
    - "IOTA_MONGO_USER=test"
    - "IOTA_MONGO_PASSWORD=test"
    - "IOTA_MONGO_PORT=27017"
    - "IOTA_MONGO_DB=iotagent-json"
    - "IOTA_HTTP_PORT=7896"
    - "IOTA_PROVIDER_URL=http://iot-agent:4041"

Log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions