o
    ɶd'                     @   sX  d Z ddlm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
mZmZmZ ddlmZ ddlmZmZmZmZ g d	Zd
dddZejdddZejdddZdZedededdZh dZddiZdhZ dZ!dZ"dZ#dZ$e%e&Z'dd Z(ee(iZ)eefddZ*d d! Z+d"d# Z,d$d% Z-d&d' Z.d(d) Z/d*d+ Z0d,d- Z1dS ).a  
Helpers for loading and validating service configuration.

You can configure services by passing a config dict directly, or by loading
one from a configuration file.

The following keys are required::

    fingerprint
    key_file
    region
    tenancy
    user

Additionally, the following keys are optional::

    additional_user_agent (default "")
    log_requests (default False)
    pass_phrase (required if your key_file has a passphrase)

    )absolute_importN)six   )ConfigFileNotFoundProfileNotFoundInvalidConfigInvalidKeyFilePath)signers)AUTHENTICATION_TYPE_FIELD_NAME#get_authentication_type_from_config DELEGATION_TOKEN_FILE_FIELD_NAME<DELEGATION_TOKEN_WITH_INSTANCE_PRINCIPAL_AUTHENTICATION_TYPE)DEFAULT_CONFIG	from_filevalidate_configF )log_requestsZadditional_user_agentZpass_phrase~z.ociconfigz
.oraclebmcDEFAULTz>^([0-9a-zA-Z-_]+[.:])([0-9a-zA-Z-_]*[.:]){3,}([0-9a-zA-Z-_]+)$z^([0-9a-f]{2}:){15}[0-9a-f]{2}$)tenancyuserfingerprint>   regionr   r   key_filer   r   Zkey_contentZOCI_CONFIG_FILEZ
OCI_REGIONr   zFor more info about config file and how to get required information, see https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htmc                 C   sD   |  t}|d u rtdtj|}tj|s td|d S )NzNERROR: Please specify the location of the delegation_token_file in the config.z%Delegation token file not found at {})getr   r   ospath
expanduserisfileformat)r   Zdelegation_token_file_pathZ#expanded_delegation_token_file_path r!   .usr/lib/python3.10/site-packages/oci/config.py2_validate_delegation_token_with_instance_principalL   s   
r#   c                 C   s   t | }tjdd}||std|||vr$td||t tt	}|
||  t|d |d< tD ]}||v rHtd|t q9t||| |S )a  Create a config dict from a file.

    :param file_location: Path to the config file.  Defaults to ~/.oci/config and with a fallback
                            to environment variable OCI_CONFIG_FILE, then ~/.oraclebmc/config.
    :param profile_name: The profile to load from the config file.  Defaults to "DEFAULT"
    :return: A config dict that can be used to create clients.
    N)interpolationzCould not find config file at {}, please follow the instructions in the link to setup the config file https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htmz)Profile '{}' not found in config file {} r   z'{}' cannot be specified in a config file for security reasons. To use this key you must add it to the config programmatically. )_get_config_path_with_fallbackconfigparserConfigParserreadr   r    r   !CONFIG_FILE_DEBUG_INFORMATION_LOGdictr   update_as_boolCONFIG_FILE_BLACKLISTED_KEYS
ValueErrorinvalid_key_file_path_checker)file_locationprofile_nameexpanded_file_locationparserr   keyr!   r!   r"   r   _   s   
r   c           
      K   s<  d|v rt |d tjst |d tjst |d tjrd S t| v r1t| }t|}||  d S 	 i }t	D ]E}t
|}|| vsG| | d u r{|| vsQ| | d u r{|tkrwtdt tjt}|rrtdt || t< q6d||< q6d||< q6ttD ]\}}	||v rq|	| | sd||< q|rt|d S )NZsignerz<Region not found in config, checking environment variable {}z+Setting region from environment variable {}missingZ	malformed)
isinstancer	   Z%InstancePrincipalsSecurityTokenSignerZSecurityTokenSignerZKeyPairSignerr
   r   $AUTH_TYPE_TO_VALIDATION_FUNCTION_MAPr   REQUIREDREQUIRED_FALLBACKSREGION_KEY_NAMEloggerdebugr    REGION_ENV_VAR_NAMEr   environr   Z	iteritemsPATTERNSmatchr   )
r   kwargsZ	auth_typeZvalidator_functionerrorsZrequired_keyZfallback_keyZregion_from_env_varr4   patternr!   r!   r"   r   }   s>   0

(

r   c                 C   s   |  |t |S )N)r   r   )r   r4   r!   r!   r"   get_config_value_or_default   s   rD   c                 C   s:   | dv r| S |   dv rdS |   dv rdS td| )N)TF)1yestrueonT)0nofalseoffFz({!r} is not a valid alias for True/False)lowerr.   r    )xr!   r!   r"   r,      s   r,   c                 C   s4   t | dkrtd| d | rtd| d S )Nr   zError in config: {}r   z'Found the following config errors: {!r})lenr.   r    )rB   r!   r!   r"   _raise_on_errors   s
   rP   c                 C   s   t j| }t jt}| tks| tkr$t j|r$td|  |S t j	
tr>tdt t jt j	
t}|S t j|rH|}|S td| |S )NzConfig file found at {}znNo file location specified and default file does not exist. Getting path info from the environment variable {})r   r   r   FALLBACK_DEFAULT_LOCATIONDEFAULT_LOCATIONr   r;   r<   r    r>   r   CONFIG_FILE_PATH_ENV_VAR_NAME)r0   r2   Z'expanded_fallback_default_file_locationr!   r!   r"   r%      s$   r%   c                 C   sl   d| v r2t j| d}t j|s4t| d||}|r#d|nd}d|||t }t|d S d S )Nr   z at line {}r   zRConfig file {} is invalid: the key_file's value '{}'{} must be a valid file path. )	r   r   r   r   r   get_linenum_from_filer    r)   r   )r   r2   r1   Zkey_file_pathline_numberZline_messagemessager!   r!   r"   r/      s   r/   c                 C   s   t |dD}d}d| d }t|D ].\}}| }t|dkr'|d dkr'q||v r-d}|r@| |v r@|d   W  d    S qW d    d S 1 sLw   Y  d S )	NrF[]r   r   #T)open	enumeratestriprO   )r4   filenameZprofilefZprofile_foundZprofile_tokenrU   liner!   r!   r"   rT      s&   

rT   )2__doc__
__future__r   r&   r   reloggingZoci._vendorr   
exceptionsr   r   r   r   Zauthr	   utilr
   r   r   r   __all__r   r   joinrR   rQ   ZDEFAULT_PROFILEcompiler?   r8   r9   r-   rS   r=   r:   r)   	getLogger__name__r;   r#   r7   r   r   rD   r,   rP   r%   r/   rT   r!   r!   r!   r"   <module>   sV   
*
