
    ,"                        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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 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Z!dZ"dZ#dZ$e"e!jK                  d      z   dz   Z&ddjO                  e#e$g      z   dz   Z( ee&e(      Z) ee!e#      Z* ee"e$      Z+ G d de
      Z,y)z=Implementation of cors configuration command for GCS buckets.    )absolute_import)print_function)division)unicode_literalsN)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)StorageUrlFromString)UrlsAreForSingleProvider)storage_v1_messages)NO_MAX)GcloudStorageMap)CorsTranslation)REMOVE_CORS_CONFIGz&
  gsutil cors get gs://<bucket_name>
z:
  gsutil cors set <cors-json-file> gs://<bucket_name>...
z
<B>GET</B>
  Gets the CORS configuration for a single bucket. The output from
  ``cors get`` can be redirected into a file, edited and then updated using
  ``cors set``.
z
<B>SET</B>
  Sets the CORS configuration for one or more buckets. The ``cors-json-file``
  specified on the command line should be a path to a local file containing
  a JSON-formatted CORS configuration, such as the example described above.

z

a  
  Gets or sets the Cross-Origin Resource Sharing (CORS) configuration on one or
  more buckets. This command is supported for buckets only, not objects. An
  example CORS JSON file looks like the following:

    [
      {
        "origin": ["http://origin1.example.com"],
        "responseHeader": ["Content-Type"],
        "method": ["GET"],
        "maxAgeSeconds": 3600
      }
    ]

  The above CORS configuration explicitly allows cross-origin GET requests from
  http://origin1.example.com and may include the Content-Type response header.
  The preflight request may be cached for 1 hour.

  Note that requests to the authenticated browser download endpoint ``storage.cloud.google.com``
  do not allow CORS requests. For more information about supported endpoints for CORS, see
  `Cloud Storage CORS support <https://cloud.google.com/storage/docs/cross-origin#server-side-support>`_.

  The following (empty) CORS JSON file removes any CORS configuration for a
  bucket:

    []

  The cors command has two sub-commands:
z
For more info about CORS generally, see https://www.w3.org/TR/cors/.
For more info about CORS in Cloud Storage, see the
`CORS concept page <https://cloud.google.com/storage/docs/cross-origin>`_.
c                      e Zd ZdZ ej
                  dddgededdddej                  ej                  gej                   ej                  d       ej                         g ej                  d      gd	
      Z ej                   dg dddeeed      Z e eg di        eg di       di       Zd Zd Zd Zd Zy)CorsCommandz&Implementation of gsutil cors command.corsgetcorssetcors    F   )setget)command_name_aliasesusage_synopsismin_argsmax_argssupported_sub_argsfile_url_okprovider_url_okurls_start_arggs_api_supportgs_default_apiargparse_arguments)r   r   zcross-origincommand_helpz7Get or set a CORS configuration for one or more buckets)r   r   )	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_text)storagebucketsdescribeze--format=gsutiljson[key=cors_config,empty=' has no CORS configuration.',empty_prefix_key=storage_url])gcloud_commandflag_map)r2   r3   updatez--cors-filec                 ~    | j                   s| j                          | j                   d   j                         dk(  ryy)Nr   r   r   r   )args$RaiseWrongNumberOfArgumentsExceptionlower)selfs    &platform/gsutil/gslib/commands/cors.py_CalculateUrlsStartArgz"CorsCommand._CalculateUrlsStartArg   s4    99
//1yy|u$    c                 ^   | j                   d   }| j                   dd }t        |      st        d| j                  z        t	        |d      }|j                         }|j                          | j                  j                  t        |d         j                        | _        d}|D ]  }| j                  |dg      }|D ]  }|j                  }	d	}| j                  j                  d
|       |	j                  dk(  r)| j                  j!                  ||	|	j                         et#        j$                  |      }
|
st&        }
t)        j*                  |
      }| j                  j-                  |	j.                  ||	j                  dg         |st        t0        t3        |      z        y)z9Sets CORS configuration on a Google Cloud Storage bucket.r   r   Nz,"%s" command spanning providers not allowed.rFidbucket_fieldsTzSetting CORS on %s...s3provider)r   )rG   fields)r9   r   r   command_nameopenreadclose
gsutil_apiGetApiSelectorr   schemeapiGetBucketUrlIterFromArgstorage_urlloggerinfoXmlPassThroughSetCorsr   JsonCorsToMessageEntriesr   apitools_messagesBucketPatchBucketbucket_namer   list)r<   cors_argurl_args	cors_filecors_txtsome_matchedurl_strbucket_iterblrurlr   bucket_metadatas               r=   _SetCorszCorsCommand._SetCors   s   yy|Hyy}H#H-K!../ 0 0 Xs#I~~HOO--Xa[)002DH L000Ok#oo0#6::
//
/
/039< 0 E !99(C$%D-44$?/
//
%
%coo&5/2zz.2V & 5  & 3d8nDEEr?   c                    | j                  | j                  d   dg      \  }}|j                  dk(  rEt        j                  j                  | j                  j                  ||j                               y|j                  r=t        j                  j                  t        j                  |j                               yt        j                  j                  d|z         y)z:Gets CORS configuration for a Google Cloud Storage bucket.r   r   rC   rE   rF   z%s has no CORS configuration.
)GetSingleBucketUrlFromArgr9   rO   sysstdoutwriterM   XmlPassThroughGetCorsr   r   MessageEntriesToJson)r<   
bucket_urlre   s      r=   _GetCorszCorsCommand._GetCors   s    "&"@"@		!VH #A #.J D 	jj
//
/
/
9C9J9J 0 LM  
		

001E1EF	H  	

:ZGHr?   c                     | j                   j                  d      }|dk(  r| j                  }n.|dk(  r| j                  }nt	        d|d| j
                  d      t        j                  |g        |       S )z)Command entry point for the cors command.r   r   r   zInvalid subcommand "z
" for the z! command.
See "gsutil help cors".)subcommands)r9   popro   rf   r   rI   r   LogCommandParams)r<   action_subcommandfuncs      r=   
RunCommandzCorsCommand.RunCommand   sr    		a(E!]]d	e	#]]d):D<M<MOP P *;)<=6Mr?   N)__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r
   XMLJSONr	   MakeNFileURLsArgument%MakeZeroOrMoreCloudBucketURLsArgumentMakeNCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   gcloud_storage_mapr>   rf   ro   rv    r?   r=   r   r   g   s   . +**%y1!oo{'7'78 %% 4o33A6CoCCE ?/>>qAC	,( g
 
C#)" ( "
  " 	$ ',(T"r?   r   )-rz   
__future__r   r   r   r   ri   gslibr   gslib.commandr   gslib.command_argumentr	   gslib.cs_api_mapr
   gslib.exceptionr   r   gslib.help_providerr   gslib.storage_urlr   r   "gslib.third_party.storage_apitoolsr   rW   gslib.utils.constantsr   gslib.utils.shim_utilr   gslib.utils.translation_helperr   r   _GET_SYNOPSIS_SET_SYNOPSIS_GET_DESCRIPTION_SET_DESCRIPTIONlstripr|   join_DESCRIPTIONr   r   r   r   r   r?   r=   <module>r      s    D & %  ' 
  ! 2 ( , 2 . 2 6 W ( 2 : =   M0066?	8 ii!#34596899 D %Y= /?@/?@N' Nr?   