
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  S	 rS
 rS rS rS rg)z'Utils for handing transfer credentials.    )absolute_import)division)unicode_literalsN)resource_property)files)configparserc                 J    X;   a  [        SR                  U 5      5      eXU '   g)z@Assigns value to results_dict and raises error on duplicate key.zDuplicate key in file: {}N)KeyErrorformat)keyvalueresult_dicts      5lib/googlecloudsdk/command_lib/transfer/creds_util.py_assign_with_error_on_duplicater      s'    
.55c:
;;c    c                     U  Ho  nX1;   a  [        X1U   U5        M  [        R                  " U5      n[        R                  " U5      nXA;   a  [        UX   U5        XQ;   d  M`  [        UX   U5        Mq     g)zHConverts key to multiple cases and attempts to extract from search_dict.N)r   r   ConvertToCamelCaseConvertToSnakeCase)keyssearch_dictr   original_keycamel_case_keysnake_case_keys         r   _extract_keysr   &   sz    l"%l4M&13 );;LIn(;;LIn		&'(3(C(3	5 
	&'(3(C(3	5 r   c                 T   0 n[         R                  R                  [         R                  R                  U 5      5      n[        R
                  " U5       n [        R                  " UR                  5       5      n[        XU5        SSS5        U$ ! [        R                   ap    [        R                  " 5       n UR                  U5        O#! [        R                   a    [        S5      ef = fU H  n[        Xg   5      n[        XU5        M      Nf = f! , (       d  f       U$ = f)a  Reads JSON or INI file and returns dict with values for requested keys.

JSON file keys should be top level.
INI file sections will be flattened.

Args:
  file_path (str): Path of JSON or INI file to read.
  keys (list[str]): Search for these keys to return from file.

Returns:
  Dict[cred_key: cred_value].

Raises:
  ValueError: The file was the incorrect format.
  KeyError: Duplicate key found.
z-Source creds file must be JSON or INI format.N)ospathrealpath
expanduserr   
FileReaderjsonloadsreadr   JSONDecodeErrorr   ConfigParserParsingError
ValueErrordict)		file_pathr   result	real_pathfile_reader	file_dictconfigsectionsection_dicts	            r   get_values_for_keys_from_filer1   <   s    " &ggrww11)<=)	"k2**[--/0iDV, #  
-  
2((*fJI&& JHIIJ 'FO,d&1 
2	 #"  
-sB   D0B)D;CD C--%DDDD
D'c                     [        U / SQ5      nUR                  SUR                  SS5      5      nUR                  SUR                  SS5      5      nUR                  SS5      nX#U4$ )zScans file for AWS credentials keys.

Key fields prefixed with "aws" take precedence.

Args:
  file_path (str): Path to creds file.

Returns:
  Tuple of (access_key_id, secret_access_key).
  Each tuple entry can be a string or None.
)aws_access_key_idaws_secret_access_keyaccess_key_idsecret_access_keyrole_arnr3   r5   Nr4   r6   r7   )r1   get)r)   
creds_dictr5   r6   r7   s        r   get_aws_creds_from_filer:   b   st     -Y 9 * ..!4!+!FH- nn%<%/^^4G%NP^^J-(	8	33r   c                      [         R                  R                  5       R                  5       n U (       a  U R                  U R
                  4$ g)a  Returns creds from common AWS config file paths.

Currently does not return "role_arn" because there is no way to extract
this data from a boto3 Session object.

Returns:
  Tuple of (access_key_id, secret_access_key, role_arn).
  Each tuple entry can be a string or None.
)NN)boto3sessionSessionget_credentials
access_key
secret_key)credentialss    r   get_default_aws_credsrC   z   s<     %%'779+!!;#9#999	r   )__doc__
__future__r   r   r   r!   r   r<   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   	six.movesr   r   r   r1   r:   rC    r   r   <module>rJ      s?    . &  '  	  : * "5,#L40r   