
    .                     h   S r SSKJ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S
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSKJ!r!  \RD                  r#\RH                  " 5       (       a  Sr%OSr% " S S\&5      r'g)z>JSON gsutil Cloud API implementation for Google Cloud Storage.    )absolute_import)print_function)division)unicode_literalsN)
exceptions)config)AccessDeniedException)BadRequestException)NotFoundException)PreconditionException)ServiceException)NoOpCredentials)iamcredentials_v1_client)iamcredentials_v1_messages)system_util)GetCertsFile)GetMaxRetryDelay)
GetNewHttp)GetNumRetrieszVInsufficient OAuth2 scope to perform this operation. Please re-run `gcloud auth login`zRInsufficient OAuth2 scope to perform this operation. Please re-run `gsutil config`c                   \   ^  \ rS rSrSrSU 4S jjrS rS rSS jrSS jr	S r
S	 rS
rU =r$ )IamcredentailsApi6   zCWraps calls to the Cloud IAM Credentials v1 interface via apitools.c           	      L  > [         [        U ]  5         Xl        X l        [        5       U l        [        5       U l        SU l	        Sn[        R                  " SS[        R                  " SUS5      5      U l        [        R                  " SSS5      nU(       a  SU-   OS	U l        U R                  U R                  -   U R                  -   U l        US
:  nUS
:  n[        R                   " U R                  U R                  UUU R                  S9U l        [%        5       U l        U R&                  U R"                  l        [)        5       U l        U R*                  U R"                  l        [-        U R                  [.        5      (       a  U R"                  R1                  SS5        gg)a  Performs necessary setup for interacting with Google Cloud IAM
Credentials.

Args:
  logger: logging.logger for outputting log messages.
  credentials: Credentials to be used for interacting with Cloud IAM
  debug: Debug level for the API implementation (0..3).
zhttps://gs_iamcredentails_hostCredentialsgs_iamcredentials_hostziamcredentials.googleapis.comgs_iamcredentails_portN:    )urlhttplog_requestlog_responsecredentialskey'AIzaSyDnacJHrKma0048b13sh8cgxNUwulubmJM)superr   __init__loggerr%   r   
certs_filer   r"   	http_baser   get	host_base	host_porturl_baseapitools_clientIamcredentialsV1
api_clientr   num_retriesr   max_retry_wait
isinstancer   AddGlobalParam)	selfr*   r%   debuglegacy_typogs_iamcred_portr#   r$   	__class__s	           +platform/gsutil/gslib/iamcredentials_api.pyr)   IamcredentailsApi.__init__9   sV    

T+-K""nDODIDN +KZZ/

=+/NOQDN jj0H$OO0?cO+RDN^^dnn4t~~EDMA:KQJL%66MMYY!$$&DO %D"&"2"2DOO*,D%)%8%8DOO"$""O44 oo$$
;= 5    c                     SU-  n[         R                  " US9n[         R                  " X4S9n U R                  R                  R                  U5      $ ! [         a  nU R                  Xa5         SnAgSnAff = f)z0Sign the blob using iamcredentials.SignBlob API.projects/-/serviceAccounts/%s)payload)namesignBlobRequestN)apitools_messagesSignBlobRequest4IamcredentialsProjectsServiceAccountsSignBlobRequestr3   projects_serviceAccountsSignBlob TRANSLATABLE_APITOOLS_EXCEPTIONS_TranslateExceptionAndRaise)r8   service_account_idmessagerC   sign_blob_requestrequestes          r=   rI   IamcredentailsApi.SignBlobj   su    *-??D)99'JNN	: >__55>>wGG+ >
&&q==>s   $A 
A9A44A9c                     SU-  n[         R                  " US9n[         R                  " UUS9n U R                  R                  R                  U5      $ ! [         a  nU R                  Xa5         SnAgSnAff = f)z8Generates an access token for the given service account.rA   )scope)rC   generateAccessTokenRequestN)rE   GenerateAccessTokenRequest?IamcredentialsProjectsServiceAccountsGenerateAccessTokenRequestr3   rH   GenerateAccessTokenrJ   rK   )r8   rL   scopesrC   generate_access_token_requestrO   rP   s          r=   rW   %IamcredentailsApi.GenerateAccessTokenv   s    *-??D$5$P$P%! NNO.KMG
>__55II
 + >
&&q==>s   $A 
A:A55A:c                     U R                   R                  [        R                  5      (       a/  U R                   R	                  S[
        R                  " 5       5        U R                  XS9nU(       a  Uee )a  Translates an HTTP exception and raises the translated or original value.

Args:
  e: Any Exception.
  service_account_id: Optional service account in request that caused the exception.

Raises:
  Translated CloudApi exception, or the original exception if it was not
  translatable.
zTranslateExceptionAndRaise: %s)rL   )r*   isEnabledForloggingDEBUGr9   	traceback
format_exc_TranslateApitoolsException)r8   rP   rL   translated_exceptions       r=   rK   -IamcredentailsApi._TranslateExceptionAndRaise   sc     {{..
kk8!,,.0;;	 < 2  r?   c                 t   [        U[        R                  5      (       Ga  U R                  U5      nUR                  S:X  a  [        U=(       d    SUR                  S9$ UR                  S:X  ae  S[        U5      ;   a  [        U=(       d    SUR                  S9$ S[        U5      ;   a(  [        [        UR                  U R                  U5      S9$ GOUR                  S	:X  Ga9  S
[        U5      ;   a  [        S5      $ S[        U5      ;   a  [        U5      $ S[        U5      ;   a  [        U=(       d    SUR                  S9$ S[        U5      ;   a  [        U=(       d    SUR                  S9$ S[        U5      ;   a  [        SUR                  S9$ S[        U5      ;   a  [        SUR                  S9$ S[        U5      ;   a(  [        [        UR                  U R                  U5      S9$ [        U=(       d    UR                  =(       d    UUR                  S9$ UR                  S:X  a'  [        U=(       d    UR                  UR                  S9$ UR                  S:X  a  U(       a  [        SU-  UR                  S9$ UR                  S:X  a  [        X1R                  S9$ [        X1R                  S9$ g)a%  Translates apitools exceptions into their gsutil equivalents.

Args:
  e: Any exception in TRANSLATABLE_APITOOLS_EXCEPTIONS.
  service_account_id: Optional service account ID that caused the exception.

Returns:
  CloudStorageApiServiceException for translatable exceptions, None
  otherwise.
i  zBad Request)statusi  zLogin RequiredzAccess denied: login required.insufficient_scope)re   bodyi  z#The caller does not have permissiona  Service account impersonation failed. Please go to the Google Cloud Platform Console (https://cloud.google.com/console), select IAM & admin, then Service Accounts, and grant your originating account the Service Account Token Creator role on the target service account.z5IAM Service Account Credentials API has not been usedz7The account for the specified project has been disabledzAccount disabled.z,Daily Limit for Unauthenticated Use Exceededz8Access denied: quota exceeded. Is your project ID valid?zUser Rate Limit Exceededz5Rate limit exceeded. Please retry this request later.zAccess Not ConfiguredzAccess Not Configured. Please go to the Google Cloud Platform Console (https://cloud.google.com/console#/project) for your project, select APIs & services, and enable the Google Cloud IAM Credentials API.i  i  zThe key %s already exists.i  N)r6   apitools_exceptions	HttpError_GetMessageFromHttpErrorstatus_coder
   strr	   "_INSUFFICIENT_OAUTH2_SCOPE_MESSAGE!_GetAcceptableScopesFromHttpErrorrM   r   r   r   )r8   rP   rL   rM   s       r=   ra   -IamcredentailsApi._TranslateApitoolsException   s    !(2233--a0g	
#	 #7#;m*+--9 	9==Cs1v%&w (H'G./mm= = "SV+ '0]]99!<> > , ==C 1CF:&,- - Cc!fL&w/
/DAN&w'E2E./mm= =;s1vE&w (C(C./mm= = (3q61&]]$ $ %A.&% ]]$ $ "SV+ '0]]99!<> >
 'w (:!)) (:'9./mm= = ==C !5AIIammLL==C$6 < 2!3'(}}6 	6 ==C$W]]CCgmm<<U 4r?   c                     [        U[        R                  5      (       aM  [        USS 5      (       a:   [        R
                  " UR                  5      nSU;   a  SUS   ;   a  US   S   $ g g g g ! [         a     g f = f)NcontenterrorrM   )r6   rh   ri   getattrjsonloadsrq   	Exception)r8   
http_errorjson_objs      r=   rj   *IamcredentailsApi._GetMessageFromHttpError   s    *1;;<<	Y	-	-	ZZ
 2 23( Y(72C%CG$Y// &D  
. =  	
	s   6A. .
A;:A;c                      UR                   S   nUR                  S5      nUS:  a  X#S  R                  S5      S   nSU-  $ g ! [         a     g f = f)Nzwww-authenticatezscope="r   "   zAcceptable scopes: %s)responsefindsplitrv   )r8   rw   www_authenticate	scope_idxrX   s        r=   rn   3IamcredentailsApi._GetAcceptableScopesFromHttpError   sp    #,,-?@ #''	2i	a!*-33C8;&// 
  
s   A A 
AA)r3   r+   r%   r.   r/   r"   r,   r*   r5   r4   r0   )NNr   )N)__name__
__module____qualname____firstlineno____doc__r)   rI   rW   rK   ra   rj   rn   __static_attributes____classcell__)r<   s   @r=   r   r   6   s1    K/=b
>> *V=p	 r?   r   )(r   
__future__r   r   r   r   rt   r]   r_   apitools.base.pyr   rh   botor   gslib.cloud_apir	   r
   r   r   r   gslib.no_op_credentialsr   )gslib.third_party.iamcredentials_apitoolsr   r1   r   rE   gslib.utilsr   gslib.utils.boto_utilr   r   r   r   ri   rJ   InvokedViaCloudSdkrm   objectr    r?   r=   <module>r      s    E & %  '    >  1 / - 1 , 3 a e # . 2 , /$7$A$A  !!##* %
& %
Z Zr?   