zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
U
£ý°d3ã@sdZddlZddlZddlmZddlmZddlmZm    Z    m
Z
ddl m Z ddl m Z z8dd    lmZdd
lmZmZdd lmZdd lmZWn ek
r°e  d ¡d‚YnXzddlmZmZWn ek
ræe  d¡d‚YnXddl mZdZdZdZdZ dZ!Gdd„deƒZ"dS)zOCI Authentication Plugin.éN)Ú    b64encode)ÚPath)ÚAnyÚDictÚOptionalé)Úerrors)Úlogger)ÚUnsupportedAlgorithm)ÚhashesÚ serialization)Úpadding)ÚPRIVATE_KEY_TYPESz'Package 'cryptography' is not installed)ÚconfigÚ
exceptionszGPackage 'oci' (Oracle Cloud Infrastructure Python SDK) is not installedé)ÚBaseAuthPluginÚ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@s®eZdZUdZdZeed<dZeed<dZ    e
ed<dZ eed    <e j Zeed
<eeeee
fed œd d „ƒZeeedœdd„ƒZeee
fdœdd„Zdeeedœdd„ZdS)rz2Implement the MySQL OCI IAM authentication plugin.Zauthentication_oci_clientÚ plugin_nameFÚ requires_sslNÚcontextÚDEFAULTÚoci_config_profileÚoci_config_file)Ú    signatureÚ
oci_configÚreturnc
Csžt|ƒ}|d| ¡dœ}| d¡rz8t|dƒ}| ¡jtkrJt t    ¡‚|j
dd|d<Wn2t t fk
rŽ}zt t ¡|‚W5d}~XYnXtj|dd    S)
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)rrÚsecurity_token_filezutf-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)rrZ signature_64Ú auth_responserÚerr©r3úhd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\mysql/connector/plugins/authentication_oci_client.pyÚ_prepare_auth_responsePs"þ    
 
ÿþz)MySQLOCIAuthPlugin._prepare_auth_response)Úkey_pathrc Cszz4ttj |¡dƒ}tj| ¡dd}W5QRXWn@ttt    t
fk
rt}zt   d|›d|›¡‚W5d}~XYnX|S)z+Get the private_key form the given locationÚrbN)Úpasswordz2An error occurred while reading the API_KEY from "z": ) ÚopenÚosÚpathÚ
expanduserr Zload_pem_private_keyÚreadÚ    TypeErrorr,Ú
ValueErrorr
rr))r6Úkey_fileÚ private_keyr2r3r3r4Ú_get_private_keyxsþÿz#MySQLOCIAuthPlugin._get_private_key)rc Csg}dd„dd„dœ}i}z€t |jp*tj|jp2d¡}| ¡D]X\}}z*||rn|||ƒsn| d|›d¡Wq>tk
r”| d|›¡Yq>Xq>WnDtj    tj
tj tj tj fk
rÞ}z| t|ƒ¡W5d    }~XYnX|rüt d
|j›d |›¡‚|S) z=Get a valid OCI config from the given configuration file pathcSs t|ƒdkS)Né )Úlen©Úxr3r3r4Ú<lambda>óz:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>cSstj tj |¡¡S)N)r:r;Úexistsr<rEr3r3r4rGŽrH)rr@rz Parameter "z " is invalidzDoes not contain parameter NzInvalid oci-config-file: z. Errors found: )rÚ    from_filerÚDEFAULT_LOCATIONrÚitemsÚappendÚKeyErrorrZConfigFileNotFoundZ InvalidConfigZInvalidKeyFilePathZInvalidPrivateKeyZProfileNotFoundÚstrrr))ÚselfZ
error_listZreq_keysrZreq_keyZ    req_valuer2r3r3r4Ú_get_valid_oci_config‰s:þ
þû ÿz(MySQLOCIAuthPlugin._get_valid_oci_config)Ú    auth_datarcCsdt d|jt|jƒ¡| ¡}| |d¡}| |jt ¡t     
¡¡}|  ||¡}t d|¡|  ¡S)z-Prepare authentication string for the server.zserver nonce: %s, len %dr@zauthentication response: %s) r    ÚdebugZ
_auth_datarDrQrBÚsignr ZPKCS1v15r ÚSHA256r5Úencode)rPrRrrArr1r3r3r4r1±sÿ  z MySQLOCIAuthPlugin.auth_response)N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrOÚ__annotations__rÚboolrrrrrKrÚ staticmethodÚbytesrr5rrBrQrr1r3r3r3r4rGs
    '()#rZr/r:Úbase64rÚpathlibrÚtypingrrrÚrr    Zcryptography.exceptionsr
Zcryptography.hazmat.primitivesr r Z)cryptography.hazmat.primitives.asymmetricr Z/cryptography.hazmat.primitives.asymmetric.typesrÚ ImportErrorr)ZocirrrZAUTHENTICATION_PLUGIN_CLASSr(r*r.ZOCI_PROFILE_MISSING_PROPERTIESrr3r3r3r4Ú<module>s>      ÿþ ÿÿ