HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/mongodb.cpython-310.pyc
o

�N�g�v�@s�dZddlZddlZddlZddlmZddlm	Z	zddl
Z
dZWney-dZYnwe�
e�Zdd�Z	d7d
d�Zdd
�Zd8dd�Zd8dd�Zd8dd�Zdd�Z	d7dd�Z	d7dd�Z	d7dd�Z	d7dd�Z								d9dd�Z	d7d d!�Z	d8d"d#�Z	d8d$d%�Z	d8d&d'�Z							d7d(d)�Z 							d7d*d+�Z!							d7d,d-�Z"							d7d.d/�Z#							d7d0d1�Z$								d:d2d3�Z%								4	d;d5d6�Z&dS)<a�
Module to provide MongoDB functionality to Salt

:configuration: This module uses PyMongo, and accepts configuration details as
    parameters as well as configuration settings::

        mongodb.host: 'localhost'
        mongodb.port: 27017
        mongodb.user: ''
        mongodb.password: ''

    This data can also be passed into pillar. Options passed into opts will
    overwrite options passed into pillar.
�N)�get_error_message)�VersionTFcCstrdSdS)z7
    Only load this module if pymongo is installed
    Zmongodb)FzTThe mongodb execution module cannot be loaded: the pymongo library is not available.)�HAS_MONGODB�rr�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/mongodb.py�__virtual__!sr�admincCs�|stdd�}|stdd�}|stdd�}|s tdd�}|s$|}z#tj||d�}tj�||�}|rB|rE|j|||d�W|SW|SW|StjjyYt�	d|�Yd	Sw)
zv
    Returns a tuple of (user, host, port) with config, pillar, or default
    values assigned to missing values.
    z
config.optionzmongodb.userzmongodb.passwordzmongodb.hostzmongodb.port)�host�port)�sourcezError connecting to database %sF)
Z__salt__�pymongoZMongoClient�database�DatabaseZauthenticate�errors�PyMongoError�log�error)�user�passwordr	r
r
�authdb�conn�mdbrrr�_connect/s0����rc
CsLzt|t�rtjj�|�}W|SW|Sty%}zt�d|��d}~ww)zF
    Potentially interprets a string as JSON for usage with mongo
    zCould not parse objects: %sN)	�
isinstance�str�salt�utils�json�loads�
ValueErrorrr)�objects�errrrr�_to_dictMs
����r"c
Csjt|||||d�}|s
dSz
t�d�|��WStjjy4}zt�|�t|�WYd}~Sd}~ww)a
    List all MongoDB databases

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.db_list <user> <password> <host> <port>
    �r�#Failed to connect to mongo databasezListing databasesN)	rr�infoZlist_database_namesrrrrr)rrr	r
rrr!rrr�db_list[s


��r&cCs(t|||||d�}t|t�rdS||vS)a]
    Checks if a database exists in MongoDB

    name
        The name of the database to check for.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.db_exists <name> <user> <password> <host> <port>
    r#F)r&rr)�namerrr	r
rZdbsrrr�	db_exists�s
r(c
Csvt|||||d�}|s
dSzt�d|�|�|�WdStjjy:}zt�d||�t|�WYd}~Sd}~ww)aM
    Remove a MongoDB database

    name
        The name of the database to remove.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.db_remove <name> <user> <password> <host> <port>
    r#r$zRemoving database %sz*Removing database %s failed with error: %sNT)	rrr%Z
drop_databaserrrrr)r'rrr	r
rrr!rrr�	db_remove�s���r)cCs|�d�dS)NZ	buildInfo�version)�command)rrrr�_version�sr,c

Cs�t|||||d�}|sd|�d|��}t�|�d|fSztj�||�}t|�WStjjyF}	zt�d|	�t	|	�WYd}	~	Sd}	~	ww)a
    Get MongoDB instance version

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.version <user> <password> <host> <port> <database>
    r#�&Failed to connect to MongoDB database �:F�#Listing users failed with error: %sN)
rrrrr
rr,rrr)
rrr	r
r
rr�err_msgrr!rrrr*�s

��r*c
Cs�t|||||d�}|sd|�d|��}t�|�d|fStj�||�}	z	|	�d|�dWStjjyL}
zt�d|
�dt	|
�fWYd}
~
Sd}
~
ww)	a�
    Get single user from MongoDB

    name
        The name of the user to find.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    database
        The MongoDB database to use when looking for the user. Default is ``admin``.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_find <name> <user> <password> <host> <port> <database> <authdb>
    r#r-r.F�	usersInfo�usersr/N)
rrrrr
rr+rrr)r'rrr	r
r
rrr0rr!rrr�	user_find�s!
��r3c
Cs�t|||||d�}|s
dSzMt�d�tj�||�}g}t|�}	t|	�td�krA|�d�dD]}|�	|d|dd	��q/|WS|j
j��D]}|�	|d|�
d
d�d��qG|WStjjyx}
zt�d
|
�t|
�WYd}
~
Sd}
~
ww)a~
    List users of a MongoDB database

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    database
        The MongoDB database to use when listing users. Default is ``admin``.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_list <user> <password> <host> <port> <database>
    r#r$z
Listing usersz2.6r1r2r�roles)rr4�readOnly�None)rr5r/N)rrr%rr
rr,rr+�append�systemr2�find�getrrrr)rrr	r
r
rrr�outputZmongodb_versionr!rrr�	user_list*s,
����r<cCsFt||||||�}t|t�rdS|D]}|t|��d�kr dSqdS)a�
    Checks if a user exists in MongoDB

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    database
        The MongoDB database to use when checking if the user exists. Default is ``admin``.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_exists <name> <user> <password> <host> <port> <database>
    r$rTF)r<rr�dictr:)r'rrr	r
r
rr2rrr�user_existsbs
�r>c	

s�t|||||d�}	|	s
dS|sg}�fdd�|D�}
zt�d|�tj�|	��}|jd|||
d�WdStjjyO}zt�	d||�WYd	}~d
Sd	}~ww)au
    Create a MongoDB user

    name
        The name of the user to create.

    passwd
        The password for the user that is being created.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    database
        The MongoDB database to use when checking if the user exists. Default is ``admin``.

    authdb
        The MongoDB database to use for authentication. Default is None.

    roles
        The roles that should be associated with the user. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_create <user_name> <user_password> <roles> <user> <password> <host> <port> <database>
    r#r$csg|]}|�d��qS)��roleZdbr)�.0Z_role�r
rr�
<listcomp>�szuser_create.<locals>.<listcomp>zCreating user %sZ
createUser)�pwdr4z&Creating user %s failed with error: %sNFT�
rrr%rr
rr+rrr)
r'�passwdrrr	r
r
rr4rZ_rolesrr!rrBr�user_create�s /���rGc

Cs|t||||�}|sdSzt�d|�tj�||�}|�d|�WdStjjy=}	zt�	d||	�WYd}	~	dSd}	~	ww)a_
    Remove a MongoDB user

    name
        The name of the user that should be removed.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.user_remove <name> <user> <password> <host> <port> <database>
    r$zRemoving user %sZdropUserz&Removing user %s failed with error: %sNFTrE)
r'rrr	r
r
rrrr!rrr�user_remove�s���rHc
Cs�zt|�}Wn
tyYdSwt||||||�}t|t�r!dS|D]+}|t|��d�krN|D]}	t|	t�s<|	|d�}	|	t|��dg�vrJdSq0dSq#dS)a

    Checks if a user of a MongoDB database has specified roles

    name
        The name of the user to check for the specified roles.

    roles
        The roles to check are associated with the specified user.

    database
        The database to check has the specified roles for the specified user.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Examples:

    .. code-block:: bash

        salt '*' mongodb.user_roles_exists johndoe '["readWrite"]' dbname admin adminpwd localhost 27017

    .. code-block:: bash

        salt '*' mongodb.user_roles_exists johndoe '[{"role": "readWrite", "db": "dbname" }, {"role": "read", "db": "otherdb"}]' dbname admin adminpwd localhost 27017
    �Roles provided in wrong formatr$rr?r4FT)r"�	Exceptionr<rrr=r:)
r'r4r
rrr	r
rr2r@rrr�user_roles_exists�s&(�


��	rKc
C�t|||||d�}|s
dSzt|�}Wn
tyYdSwzt�d||�tj�||�}	|	jd||d�Wd	Stj	j
yW}
zt�d|||
�t|
�WYd}
~
Sd}
~
ww)
a�
    Grant one or many roles to a MongoDB user

    name
        The user to grant the specified roles to.

    roles
        The roles to grant to the specified user.

    database
        The database to great the roles against for the specified user.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Examples:

    .. code-block:: bash

        salt '*' mongodb.user_grant_roles johndoe '["readWrite"]' dbname admin adminpwd localhost 27017

    .. code-block:: bash

        salt '*' mongodb.user_grant_roles janedoe '[{"role": "readWrite", "db": "dbname" }, {"role": "read", "db": "otherdb"}]' dbname admin adminpwd localhost 27017
    r#r$rIzGranting roles %s to user %sZgrantRolesToUser�r4z2Granting roles %s to user %s failed with error: %sNT�
rr"rJrr%rr
rr+rrrr�r'r4r
rrr	r
rrrr!rrr�user_grant_roles9s((�����rPc
CrL)
a�
    Revoke one or many roles to a MongoDB user

    user
        The user to connect to MongoDB as. Default is None.

    roles
        The roles to revoke from the specified user.

    database
        The database to revoke the roles from for the specified user.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Examples:

    .. code-block:: bash

        salt '*' mongodb.user_revoke_roles johndoe '["readWrite"]' dbname admin adminpwd localhost 27017

    .. code-block:: bash

        salt '*' mongodb.user_revoke_roles janedoe '[{"role": "readWrite", "db": "dbname" }, {"role": "read", "db": "otherdb"}]' dbname admin adminpwd localhost 27017
    r#r$rIzRevoking roles %s from user %sZrevokeRolesFromUserrMz4Revoking roles %s from user %s failed with error: %sNTrNrOrrr�user_revoke_rolesws(%�����rQc

C�t||||||�}|s
dSzt�d||�tj�||�}|�|�WdStjjyA}	zt�	d|||	�|	WYd}	~	Sd}	~	ww)a�
    .. versionadded:: 3006.0

    Create a collection in the specified database.

    collection
        The name of the collection to create.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.collection_create mycollection <user> <password> <host> <port> <database>

    r$zCreating %s.%s�.Creating collection %r.%r failed with error %sNT)
rrr%rr
rZcreate_collectionrrr�
�
collectionrrr	r
r
rrrr!rrr�collection_create��'����rVc

CrR)a�
    .. versionadded:: 3006.0

    Drop a collection in the specified database.

    collection
        The name of the collection to drop.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.collection_drop mycollection <user> <password> <host> <port> <database>

    r$zDropping %s.%srSNT)
rrr%rr
rZdrop_collectionrrrrTrrr�collection_drop�rWrXc

Cspt||||||�}|s
dSztj�||�}|��}W|Stjjy7}	zt�d|	�|	WYd}	~	Sd}	~	ww)an
    .. versionadded:: 3006.0

    List the collections available in the specified database.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.collections_list mycollection <user> <password> <host> <port> <database>

    r$z(Listing collections failed with error %sN)	rrr
rZlist_collection_namesrrrr)
rrr	r
r
rrr�retr!rrr�collections_list s#
���rZc

Cs�t||||||�}|s
dSzt|�}Wnty'}	z|	WYd}	~	Sd}	~	wwzt�d|||�tj�||�}
t|
|�}|�	|�}|j
WStjjyb}	zt�
d||	�|	WYd}	~	Sd}	~	ww)a"
    Insert an object or list of objects into a collection

    objects
        The objects to insert into the collection, should be provided as a list.

    collection
        The collection to insert the objects into.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.insert '[{"foo": "FOO", "bar": "BAR"}, {"foo": "BAZ", "bar": "BAM"}]' mycollection <user> <password> <host> <port> <database>

    r$NzInserting %r into %s.%sz)Inserting objects %r failed with error %s)rr"rJrr%rr
r�getattrZinsert_manyZacknowledgedrrr)
r rUrrr	r
r
rrr!r�col�idsrrr�insertPs()��

��r^cCs�t||||||�}|s
dSt|�}t�d|�}	t|	�dkr 	dS|	dd|	d<d|	d|	d<g}
|	D]#}zt|�}|
�|�Wq4tyW}z
|WYd	}~Sd	}~ww|
d}
|
d}t||	d||||||�}t	|t
�std
S|sxdSt|�dkr�dSz%t�d
|
||�t
j�||�}t||�}|�|
d|i�}|j}|�d�WSt
jjy�}zt�d||�|WYd	}~Sd	}~ww)a�
    Update an object into a collection
    http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_one

    .. versionadded:: 2016.11.0

    objects
        The objects to update in the collection, should be provided as a list.

    collection
        The collection to insert the objects into.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.update_one '{"_id": "my_minion"} {"bar": "BAR"}' mycollection <user> <password> <host> <port> <database>

    r$z}\s+{�zMYour request does not contain a valid '{_"id": "my_id"} {"my_doc": "my_val"}'r�}�{�NzGThe find result is not well formatted. An error appears; cannot update.z9Did not find any result. You should try an insert before.z1Too many results. Please try to be more specific.zUpdating %r into %s.%sz$setz objects updatedz'Updating object %s failed with error %s)rr�re�split�lenr"r7rJr9r�listrr%rr
rr[�
update_oneZmodified_countrrr)r rUrrr	r
r
rrZobjsZdocument�objr!Z	_id_fieldZ_update_docZtest_frr\r]Znb_modrrrrg�sP,���

��rgc
Cst||||||�}|s
dSzt|�}Wnty'}	z|	WYd}	~	Sd}	~	wwzBt�d||�tj�||�}
t|
|�}t	|t
�r\g}|D]}
|�|
�}dd�|D�}|�|�qE|WS|�|�}dd�|D�}|WStj
jy�}	zt�d|	�|	WYd}	~	Sd}	~	ww)a�
    Find an object or list of objects in a collection

    collection
        The collection to find the objects in.

    query
        The query to use when locating objects in the collection.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.find mycollection '[{"foo": "FOO", "bar": "BAR"}]' <user> <password> <host> <port> <database>

    r$NzSearching for %r in %scS�g|]}|�qSrr�rAZ_resrrrrC$�zfind.<locals>.<listcomp>cSrirrrjrrrrC(rkz'Searching objects failed with error: %s)rr"rJrr%rr
rr[rrfr9�extendrrr)rU�queryrrr	r
r
rrr!rr\rY�_query�resZ_retrrrr9�s8)��



���r9rbc	
Cs*t||||||�}	|	s
dSzt|�}Wnty)}
z
t|
�WYd}
~
Sd}
~
wwzJt�d||�tj�|	|�}t	||�}d}
t
|t�r]|D]}td|�D]}|�
|�}|
|j7}
qNqGntd|�D]}|�
|�}|
|j7}
qb|
�d�WStjjy�}
zt�dt|
��t|
�WYd}
~
Sd}
~
ww)a�
    Remove an object or list of objects from a collection

    collection
        The collection to remove objects from based on the query.

    query
        Query to determine which objects to remove.

    user
        The user to connect to MongoDB as. Default is None.

    password
        The password to use to connect to MongoDB as.  Default is None.

    host
        The host where MongoDB is running. Default is None.

    port
        The host where MongoDB is running. Default is None.

    database
        The database where the collection is.

    w
        The number of matches to remove from the collection.

    authdb
        The MongoDB database to use for authentication. Default is None.

    CLI Example:

    .. code-block:: bash

        salt '*' mongodb.remove mycollection '[{"foo": "FOO", "bar": "BAR"}, {"foo": "BAZ", "bar": "BAM"}]' <user> <password> <host> <port> <database>

    r$NzRemoving %r from %srz objects removedz&Removing objects failed with error: %s)rr"rJ�_get_error_messagerr%rr
rr[rrf�rangeZ
delete_one�
deleted_countrrr)rUrmrrr	r
r
�wrrr!rr\rrrn�countrorrr�remove/s<0��


��
��ru)NNNNrN)NNNNN)NNNNrNN)NNNNNrN)NNNNNrrbN)'�__doc__�loggingrcZsalt.utils.jsonrZsalt.exceptionsrrpZsalt.utils.versionsrrr�ImportError�	getLogger�__name__rrrr"r&r(r)r,r*r3r<r>rGrHrKrPrQrVrXrZr^rgr9rurrrr�<module>s��

�

%
$*
�*
�0
�9
�-
�C
�-
�@
�?
�=
�9
�8
�3
�@
�_
�G�