2857: strip signatures from manifests before testing equality.
[arvados.git] / services / api / test / functional / arvados / v1 / collections_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
4
5   test "should get index" do
6     authorize_with :active
7     get :index
8     assert_response :success
9     assert_not_nil assigns(:objects)
10   end
11
12   [0,1,2].each do |limit|
13     test "get index with limit=#{limit}" do
14       authorize_with :active
15       get :index, limit: limit
16       assert_response :success
17       assert_equal limit, assigns(:objects).count
18       resp = JSON.parse(@response.body)
19       assert_equal limit, resp['limit']
20     end
21   end
22
23   test "items.count == items_available" do
24     authorize_with :active
25     get :index, limit: 100000
26     assert_response :success
27     resp = JSON.parse(@response.body)
28     assert_equal resp['items_available'], assigns(:objects).length
29     assert_equal resp['items_available'], resp['items'].count
30     unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
31     assert_equal unique_uuids.count, resp['items'].count
32   end
33
34   test "get index with limit=2 offset=99999" do
35     # Assume there are not that many test fixtures.
36     authorize_with :active
37     get :index, limit: 2, offset: 99999
38     assert_response :success
39     assert_equal 0, assigns(:objects).count
40     resp = JSON.parse(@response.body)
41     assert_equal 2, resp['limit']
42     assert_equal 99999, resp['offset']
43   end
44
45   test "should create" do
46     authorize_with :active
47     test_collection = {
48       manifest_text: <<-EOS
49 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
50 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
51 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
52 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
53 EOS
54     }
55     test_collection[:uuid] =
56       Digest::MD5.hexdigest(test_collection[:manifest_text]) +
57       '+' +
58       test_collection[:manifest_text].length.to_s
59
60     # post :create will modify test_collection in place, so we save a copy first.
61     # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
62     post_collection = Marshal.load(Marshal.dump(test_collection))
63     post :create, {
64       collection: post_collection
65     }
66
67     assert_response :success
68     assert_nil assigns(:objects)
69
70     get :show, {
71       id: test_collection[:uuid]
72     }
73     assert_response :success
74     assert_not_nil assigns(:object)
75     resp = JSON.parse(@response.body)
76     assert_equal test_collection[:uuid], resp['uuid']
77
78     # The manifest in the response will have had permission hints added.
79     # Remove any permission hints in the response before comparing it to the source.
80     stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
81     assert_equal test_collection[:manifest_text], stripped_manifest
82     assert_equal 9, resp['data_size']
83     assert_equal [['.', 'foo.txt', 0],
84                   ['.', 'bar.txt', 6],
85                   ['./baz', 'bar.txt', 3]], resp['files']
86   end
87
88   test "list of files is correct for empty manifest" do
89     authorize_with :active
90     test_collection = {
91       manifest_text: "",
92       uuid: "d41d8cd98f00b204e9800998ecf8427e+0"
93     }
94     post :create, {
95       collection: test_collection
96     }
97     assert_response :success
98
99     get :show, {
100       id: "d41d8cd98f00b204e9800998ecf8427e+0"
101     }
102     assert_response :success
103     resp = JSON.parse(@response.body)
104     assert_equal [], resp['files']
105   end
106
107   test "create with owner_uuid set to owned group" do
108     authorize_with :active
109     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
110     post :create, {
111       collection: {
112         owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
113         manifest_text: manifest_text,
114         uuid: "d30fe8ae534397864cb96c544f4cf102"
115       }
116     }
117     assert_response :success
118     resp = JSON.parse(@response.body)
119     assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
120   end
121
122   test "create with owner_uuid set to group i can_manage" do
123     authorize_with :active
124     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
125     post :create, {
126       collection: {
127         owner_uuid: 'zzzzz-j7d0g-8ulrifv67tve5sx',
128         manifest_text: manifest_text,
129         uuid: "d30fe8ae534397864cb96c544f4cf102"
130       }
131     }
132     assert_response :success
133     resp = JSON.parse(@response.body)
134     assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
135   end
136
137   test "create with owner_uuid set to group with no can_manage permission" do
138     authorize_with :active
139     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
140     post :create, {
141       collection: {
142         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
143         manifest_text: manifest_text,
144         uuid: "d30fe8ae534397864cb96c544f4cf102"
145       }
146     }
147     assert_response 403
148   end
149
150   test "admin create with owner_uuid set to group with no permission" do
151     authorize_with :admin
152     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
153     post :create, {
154       collection: {
155         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
156         manifest_text: manifest_text,
157         uuid: "d30fe8ae534397864cb96c544f4cf102"
158       }
159     }
160     assert_response :success
161   end
162
163   test "should create with collection passed as json" do
164     authorize_with :active
165     post :create, {
166       collection: <<-EOS
167       {
168         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
169         "uuid":"d30fe8ae534397864cb96c544f4cf102"\
170       }
171       EOS
172     }
173     assert_response :success
174   end
175
176   test "should fail to create with checksum mismatch" do
177     authorize_with :active
178     post :create, {
179       collection: <<-EOS
180       {
181         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
182         "uuid":"d30fe8ae534397864cb96c544f4cf102"\
183       }
184       EOS
185     }
186     assert_response 422
187   end
188
189   test "collection UUID is normalized when created" do
190     authorize_with :active
191     post :create, {
192       collection: {
193         manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
194         uuid: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
195       }
196     }
197     assert_response :success
198     assert_not_nil assigns(:object)
199     resp = JSON.parse(@response.body)
200     assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['uuid']
201   end
202
203   test "get full provenance for baz file" do
204     authorize_with :active
205     get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
206     assert_response :success
207     resp = JSON.parse(@response.body)
208     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
209     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
210     assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
211     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
212     assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
213   end
214
215   test "get no provenance for foo file" do
216     # spectator user cannot even see baz collection
217     authorize_with :spectator
218     get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
219     assert_response 404
220   end
221
222   test "get partial provenance for baz file" do
223     # spectator user can see bar->baz job, but not foo->bar job
224     authorize_with :spectator
225     get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
226     assert_response :success
227     resp = JSON.parse(@response.body)
228     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
229     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
230     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq']     # bar->baz
231     assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon']         # foo->bar
232     assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
233   end
234
235   test "search collections with 'any' operator" do
236     authorize_with :active
237     get :index, {
238       where: { any: ['contains', '7f9102c395f4ffc5e3'] }
239     }
240     assert_response :success
241     found = assigns(:objects).collect(&:uuid)
242     assert_equal 1, found.count
243     assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
244   end
245
246   test "create collection with signed manifest" do
247     authorize_with :active
248     locators = %w(
249       d41d8cd98f00b204e9800998ecf8427e+0
250       acbd18db4cc2f85cedef654fccc4a4d8+3
251       ea10d51bcf88862dbcc36eb292017dfd+45)
252
253     unsigned_manifest = locators.map { |loc|
254       ". " + loc + " 0:0:foo.txt\n"
255     }.join()
256     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
257       '+' +
258       unsigned_manifest.length.to_s
259
260     # build a manifest with both signed and unsigned locators.
261     # TODO(twp): in phase 4, all locators will need to be signed, so
262     # this test should break and will need to be rewritten. Issue #2755.
263     signing_opts = {
264       key: Rails.configuration.blob_signing_key,
265       api_token: api_token(:active),
266     }
267     signed_manifest =
268       ". " + locators[0] + " 0:0:foo.txt\n" +
269       ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
270       ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
271
272     post :create, {
273       collection: {
274         manifest_text: signed_manifest,
275         uuid: manifest_uuid,
276       }
277     }
278     assert_response :success
279     assert_not_nil assigns(:object)
280     resp = JSON.parse(@response.body)
281     assert_equal manifest_uuid, resp['uuid']
282     assert_equal 48, resp['data_size']
283     # All of the locators in the output must be signed.
284     resp['manifest_text'].lines.each do |entry|
285       m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
286       if m
287         assert Blob.verify_signature m[0], signing_opts
288       end
289     end
290   end
291
292   test "create collection with signed manifest and explicit TTL" do
293     authorize_with :active
294     locators = %w(
295       d41d8cd98f00b204e9800998ecf8427e+0
296       acbd18db4cc2f85cedef654fccc4a4d8+3
297       ea10d51bcf88862dbcc36eb292017dfd+45)
298
299     unsigned_manifest = locators.map { |loc|
300       ". " + loc + " 0:0:foo.txt\n"
301     }.join()
302     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
303       '+' +
304       unsigned_manifest.length.to_s
305
306     # build a manifest with both signed and unsigned locators.
307     # TODO(twp): in phase 4, all locators will need to be signed, so
308     # this test should break and will need to be rewritten. Issue #2755.
309     signing_opts = {
310       key: Rails.configuration.blob_signing_key,
311       api_token: api_token(:active),
312       ttl: 3600   # 1 hour
313     }
314     signed_manifest =
315       ". " + locators[0] + " 0:0:foo.txt\n" +
316       ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
317       ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
318
319     post :create, {
320       collection: {
321         manifest_text: signed_manifest,
322         uuid: manifest_uuid,
323       }
324     }
325     assert_response :success
326     assert_not_nil assigns(:object)
327     resp = JSON.parse(@response.body)
328     assert_equal manifest_uuid, resp['uuid']
329     assert_equal 48, resp['data_size']
330     # All of the locators in the output must be signed.
331     resp['manifest_text'].lines.each do |entry|
332       m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
333       if m
334         assert Blob.verify_signature m[0], signing_opts
335       end
336     end
337   end
338
339   test "create fails with invalid signature" do
340     authorize_with :active
341     signing_opts = {
342       key: Rails.configuration.blob_signing_key,
343       api_token: api_token(:active),
344     }
345
346     # Generate a locator with a bad signature.
347     unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
348     bad_locator = unsigned_locator + "+Affffffff@ffffffff"
349     assert !Blob.verify_signature(bad_locator, signing_opts)
350
351     # Creating a collection with this locator should
352     # produce 403 Permission denied.
353     unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
354     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
355       '+' +
356       unsigned_manifest.length.to_s
357
358     bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
359     post :create, {
360       collection: {
361         manifest_text: bad_manifest,
362         uuid: manifest_uuid
363       }
364     }
365
366     assert_response 403
367   end
368
369   test "create fails with uuid of signed manifest" do
370     authorize_with :active
371     signing_opts = {
372       key: Rails.configuration.blob_signing_key,
373       api_token: api_token(:active),
374     }
375
376     unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
377     signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
378     signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
379     manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
380       '+' +
381       signed_manifest.length.to_s
382
383     post :create, {
384       collection: {
385         manifest_text: signed_manifest,
386         uuid: manifest_uuid
387       }
388     }
389
390     assert_response 422
391   end
392
393   test "multiple locators per line" do
394     authorize_with :active
395     locators = %w(
396       d41d8cd98f00b204e9800998ecf8427e+0
397       acbd18db4cc2f85cedef654fccc4a4d8+3
398       ea10d51bcf88862dbcc36eb292017dfd+45)
399
400     manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
401     manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
402       '+' +
403       manifest_text.length.to_s
404
405     test_collection = {
406       manifest_text: manifest_text,
407       uuid: manifest_uuid,
408     }
409     post_collection = Marshal.load(Marshal.dump(test_collection))
410     post :create, {
411       collection: post_collection
412     }
413     assert_response :success
414     assert_not_nil assigns(:object)
415     resp = JSON.parse(@response.body)
416     assert_equal manifest_uuid, resp['uuid']
417     assert_equal 48, resp['data_size']
418
419     # The manifest in the response will have had permission hints added.
420     # Remove any permission hints in the response before comparing it to the source.
421     stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
422     assert_equal manifest_text, stripped_manifest
423   end
424
425   test "multiple signed locators per line" do
426     authorize_with :active
427     locators = %w(
428       d41d8cd98f00b204e9800998ecf8427e+0
429       acbd18db4cc2f85cedef654fccc4a4d8+3
430       ea10d51bcf88862dbcc36eb292017dfd+45)
431
432     signing_opts = {
433       key: Rails.configuration.blob_signing_key,
434       api_token: api_token(:active),
435     }
436
437     unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
438     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
439       '+' +
440       unsigned_manifest.length.to_s
441
442     signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
443     signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
444
445     post :create, {
446       collection: {
447         manifest_text: signed_manifest,
448         uuid: manifest_uuid,
449       }
450     }
451     assert_response :success
452     assert_not_nil assigns(:object)
453     resp = JSON.parse(@response.body)
454     assert_equal manifest_uuid, resp['uuid']
455     assert_equal 48, resp['data_size']
456     # All of the locators in the output must be signed.
457     # Each line is of the form "path locator locator ... 0:0:file.txt"
458     # entry.split[1..-2] will yield just the tokens in the middle of the line
459     returned_locator_count = 0
460     resp['manifest_text'].lines.each do |entry|
461       entry.split[1..-2].each do |tok|
462         returned_locator_count += 1
463         assert Blob.verify_signature tok, signing_opts
464       end
465     end
466     assert_equal locators.count, returned_locator_count
467   end
468 end