
    _                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl	m
Z
 ddlmZ ddlmc mZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ  e eddd             ddlmZ d Z  e d      Z! e d      Z" e d      Z#dZ$ G d de%      Z& ed       ed       G d dejN                                      Z( ed       G d d ejN                               Z) G d! d"ejT                        Z+y)#z'Integration tests for the hmac command.    )absolute_import)division)print_function)unicode_literalsN)hmac)PopulateProjectId)	SkipForS3)
SkipForXML)SetBotoConfigForTest)SetEnvironmentForTest)unittest)Retry)	shim_util)add_moveMovedModulemockzunittest.mock)r   c                 B    t         j                  j                  d|       S )NGSUtil)botoconfig	get_value)account_fields    (platform/gsutil/gslib/tests/test_hmac.py_LoadServiceAccountr   *   s    			x	77    test_hmac_service_accounttest_hmac_alt_service_accounttest_hmac_list_service_account
   c                       e Zd Zy)KeyLimitErrorN)__name__
__module____qualname__ r   r   r!   r!   5   s    r   r!   z"S3 does not have an equivalent APIz"XML HMAC control is not supported.c                      e Zd ZdZd Zd Z	 	 	 	 ddZd Z ee	dd      d	        Z
d
 Zd Zd Z ej                  ed      d        Zd Z ej                  ed      d        Z ej                  ed      d        Zd Z ej                  ed      d        Zd Zd Z ej                  exr ed      d        Zd Z ej                  exr ed      d        Zd Z ej                  ed      d        Zy)TestHmacIntegrationa)  Hmac integration test cases.

  These tests rely on the presence of 3 service accounts specified in the BOTO
  config. test_hmac_service_account and test_hmac_alt_service_account should not
  have any undeleted keys and test_hmac_list_service_account should have only
  deleted and active keys.
  c                 |    t        j                  d|      }|s| j                  d|z         |j                  d      S )Nz(GOOG[\S]*)z.Couldn't find Access Id in output string:
"%s"r   researchfailgroup)selfoutput_stringid_matchs      r   ExtractAccessIdz#TestHmacIntegration.ExtractAccessIdD   s;    yy7H
iiB >>!r   c                 |    t        j                  d|      }|s| j                  d|z         |j                  d      S )Nz\sEtag:\s+([\S]+)$z)Couldn't find Etag in output string:
"%s"   r)   )r.   r/   
etag_matchs      r   ExtractEtagzTestHmacIntegration.ExtractEtagK   s9    0-@J
ii=MNAr   c                     | j                  |d|z         | j                  |d|z         | j                  |d|z         | j                  |d|z         | j                  |d       | j                  |d       y )NzAccess ID %s:z\sState:\s+%sz\s+Service Account:\s+%s\nz\s+Project:\s+%sz\s+Time Created:\s+.*z\s+Time Last Updated:\s+.*)assertRegex)r.   r/   	access_idstateservice_accountprojects         r   AssertKeyMetadataMatchesz,TestHmacIntegration.AssertKeyMetadataMatchesQ   s     	]$4y$@A]$4u$<=]2_DF]$7'$AB]$<=]$ABr   c                     	 | j                  dddd|g       | j                  dd|g       y # t        $ r}dt        |      vr Y d }~1d }~ww xY w)Nr   update-sINACTIVEz!Update must modify the credentialdelete)	RunGsUtilAssertionErrorstr)r.   r8   es      r   CleanupHelperz!TestHmacIntegration.CleanupHelper_   sY    
nnfhj)DE
 	NNFHi01	  	,CF	: 
;s   - 	AA		A      )triestimeout_secsc                     	 | j                  dd|gd      S # t        $ r'}dt        |      v rt        t        |             d}~ww xY w)z%Retry creation on key limit failures.r   createTreturn_stdoutzHMAC key limit reachedN)rB   rC   rD   r!   )r.   r:   rE   s      r   _CreateWithRetryz$TestHmacIntegration._CreateWithRetryi   sW    ^^VX?*.  0 0 	!SV	+CF##	s    	A"AAc                 F    | j                  |      }| j                  |      S )N)rO   r1   )r.   r:   stdouts      r   CreateHelperz TestHmacIntegration.CreateHelperv   s#    ""?3F''r   c                     g d}|D ]i  \  }}}| j                   rdnd}| j                  |j                         d|      }| j                   r| j                  ||       X| j                  ||       k y )N)
)zhmac createrequires a service account+argument SERVICE_ACCOUNT: Must be specified)zhmac create -p projrT   rU   )zhmac deleterequires an Access ID%argument ACCESS_ID: Must be specified)zhmac delete -p projrV   rW   )zhmac getrV   rW   )zhmac get -p projrV   rW   )zhmac list account1zunexpected argumentszunrecognized arguments)zhmac update keynamezstate flag must be suppliedz=Exactly one of (--activate | --deactivate) must be specified.)zhmac update -s INACTIVErV   rW   )zhmac update -s INACTIVE -p projrV   rW      r3   Treturn_stderrexpected_status)_use_gcloud_storagerB   splitassertIn)r.   paramscommandgsutil_error_substrgcloud_error_substrr[   stderrs          r   test_malformed_commandsz+TestHmacIntegration.test_malformed_commandsz   sz    F, >D9$&9!551o~~gmmo,0/>  Af 
	!	!)62)62 >Dr   c                     | j                   rt        j                  d      ddg}|D ]9  \  }}| j                  |j	                         dd      }| j                  ||       ; y )Nz3These commands cannot be translated using the shim.)zhmac create -u emailrT   )zhmac update -s KENTUCKYzstate flag value must be one ofTr3   rY   )r\   r   SkipTestrB   r]   r^   )r.   r_   r`   ra   rc   s        r   @test_malformed_commands_that_cannot_be_translated_using_the_shimzTTestHmacIntegration.test_malformed_commands_that_cannot_be_translated_using_the_shim   sv     ' ( ( 	?FF )/$$~~gmmo,0./  1f mm'0	 )/r   z,Test requires service account configuration.c                    | j                  ddt        gd      }	 | j                  |d       | j                  |d       | j                  |      }| j	                  |       y # | j                  |      }| j	                  |       w xY w)Nr   rL   TrM   zAccess ID:\s+\S+zSecret:\s+\S+)rB   SERVICE_ACCOUNTr7   r1   rF   )r.   rQ   r8   s      r   test_createzTestHmacIntegration.test_create   s     ^^VX?*.  0F$
v23
v/0&&v.i
# &&v.i
#s   $A# #$Bc                 T    | j                  g ddd      }| j                  d|       y )N)r   rL   zDNE@mail.comTr3   rY   z)Service Account 'DNE@mail.com' not found.)rB   r^   r.   rc   s     r   test_create_sa_not_foundz,TestHmacIntegration.test_create_sa_not_found   s/    ^^>*.,-  /F 	MM?Hr   c                     | j                  t              }| j                  dddd|g       | j                  dd|g       | j                  dddt        gd	      }| j                  ||       y )
Nr   r>   r?   r@   rA   list-uTrM   )rR   ALT_SERVICE_ACCOUNTrB   assertNotInr.   r8   rQ   s      r   test_deletezTestHmacIntegration.test_delete   sq     !!"56INNFHdJ	BCNNFHi01^^VVT3FG*.  0FY'r   c                    | j                  t              }| j                  dd|gdd      }	 | j                  r| j	                  d|       n| j	                  d|       | j                  |       y # | j                  |       w xY w)Nr   rA   Tr3   rY   z2HTTPError 400: Cannot delete keys in ACTIVE state.z'400 Cannot delete keys in ACTIVE state.)rR   ri   rB   r\   r^   rF   )r.   r8   rc   s      r   test_delete_active_keyz*TestHmacIntegration.test_delete_active_key   s     !!/2I^^VXy9*.,-  /F$		!	!J	 	?H
#d#s   1A1 1Bc                     | j                  g ddd      }| j                  r| j                  d|       y | j                  d|       y )N)r   rA   GOOG1234DNETr3   rY   -HTTPError 404: Access ID not found in project404 Access ID not foundrB   r\   r^   rl   s     r   test_delete_key_not_foundz-TestHmacIntegration.test_delete_key_not_found   sG    ^^=*.,-  /F 
mmCVL
mm-v6r   c                     | j                  t              }| j                  dd|gd      }	 | j                  ||t        t	        d              | j                  |       y # | j                  |       w xY w)Nr   getTrM   )r8   r:   r;   )rR   rq   rB   r<   r   rF   rs   s      r   test_getzTestHmacIntegration.test_get   sv     !!"56I^^VUI6d^KF$
##
-#D)	 $  #d#s   "A! !A4c                     | j                  g ddd      }| j                  r| j                  d|       y | j                  d|       y )N)r   r~   rx   Tr3   rY   ry   rz   r{   rl   s     r   test_get_not_foundz&TestHmacIntegration.test_get_not_found   sG    ^^:*.,-  /F 
mmCVL
mm-v6r   c                 `    t        t              D ]  }| j                  ddt        gd         y )Nr   rL   )r[   )rangeMAX_SA_HMAC_KEYSrB   LIST_SERVICE_ACCOUNT)r.   _s     r   setUpListTestz!TestHmacIntegration.setUpListTest   s0    #$
nnfh(<=%)  + %r   c                 .   | j                          | j                  t              }| j                  dddd|g       | j                  dddt        gd      }|j                         j                  d	      }d
}|D ]U  }|j                         \  }}}| j                  d|       | j                  |t               | j                  |d       |dz  }W | j                  |t               | j                  ddgd      }|j                         j                  d	      }d
}d}	|D ]-  }|j                         \  }
}}|dz  }|t        k(  s&|dk(  s,d}	/ | j                  ||       | j                  |	       | j                  dd|g       | j                  g dd      }|j                         j                  d	      }d
}d}|D ]-  }|j                         \  }
}}|dz  }|t        k(  s&|dk(  s,d}/ | j                  |       | j                  ||       y )Nr   r>   r?   r@   ro   rp   TrM   
r   GOOGACTIVEr3   FrA   )r   ro   -aDELETED)r   rR   ri   rB   r   stripr]   r^   assertEqualr   assertGreater
assertTrue)r.   alt_access_idrQ   list_account_key_countliner8   r9   accountproject_key_countinactive_key_listedr   deleted_key_listeds               r   	test_listzTestHmacIntegration.test_list  s    
 	%%o6MNNFHdJFG ^^VVT3GH*.  0F\\^!!$'F"&**,i
mmFI&
w 45
uh'!  	+-=> ^^VV,D^AF\\^!!$'F**,a1	O	#(;"	  	(*@AOO'( 	NNFHm45^^2$^GF\\^!!$'F**,a1	O	#(:!	 
 	OO&'(*@Ar   c              #      K   d}|j                  d      D ]&  }|r|j                  d      r| |}n||z  }|dz  }( y w)N r   z	Access ID)r]   
startswith)r.   rQ   current_keyls       r   ParseListOutputz#TestHmacIntegration.ParseListOutput:  sK     K\\$	k2qTk  s   >A c                    | j                          | j                  t              }| j                  dddd|g       | j                  g dd      }	 | j	                  d|       | j	                  d|       | j	                  t        |       | j	                  t
        |       | j                  |      D ]  }| j                  |d	
        	 | j                  |       y # | j                  |       w xY w)Nr   r>   r?   r@   )r   ro   -lTrM   z ACTIVE.*r9   )	r   rR   rq   rB   r^   r   r   r<   rF   )r.   r   rQ   key_metadatas       r   test_list_long_formatz)TestHmacIntegration.test_list_long_formatD  s    
 	%%&9:MNNFHdJFG^^2$^GF(
mmIv&
mmJ'
mm'0
mm(&1..v6,%%l$%? 7 'd's   A9C C1c                     d}| j                  ddd|gdd      }| j                  r"| j                  dj                  |      |       y | j                  d	|z  |       y )
Nzservice-account-DNE@gmail.comr   ro   rp   Tr3   rY   z.HTTPError 404: Service Account '{}' not found.zService Account '%s' not found.)rB   r\   r^   format)r.   r:   rc   s      r   #test_list_service_account_not_foundz7TestHmacIntegration.test_list_service_account_not_foundY  so    5O^^VVT?C*.,-  /F 
mm
<
C
C &( mm7/Ir   c           	         | j                  t              }| j                  dd|gd      }| j                  |      }	 | j	                  |d       | j                  dddd	d
||gd      }| j	                  |d	       | j                  dd|gd      }| j	                  |d	       | j                  dddd|gd      }| j	                  |d       | j                  dd|gd      }| j	                  |d       | j                  dddd	d
d|gdd      }| j                  d|       | j                  |       y # | j                  |       w xY w)Nr   r~   TrM   r   r   r>   r?   r@   -ebadEtagr3   rY   z#Etag does not match expected value.)rR   rq   rB   r5   r<   r^   rF   )r.   r8   rQ   etagrc   s        r   test_updatezTestHmacIntegration.test_updateg  sz    !!"56I^^VUI6d^KFF#D$
##F(#;~~8T:tT9
E  f ##F*#=~~vui8~Mf
##F*#=~~vxxK,0  2f
##F(#;~~vui8~Mf
##F(#;~~8T:tY	
J  f mm96B
#d#s   C*D: :EN)zGOOG.*r   r   r   ) r"   r#   r$   __doc__r1   r5   r<   rF   r   r!   rO   rR   rd   rg   r   
skipUnlessri   rj   rm   rq   rt   rv   r|   r   r   r   r   r   r   r   r   r   r%   r   r   r'   r'   9   s    *2%-/3'+C2 	aa0
 1
(!3F1 8EG$G$I 8*EG(G( 8EG$G$ 7 8*EG$G$7+
 8+?EG2BG2Bh 8+C0CEG(G(& 8*EG$G$r   r'   c                       e Zd ZdZg dZd Zy)TestHmacXmlIntegrationz-XML integration tests for the "hmac" command.))Credentialsgs_oauth2_refresh_tokenN)r   gs_service_client_idN)r   gs_service_key_fileN)r   gs_service_key_file_passwordN)r   gs_access_key_iddummykey)r   gs_secret_access_keydummysecretc                     t        | j                        5  dD ],  }d|g}| j                  |dd      }| j                  d|       . 	 d d d        y # 1 sw Y   y xY w)N)rL   rA   r~   ro   r>   r   r3   T)r[   rZ   z9The "hmac" command can only be used with the GCS JSON API)r   boto_config_hmac_auth_onlyrB   r^   )r.   
subcommandr`   rc   s       r   test_hmac_fails_for_xmlz.TestHmacXmlIntegration.test_hmac_fails_for_xml  sW    	d==	>E*:&$OG	Q F 
?	>	>s   2AAN)r"   r#   r$   r   r   r   r%   r   r   r   r     s    5	 Qr   r   c                   b   e Zd Zej                  j                  ej                  d ej                               d        Z	ej                  j                  ej                  d ej                               d        Z
ej                  j                  ej                  d ej                               d        Zej                  j                  ej                  d ej                               d        Zej                  j                  ej                  d ej                               d        Zej                  j                  ej                  d ej                               d        Zej                  j                  ej                  d ej                               d	        Zy
)TestHmacUnitShim
RunCommand)newc           
         d}d}d}t        ddg      5  t        d|d      5  | j                  dd	d
||gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      t        j                  ||      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nfake_dir9test.service.account@test_project.iam.gserviceaccount.comtest_projectr   use_gcloud_storageTruer   hidden_shim_modedry_runr   (CLOUDSDK_CORE_PASS_CREDENTIALS_TO_GSUTILCLOUDSDK_ROOT_DIRr   rL   -pTargsreturn_log_handlerr   infozAGcloud Storage Command: {} storage hmac create {} --project {} {})r   r   r   joinmessagesr^   r   r   _get_gcloud_binary_pathr   _CREATE_COMMAND_FORMAT)r.   fake_cloudsdk_dirr:   r;   mock_log_handler
info_liness         r   (test_shim_translates_hmac_create_commandz9TestHmacUnitShim.test_shim_translates_hmac_create_command  s    "C G	GHJ 
K 6<0" 	  ??D'?;# + % YY/88@A
 9:@&$<<ZH66*;, .8		9	
K 
K	 	
K 
K#   B<A<B0B<0B9	5B<<Cc           	      r   d}d}d}t        ddg      5  t        d|d      5  | j                  dd	d
||gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      ||      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   test-projectfake123456789r   r   r   r   r   rA   r   Tr   r   r   z>Gcloud Storage Command: {} storage hmac delete --project {} {})	r   r   r   r   r   r^   r   r   r   r.   r   r;   r8   r   r   s         r   #test_shim_translates_delete_commandz4TestHmacUnitShim.test_shim_translates_delete_command  s    "GI	GHJ 
K 6<0" 	  ??D'95# + % YY/88@A
 67=v$<<ZH"I8/0:	<	
K 
K	 	
K 
Ks#   B-A-B!B-!B*	&B--B6c           
         d}d}d}t        ddg      5  t        d|d      5  | j                  dd	d
||gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      t        j                  ||      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   r   r   r   r   r   r   r~   r   Tr   r   r   zCGcloud Storage Command: {} storage hmac describe {} --project {} {}r   r   r   r   r   r^   r   r   r   r   _DESCRIBE_COMMAND_FORMATr   s         r   !test_shim_translates_get_commanndz2TestHmacUnitShim.test_shim_translates_get_commannd  s    "GI	GHJ 
K 6<0" 	  ??w	2# + %
 YY/88@A
 ;<BF$<<ZH88'9=N !		"	
K 
K	 	
K 
Kr   c                    d}d}d}t        ddg      5  t        d|d      5  | j                  dd	d
d|d|gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      t        j                  ||      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   r   r   r   r   r   r   ro   r   rp   r   Tr   r   r   zWGcloud Storage Command: {} storage hmac list {} --all --service-account {} --project {})r   r   r   r   r   r^   r   r   r   r   _LIST_COMMAND_SHORT_FORMATr.   r   r;   r:   r   r   s         r   9test_shim_translates_hmac_list_command_using_short_formatzJTestHmacUnitShim.test_shim_translates_hmac_list_command_using_short_format  s    "GC 	GHJ 
K 6<0" 	  ??$otWE# + % YY/88@A
EFLf22:>00/7GL 		
K 
K	 	
K 
Ks#   B>A>B2!B>2B;	7B>>Cc                    d}d}d}t        ddg      5  t        d|d      5  | j                  dd	d
d|dd|gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      t        j                  ||      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   r   r   r   r   r   r   ro   r   rp   r   r   Tr   r   r   z^Gcloud Storage Command: {} storage hmac list {} --all --service-account {} --long --project {}r   r   s         r   8test_shim_translates_hmac_list_command_using_long_formatzITestHmacUnitShim.test_shim_translates_hmac_list_command_using_long_format  s    "GC 	GHJ 
K 6<0" 	  ??$otT7K# + % YY/88@A
LfY66zB22OWN		
K 
K	 	
K 
Ks#   B?A?B3"B?3B<	8B??Cc                    d}d}d}d}t        ddg      5  t        d|d      5  | j                  d	d
d|d|dd|gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      t        j                  ||d|      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   ABCDEFGHIK=r   r   r   r   r   r   r   r>   r   r   r?   r   Tr   r   r   PGcloud Storage Command: {} storage hmac update {} --etag {} --project {} --{} {}activater   r.   r   r   r;   r8   r   r   s          r   Ktest_shim_translates_hmac_update_command_when_active_state_option_is_passedz\TestHmacUnitShim.test_shim_translates_hmac_update_command_when_active_state_option_is_passed  s     #DGI	GHJ 
K 6<0" 	  ??6/7tT/6h/81-
 ?C + D YY/88@A
 HIO$<<ZH88$%yJ2 4>		?	
K 
K	 	
K 
K#   CBB8'C8C	=CCc                    d}d}d}d}t        ddg      5  t        d|d      5  | j                  d	d
d|d|dd|gd      }dj                  |j                  d         }| j                  dj                  t        j                  d      t        j                  ||d|      |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   r   r   r   r   r   r   r   r>   r   r   r?   r@   Tr   r   r   r   
deactivater   r   s          r   Mtest_shim_translates_hmac_update_command_when_inactive_state_option_is_passedz^TestHmacUnitShim.test_shim_translates_hmac_update_command_when_inactive_state_option_is_passed0  s    #DGI	GHJ 
K 6<0" 	  ??6/7tT/6j/81-
 ?C + D YY/88@A
 HIO$<<ZH88$'J4 6@		A	
K 
K	 	
K 
Kr   N)r"   r#   r$   r   patchobjectr   HmacCommandMockr   r   r   r   r   r   r   r%   r   r   r   r     s   ::T%%|E9 F9, ::T%%|E< F<( ::T%%|E" F", ::T%%|E F. ::T%%|E F. ::T%%|E? F?6 ::T%%|EA FAr   r   ),r   
__future__r   r   r   r   r   osr*   gslib.commandsr   gslib.project_idr   gslib.tests.testcaseteststestcase)gslib.tests.testcase.integration_testcaser	   r
   gslib.tests.utilr   r   r   gslib.utils.retry_utilr   gslib.utilsr   sixr   r   	six.movesr   r   ri   rq   r   r   	Exceptionr!   GsUtilIntegrationTestCaser'   r   ShimUnitTestBaser   r%   r   r   <module>r     s   . &  % '  	 	  . ' ' ? @ 1 2 % ( ! % VV_	5 6 8 &&AB)*IJ *+KL  I  /001J$(<< J$ 2 1J$Z
 /0QX?? Q 1Q,kAx00 kAr   