
    hfU                        d Z ddlZddlZddlZddlZddlZ	 ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dZdd
lmZmZmZ ddlmZ ddlmZmZmZ ddlmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 dZ9dZ:dZ; ee$e      Z< ee!e      Z=ej|                  d        Z? G d de      Z@ G d d e      ZA G d! d"e      ZB G d# d$e      ZCy# e$ r d	ZeZY w xY w)%z8Support for explicit client-side field level encryption.    N)AutoEncrypter)MongoCryptError)ExplicitEncrypter)MongoCryptOptions)MongoCryptCallbackTF)_dict_to_bsondecodeencode)CodecOptions)BinarySTANDARDUUID_SUBTYPE)	BSONError)DEFAULT_RAW_BSON_OPTIONSRawBSONDocument_inflate_bson)SON)ConfigurationErrorEncryptionErrorInvalidOperationServerSelectionTimeoutError)MongoClient)_configured_socketPoolOptions)ReadConcern)get_ssl_context)
parse_host)WriteConcern)_spawn_daemoni  
   i  )document_classuuid_representationc               #   `   K   	 d y# t         $ r  t        $ r} t        |       d} ~ ww xY ww)z2Context manager to wrap encryption related errors.N)r   	Exceptionr   )excs    U/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/encryption.py_wrap_encryption_errorsr'   E   s5     #  	 #c""#s   .	 .+&+.c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)_EncryptionIOc                     |t        j                  |      | _        nd| _        |j                  t        t        d      t        d            | _        || _        || _	        d| _
        y)z8Internal class to perform I/O on behalf of pymongocrypt.Nmajority)level)w)codec_optionsread_concernwrite_concernF)weakrefref
client_refwith_options_KEY_VAULT_OPTSr   r   key_vault_collmongocryptd_clientopts_spawned)selfclientr6   r7   r8   s        r&   __init__z_EncryptionIO.__init__S   sd     %kk&1DO"DO,99)$:6&4 : 6 #5	    c           
         |j                   }|j                  }t        |t              \  }}t	        dddddddd      }t        t        t        |      }t        ||f|      }	 |j                  |       |j                  dkD  r<|j                  |j                        }	|j                  |	       |j                  dkD  r<|j                          y# |j                          w xY w)zComplete a KMS request.

        :Parameters:
          - `kms_context`: A :class:`MongoCryptKmsContext`.

        :Returns:
          None
        NT)connect_timeoutsocket_timeoutssl_contextr   )endpointmessager   _HTTPS_PORTr   r   _KMS_CONNECT_TIMEOUTr   sendallbytes_neededrecvfeedclose)
r:   kms_contextrB   rC   hostportctxr8   conndatas
             r&   kms_requestz_EncryptionIO.kms_requestb   s     ''%%+6
ddD$dD$M+?*>'*, "4,5	LL!**Q.yy!9!9:  & **Q. JJLDJJLs   #AC C"c                     | j                         |   j                  t        |            5 }|D ]  }t        |dt              c cddd       S  	 ddd       y# 1 sw Y   yxY w)a  Get the collection info for a namespace.

        The returned collection info is passed to libmongocrypt which reads
        the JSON schema.

        :Parameters:
          - `database`: The database on which to run listCollections.
          - `filter`: The filter to pass to listCollections.

        :Returns:
          The first document from the listCollections command response as BSON.
        )filterFN)r3   list_collectionsr   r   _DATA_KEY_OPTS)r:   databaserS   cursordocs        r&   collection_infoz_EncryptionIO.collection_info{   ss     __x(99&v. : 0 	A39 A$S%@@	A 	AA	A 	A 	As   AAA#c                     d| _         | j                  j                  xs dg}|j                  | j                  j                         t        |       y)z~Spawn mongocryptd.

        Note this method is thread safe; at most one mongocryptd will start
        successfully.
        TmongocryptdN)r9   r8   _mongocryptd_spawn_pathextend_mongocryptd_spawn_argsr   )r:   argss     r&   spawnz_EncryptionIO.spawn   s@     		11B]CDII556dr=   c                    | j                   s&| j                  j                  s| j                          t	        |t
              }	 | j                  |   j                  |t
              }|j                  S # t        $ rY | j                  j                  r | j                          | j                  |   j                  |t
              }Y |j                  S w xY w)zMark a command for encryption.

        :Parameters:
          - `database`: The database on which to run this command.
          - `cmd`: The BSON command to run.

        :Returns:
          The marked command response from mongocryptd.
        r.   )
r9   r8   _mongocryptd_bypass_spawnr`   r   r   r7   commandr   raw)r:   rV   cmdinflated_cmdress        r&   mark_commandz_EncryptionIO.mark_command   s     }}TYY%H%HJJL %S*BC
	8))(3;;6 < 8C ww + 	8yy22JJL))(3;;6 < 8C ww	8s   $A4 4ACCc              #      K   | j                   j                  t        |            5 }|D ]  }|j                    	 ddd       y# 1 sw Y   yxY ww)zYields one or more keys from the key vault.

        :Parameters:
          - `filter`: The filter to pass to find.

        :Returns:
          A generator which yields the requested keys from the key vault.
        N)r6   findr   re   )r:   rS   rW   keys       r&   
fetch_keysz_EncryptionIO.fetch_keys   sO        %%of&=> 	& gg	 	 	s   %AA	AAAc                     t        |      }|j                  d      }t        |t        j                        st        d      | j                  j                  |       t        |j                  t              S )zInsert a data key into the key vault.

        :Parameters:
          - `data_key`: The data key document to insert.

        :Returns:
          The _id of the inserted data key document.
        _idzdata_key _id must be a UUID)subtype)r   get
isinstanceuuidUUID	TypeErrorr6   
insert_oner   bytesr   )r:   data_keyraw_docdata_key_ids       r&   insert_data_keyz_EncryptionIO.insert_data_key   s]     "(+kk%(+tyy19::&&w/k''>>r=   c                     t        |      S )zEncode a document to BSON.

        A document can be any mapping type (like :class:`dict`).

        :Parameters:
          - `doc`: mapping type representing a document

        :Returns:
          The encoded BSON bytes.
        )r
   )r:   rX   s     r&   bson_encodez_EncryptionIO.bson_encode   s     c{r=   c                 |    d| _         d| _        | j                  r"| j                  j                          d| _        yy)zjRelease resources.

        Note it is not safe to call this method from __del__ or any GC hooks.
        N)r3   r6   r7   rJ   r:   s    r&   rJ   z_EncryptionIO.close   s;    
 """##))+&*D# #r=   N)__name__
__module____qualname__r<   rQ   rY   r`   ri   rm   r{   r}   rJ    r=   r&   r)   r)   R   s0    2A$	8?"	+r=   r)   c                   :    e Zd Zd Zd Zd Zd Zd Zed        Z	y)
_Encrypterc                     |j                   d}nt        |j                   dt              }t        |t	        |j
                  |            | _        |j                  | _        d| _        y)a1  Encrypts and decrypts MongoDB commands.

        This class is used to support automatic encryption and decryption of
        MongoDB commands.

        :Parameters:
          - `io_callbacks`: A :class:`MongoCryptCallback`.
          - `opts`: The encrypted client's :class:`AutoEncryptionOpts`.
        NF)	_schema_mapr   rU   r   r   _kms_providers_auto_encrypter_bypass_auto_encryption_closed)r:   io_callbacksr8   
schema_maps       r&   r<   z_Encrypter.__init__   s`     #J&t'7'7OJ,\;L<-  .'+'C'C$r=   c                    | j                          |xr |j                  dd      }t        |||      }t               5  | j                  j                  ||      }t        |t              }|r||d<   |cddd       S # 1 sw Y   yxY w)ab  Encrypt a MongoDB command.

        :Parameters:
          - `database`: The database for this command.
          - `cmd`: A command document.
          - `check_keys`: If True, check `cmd` for invalid keys.
          - `codec_options`: The CodecOptions to use while encoding `cmd`.

        :Returns:
          The encrypted command to execute.
        z$clusterTimeN)_check_closedpopr   r'   r   encryptr   r   )	r:   rV   rf   
check_keysr.   cluster_timeencoded_cmdencrypted_cmdencrypt_cmds	            r&   r   z_Encrypter.encrypt   s     	 "Ccggnd&C#C]C$& 	 0088;OM'79K.:N+	 	 	s   5A==Bc                     | j                          t               5  | j                  j                  |      cddd       S # 1 sw Y   yxY w)zDecrypt a MongoDB command response.

        :Parameters:
          - `response`: A MongoDB command response as BSON.

        :Returns:
          The decrypted command response.
        N)r   r'   r   decrypt)r:   responses     r&   r   z_Encrypter.decrypt  s?     	$& 	:''//9	: 	: 	:s   A  A	c                 2    | j                   rt        d      y )Nz"Cannot use MongoClient after close)r   r   r   s    r&   r   z_Encrypter._check_closed&  s    <<"#GHH r=   c                 F    d| _         | j                  j                          y)zCleanup resources.TN)r   r   rJ   r   s    r&   rJ   z_Encrypter.close*  s    ""$r=   c                     |j                   xs | }|j                  j                  dd      \  }}||   |   }t        |j                  dt
              }t        | |||      }t        ||      S )a
  Create a _CommandEncyptor for a client.

        :Parameters:
          - `client`: The encrypted MongoClient.
          - `opts`: The encrypted client's :class:`AutoEncryptionOpts`.

        :Returns:
          A :class:`_CommandEncrypter` for this client.
        .   F)connectserverSelectionTimeoutMS)_key_vault_client_key_vault_namespacesplitr   _mongocryptd_uri_MONGOCRYPTD_TIMEOUT_MSr)   r   )r;   r8   key_vault_clientdbcollr6   r7   r   s           r&   createz_Encrypter.create/  s|      11;V,,223:D)"-d3(!!5%<> %N$6>,--r=   N)
r   r   r   r<   r   r   r   rJ   staticmethodr   r   r=   r&   r   r      s0    &4:I%
 . .r=   r   c                       e Zd ZdZdZdZy)	Algorithmz9An enum that defines the supported encryption algorithms.z+AEAD_AES_256_CBC_HMAC_SHA_512-Deterministicz$AEAD_AES_256_CBC_HMAC_SHA_512-RandomN)r   r   r   __doc__+AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic$AEAD_AES_256_CBC_HMAC_SHA_512_Randomr   r=   r&   r   r   G  s    C5 0 	/ )r=   r   c                   H    e Zd ZdZd Z	 	 ddZddZd Zd Zd Z	d	 Z
d
 Zy)ClientEncryptionz,Explicit client-side field level encryption.c                 >   t         st        d      t        |t              st	        d      || _        || _        || _        || _        |j                  dd      \  }}||   |   }t        d|dd      | _        t        | j                  t        |d            | _        y)a  Explicit client-side field level encryption.

        The ClientEncryption class encapsulates explicit operations on a key
        vault collection that cannot be done directly on a MongoClient. Similar
        to configuring auto encryption on a MongoClient, it is constructed with
        a MongoClient (to a MongoDB cluster containing the key vault
        collection), KMS provider configuration, and keyVaultNamespace. It
        provides an API for explicitly encrypting and decrypting values, and
        creating data keys. It does not provide an API to query keys from the
        key vault collection, as this can be done directly on the MongoClient.

        See :ref:`explicit-client-side-encryption` for an example.

        :Parameters:
          - `kms_providers`: Map of KMS provider options. Two KMS providers
            are supported: "aws" and "local". The kmsProviders map values
            differ by provider:

              - `aws`: Map with "accessKeyId" and "secretAccessKey" as strings.
                These are the AWS access key ID and AWS secret access key used
                to generate KMS messages.
              - `azure`: Map with "tenantId", "clientId", and "clientSecret" as
                strings. Additionally, "identityPlatformEndpoint" may also be
                specified as a string (defaults to 'login.microsoftonline.com').
                These are the Azure Active Directory credentials used to
                generate Azure Key Vault messages.
              - `gcp`: Map with "email" as a string and "privateKey"
                as `bytes` or a base64 encoded string (unicode on Python 2).
                Additionally, "endpoint" may also be specified as a string
                (defaults to 'oauth2.googleapis.com'). These are the
                credentials used to generate Google Cloud KMS messages.
              - `local`: Map with "key" as `bytes` (96 bytes in length) or
                a base64 encoded string (unicode on Python 2) which decodes
                to 96 bytes. "key" is the master key used to encrypt/decrypt
                data keys. This key should be generated and stored as securely
                as possible.

          - `key_vault_namespace`: The namespace for the key vault collection.
            The key vault collection contains all data keys used for encryption
            and decryption. Data keys are stored as documents in this MongoDB
            collection. Data keys are protected with encryption by a KMS
            provider.
          - `key_vault_client`: A MongoClient connected to a MongoDB cluster
            containing the `key_vault_namespace` collection.
          - `codec_options`: An instance of
            :class:`~bson.codec_options.CodecOptions` to use when encoding a
            value for encryption and decoding the decrypted BSON value. This
            should be the same CodecOptions instance configured on the
            MongoClient, Database, or Collection used to access application
            data.

        .. versionadded:: 3.9
        zclient-side field level encryption requires the pymongocrypt library: install a compatible version with: python -m pip install 'pymongo[encryption]'zDcodec_options must be an instance of bson.codec_options.CodecOptionsr   r   N)_HAVE_PYMONGOCRYPTr   rr   r   ru   r   r   r   _codec_optionsr   r)   _io_callbacksr   r   _encryption)r:   kms_providerskey_vault_namespacer   r.   r   r   r6   s           r&   r<   zClientEncryption.__init__R  s    n "$>? ?
 -6 > ? ? ,$7!!1+&,,S!4D)"-d3*4tL, 1- FHr=   Nc                     | j                          t               5  | j                  j                  |||      cddd       S # 1 sw Y   yxY w)a
  Create and insert a new data key into the key vault collection.

        :Parameters:
          - `kms_provider`: The KMS provider to use. Supported values are
            "aws" and "local".
          - `master_key`: Identifies a KMS-specific key used to encrypt the
            new data key. If the kmsProvider is "local" the `master_key` is
            not applicable and may be omitted.

            If the `kms_provider` is "aws" it is required and has the
            following fields::

              - `region` (string): Required. The AWS region, e.g. "us-east-1".
              - `key` (string): Required. The Amazon Resource Name (ARN) to
                 the AWS customer.
              - `endpoint` (string): Optional. An alternate host to send KMS
                requests to. May include port number, e.g.
                "kms.us-east-1.amazonaws.com:443".

            If the `kms_provider` is "azure" it is required and has the
            following fields::

              - `keyVaultEndpoint` (string): Required. Host with optional
                 port, e.g. "example.vault.azure.net".
              - `keyName` (string): Required. Key name in the key vault.
              - `keyVersion` (string): Optional. Version of the key to use.

            If the `kms_provider` is "gcp" it is required and has the
            following fields::

              - `projectId` (string): Required. The Google cloud project ID.
              - `location` (string): Required. The GCP location, e.g. "us-east1".
              - `keyRing` (string): Required. Name of the key ring that contains
                the key to use.
              - `keyName` (string): Required. Name of the key to use.
              - `keyVersion` (string): Optional. Version of the key to use.
              - `endpoint` (string): Optional. Host with optional port.
                Defaults to "cloudkms.googleapis.com".

          - `key_alt_names` (optional): An optional list of string alternate
            names used to reference a key. If a key is created with alternate
            names, then encryption may refer to the key by the unique alternate
            name instead of by ``key_id``. The following example shows creating
            and referring to a data key by alternate name::

              client_encryption.create_data_key("local", keyAltNames=["name1"])
              # reference the key with the alternate name
              client_encryption.encrypt("457-55-5462", keyAltName="name1",
                                        algorithm=Algorithm.Random)

        :Returns:
          The ``_id`` of the created data key document as a
          :class:`~bson.binary.Binary` with subtype
          :data:`~bson.binary.UUID_SUBTYPE`.
        )
master_keykey_alt_namesN)r   r'   r   create_data_key)r:   kms_providerr   r   s       r&   r   z ClientEncryption.create_data_key  sM    r 	$& 	-##33+ 4 -	- 	- 	-s   AAc                 N   | j                          |.t        |t              r|j                  t        k(  st        d      t        d|i| j                        }t               5  | j                  j                  ||||      }t        |      d   cddd       S # 1 sw Y   yxY w)a  Encrypt a BSON value with a given key and algorithm.

        Note that exactly one of ``key_id`` or  ``key_alt_name`` must be
        provided.

        :Parameters:
          - `value`: The BSON value to encrypt.
          - `algorithm` (string): The encryption algorithm to use. See
            :class:`Algorithm` for some valid options.
          - `key_id`: Identifies a data key by ``_id`` which must be a
            :class:`~bson.binary.Binary` with subtype 4 (
            :attr:`~bson.binary.UUID_SUBTYPE`).
          - `key_alt_name`: Identifies a key vault document by 'keyAltName'.

        :Returns:
          The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
        Nz2key_id must be a bson.binary.Binary with subtype 4vrb   )key_idkey_alt_name)r   rr   r   rp   r   ru   r
   r   r'   r   r   r	   )r:   value	algorithmr   r   rX   encrypted_docs          r&   r   zClientEncryption.encrypt  s    $ 	66*,.DF F c5\1D1DE$& 	. ,,44YvL 5 JM-(-	. 	. 	.s   $-BB$c                 :   | j                          t        |t              r|j                  dk(  st	        d      t               5  t        d|i      }| j                  j                  |      }t        || j                        d   cddd       S # 1 sw Y   yxY w)zDecrypt an encrypted value.

        :Parameters:
          - `value` (Binary): The encrypted value, a
            :class:`~bson.binary.Binary` with subtype 6.

        :Returns:
          The decrypted BSON value.
           z<value to decrypt must be a bson.binary.Binary with subtype 6r   rb   N)r   rr   r   rp   ru   r'   r
   r   r   r	   r   )r:   r   rX   decrypted_docs       r&   r   zClientEncryption.decrypt  s     	5&)emmq.@NP P %& 	B#u&C ,,44S9M-(,(;(;==@B	B 	B 	Bs   ABBc                     | S Nr   r   s    r&   	__enter__zClientEncryption.__enter__  s    r=   c                 $    | j                          y r   )rJ   )r:   exc_typeexc_valexc_tbs       r&   __exit__zClientEncryption.__exit__  s    

r=   c                 2    | j                   t        d      y )Nz"Cannot use closed ClientEncryption)r   r   r   s    r&   r   zClientEncryption._check_closed  s    #"#GHH $r=   c                     | j                   rC| j                   j                          | j                  j                          d| _         d| _        yy)aE  Release resources.

        Note that using this class in a with-statement will automatically call
        :meth:`close`::

            with ClientEncryption(...) as client_encryption:
                encrypted = client_encryption.encrypt(value, ...)
                decrypted = client_encryption.decrypt(encrypted)

        N)r   rJ   r   r   s    r&   rJ   zClientEncryption.close  sF     $$&""$!%D#D	 r=   )NN)r   r   r   r   r<   r   r   r   r   r   r   rJ   r   r=   r&   r   r   O  s;    6KHZ 8<&*=-~.>B*I$r=   r   )Dr   
contextlibos
subprocessrs   r1   pymongocrypt.auto_encrypterr   pymongocrypt.errorsr   pymongocrypt.explicit_encrypterr   pymongocrypt.mongocryptr   pymongocrypt.state_machiner   r   ImportErrorobjectbsonr   r	   r
   bson.codec_optionsr   bson.binaryr   r   r   bson.errorsr   bson.raw_bsonr   r   r   bson.sonr   pymongo.errorsr   r   r   r   pymongo.mongo_clientr   pymongo.poolr   r   pymongo.read_concernr   pymongo.ssl_supportr   pymongo.uri_parserr   pymongo.write_concernr   pymongo.daemonr   rD   rE   r   rU   r5   contextmanagerr'   r)   r   r   r   r   r=   r&   <module>r      s   ?  	   	 93A9=
 / . +' ' "* * 9 9 - 8 , / ) . (   ShO o3;= 	# 	#V+& V+rY. Y.x0 0\$v \$c	    s    C9 9	DD