
    "                        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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s  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$J%r%  \$" \%" SSS5      5        SSK&J'r'  SSK(J(r(  S r)\RT                  RW                  SS5      r, " S S\RZ                  5      r. " S S \R^                  5      r0g)!9Tests for various combinations of configured credentials.    )absolute_import)print_function)division)unicode_literalsN)
exceptions)http_wrapper)AccessDeniedException)	CredTypes)DiscardMessagesQueue)CommandException)
GcsJsonApi)MockLoggingHandler)	SkipForS3)
SkipForXML)ObjectToURI)SetBotoConfigForTest)SetEnvironmentForTest)unittest)add_moveMovedModulemockzunittest.mock)r   )datetimec                 r    [         R                  R                  [        R                  " SS0SU -  SS95      $ )Nstatusi  z{"error": {"message": "%s"}}zhttp://www.google.com)infocontentrequest_url)apitools_exceptions	HttpErrorFromResponser	   Response)messages    0platform/gsutil/gslib/tests/test_creds_config.py_Make403r%   0   s=    

'
'
4
4(C$BW$L(?AB C    GSUtil test_impersonate_service_accountc                      ^  \ rS rSrSrU 4S jrS r\" S5      \" S5      \	R                  " S5      S 5       5       5       r\" S5      \" S5      \	R                  " S5      S	 5       5       5       rS
rU =r$ )TestCredsConfig;   r   c                    > [         [        U ]  5         [        5       U l        U R
                  R                  U R                  5        g )N)superr*   setUpr   log_handlerlogger
addHandler)self	__class__s    r$   r.   TestCredsConfig.setUp>   s3    	/4&()+DKK4++,r&   c                    [        / SQ5          [        S U R                  [        5       5        U R	                  S5        S S S 5        g ! [
         ag  n[        U5      nU R                  SU5        U R                  [        R                  U5        U R                  [        R                  U5         S nANuS nAff = f! , (       d  f       g = f)N)Credentialsgs_oauth2_refresh_tokenfoo)r7   gs_service_client_idbar)r7   gs_service_key_filebazr7   gs_impersonate_service_accountNz2Succeeded with multiple types of configured creds.ztypes of configured credentials)r   r   r0   r   failr   strassertInr   OAUTH2_USER_ACCOUNTOAUTH2_SERVICE_ACCOUNT)r2   emsgs      r$   testMultipleConfiguredCreds+TestCredsConfig.testMultipleConfiguredCredsC   s    	  
=4&:&<=		FG
 
  =!f7=i33S9i66<<	=
 
s/   B=0A		
B:AB50B=5B::B==
CTests only uses gs credentials.Tests only run on JSON API.zgslib.third_party.iamcredentials_apitools.iamcredentials_v1_client.IamcredentialsV1.ProjectsServiceAccountsService.GenerateAccessTokenc                    [        / SQ5         [        S5      Ul         [        S U R                  [        5       5        U R                  S5        [        S5      Ul         [        S U R                  [        5       5        U R                  S5        S S S 5        g ! [         a%  nU R                  S[        U5      5         S nANtS nAff = f! [         a%  nU R                  S[        U5      5         S nANeS nAff = f! , (       d  f       g = f)Nr6   r7   r:   Nr7   r<   N)r7   r?   r;   z#The caller does not have permissionz-Succeeded when IAM Credentials threw an errorz%Service account impersonation failed.z5IAM Service Account Credentials API has not been usedz+IAM Service Account Credentials API has not)
r   r%   side_effectr   r0   r   r@   r
   rB   rA   )r2   $mock_iam_creds_generate_access_tokenrE   s      r$   1testImpersonationBlockedByIamCredentialsApiErrorsATestCredsConfig.testImpersonationBlockedByIamCredentialsApiErrorsT   s     
  
 8
9 +6G4&:&<=		AB
 J
K +6M4&:&<=		AB%
 
 # G=s1vFFG # MCSVLLM'
 
sX   C>0BC>!0C
C	$C?C>C		C>
C;C61C>6C;;C>>
Dc                 z   [        / SQ5         Sn[        R                  " 5       R                  S5      nX!R                  l        X1R                  l        [        S U R                  [        5       5      nU R                  U[        UR                  R                  5      5        S S S 5        g ! , (       d  f       g = f)NrL   z#Mock token from IAM Credentials APIz%Y-%m-%dT23:59:59Z)r   r   nowstrftimereturn_valueaccessToken
expireTimer   r0   r   rB   rA   credentialsaccess_token)r2   rP   
fake_tokenexpire_timeapis        r$   &testImpersonationSuccessfullyUsesToken6TestCredsConfig.testImpersonationSuccessfullyUsesTokenq   s     
  
 9jLLN++,@AkFP77CEP77BtT[[*>*@Ac
mmJCOO$@$@ AB
 
 
s   BB,,
B:)r/   )__name__
__module____qualname____firstlineno____doc__r.   rG   r   r   r   patchrQ   r^   __static_attributes____classcell__)r3   s   @r$   r*   r*   ;   s    A-
=" ./+,:: D EME - 0
M0 ./+,:: D ECE - 0
Cr&   r*   c                      \ rS rSr\" S5      S 5       r\R                  " \S5      \" S5      \	" S5      S 5       5       5       r
\R                  " \S5      \" S5      \	" S5      S 5       5       5       r\R                  " \S5      \" S5      \	" S5      S 5       5       5       r\R                  " \S5      \" S5      \	" S5      S	 5       5       5       rS
rg)TestCredsConfigIntegration   rI   c                     U R                  5       n[        / SQSS9   U R                  S[        U5      /SSS9nU R	                  SU5        S S S 5        g ! , (       d  f       g = f)	N)r6   rM   rN   r>   F)use_existing_configls   T)expected_statusreturn_stderrzcredentials are invalid)CreateBucketr   	RunGsUtilsurirB   )r2   
bucket_uristderrs      r$   testExactlyOneInvalid0TestCredsConfigIntegration.testExactlyOneInvalid   sk    ""$J		B "
# ~~tT*%56./,0  2f mm-v6
# 
# 
#s   /A
A#z/Test requires test_impersonate_service_account.rJ   c                     [        SS[        4/5         [        0 5         U R                  SS/SS9nU R	                  SU5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr7   r?   rm   gs://pubTrp   #using service account impersonation)r   SERVICE_ACCOUNTr   rr   rB   r2   ru   s     r$   *testImpersonationCredentialsFromBotoConfigETestCredsConfigIntegration.testImpersonationCredentialsFromBotoConfig   sm    
 
/O / 1 2 
3 $z 2$G;VD %
3 
3$$
3 
3s"   A'%AA'
A$	 A''
A5c                     [        S/5         [        S[        05         U R                  SS/SS9nU R	                  SU5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr>   )CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNTrm   ry   Trz   r{   )r   r   r|   rr   rB   r}   s     r$   &testImpersonationCredentialsFromGCloudATestCredsConfigIntegration.testImpersonationCredentialsFromGCloud   su    
 
  & ' 
( 6
HJz 2$G;VDJ
( 
(J J
( 
(s"   A&%AA&
A#	A&&
A4c                     [        S/5         [        SS05         U R                  S[        SS/SS9nU R	                  S	[        -  U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)
N)r7   r?   zfoo@google.comr   zbar@google.comz-irm   ry   Trz   !API calls will be executed as [%s)r   r   rr   r|   rB   r}   s     r$   5testImpersonationCredentialsFromOptionOverridesOthersPTestCredsConfigIntegration.testImpersonationCredentialsFromOptionOverridesOthers   s    
 
  2 3 
4 68H
IKj I.2   49OK		K
4 
4K K
4 
4s"   A/2AA/
A,	(A//
A=c                     [        SS[        4/5         U R                  SS/SS9nU R                  S[        -  U5        S S S 5        g ! , (       d  f       g = f)Nr7   r?   rm   ry   Trz   r   )r   r|   rr   rB   r}   s     r$   testImpersonationSuccess3TestCredsConfigIntegration.testImpersonationSuccess   s\    
 
/O / 1 2 
3~~tZ0~Ef
mm7/I
3 
3 
3s   ,A		
A N)r`   ra   rb   rc   r   rv   r   
skipUnlessr|   r   r~   r   r   r   rf   r   r&   r$   ri   ri      s&   ./7 07 HJ./+,E - 0JE HJ./+,E - 0JE HJ./+, - 0J HJ./+, - 0Jr&   ri   )1rd   
__future__r   r   r   r   botoapitools.base.pyr   r   r	   gslib.cloud_apir
   gslib.cred_typesr   gslib.discard_messages_queuer   gslib.exceptionr   gslib.gcs_json_apir    gslib.tests.mock_logging_handlerr   gslib.tests.testcaseteststestcase)gslib.tests.testcase.integration_testcaser   r   gslib.tests.utilr   rs   r   r   r   sixr   r   	six.movesr   r   r%   config	get_valuer|   GsUtilUnitTestCaser*   GsUtilIntegrationTestCaseri   r   r&   r$   <module>r      s    @ & %  '  > ) 1 & = , ) ? ' ' ? @ 0 1 2 % % VV_	5 6  C ++''(JLHCh11 HCV>!C!C >r&   