
    a-h$                     ^   d Z ddlZddlZddl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 erdd	lmZ 	 dd
lmZ ddlmZmZ ddlmZ ddlmZ 	 ddlmZmZ ddlmZ dZ dZ!dZ"dZ#dZ$ G d de      Z%y# e$ r  ej6                  d      dw xY w# e$ r  ej6                  d      dw xY w)zOCI Authentication Plugin.    N)	b64encode)Path)TYPE_CHECKINGAnyDictOptional   )errors)logger)MySQLSocket)UnsupportedAlgorithm)hashesserialization)padding)PRIVATE_KEY_TYPESz'Package 'cryptography' is not installed)config
exceptionszGPackage 'oci' (Oracle Cloud Infrastructure Python SDK) is not installed   )MySQLAuthPluginMySQLOCIAuthPlugini (  z0Ephemeral security token is too large (10KB max)zGEphemeral security token file ('security_token_file') could not be readzKOCI configuration file does not contain a 'fingerprint' or 'key_file' entryc                      e Zd ZU dZdZeed<   dZeed<   e	j                  Zeed<   ededeeef   d	efd
       Zeded	efd       Zd	eeef   fdZed	efd       Zed	efd       Zdeded	ee   fdZdddeded	efdZy)r   z2Implement the MySQL OCI IAM authentication plugin.NcontextDEFAULToci_config_profileoci_config_file	signature
oci_configreturnc                    t        |       }|d   |j                         d}|j                  d      r^	 t        |d         }|j	                         j
                  t        kD  rt        j                  t              |j                  d      |d<   t        j                  |d	      S # t        t        f$ r}t        j                  t              |d}~ww xY w)
a=  Prepare client's authentication response

        Prepares client's authentication response in JSON format
        Args:
            signature (bytes):  server's nonce to be signed by client.
            oci_config (dict): OCI configuration object.

        Returns:
            str: JSON string with the following format:
                 {"fingerprint": str, "signature": str, "token": base64.base64.base64}

        Raises:
            ProgrammingError: If the ephemeral security token file can't be open or the
                              token is too large.
        fingerprint)r    r   security_token_filezutf-8)encodingtokenN),:)
separators)r   decodegetr   statst_sizeOCI_SECURITY_TOKEN_MAX_SIZEr
   ProgrammingErrorOCI_SECURITY_TOKEN_TOO_LARGE	read_textOSErrorUnicodeError%OCI_SECURITY_TOKEN_FILE_NOT_AVAILABLEjsondumps)r   r   signature_64auth_responser!   errs         w/var/www/core.comfia.cic-ware.com/crm/lib/python3.12/site-packages/mysql/connector/plugins/authentication_oci_client.py_prepare_auth_responsez)MySQLOCIAuthPlugin._prepare_auth_responseQ   s    " !+%m4%,,.
 >>/0	&*:6K+L&M#&++-558SS 112NOO)<)F)FPW)F)Xg&
 zz-J??	 \* --9s   AB( (C7CCkey_pathc                 H   	 t        t        j                  j                  |       d      5 }t	        j
                  |j                         d      }ddd       |S # 1 sw Y   S xY w# t        t        t        t        f$ r }t        j                  d|  d|       d}~ww xY w)z+Get the private_key form the given locationrbN)passwordz2An error occurred while reading the API_KEY from "z": )openospath
expanduserr   load_pem_private_keyread	TypeErrorr/   
ValueErrorr   r
   r,   )r9   key_fileprivate_keyr6   s       r7   _get_private_keyz#MySQLOCIAuthPlugin._get_private_keyy   s    
	bgg((2D9 X+@@MMO!   7J0DE 	)):S' 	s4   )A( &AA( A% A( %A( (B!BB!c                    g }d d d}i }	 t        j                  | j                  xs t         j                  | j                  xs d      }|j                         D ]+  \  }}	 ||   r  |||         s|j                  d| d       - 	 |r%t        j                   d	| j                   d
|       |S # t        $ r |j                  d|        Y ww xY w# t        j                  t        j                  t        j                  t        j                  t        j                  f$ r$}|j                  t        |             Y d}~d}~ww xY w)z=Get a valid OCI config from the given configuration file pathc                     t        |       dkD  S )N    )lenxs    r7   <lambda>z:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>   s    c!frk     c                 z    t         j                  j                  t         j                  j                  |             S )N)r>   r?   existsr@   rL   s    r7   rN   z:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>   s"    277>>"''2D2DQ2G#H rO   )r    rE   r   zParameter "z" is invalidzDoes not contain parameter NzInvalid oci-config-file: z. Errors found: )r   	from_filer   DEFAULT_LOCATIONr   itemsappendKeyErrorr   ConfigFileNotFoundInvalidConfigInvalidKeyFilePathInvalidPrivateKeyProfileNotFoundstrr
   r,   )self
error_listreq_keysr   req_key	req_valuer6   s          r7   _get_valid_oci_configz(MySQLOCIAuthPlugin._get_valid_oci_config   sa   
1H

 &(
	())$$?(?(?''49J '/nn&6 O"O!'*9Z=P3Q"))Ky*MN	O" ))+D,@,@+A B!!+. 
 %   O%%(CG9&MNO ))$$))((&&
 	( c#h''	(s=   AC $%B5	C 5CC CC AE(EEc                      y)zPlugin official name.authentication_oci_client r]   s    r7   namezMySQLOCIAuthPlugin.name   s     +rO   c                      y)z'Signals whether or not SSL is required.Fre   rf   s    r7   requires_sslzMySQLOCIAuthPlugin.requires_ssl   s     rO   	auth_datakwargsc                 h   t        j                  d|t        |             | j                         }| j	                  |d         }|j                  |t        j                         t        j                               }| j                  ||      }t        j                  d|       |j                         S )z-Prepare authentication string for the server.zserver nonce: %s, len %drE   zauthentication response: %s)r   debugrK   rb   rG   signr   PKCS1v15r   SHA256r8   encode)r]   rj   rk   r   rF   r   r5   s          r7   r5   z MySQLOCIAuthPlugin.auth_response   s    /C	NK//1
++Jz,BC$$Y0@0@0BFMMOT	33IzJ2MB##%%rO   sockr   c                    |j                  dd      | _        |j                  dt        j                        | _        t        j                  d| j                          | j                  |fi |}|t        j                  d      t        j                  d|t        |             |j                  |       |j                         }t        j                  d|       t        |      S )aS  Handles server's `auth switch request` response.

        Args:
            sock: Pointer to the socket connection.
            auth_data: Plugin provided data (extracted from a packet
                       representing an `auth switch request` response).
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked. The parameters defined here will override the ones
                    defined in the auth plugin itself.

        Returns:
            packet: Last server's response after back-and-forth
                    communication.
        r   r   r   z!# oci configuration file path: %szGot a NULL auth responsez# request: %s size: %sz# server response packet: %s)r(   r   r   rS   r   r   rm   r5   r
   InterfaceErrorrK   sendrecvbytes)r]   rr   rj   rk   responsepackets         r7   auth_switch_responsez'MySQLOCIAuthPlugin.auth_switch_response   s    "  &zz*;YG"(** &"9"9#
 	8$:N:NO%4%%i:6:''(BCC-xXG		(3V<V}rO   )__name__
__module____qualname____doc__r   r   __annotations__r   r\   r   rS   r   staticmethodrw   r   r8   r   rG   rb   propertyrg   boolri   r   r5   rz   re   rO   r7   r   r   J   s   <GS''!22OS2%@% %@T#s(^ %@PS %@ %@N 3 +<   &tCH~ &P +c + + d  &u & & &!!!.3!?B!	!rO   )&r~   r2   r>   base64r   pathlibr   typingr   r   r   r    r
   r   networkr   cryptography.exceptionsr   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   /cryptography.hazmat.primitives.asymmetric.typesr   ImportErrorr,   ocir   r   r   AUTHENTICATION_PLUGIN_CLASSr+   r-   r1   OCI_PROFILE_MISSING_PROPERTIESr   re   rO   r7   <module>r      s   > !  	   5 5  %W<DAQ& 2 ' Q M & R 
` `/  W
!&
!
!"K
LRVVW
  
!&
!
!Qs   A7 B 7BB,