OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 Google Inc. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from xml.dom.minidom import parseString |
| 16 import xml.sax.xmlreader |
| 17 from boto import handler |
| 18 from boto.gs.cors import Cors |
| 19 from gslib.command import Command |
| 20 from gslib.command import COMMAND_NAME |
| 21 from gslib.command import COMMAND_NAME_ALIASES |
| 22 from gslib.command import CONFIG_REQUIRED |
| 23 from gslib.command import FILE_URIS_OK |
| 24 from gslib.command import MAX_ARGS |
| 25 from gslib.command import MIN_ARGS |
| 26 from gslib.command import PROVIDER_URIS_OK |
| 27 from gslib.command import SUPPORTED_SUB_ARGS |
| 28 from gslib.command import URIS_START_ARG |
| 29 from gslib.exception import CommandException |
| 30 from gslib.help_provider import HELP_NAME |
| 31 from gslib.help_provider import HELP_NAME_ALIASES |
| 32 from gslib.help_provider import HELP_ONE_LINE_SUMMARY |
| 33 from gslib.help_provider import HELP_TEXT |
| 34 from gslib.help_provider import HelpType |
| 35 from gslib.help_provider import HELP_TYPE |
| 36 from gslib.util import NO_MAX |
| 37 |
| 38 _detailed_help_text = (""" |
| 39 <B>SYNOPSIS</B> |
| 40 gsutil setcors cors-xml-file uri... |
| 41 |
| 42 |
| 43 <B>DESCRIPTION</B> |
| 44 Sets the Cross-Origin Resource Sharing (CORS) configuration on one or more |
| 45 buckets. This command is supported for buckets only, not objects. The |
| 46 cors-xml-file specified on the command line should be a path to a local |
| 47 file containing an XML document with the following structure: |
| 48 |
| 49 <?xml version="1.0" ?> |
| 50 <CorsConfig> |
| 51 <Cors> |
| 52 <Origins> |
| 53 <Origin>origin1.example.com</Origin> |
| 54 </Origins> |
| 55 <Methods> |
| 56 <Method>GET</Method> |
| 57 </Methods> |
| 58 <ResponseHeaders> |
| 59 <ResponseHeader>Content-Type</ResponseHeader> |
| 60 </ResponseHeaders> |
| 61 </Cors> |
| 62 </CorsConfig> |
| 63 |
| 64 The above XML document explicitly allows cross-origin GET requests from |
| 65 origin1.example.com and may include the Content-Type response header. |
| 66 |
| 67 For more info about CORS, see http://www.w3.org/TR/cors/. |
| 68 """) |
| 69 |
| 70 class SetCorsCommand(Command): |
| 71 """Implementation of gsutil setcors command.""" |
| 72 |
| 73 # Command specification (processed by parent class). |
| 74 command_spec = { |
| 75 # Name of command. |
| 76 COMMAND_NAME : 'setcors', |
| 77 # List of command name aliases. |
| 78 COMMAND_NAME_ALIASES : [], |
| 79 # Min number of args required by this command. |
| 80 MIN_ARGS : 2, |
| 81 # Max number of args required by this command, or NO_MAX. |
| 82 MAX_ARGS : NO_MAX, |
| 83 # Getopt-style string specifying acceptable sub args. |
| 84 SUPPORTED_SUB_ARGS : '', |
| 85 # True if file URIs acceptable for this command. |
| 86 FILE_URIS_OK : False, |
| 87 # True if provider-only URIs acceptable for this command. |
| 88 PROVIDER_URIS_OK : False, |
| 89 # Index in args of first URI arg. |
| 90 URIS_START_ARG : 1, |
| 91 # True if must configure gsutil before running command. |
| 92 CONFIG_REQUIRED : True, |
| 93 } |
| 94 help_spec = { |
| 95 # Name of command or auxiliary help info for which this help applies. |
| 96 HELP_NAME : 'setcors', |
| 97 # List of help name aliases. |
| 98 HELP_NAME_ALIASES : ['cors', 'cross-origin'], |
| 99 # Type of help) |
| 100 HELP_TYPE : HelpType.COMMAND_HELP, |
| 101 # One line summary of this help. |
| 102 HELP_ONE_LINE_SUMMARY : 'Set a CORS XML document for one or more buckets', |
| 103 # The full help text. |
| 104 HELP_TEXT : _detailed_help_text, |
| 105 } |
| 106 |
| 107 # Command entry point. |
| 108 def RunCommand(self): |
| 109 cors_arg = self.args[0] |
| 110 uri_args = self.args[1:] |
| 111 # Disallow multi-provider setcors requests. |
| 112 storage_uri = self.UrisAreForSingleProvider(uri_args) |
| 113 if not storage_uri: |
| 114 raise CommandException('"%s" command spanning providers not allowed.' % |
| 115 self.command_name) |
| 116 |
| 117 # Open, read and parse file containing XML document. |
| 118 cors_file = open(cors_arg, 'r') |
| 119 cors_txt = cors_file.read() |
| 120 cors_file.close() |
| 121 cors_obj = Cors() |
| 122 |
| 123 # Parse XML document and convert into Cors object. |
| 124 h = handler.XmlHandler(cors_obj, None) |
| 125 try: |
| 126 xml.sax.parseString(cors_txt, h) |
| 127 except xml.sax._exceptions.SAXParseException, e: |
| 128 raise CommandException('Requested CORS is invalid: %s at line %s, ' |
| 129 'column %s' % (e.getMessage(), e.getLineNumber(), |
| 130 e.getColumnNumber())) |
| 131 |
| 132 # Iterate over URIs, expanding wildcards, and setting the CORS on each. |
| 133 some_matched = False |
| 134 for uri_str in uri_args: |
| 135 for blr in self.WildcardIterator(uri_str): |
| 136 uri = blr.GetUri() |
| 137 if not uri.names_bucket(): |
| 138 raise CommandException('URI %s must name a bucket for the %s command' |
| 139 % (str(uri), self.command_name)) |
| 140 some_matched = True |
| 141 print 'Setting CORS on %s...' % uri |
| 142 uri.set_cors(cors_obj, False, self.headers) |
| 143 if not some_matched: |
| 144 raise CommandException('No URIs matched') |
| 145 |
| 146 return 0 |
| 147 |
| 148 # Test specification. See definition of test_steps in base class for |
| 149 # details on how to populate these fields. |
| 150 empty_doc1 = parseString('<CorsConfig/>').toprettyxml(indent=' ') |
| 151 |
| 152 empty_doc2 = parseString( |
| 153 '<CorsConfig></CorsConfig>').toprettyxml(indent=' ') |
| 154 |
| 155 empty_doc3 = parseString( |
| 156 '<CorsConfig><Cors/></CorsConfig>').toprettyxml(indent=' ') |
| 157 |
| 158 empty_doc4 = parseString( |
| 159 '<CorsConfig><Cors></Cors></CorsConfig>').toprettyxml(indent=' ') |
| 160 |
| 161 cors_bad1 = ('<?xml version="1.0" ?><CorsConfig><Cors><Methods><Method>GET' |
| 162 '</ResponseHeader></Methods></Cors></CorsConfig>') |
| 163 |
| 164 cors_bad2 = ('<?xml version="1.0" ?><CorsConfig><Cors><Methods><Cors>GET' |
| 165 '</Cors></Methods></Cors></CorsConfig>') |
| 166 |
| 167 cors_bad3 = ('<?xml version="1.0" ?><CorsConfig><Methods><Method>GET' |
| 168 '</Method></Methods></Cors></CorsConfig>') |
| 169 |
| 170 cors_bad4 = ('<?xml version="1.0" ?><CorsConfig><Cors><Method>GET' |
| 171 '</Method></Cors></CorsConfig>') |
| 172 |
| 173 cors_doc=parseString('<CorsConfig><Cors><Origins>' |
| 174 '<Origin>origin1.example.com</Origin>' |
| 175 '<Origin>origin2.example.com</Origin>' |
| 176 '</Origins><Methods><Method>GET</Method>' |
| 177 '<Method>PUT</Method><Method>POST</Method></Methods>' |
| 178 '<ResponseHeaders><ResponseHeader>foo</ResponseHeader>' |
| 179 '<ResponseHeader>bar</ResponseHeader></ResponseHeaders>' |
| 180 '<MaxAgeSec>3600</MaxAgeSec></Cors>' |
| 181 '<Cors><Origins><Origin>origin3.example.com</Origin></Origins>' |
| 182 '<Methods><Method>GET</Method><Method>DELETE</Method></Methods>' |
| 183 '<ResponseHeaders><ResponseHeader>foo2</ResponseHeader>' |
| 184 '<ResponseHeader>bar2</ResponseHeader></ResponseHeaders>' |
| 185 '</Cors></CorsConfig>').toprettyxml(indent=' ') |
| 186 |
| 187 |
| 188 test_steps = [ |
| 189 # (test name, cmd line, ret code, (result_file, expect_file)) |
| 190 ('setup empty doc 1', 'echo \'' + empty_doc1 + '\' >$F9', 0, None), |
| 191 ('setup empty doc 2', 'echo \'' + empty_doc2 + '\' >$F8', 0, None), |
| 192 ('setup empty doc 3', 'echo \'' + empty_doc3 + '\' >$F7', 0, None), |
| 193 ('setup empty doc 4', 'echo \'' + empty_doc4 + '\' >$F6', 0, None), |
| 194 ('setup cors doc', 'echo \'' + cors_doc + '\' >$F5', 0, None), |
| 195 ('setup bad cors 1', 'echo \'' + cors_bad1 + '\' >$F4', 0, None), |
| 196 ('setup bad cors 2', 'echo \'' + cors_bad2 + '\' >$F3', 0, None), |
| 197 ('setup bad cors 3', 'echo \'' + cors_bad3 + '\' >$F2', 0, None), |
| 198 ('setup bad cors 4', 'echo \'' + cors_bad4 + '\' >$F1', 0, None), |
| 199 ('verify default cors', 'gsutil getcors gs://$B0 >$F0', |
| 200 0, ('$F0', '$F9')), |
| 201 ('set empty cors 1', 'gsutil setcors $F9 gs://$B0', 0, None), |
| 202 ('verify empty cors 1', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')), |
| 203 ('set empty cors 2', 'gsutil setcors $F8 gs://$B0', 0, None), |
| 204 ('verify empty cors 2', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')), |
| 205 ('set empty cors 3', 'gsutil setcors $F7 gs://$B0', 0, None), |
| 206 ('verify empty cors 3', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F7')), |
| 207 ('set empty cors 4', 'gsutil setcors $F6 gs://$B0', 0, None), |
| 208 ('verify empty cors 4', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F7')), |
| 209 ('set non-null cors', 'gsutil setcors $F5 gs://$B0', 0, (0, None)), |
| 210 ('verify non-null cors', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F5')), |
| 211 ('bad1 cors fails', 'gsutil setcors $F4 gs://$B0', 1, (0, None)), |
| 212 ('bad2 cors fails', 'gsutil setcors $F3 gs://$B0', 1, (0, None)), |
| 213 ('bad3 cors fails', 'gsutil setcors $F2 gs://$B0', 1, (0, None)), |
| 214 ('bad4 cors fails', 'gsutil setcors $F1 gs://$B0', 1, (0, None)), |
| 215 ('reset empty cors', 'gsutil setcors $F9 gs://$B0', 0, None), |
| 216 ('verify empty cors', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')), |
| 217 ('set multi non-null cors', 'gsutil setcors $F5 gs://$B0 gs://$B1', |
| 218 0, (0, None)), |
| 219 ('verify non-null 1', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F5')), |
| 220 ('verify non-null 2', 'gsutil getcors gs://$B1 >$F0', 0, ('$F0', '$F5')), |
| 221 ('reset empty cors', 'gsutil setcors $F9 gs://$B0', 0, None), |
| 222 ('verify empty cors', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')), |
| 223 ('set wildcard non-null cors', 'gsutil setcors $F5 gs://gsutil_test*', |
| 224 0, (0, None)), |
| 225 ('verify non-null 1', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F5')), |
| 226 ('verify non-null 2', 'gsutil getcors gs://$B1 >$F0', 0, ('$F0', '$F5')), |
| 227 ] |
| 228 |
OLD | NEW |