salt icon indicating copy to clipboard operation
salt copied to clipboard

[Bug]: mongodb module not working with pymongo v4

Open edualb opened this issue 10 months ago • 3 comments

Context

I was migrating my mongodb server to mongodb v8. I am using the native mongodb module where there is a dependency for pymongo. In order to work with mongodb v8, the minimal version of pymongo would be v4.9. When I have tried to execute I faced an issue on the _connect function of the mongodb module.

P.S.: missed the log error since it was some days ago, sorry for it.

How to reproduce

Install any pymongo v4 in onedir of salt and try to create any user using the mongodb_user.present state using the fields password and user:

mongodb_user_myusername:
  mongodb_user.present:
    - name: 'myuser'
    - passwd: 'mypasswd'
    - host: 'localhost'
    - user: 'myusername' // <- it must be here to reproduce the bug
    - password: 'mypassword' // <- it must be here to reproduce the bug
    - authdb: 'admin'
    - database: 'admin'
    - roles:
      - readWrite
      - dbOwner

In your salt master, execute:

$ salt {{ minion target }} state.apply

Root cause

On pymongo v4 the function MongoClient.Database.authenticate was removed and it is still in use by the mongodb._connect function:

def _connect(
    user=None, password=None, host=None, port=None, database="admin", authdb=None
):
    """
    Returns a tuple of (user, host, port) with config, pillar, or default
    values assigned to missing values.
    """
    if not user:
        user = __salt__["config.option"]("mongodb.user")
    if not password:
        password = __salt__["config.option"]("mongodb.password")
    if not host:
        host = __salt__["config.option"]("mongodb.host")
    if not port:
        port = __salt__["config.option"]("mongodb.port")
    if not authdb:
        authdb = database

    try:
        conn = pymongo.MongoClient(host=host, port=port)
        mdb = pymongo.database.Database(conn, database)
        if user and password:
            mdb.authenticate(user, password, source=authdb)
    except pymongo.errors.PyMongoError:
        log.error("Error connecting to database %s", database)
        return False

    return conn

Workaround

I found a workaround to allow you to work with mongodb v8 and pymongo v4.9+:

The MongoClient allows the use of mongodb URI in the host parameter. With that in mind, you probably have an idea what we can do:

mongodb_user_myusername:
  mongodb_user.present:
    - name: 'myuser'
    - passwd: 'mypasswd'
    - host: 'mongodb://myusername:mypassword@localhost'
    - authdb: 'admin'
    - database: 'admin'
    - roles:
      - readWrite
      - dbOwner

With this state, we are bypassing the MongoClient.Database.authenticate function since the user and the password are not being set but I suppose it would not be the correct way to use the mongodb module.

Type of salt install

Official deb

Major version

3006.x

What supported OS are you seeing the problem on? Can select multiple. (If bug appears on an unsupported OS, please open a GitHub Discussion instead)

debian-12

salt --versions-report output

Salt Version:
          Salt: 3006.16

Python Version:
        Python: 3.10.18 (main, Sep  5 2025, 22:48:51) [GCC 11.2.0]

Dependency Versions:
          cffi: 1.14.6
      cherrypy: 18.10.0
  cryptography: 42.0.5
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 3.1.6
       libgit2: Not Installed
  looseversion: 1.0.2
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 24.0
     pycparser: 2.21
      pycrypto: Not Installed
  pycryptodome: 3.19.1
        pygit2: Not Installed
  python-gnupg: 0.4.8
        PyYAML: 6.0.1
         PyZMQ: 23.2.0
        relenv: 0.20.6
         smmap: Not Installed
       timelib: 0.3.0
       Tornado: 4.5.3
           ZMQ: 4.3.4

System Versions:
          dist: debian 12 bookworm
        locale: utf-8
       machine: x86_64
       release: 6.1.0-39-amd64
        system: Linux
       version: Debian GNU/Linux 12 bookworm

edualb avatar Jan 26 '26 09:01 edualb

@edualb let me work on this issue. please assign it to me

smartcoder0777 avatar Jan 27 '26 09:01 smartcoder0777

@smartcoder0777 I think I don't have permission to assign you to it, but maybe it is possible to contribute by taking a look into the Contributing page.

edualb avatar Jan 27 '26 10:01 edualb

@edualb I just opened a PR: https://github.com/saltstack/salt/pull/68662 Would you review it please?

smartcoder0777 avatar Jan 27 '26 13:01 smartcoder0777