
    }w                         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\5      rg)zBGsutil API delegator for interacting with cloud storage providers.    )absolute_import)print_function)division)unicode_literalsN)config)context_config)ArgumentException)CloudApi)ApiMapConstants)ApiSelector)CommandException)	boto_utilc                     ^  \ rS rSrSr      S9U 4S jjrS rS rS:S jrS:S	 jr	S;S
 jr
S;S jrS:S jrS<S jr     S=S jrS:S jr    S>S jrS;S jr   S<S jr  S;S jr     S=S jr   S<S jr     S=S jrSSSS\R0                  R2                  SSSSSS4S jr        S?S jr       S@S jr          SAS jr         SBS jr    S>S jr   S<S jr    S<S jr!S:S  jr"S:S! jr#S:S" jr$    S>S# jr%S:S$ jr&S:S% jr'S:S& jr(S:S' jr)S:S( jr*S:S) jr+S:S* jr,  SCS+ jr-S, r.S:S- jr/SCS. jr0   SDS/ jr1S:S0 jr2S:S1 jr3S:S2 jr4 S:S3 jr5S:S4 jr6S:S5 jr7S:S6 jr8S:S7 jr9S8r:U =r;$ )ECloudApiDelegator!   a  Class that handles delegating requests to gsutil Cloud API implementations.

This class is responsible for determining at runtime which gsutil Cloud API
implementation should service the request based on the Cloud storage provider,
command-level API support, and configuration file override.

During initialization it takes as an argument a gsutil_api_map which maps
providers to their default and supported gsutil Cloud API implementations
(see comments in cs_api_map for details).

Instantiation of multiple delegators per-thread is required for multiprocess
and/or multithreaded operations. Calling methods on the same delegator in
multiple threads is unsafe.
Nr   c                   > [         [        U ]  UUUUUUUU	U
S9	  X l        [        R
                  R                  SSS5      R                  5       U l        0 U l	        U R                  [        R                     (       d  [        S5      eg)a  Performs necessary setup for delegating cloud storage requests.

This function has different arguments than the gsutil Cloud API __init__
function because of the delegation responsibilties of this class.

Args:
  bucket_storage_uri_class: boto storage_uri class, used by APIs that
                            provide boto translation or mocking.
  gsutil_api_map: Map of providers and API selector tuples to api classes
                  which can be used to communicate with those providers.
  logger: logging.logger for outputting log messages.
  status_queue: Queue for relaying status to UI.
  provider: Default provider prefix describing cloud storage provider to
            connect to.
  debug: Debug level for the API implementation (0..3).
  http_headers (dict|None): Arbitrary headers to be included in every request.
  trace_token: Apiary trace token to pass to API.
  perf_trace_token: Performance trace token to use when making API calls.
  user_project: Project to be billed for this project.
providerdebughttp_headerstrace_tokenperf_trace_tokenuser_projectGSUtil
prefer_api z.No apiclass supplied for gsutil Cloud API map.N)superr   __init__api_mapbotor   getupperr   loaded_apisr   API_MAPr	   )selfbucket_storage_uri_classgsutil_api_maploggerstatus_queuer   r   r   r   r   r   	__class__s              ,platform/gsutil/gslib/cloud_api_delegator.pyr   CloudApiDelegator.__init__1   s    > 

T+,D,2,85=279E8C=M9E , G "LkkoohbAGGIDOD<<//0NOO 1    c                 .   U=(       d    U R                   nU(       d  [        S5      e[        U5      nXR                  ;  a  0 U R                  U'   U R	                  U5      nX R                  U   ;  a  U R                  X5        U R                  U   U   $ )a  Returns a valid CloudApi for use by the caller.

This function lazy-loads connection and credentials using the API map
and credential store provided during class initialization.

Args:
  provider: Provider to load API for. If None, class-wide default is used.

Raises:
  ArgumentException if there is no matching API available in the API map.

Returns:
  Valid API instance that can be used to communicate with the Cloud
  Storage provider.
z No provider selected for _GetApi)r   r	   strr#   GetApiSelector_LoadApir%   r   api_selectors      r+   _GetApiCloudApiDelegator._GetApi`   s      (4==H@AA8}H'''#%dx &&x0L++H55
mmH+H%l33r-   c                    XR                   [        R                     ;  a  [        SU-  5      eX R                   [        R                     U   ;  a  [        SU< SU< S35      eU R                   [        R                     U   U   " U R                  U R
                  U R                  UU R                  U R                  U R                  U R                  U R                  S9	U R                  U   U'   g)zLoads a CloudApi into the loaded_apis map for this class.

Args:
  provider: Provider to load the API for.
  api_selector: cs_api_map.ApiSelector defining the API type.
z7gsutil Cloud API map contains no entry for provider %s.z*gsutil Cloud API map does not support API z for provider .r   N)r   r   r$   r	   r&   r(   r)   r   r   r   r   r   r#   r2   s      r+   r1   CloudApiDelegator._LoadApi~   s     ||O$;$;<<
Ch
NP P<<(?(?@JJ#$ $ 	_,,-h7E))KK****((!22**		, 	X|,r-   c                 T   U=(       d    U R                   nU(       d  [        S5      eX R                  [        R                     ;  dB  U R                  [        R                     U   U R                  [        R
                     U   ;  a  [        SU-  5      eX R                  [        R                     ;  a  [        SU-  5      eU R                  [        R                     U   nUS:H  =(       a    [        R                  " 5       nUS:H  =(       a5    [        R                  " SS5      =(       d    [        R                  " SS5      nU(       a  U(       a  [        S5      eU(       a  [        R                  nORU(       a  [        R                  nO:U R                  U R                  [        R                     U   ;   a  U R                  nU[        R                  :X  aI  [         R"                  " 5       (       a/  [         R"                  " 5       R$                  (       a  [        S	5      eU$ )
a  Returns a cs_api_map.ApiSelector based on input and configuration.

Args:
  provider: Provider to return the ApiSelector for.  If None, class-wide
            default is used.

Returns:
  cs_api_map.ApiSelector that will be used for calls to the delegator
  for this provider.
z!No provider selected for CloudApiz(No default api available for provider %sz+No supported apis available for provider %sgsr   encryption_keydecryption_key1a  gsutil does not support HMAC credentials with customer-supplied encryption keys (CSEK) or customer-managed KMS encryption keys (CMEK). Please generate and include non-HMAC credentials in your .boto configuration file, or to access public encrypted objects, remove your HMAC credentials.zUser enabled mTLS by setting "use_client_certificate", but mTLS is not supported for the selected XML API. Try configuring for  the GCS JSON API or setting "use_client_certificate" to "False" in the Boto config.)r   r	   r   r   DEFAULT_MAPr$   SUPPORT_MAPr   UsingGsHmacr   
has_optionr   r   XMLJSONr   r   get_context_configuse_client_certificate)r%   r   selected_providerapiusing_gs_hmacconfigured_encryptions         r+   r0    CloudApiDelegator.GetApiSelector   s    !1DMMABBo.I.I!JJ_0012CD||O3345FG	HH/0 1 1 _-H-H IIK/0 1 1 ,,22
34E
FC$@)>)>)@M%- N$//:JK M$//:KL  .34 4 
OOc 
c	_0012CD
FOOc{>#D#D#F#F))+BB  Jr-   c                 @    U R                  U5      R                  5       $ N)r4   GetServiceAccountId)r%   r   s     r+   rL   %CloudApiDelegator.GetServiceAccountId   s    <<!5577r-   c                 >    U R                  U5      R                  XS9$ N)fields)r4   	GetBucketr%   bucket_namer   rP   s       r+   rQ   CloudApiDelegator.GetBucket   s    <<!++K+GGr-   c                 >    U R                  U5      R                  XS9$ rO   )r4   GetBucketIamPolicyrR   s       r+   rV   $CloudApiDelegator.GetBucketIamPolicy   s    <<!44[4PPr-   c                 B    U R                  U5      R                  X5      $ rK   )r4   SetBucketIamPolicy)r%   rS   policyr   s       r+   rY   $CloudApiDelegator.SetBucketIamPolicy   s    <<!44[IIr-   c                 @    U R                  U5      R                  UUS9$ )N)
project_idrP   )r4   ListBuckets)r%   r]   r   rP   s       r+   r^   CloudApiDelegator.ListBuckets   s(    <<!--5; . = =r-   c           	      H    U R                  U5      R                  UUUUUUS9$ )N)
canned_aclcanned_def_aclpreconditionsrP   )r4   PatchBucket)r%   rS   metadatara   rb   rc   r   rP   s           r+   rd   CloudApiDelegator.PatchBucket   s6     <<!--k.69C=K<I5; . = =r-   c                 B    U R                  U5      R                  UUUS9$ )N)r   )r4   LockRetentionPolicy)r%   rS   metagenerationr   s       r+   rh   %CloudApiDelegator.LockRetentionPolicy   s-    <<!55k6D?G 6 I Ir-   c                 D    U R                  U5      R                  UUUUS9$ )N)r]   re   rP   )r4   CreateBucket)r%   rS   r]   re   r   rP   s         r+   rl   CloudApiDelegator.CreateBucket  s0     <<!..{:D8@6< / > >r-   c                 @    U R                  U5      R                  UUS9$ )N)rc   )r4   DeleteBucket)r%   rS   rc   r   s       r+   ro   CloudApiDelegator.DeleteBucket  s*    <<!..{=J / L Lr-   c                 D    U R                  U5      R                  UUUUS9$ rO   )r4   GetObjectIamPolicyr%   rS   object_name
generationr   rP   s         r+   rr   $CloudApiDelegator.GetObjectIamPolicy  s2     <<!44[5@5?<B 5 D Dr-   c                 D    U R                  U5      R                  XX45      $ rK   )r4   SetObjectIamPolicy)r%   rS   rt   rZ   ru   r   s         r+   rx   $CloudApiDelegator.SetObjectIamPolicy  s'     <<!44[5;I Ir-   c                 F    U R                  U5      R                  UUUUUS9$ )N)prefix	delimiterall_versionsrP   )r4   ListObjects)r%   rS   r{   r|   r}   r   rP   s          r+   r~   CloudApiDelegator.ListObjects'  s3     <<!--k5;8A;G5;	 . = =r-   c                 D    U R                  U5      R                  UUUUS9$ )N)ru   rP   )r4   GetObjectMetadatars   s         r+   r   #CloudApiDelegator.GetObjectMetadata4  s2     <<!33K4??I;A 4 C Cr-   c	           
      J    U R                  U5      R                  UUUUUUUS9$ )N)ra   ru   rc   rP   )r4   PatchObjectMetadata)	r%   rS   rt   re   ra   ru   rc   r   rP   s	            r+   r   %CloudApiDelegator.PatchObjectMetadata?  s:     <<!55# 6  r-   Fc                 V    U R                  U5      R                  UUUUUU	U
UUUUUUS9$ )N)
compressed_encodingdownload_strategy
start_byteend_byteru   object_sizeprogress_callbackserialization_data	digestersdecryption_tuple)r4   GetObjectMedia)r%   rS   rt   download_streamr   ru   r   r   r   r   r   r   r   r   r   s                  r+   r    CloudApiDelegator.GetObjectMediaQ  sL     <<!00/++-) 1 + +r-   c                 N    U R                  U5      R                  UUUUUUUU	U
S9	$ )N)sizera   rc   r   encryption_tuplerP   gzip_encoded)r4   UploadObject)r%   upload_streamobject_metadatar   ra   rc   r   r   r   rP   r   s              r+   r   CloudApiDelegator.UploadObjecto  s@     <<!..#+)! / 	# 	#r-   c
                 L    U R                  U5      R                  UUUUUUUU	S9$ )N)ra   rc   r   r   rP   r   )r4   UploadObjectStreaming)
r%   r   r   ra   rc   r   r   r   rP   r   s
             r+   r   'CloudApiDelegator.UploadObjectStreaming  s=     <<!77#+)! 8 # #r-   c                 R    U R                  U
5      R                  UUUUUUUUU	UUS9$ )N)	ra   rc   r   r   tracker_callbackr   r   rP   r   )r4   UploadObjectResumable)r%   r   r   ra   rc   r   r   r   r   r   r   rP   r   s                r+   r   'CloudApiDelegator.UploadObjectResumable  sF     <<!77#-)+)! 8 # #r-   c                 P    U R                  U
5      R                  UUUUUUUUU	US9
$ )N)src_generationra   rc   r   max_bytes_per_callr   r   rP   )r4   
CopyObject)r%   src_obj_metadatadst_obj_metadatar   ra   rc   r   r   r   r   r   rP   s               r+   r   CloudApiDelegator.CopyObject  sC     <<!,,%#+-)) - 
 
r-   c                 F    U R                  U5      R                  UUUUUS9$ )N)rc   r   rP   )r4   ComposeObject)r%   src_objs_metadatar   rc   r   r   rP   s          r+   r   CloudApiDelegator.ComposeObject  s4     <<!//#) 0  r-   c                 D    U R                  U5      R                  UUUUS9$ )N)rc   ru   )r4   DeleteObject)r%   rS   rt   rc   ru   r   s         r+   r   CloudApiDelegator.DeleteObject  s2     <<!..{/:=J:D / F Fr-   c                 F    U R                  U5      R                  UUUUUS9$ )N)tokenrP   )r4   WatchBucket)r%   rS   address
channel_idr   r   rP   s          r+   r   CloudApiDelegator.WatchBucket  s3     <<!--k.5.8495;	 . = =r-   c                 B    U R                  U5      R                  X5      $ rK   )r4   StopChannel)r%   r   resource_idr   s       r+   r   CloudApiDelegator.StopChannel  s    <<!--jFFr-   c                 B    U R                  U5      R                  U5      $ rK   )r4   ListChannelsr%   rS   r   s      r+   r   CloudApiDelegator.ListChannels  s    <<!..{;;r-   c                 B    U R                  U5      R                  U5      $ rK   )r4   GetProjectServiceAccount)r%   project_numberr   s      r+   r   *CloudApiDelegator.GetProjectServiceAccount  s    <<!::>JJr-   c                 F    U R                  U5      R                  XX4XV5      $ rK   )r4   CreateNotificationConfig)r%   rS   pubsub_topicpayload_formatevent_typescustom_attributesobject_name_prefixr   s           r+   r   *CloudApiDelegator.CreateNotificationConfig  s(     <<!::>/ /r-   c                 B    U R                  U5      R                  X5      $ rK   )r4   DeleteNotificationConfig)r%   rS   notificationr   s       r+   r   *CloudApiDelegator.DeleteNotificationConfig  s!    <<!::# #r-   c                 B    U R                  U5      R                  U5      $ rK   )r4   ListNotificationConfigsr   s      r+   r   )CloudApiDelegator.ListNotificationConfigs
  s    <<!99+FFr-   c                 B    U R                  U5      R                  U5      $ rK   )r4   ListBucketAccessControlsr   s      r+   r   *CloudApiDelegator.ListBucketAccessControls  s    <<!::;GGr-   c                 B    U R                  U5      R                  X5      $ rK   )r4   ListObjectAccessControls)r%   rS   rt   r   s       r+   r   *CloudApiDelegator.ListObjectAccessControls  s!    <<!::" "r-   c                 D    U R                  U5      R                  UU5      $ rK   )r4   CreateHmacKey)r%   r]   service_account_emailr   s       r+   r   CloudApiDelegator.CreateHmacKey  s%    <<!//
0EG Gr-   c                 B    U R                  U5      R                  X5      $ rK   )r4   DeleteHmacKeyr%   r]   	access_idr   s       r+   r   CloudApiDelegator.DeleteHmacKey  s    <<!//
FFr-   c                 B    U R                  U5      R                  X5      $ rK   )r4   
GetHmacKeyr   s       r+   r   CloudApiDelegator.GetHmacKey  s    <<!,,ZCCr-   c                 F    U R                  U5      R                  UUU5      $ rK   )r4   ListHmacKeys)r%   r]   r   show_deleted_keysr   s        r+   r   CloudApiDelegator.ListHmacKeys  s*    
 <<!..z/D/@B Br-   c
                 L    U R                  U5      R                  UUUUUUUU	S9$ )N)methoddurationpathru   r(   regionsigned_headersstring_to_sign_debug)r4   SignUrl)
r%   r   r   r   r   ru   r(   r   r   r   s
             r+   r   CloudApiDelegator.SignUrl'  s;    <<!))%1 * 3 3r-   c                 F    U R                  U5      R                  XUU5      $ rK   )r4   UpdateHmacKey)r%   r]   r   stateetagr   s         r+   r   CloudApiDelegator.UpdateHmacKey3  s%    <<!//
u046 6r-   c                 @    U R                  U5      R                  UUS9$ )a  XML compatibility function for getting ACLs.

Args:
  storage_url: StorageUrl object.
  def_obj_acl: If true, get the default object ACL on a bucket.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  ACL XML for the resource specified by storage_url.
)def_obj_acl)r4   XmlPassThroughGetAcl)r%   storage_urlr   r   s       r+   r   &CloudApiDelegator.XmlPassThroughGetAcl7  s,      <<!66{CN 7 P Pr-   c                 F    U R                  U5      R                  UUUUS9  g)a  XML compatibility function for setting ACLs.

Args:
  acl_text: XML ACL or canned ACL string.
  storage_url: StorageUrl object.
  canned: If true, acl_text is treated as a canned ACL string.
  def_obj_acl: If true, set the default object ACL on a bucket.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  None.
)cannedr   N)r4   XmlPassThroughSetAcl)r%   acl_textr   r   r   r   s         r+   r   &CloudApiDelegator.XmlPassThroughSetAclJ  s,    . 	LL//0;7=<G 0 Ir-   c                 B    U R                  U5      R                  U5      $ )a  XML compatibility function for getting CORS configuration on a bucket.

Args:
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  CORS configuration XML for the bucket specified by storage_url.
)r4   XmlPassThroughGetCorsr%   r   r   s      r+   r   'CloudApiDelegator.XmlPassThroughGetCorsf  s     <<!77DDr-   c                 D    U R                  U5      R                  X5        g)a  XML compatibility function for setting CORS configuration on a bucket.

Args:
  cors_text: Raw CORS XML string.
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  None.
N)r4   XmlPassThroughSetCors)r%   	cors_textr   r   s       r+   r  'CloudApiDelegator.XmlPassThroughSetCorsw  s      	LL00Hr-   c                 B    U R                  U5      R                  U5      $ )a  XML compatibility function for getting lifecycle config on a bucket.

Args:
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  Lifecycle configuration XML for the bucket specified by storage_url.
)r4   XmlPassThroughGetLifecycler  s      r+   r  ,CloudApiDelegator.XmlPassThroughGetLifecycle  s     <<!<<[IIr-   c                 F    U R                  U5      R                  UU5        g)a  XML compatibility function for setting lifecycle config on a bucket.

Args:
  lifecycle_text: Raw lifecycle configuration XML string.
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  None.
N)r4   XmlPassThroughSetLifecycle)r%   lifecycle_textr   r   s       r+   r  ,CloudApiDelegator.XmlPassThroughSetLifecycle  s!    & 	LL55n6ACr-   c                 B    U R                  U5      R                  U5      $ )a  XML compatibility function for getting logging configuration on a bucket.

Args:
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  Logging configuration XML for the bucket specified by storage_url.
)r4   XmlPassThroughGetLoggingr  s      r+   r  *CloudApiDelegator.XmlPassThroughGetLogging       <<!::;GGr-   c                 B    U R                  U5      R                  X5      $ )a,  XML compatibility function for setting tagging configuration on a bucket.

This passthrough provides support for setting a tagging configuration
(equivalent to a label configuration) on a cloud bucket.

Args:
  tags_text: Raw tagging configuration XML string.
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  None.
)r4   XmlPassThroughSetTagging)r%   	tags_textr   r   s       r+   r  *CloudApiDelegator.XmlPassThroughSetTagging  s#    & <<!::   r-   c                 B    U R                  U5      R                  U5      $ )a  XML compatibility function for getting tagging configuration on a bucket.

Args:
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  Tagging configuration XML for the bucket specified by storage_url.
)r4   XmlPassThroughGetTaggingr  s      r+   r  *CloudApiDelegator.XmlPassThroughGetTagging  r  r-   c                 B    U R                  U5      R                  U5      $ )a  XML compatibility function for getting website configuration on a bucket.

Args:
  storage_url: StorageUrl object.
  provider: Cloud storage provider to connect to.  If not present,
            class-wide default is used.

Raises:
  ArgumentException for errors during input validation.
  ServiceException for errors interacting with cloud storage providers.

Returns:
  Website configuration XML for the bucket specified by storage_url.
)r4   XmlPassThroughGetWebsiter  s      r+   r  *CloudApiDelegator.XmlPassThroughGetWebsite  r  r-   )r   r#   r   )Nr   NNNNrK   )NN)NNN)NNNNN)NNNN)NNNNNNNF)NNNNNNF)
NNNNNNNNNF)	NNNNNNNNN)FN)TFN)<__name__
__module____qualname____firstlineno____doc__r   r4   r1   r0   rL   rQ   rV   rY   r^   rd   rh   rl   ro   rr   rx   r~   r   r   r
   DownloadStrategyONE_SHOTr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  __static_attributes____classcell__)r*   s   @r+   r   r   !   s   (   $ -P^4<-4EN8HQJ= "!% $=I #  	>L %)"& $	D %)"&I  #=  $(!%#	C &*%)(,#'!%, # $!%).'/'@'@'I'I !"'+(,#&*+B "!%%)$(  %#2 (,*..2-1%)#').#. (,*.!%/3-1.2-1%)#').#: !% ##'$("&"&6 #'%)!" "&" 	F =G<K ,01526(,
/#GH"GGD &+ 	B
36P, #'',$(I8E"I$J( +/C,H" ,H"H Hr-   r   )r   
__future__r   r   r   r   r    r   gslibr   gslib.cloud_apir	   r
   gslib.cs_api_mapr   r   gslib.exceptionr   gslib.utilsr   r    r-   r+   <module>r,     sB    I & %  '     - $ , ( , !VH VHr-   