Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(703)

Side by Side Diff: third_party/gsutil/gslib/commands/setcors.py

Issue 10199002: Upgrade gsutil to 3.4 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 import xml.dom.minidom
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 from gslib.wildcard_iterator import ContainsWildcard
38
39 _detailed_help_text = ("""
40 <B>SYNOPSIS</B>
41 gsutil setcors cors-xml-file uri...
42
43
44 <B>DESCRIPTION</B>
45 Sets the Cross-Origin Resource Sharing (CORS) configuration on one or more
46 buckets. This command is supported for buckets only, not objects. The
47 cors-xml-file specified on the command line should be a path to a local
48 file containing an XML document with the following structure:
49
50 <?xml version="1.0" ?>
51 <CorsConfig>
52 <Cors>
53 <Origins>
54 <Origin>origin1.example.com</Origin>
55 </Origins>
56 <Methods>
57 <Method>GET</Method>
58 </Methods>
59 <ResponseHeaders>
60 <ResponseHeader>Content-Type</ResponseHeader>
61 </ResponseHeaders>
62 </Cors>
63 </CorsConfig>
64
65 The above XML document explicitly allows cross-origin GET requests from
66 origin1.example.com and may include the Content-Type response header.
67
68 For more info about CORS, see http://www.w3.org/TR/cors/.
69 """)
70
71 class SetCorsCommand(Command):
72 """Implementation of gsutil setcors command."""
73
74 # Command specification (processed by parent class).
75 command_spec = {
76 # Name of command.
77 COMMAND_NAME : 'setcors',
78 # List of command name aliases.
79 COMMAND_NAME_ALIASES : [],
80 # Min number of args required by this command.
81 MIN_ARGS : 2,
82 # Max number of args required by this command, or NO_MAX.
83 MAX_ARGS : NO_MAX,
84 # Getopt-style string specifying acceptable sub args.
85 SUPPORTED_SUB_ARGS : '',
86 # True if file URIs acceptable for this command.
87 FILE_URIS_OK : False,
88 # True if provider-only URIs acceptable for this command.
89 PROVIDER_URIS_OK : False,
90 # Index in args of first URI arg.
91 URIS_START_ARG : 1,
92 # True if must configure gsutil before running command.
93 CONFIG_REQUIRED : True,
94 }
95 help_spec = {
96 # Name of command or auxiliary help info for which this help applies.
97 HELP_NAME : 'setcors',
98 # List of help name aliases.
99 HELP_NAME_ALIASES : ['cors', 'cross-origin'],
100 # Type of help)
101 HELP_TYPE : HelpType.COMMAND_HELP,
102 # One line summary of this help.
103 HELP_ONE_LINE_SUMMARY : 'Set a CORS XML document for one or more buckets',
104 # The full help text.
105 HELP_TEXT : _detailed_help_text,
106 }
107
108 # Command entry point.
109 def RunCommand(self):
110 cors_arg = self.args[0]
111 uri_args = self.args[1:]
112 # Disallow multi-provider setcors requests.
113 storage_uri = self.UrisAreForSingleProvider(uri_args)
114 if not storage_uri:
115 raise CommandException('"%s" command spanning providers not allowed.' %
116 self.command_name)
117
118 # Open, read and parse file containing XML document.
119 cors_file = open(cors_arg, 'r')
120 cors_txt = cors_file.read()
121 cors_file.close()
122 cors_obj = Cors()
123
124 # Parse XML document and convert into Cors object.
125 h = handler.XmlHandler(cors_obj, None)
126 try:
127 xml.sax.parseString(cors_txt, h)
128 except xml.sax._exceptions.SAXParseException, e:
129 raise CommandException('Requested CORS is invalid: %s at line %s, '
130 'column %s' % (e.getMessage(), e.getLineNumber(),
131 e.getColumnNumber()))
132
133 # Iterate over URIs, expanding wildcards, and setting the CORS on each.
134 some_matched = False
135 for uri_str in uri_args:
136 for blr in self.exp_handler.WildcardIterator(uri_str):
137 uri = blr.GetUri()
138 if not uri.names_bucket():
139 raise CommandException('URI %s must name a bucket for the %s command'
140 % (str(uri), self.command_name))
141 some_matched = True
142 print 'Setting CORS on %s...' % uri
143 uri.set_cors(cors_obj, False, self.headers)
144 if not some_matched:
145 raise CommandException('No URIs matched')
146
147 # test specification, see definition of test_steps in base class for
148 # details on how to populate these fields
149 empty_doc1 = ('<?xml version="1.0" ?>\n'
150 '<CorsConfig/>\n')
151
152 empty_doc2 = ('<?xml version="1.0" ?>\n'
153 '<CorsConfig></CorsConfig>\n')
154
155 empty_doc3 = ('<?xml version="1.0" ?>\n'
156 '<CorsConfig>\n'
157 ' <Cors/>\n'
158 '</CorsConfig>\n')
159
160 empty_doc4 = ('<?xml version="1.0" ?>\n'
161 '<CorsConfig><Cors></Cors></CorsConfig>\n')
162
163 cors_bad1 = ('<?xml version="1.0" ?><CorsConfig><Cors><Methods><Method>GET'
164 '</ResponseHeader></Methods></Cors></CorsConfig>')
165
166 cors_bad2 = ('<?xml version="1.0" ?><CorsConfig><Cors><Methods><Cors>GET'
167 '</Cors></Methods></Cors></CorsConfig>')
168
169 cors_bad3 = ('<?xml version="1.0" ?><CorsConfig><Methods><Method>GET'
170 '</Method></Methods></Cors></CorsConfig>')
171
172 cors_bad4 = ('<?xml version="1.0" ?><CorsConfig><Cors><Method>GET'
173 '</Method></Cors></CorsConfig>')
174
175 cors_doc = ('<?xml version="1.0" ?>\n'
176 '<CorsConfig>\n'
177 ' <Cors>\n'
178 ' <Origins>\n'
179 ' <Origin>\n'
180 ' origin1.example.com\n'
181 ' </Origin>\n'
182 ' <Origin>\n'
183 ' origin2.example.com\n'
184 ' </Origin>\n'
185 ' </Origins>\n'
186 ' <Methods>\n'
187 ' <Method>\n'
188 ' GET\n'
189 ' </Method>\n'
190 ' <Method>\n'
191 ' PUT\n'
192 ' </Method>\n'
193 ' <Method>\n'
194 ' POST\n'
195 ' </Method>\n'
196 ' </Methods>\n'
197 ' <ResponseHeaders>\n'
198 ' <ResponseHeader>\n'
199 ' foo\n'
200 ' </ResponseHeader>\n'
201 ' <ResponseHeader>\n'
202 ' bar\n'
203 ' </ResponseHeader>\n'
204 ' </ResponseHeaders>\n'
205 ' <MaxAgeSec>\n'
206 ' 3600\n'
207 ' </MaxAgeSec>\n'
208 ' </Cors>\n'
209 ' <Cors>\n'
210 ' <Origins>\n'
211 ' <Origin>\n'
212 ' origin3.example.com\n'
213 ' </Origin>\n'
214 ' </Origins>\n'
215 ' <Methods>\n'
216 ' <Method>\n'
217 ' GET\n'
218 ' </Method>\n'
219 ' <Method>\n'
220 ' DELETE\n'
221 ' </Method>\n'
222 ' </Methods>\n'
223 ' <ResponseHeaders>\n'
224 ' <ResponseHeader>\n'
225 ' foo2\n'
226 ' </ResponseHeader>\n'
227 ' <ResponseHeader>\n'
228 ' bar2\n'
229 ' </ResponseHeader>\n'
230 ' </ResponseHeaders>\n'
231 ' </Cors>\n'
232 '</CorsConfig>\n')
233
234 test_steps = [
235 # (test name, cmd line, ret code, (result_file, expect_file))
236 ('setup empty doc 1', 'echo \'' + empty_doc1 + '\' >$F9', 0, None),
237 ('setup empty doc 2', 'echo \'' + empty_doc2 + '\' >$F8', 0, None),
238 ('setup empty doc 3', 'echo \'' + empty_doc3 + '\' >$F7', 0, None),
239 ('setup empty doc 4', 'echo \'' + empty_doc4 + '\' >$F6', 0, None),
240 ('setup cors doc', 'echo \'' + cors_doc + '\' >$F5', 0, None),
241 ('setup bad cors 1', 'echo \'' + cors_bad1 + '\' >$F4', 0, None),
242 ('setup bad cors 2', 'echo \'' + cors_bad2 + '\' >$F3', 0, None),
243 ('setup bad cors 3', 'echo \'' + cors_bad3 + '\' >$F2', 0, None),
244 ('setup bad cors 4', 'echo \'' + cors_bad4 + '\' >$F1', 0, None),
245 ('verify default cors', 'gsutil getcors gs://$B0 >$F0',
246 0, ('$F0', '$F9')),
247 ('set empty cors 1', 'gsutil setcors $F9 gs://$B0', 0, None),
248 ('verify empty cors 1', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')),
249 ('set empty cors 2', 'gsutil setcors $F8 gs://$B0', 0, None),
250 ('verify empty cors 2', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')),
251 ('set empty cors 3', 'gsutil setcors $F7 gs://$B0', 0, None),
252 ('verify empty cors 3', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F7')),
253 ('set empty cors 4', 'gsutil setcors $F6 gs://$B0', 0, None),
254 ('verify empty cors 4', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F7')),
255 ('set non-null cors', 'gsutil setcors $F5 gs://$B0', 0, (0, None)),
256 ('verify non-null cors', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F5')),
257 ('bad1 cors fails', 'gsutil setcors $F4 gs://$B0', 1, (0, None)),
258 ('bad2 cors fails', 'gsutil setcors $F3 gs://$B0', 1, (0, None)),
259 ('bad3 cors fails', 'gsutil setcors $F2 gs://$B0', 1, (0, None)),
260 ('bad4 cors fails', 'gsutil setcors $F1 gs://$B0', 1, (0, None)),
261 ('reset empty cors', 'gsutil setcors $F9 gs://$B0', 0, None),
262 ('verify empty cors', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')),
263 ('set multi non-null cors', 'gsutil setcors $F5 gs://$B0 gs://$B1',
264 0, (0, None)),
265 ('verify non-null 1', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F5')),
266 ('verify non-null 2', 'gsutil getcors gs://$B1 >$F0', 0, ('$F0', '$F5')),
267 ('reset empty cors', 'gsutil setcors $F9 gs://$B0', 0, None),
268 ('verify empty cors', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F9')),
269 ('set wildcard non-null cors', 'gsutil setcors $F5 gs://gsutil_test*',
270 0, (0, None)),
271 ('verify non-null 1', 'gsutil getcors gs://$B0 >$F0', 0, ('$F0', '$F5')),
272 ('verify non-null 2', 'gsutil getcors gs://$B1 >$F0', 0, ('$F0', '$F5')),
273 ]
274
OLDNEW
« no previous file with comments | « third_party/gsutil/gslib/commands/setacl.py ('k') | third_party/gsutil/gslib/commands/setdefacl.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698