
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r
  SSKJr  SS	KJr  S
r " S S\R                   5      r " S S\5      r " S S\5      rS rS rS rS rg)z$Manages logic for external accounts.    )absolute_import)division)unicode_literalsN)
exceptionscreds)
introspect)filesexternal_accountc                       \ rS rSrSrSrg)Error   zErrors raised by this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       3lib/googlecloudsdk/api_lib/auth/external_account.pyr   r      s    %r   r   c                       \ rS rSrSrSrg)BadCredentialFileException#   z Raised when file cannot be read.r   Nr   r   r   r   r   r   #   s    (r   r   c                       \ rS rSrSrSrg)BadCredentialJsonFileException'   z2Raised when the JSON file is in an invalid format.r   Nr   r   r   r   r   r   '   s    :r   r   c                     [         R                  " U 5      n [        R                  " U5      n[        U5      (       a  U$ g! [         a  n[        SR                  X5      5      eSnAff = f)a  Returns the JSON content if the file corresponds to an external account.

This function is useful when the content of a file need to be inspected first
before determining how to handle it. More specifically, it would check a
config file contains an external account cred and return its content which can
then be used with CredentialsFromAdcDictGoogleAuth (if the contents
correspond to an external account cred) to avoid having to open the file
twice.

Args:
  filename (str): The filepath to the ADC file representing an external
    account credentials.

Returns:
  Optional(Mapping): The JSON content if the configuration represents an
    external account. Otherwise None is returned.

Raises:
  BadCredentialFileException: If JSON parsing of the file fails.
z!Could not read json file {0}: {1}N)r
   ReadFileContentsjsonloads
ValueErrorr   formatIsExternalAccountConfig)filenamecontentcontent_jsones       r   #GetExternalAccountCredentialsConfigr*   +   sm    , ""8,'::g&L
 \** 
 
$%H%O%O&  s   A 
A*A%%A*c                 D    U =(       d    0 R                  S5      [        :H  $ )zGReturns whether a JSON content corresponds to an external account cred.type)get_EXTERNAL_ACCOUNT_TYPE)r(   s    r   r%   r%   N   s    

"	!	!&	)-C	CCr   c                     SU ;  d  U S   [         :w  a  [        S5      e[        R                  " [        R
                  " U 5      5      $ )a
  Creates external account creds from a dict of application default creds.

Args:
  external_config (Mapping): The configuration dictionary representing the
    credentials. This is loaded from the ADC file typically.

Returns:
  google.auth.external_account.Credentials: The initialized external account
    credentials.

Raises:
  BadCredentialJsonFileException: If the config format is invalid.
  googlecloudsdk.core.credentials.creds.InvalidCredentialsError: If the
    provided configuration is invalid or unsupported.
r,   z@The provided credentials configuration is not in a valid format.)r.   r   c_credsFromJsonGoogleAuthr!   dumps)external_configs    r    CredentialsFromAdcDictGoogleAuthr4   S   sH      O#f!77
(JL L 
	#	#DJJ$?	@@r   c                     [         R                  " U 5      (       d6  [         R                  " U 5      (       d  [         R                  " U 5      (       a*  [	        U SS5      =(       d    [
        R                  " U 5      $ g)a8  Returns the account identifier corresponding to the external account creds.

Args:
  creds (google.auth.credentials.Credentials): The credentials whose account
    ID is to be returned.

Returns:
  Optional(str): The corresponding account ID, or None if the credentials are
    not external_account credentials.
service_account_emailN)r0   IsExternalAccountCredentials IsExternalAccountUserCredentials*IsExternalAccountAuthorizedUserCredentialsgetattrc_introspectGetExternalAccountIdr   s    r   r<   r<   k   s_     **511..u5588??E2D9 5--e46	r   )r   
__future__r   r   r   r!   googlecloudsdk.corer   googlecloudsdk.core.credentialsr   r0   r	   r;   googlecloudsdk.core.utilr
   r.   r   r   r   r*   r%   r4   r<   r   r   r   <module>rA      si    + &  '  * < F *+ &J &) );U ; FD
A0r   