
    _                        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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  SSKJrJr  \" \" SSS5      5        SSKJr  S r \ " S5      r!\ " S5      r"\ " S5      r#Sr$ " S S\%5      r&\" S5      \" S5       " S S\RN                  5      5       5       r(\" S5       " S S \RN                  5      5       r) " S! S"\RT                  5      r+g)#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    [         R                  R                  SU 5      $ )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                       \ rS rSrSrg)KeyLimitError5    N)__name__
__module____qualname____firstlineno____static_attributes__r#   r   r   r!   r!   5   s    r   r!   z"S3 does not have an equivalent APIz"XML HMAC control is not supported.c                      \ rS rSrSrS rS r    SS jrS r\	" \
SSS	9S
 5       rS rS rS r\R                   " \S5      S 5       rS r\R                   " \S5      S 5       r\R                   " \S5      S 5       rS r\R                   " \S5      S 5       rS rS r\R                   " \=(       a    \S5      S 5       rS r\R                   " \=(       a    \S5      S 5       rS r\R                   " \S5      S 5       r Sr!g)TestHmacIntegration9   a  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                     [         R                  " SU5      nU(       d  U R                  SU-  5        UR                  S5      $ )Nz(GOOG[\S]*)z.Couldn't find Access Id in output string:
"%s"r   researchfailgroup)selfoutput_stringid_matchs      r   ExtractAccessId#TestHmacIntegration.ExtractAccessIdD   s;    yy7H
iiB >>!r   c                     [         R                  " SU5      nU(       d  U R                  SU-  5        UR                  S5      $ )Nz\sEtag:\s+([\S]+)$z)Couldn't find Etag in output string:
"%s"   r-   )r2   r3   
etag_matchs      r   ExtractEtagTestHmacIntegration.ExtractEtagK   s9    0-@J
ii=MNAr   c                     U R                  USU-  5        U R                  USU-  5        U R                  USU-  5        U R                  USU-  5        U R                  US5        U R                  US5        g )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)r2   r3   	access_idstateservice_accountprojects         r   AssertKeyMetadataMatches,TestHmacIntegration.AssertKeyMetadataMatchesQ   s     	]$4y$@A]$4u$<=]2_DF]$7'$AB]$<=]$ABr   c                      U R                  SSSSU/5        U R                  SSU/5        g ! [         a  nS[        U5      ;  a  e  S nAN4S nAff = f)Nr   update-sINACTIVEz!Update must modify the credentialdelete)	RunGsUtilAssertionErrorstr)r2   r>   es      r   CleanupHelper!TestHmacIntegration.CleanupHelper_   sY    
nnfhj)DE
 	NNFHi01	  	,CF	: 
;s   - 
AAA      )triestimeout_secsc                      U R                  SSU/SS9$ ! [         a)  nS[        U5      ;   a  [        [        U5      5      ee SnAff = f)z%Retry creation on key limit failures.r   createTreturn_stdoutzHMAC key limit reachedN)rI   rJ   rK   r!   )r2   r@   rL   s      r   _CreateWithRetry$TestHmacIntegration._CreateWithRetryi   sW    ^^VX?*.  0 0 	!SV	+CF##	s    
A$AAc                 F    U R                  U5      nU R                  U5      $ )N)rW   r5   )r2   r@   stdouts      r   CreateHelper TestHmacIntegration.CreateHelperv   s#    ""?3F''r   c                     / SQnU Ho  u  p#nU R                   (       a  SOSnU R                  UR                  5       SUS9nU R                   (       a  U R                  XF5        M^  U R                  X65        Mq     g )N)
)zhmac createrequires a service account+argument SERVICE_ACCOUNT: Must be specified)zhmac create -p projr^   r_   )zhmac deleterequires an Access ID%argument ACCESS_ID: Must be specified)zhmac delete -p projr`   ra   )zhmac getr`   ra   )zhmac get -p projr`   ra   )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 INACTIVEr`   ra   )zhmac update -s INACTIVE -p projr`   ra      r8   Treturn_stderrexpected_status)_use_gcloud_storagerI   splitassertIn)r2   paramscommandgsutil_error_substrgcloud_error_substrre   stderrs          r   test_malformed_commands+TestHmacIntegration.test_malformed_commandsz   ss    F, >D9&9!551o~~gmmo,0/>  Af 
	!	!)2)2 >Dr   c                     U R                   (       a  [        R                  " S5      eSS/nU H5  u  p#U R                  UR	                  5       SSS9nU R                  X45        M7     g )Nz3These commands cannot be translated using the shim.)zhmac create -u emailr^   )zhmac update -s KENTUCKYzstate flag value must be one ofTr8   rc   )rf   r   SkipTestrI   rg   rh   )r2   ri   rj   rk   rm   s        r   @test_malformed_commands_that_cannot_be_translated_using_the_shimTTestHmacIntegration.test_malformed_commands_that_cannot_be_translated_using_the_shim   sq     ' ( ( 	?FF )/$~~gmmo,0./  1f mm'0	 )/r   z,Test requires service account configuration.c                    U R                  SS[        /SS9n U R                  US5        U R                  US5        U R                  U5      nU R	                  U5        g ! U R                  U5      nU R	                  U5        f = f)Nr   rT   TrU   zAccess ID:\s+\S+zSecret:\s+\S+)rI   SERVICE_ACCOUNTr=   r5   rM   )r2   rZ   r>   s      r   test_createTestHmacIntegration.test_create   s     ^^VX?*.  0F$
v23
v/0&&v.i
# &&v.i
#s   $A   $Bc                 N    U R                  / SQSSS9nU R                  SU5        g )N)r   rT   zDNE@mail.comTr8   rc   z)Service Account 'DNE@mail.com' not found.)rI   rh   r2   rm   s     r   test_create_sa_not_found,TestHmacIntegration.test_create_sa_not_found   s/    ^^>*.,-  /F 	MM?Hr   c                     U R                  [        5      nU R                  SSSSU/5        U R                  SSU/5        U R                  SSS[        /SS	9nU R                  X5        g )
Nr   rE   rF   rG   rH   list-uTrU   )r[   ALT_SERVICE_ACCOUNTrI   assertNotInr2   r>   rZ   s      r   test_deleteTestHmacIntegration.test_delete   so     !!"56INNFHdJ	BCNNFHi01^^VVT3FG*.  0FY'r   c                    U R                  [        5      nU R                  SSU/SSS9n U R                  (       a  U R	                  SU5        OU R	                  SU5        U R                  U5        g ! U R                  U5        f = f)Nr   rH   Tr8   rc   z2HTTPError 400: Cannot delete keys in ACTIVE state.z'400 Cannot delete keys in ACTIVE state.)r[   ru   rI   rf   rh   rM   )r2   r>   rm   s      r   test_delete_active_key*TestHmacIntegration.test_delete_active_key   s     !!/2I^^VXy9*.,-  /F$		!	!J	 	?H
#d#s   6A3 3Bc                     U R                  / SQSSS9nU R                  (       a  U R                  SU5        g U R                  SU5        g )N)r   rH   GOOG1234DNETr8   rc   -HTTPError 404: Access ID not found in project404 Access ID not foundrI   rf   rh   ry   s     r   test_delete_key_not_found-TestHmacIntegration.test_delete_key_not_found   sG    ^^=*.,-  /F 
mmCVL
mm-v6r   c                     U R                  [        5      nU R                  SSU/SS9n U R                  UU[        [	        S 5      S9  U R                  U5        g ! U R                  U5        f = f)Nr   getTrU   )r>   r@   rA   )r[   r   rI   rB   r   rM   r   s      r   test_getTestHmacIntegration.test_get   sv     !!"56I^^VUI6d^KF$
##
-#D)	 $  #d#s   A A.c                     U R                  / SQSSS9nU R                  (       a  U R                  SU5        g U R                  SU5        g )N)r   r   r   Tr8   rc   r   r   r   ry   s     r   test_get_not_found&TestHmacIntegration.test_get_not_found   sG    ^^:*.,-  /F 
mmCVL
mm-v6r   c                 ^    [        [        5       H  nU R                  SS[        /S S9  M     g )Nr   rT   )re   )rangeMAX_SA_HMAC_KEYSrI   LIST_SERVICE_ACCOUNT)r2   _s     r   setUpListTest!TestHmacIntegration.setUpListTest   s0    #$
nnfh(<=%)  + %r   c                 .   U R                  5         U R                  [        5      nU R                  SSSSU/5        U R                  SSS[        /SS9nUR                  5       R                  S	5      nS
nU HU  nUR                  5       u  pVnU R                  SU5        U R                  U[        5        U R                  US5        US-  nMW     U R                  U[        5        U R                  SS/SS9nUR                  5       R                  S	5      nS
nSn	U H1  nUR                  5       u  pnUS-  nU[        :X  d  M'  US:X  d  M/  Sn	M3     U R                  X5        U R                  U	5        U R                  SSU/5        U R                  / SQSS9nUR                  5       R                  S	5      nS
nSnU H1  nUR                  5       u  pnUS-  nU[        :X  d  M'  US:X  d  M/  SnM3     U R                  U5        U R                  X5        g )Nr   rE   rF   rG   r}   r~   TrU   
r   GOOGACTIVEr8   FrH   )r   r}   -aDELETED)r   r[   ru   rI   r   striprg   rh   assertEqualr   assertGreater
assertTrue)r2   alt_access_idrZ   list_account_key_countliner>   r?   accountproject_key_countinactive_key_listedr   deleted_key_listeds               r   	test_list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              #      #    SnUR                  S5       H0  nU(       a  UR                  S5      (       a  Uv   UnOX#-  nUS-  nM2     g 7f)N r   z	Access ID)rg   
startswith)r2   rZ   current_keyls       r   ParseListOutput#TestHmacIntegration.ParseListOutput:  sK     K\\$	k22Tk  s   A	Ac                    U R                  5         U R                  [        5      nU R                  SSSSU/5        U R                  / SQSS9n U R	                  SU5        U R	                  SU5        U R	                  [        U5        U R	                  [
        U5        U R                  U5       H  nU R                  US	S
9  M     U R                  U5        g ! U R                  U5        f = f)Nr   rE   rF   rG   )r   r}   -lTrU   z ACTIVE.*r?   )	r   r[   r   rI   rh   r   r   rB   rM   )r2   r   rZ   key_metadatas       r   test_list_long_format)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   A8C C,c                     SnU R                  SSSU/SSS9nU R                  (       a"  U R                  SR                  U5      U5        g U R                  S	U-  U5        g )
Nzservice-account-DNE@gmail.comr   r}   r~   Tr8   rc   z.HTTPError 404: Service Account '{}' not found.zService Account '%s' not found.)rI   rf   rh   format)r2   r@   rm   s      r   #test_list_service_account_not_found7TestHmacIntegration.test_list_service_account_not_foundY  so    5O^^VVT?C*.,-  /F 
mm
<
C
C &( mm7/Ir   c           	      \   U R                  [        5      nU R                  SSU/SS9nU R                  U5      n U R	                  USS9  U R                  SSSS	S
X1/SS9nU R	                  US	S9  U R                  SSU/SS9nU R	                  US	S9  U R                  SSSSU/SS9nU R	                  USS9  U R                  SSU/SS9nU R	                  USS9  U R                  SSSS	S
SU/SSS9nU R                  SU5        U R                  U5        g ! U R                  U5        f = f)Nr   r   TrU   r   r   rE   rF   rG   -ebadEtagr8   rc   z#Etag does not match expected value.)r[   r   rI   r:   rB   rh   rM   )r2   r>   rZ   etagrm   s        r   test_updateTestHmacIntegration.test_updateg  sx    !!"56I^^VUI6d^KFF#D$
##F(#;~~8T:tT
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 D+r#   N)zGOOG.*r   r   r   )"r$   r%   r&   r'   __doc__r5   r:   rB   rM   r   r!   rW   r[   rn   rr   r   
skipUnlessru   rv   rz   r   r   r   r   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 EG$G$I *EG(G( EG$G$ 7 *EG$G$7+
 +?EG2BG2Bh +C0CEG(G(& *EG$G$r   r*   c                   &    \ rS rSrSr/ SQrS rSrg)TestHmacXmlIntegrationi  z-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                     [        U R                  5         S H*  nSU/nU R                  USSS9nU R                  SU5        M,     S S S 5        g ! , (       d  f       g = f)N)rT   rH   r   r}   rE   r   r8   T)re   rd   z9The "hmac" command can only be used with the GCS JSON API)r   boto_config_hmac_auth_onlyrI   rh   )r2   
subcommandrj   rm   s       r   test_hmac_fails_for_xml.TestHmacXmlIntegration.test_hmac_fails_for_xml  sW    	d==	>E*:&$OG	Q F 
?	>	>s   1A
Ar#   N)r$   r%   r&   r'   r   r   r   r(   r#   r   r   r   r     s    5	 Qr   r   c                   @   \ rS rSr\R
                  R                  \R                  S\R                  " 5       S9S 5       r
\R
                  R                  \R                  S\R                  " 5       S9S 5       r\R
                  R                  \R                  S\R                  " 5       S9S 5       r\R
                  R                  \R                  S\R                  " 5       S9S 5       r\R
                  R                  \R                  S\R                  " 5       S9S 5       r\R
                  R                  \R                  S\R                  " 5       S9S	 5       r\R
                  R                  \R                  S\R                  " 5       S9S
 5       rSrg)TestHmacUnitShimi  
RunCommand)newc           
         SnSnSn[        SS/5         [        SUS.5         U R                  SS	S
X2/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      [        R                  UU5      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)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   rT   -pTargsreturn_log_handlerr   infozAGcloud Storage Command: {} storage hmac create {} --project {} {})r   r   r   joinmessagesrh   r   r   _get_gcloud_binary_pathr   _CREATE_COMMAND_FORMAT)r2   fake_cloudsdk_dirr@   rA   mock_log_handler
info_liness         r   (test_shim_translates_hmac_create_command9TestHmacUnitShim.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s#   B>A9B-B>-
B;	7B>>
Cc           	      ~   SnSnSn[        SS/5         [        SUS.5         U R                  SS	S
X#/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      X#5      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr   test-projectfake123456789r   r   r   r   r   rH   r   Tr   r   r   z>Gcloud Storage Command: {} storage hmac delete --project {} {})	r   r   r   r   r   rh   r   r   r   r2   r   rA   r>   r   r   s         r   #test_shim_translates_delete_command4TestHmacUnitShim.test_shim_translates_delete_command  s    "GI	GHJ 
K 6<0" 	  ??D'5# + % YY/88@A
 67=v$<<ZH"8/0:	<	
K 
K	 	
K 
Ks#   B.A)BB.
B+	'B..
B<c           
         SnSnSn[        SS/5         [        SUS.5         U R                  SS	S
X#/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      [        R                  X#5      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)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   rh   r   r   r   r   _DESCRIBE_COMMAND_FORMATr   s         r   !test_shim_translates_get_commannd2TestHmacUnitShim.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'=N !		"	
K 
K	 	
K 
Ks#   B=A8B,B=,
B:	6B==
Cc                    SnSnSn[        SS/5         [        SUS.5         U R                  SS	S
SUSU/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      [        R                  X25      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr   r   r   r   r   r   r   r   r}   r   r~   r   Tr   r   r   zWGcloud Storage Command: {} storage hmac list {} --all --service-account {} --project {})r   r   r   r   r   rh   r   r   r   r   _LIST_COMMAND_SHORT_FORMATr2   r   rA   r@   r   r   s         r   9test_shim_translates_hmac_list_command_using_short_formatJTestHmacUnitShim.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/GL 		
K 
K	 	
K 
Ks#   C A;B/C /
B=	9C  
Cc                    SnSnSn[        SS/5         [        SUS.5         U R                  SS	S
SUSSU/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      [        R                  X25      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr   r   r   r   r   r   r   r   r}   r   r~   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_formatITestHmacUnitShim.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2ON		
K 
K	 	
K 
Ks#   CA<B0C0
B>	:C
Cc                    SnSnSnSn[        SS/5         [        SUS.5         U R                  S	S
SUSUSSU/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      [        R                  X#SU5      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr   ABCDEFGHIK=r   r   r   r   r   r   r   rE   r   r   rF   r   Tr   r   r   PGcloud Storage Command: {} storage hmac update {} --etag {} --project {} --{} {}activater   r2   r   r   rA   r>   r   r   s          r   Ktest_shim_translates_hmac_update_command_when_active_state_option_is_passed\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A?B5$C5
C	?C
Cc                    SnSnSnSn[        SS/5         [        SUS.5         U R                  S	S
SUSUSSU/SS9nSR                  UR                  S   5      nU R                  SR                  [        R                  " S5      [        R                  X#SU5      U5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr   r  r   r   r   r   r   r   r   rE   r   r   rF   rG   Tr   r   r   r  
deactivater   r  s          r   Mtest_shim_translates_hmac_update_command_when_inactive_state_option_is_passed^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  r#   N)r$   r%   r&   r'   r   patchobjectr   HmacCommandMockr   r   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   ru   r   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   