3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
5 def permit_unsigned_manifests isok=true
6 # Set security model for the life of a test.
7 Rails.configuration.permit_create_collection_with_unsigned_manifest = isok
10 def assert_signed_manifest manifest_text, label=''
11 assert_not_nil manifest_text, "#{label} manifest_text was nil"
12 manifest_text.scan(/ [[:xdigit:]]{32}\S*/) do |tok|
13 assert_match(/\+A[[:xdigit:]]+@[[:xdigit:]]{8}\b/, tok,
14 "Locator in #{label} manifest_text was not signed")
18 test "should get index" do
19 authorize_with :active
21 assert_response :success
22 assert(assigns(:objects).andand.any?, "no Collections returned in index")
23 refute(json_response["items"].any? { |c| c.has_key?("manifest_text") },
24 "basic Collections index included manifest_text")
27 test "collections.get returns signed locators" do
28 permit_unsigned_manifests
29 authorize_with :active
30 get :show, {id: collections(:foo_file).uuid}
31 assert_response :success
32 assert_signed_manifest json_response['manifest_text'], 'foo_file'
35 test "index with manifest_text selected returns signed locators" do
36 columns = %w(uuid owner_uuid manifest_text)
37 authorize_with :active
38 get :index, select: columns
39 assert_response :success
40 assert(assigns(:objects).andand.any?,
41 "no Collections returned for index with columns selected")
42 json_response["items"].each do |coll|
43 assert_equal(columns, columns & coll.keys,
44 "Collections index did not respect selected columns")
45 assert_signed_manifest coll['manifest_text'], coll['uuid']
49 [0,1,2].each do |limit|
50 test "get index with limit=#{limit}" do
51 authorize_with :active
52 get :index, limit: limit
53 assert_response :success
54 assert_equal limit, assigns(:objects).count
55 resp = JSON.parse(@response.body)
56 assert_equal limit, resp['limit']
60 test "items.count == items_available" do
61 authorize_with :active
62 get :index, limit: 100000
63 assert_response :success
64 resp = JSON.parse(@response.body)
65 assert_equal resp['items_available'], assigns(:objects).length
66 assert_equal resp['items_available'], resp['items'].count
67 unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
68 assert_equal unique_uuids.count, resp['items'].count
71 test "get index with limit=2 offset=99999" do
72 # Assume there are not that many test fixtures.
73 authorize_with :active
74 get :index, limit: 2, offset: 99999
75 assert_response :success
76 assert_equal 0, assigns(:objects).count
77 resp = JSON.parse(@response.body)
78 assert_equal 2, resp['limit']
79 assert_equal 99999, resp['offset']
82 test "admin can create collection with unsigned manifest" do
86 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
87 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
88 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
89 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
92 test_collection[:portable_data_hash] =
93 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
95 test_collection[:manifest_text].length.to_s
97 # post :create will modify test_collection in place, so we save a copy first.
98 # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
99 post_collection = Marshal.load(Marshal.dump(test_collection))
101 collection: post_collection
104 assert_response :success
105 assert_nil assigns(:objects)
107 response_collection = assigns(:object)
109 stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
110 where(portable_data_hash: response_collection['portable_data_hash']).first
112 assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
114 # The manifest in the response will have had permission hints added.
115 # Remove any permission hints in the response before comparing it to the source.
116 stripped_manifest = stored_collection['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
117 assert_equal test_collection[:manifest_text], stripped_manifest
119 # TBD: create action should add permission signatures to manifest_text in the response,
120 # and we need to check those permission signatures here.
123 [:admin, :active].each do |user|
124 test "#{user} can get collection using portable data hash" do
127 foo_collection = collections(:foo_file)
129 # Get foo_file using its portable data hash
131 id: foo_collection[:portable_data_hash]
133 assert_response :success
134 assert_not_nil assigns(:object)
135 resp = assigns(:object)
136 assert_equal foo_collection[:portable_data_hash], resp['portable_data_hash']
137 assert_signed_manifest resp['manifest_text']
139 # The manifest in the response will have had permission hints added.
140 # Remove any permission hints in the response before comparing it to the source.
141 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
142 assert_equal foo_collection[:manifest_text], stripped_manifest
146 test "create with owner_uuid set to owned group" do
147 permit_unsigned_manifests
148 authorize_with :active
149 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
152 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
153 manifest_text: manifest_text,
154 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
157 assert_response :success
158 resp = JSON.parse(@response.body)
159 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
162 test "create fails with duplicate name" do
163 permit_unsigned_manifests
164 authorize_with :admin
165 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
168 owner_uuid: 'zzzzz-tpzed-000000000000000',
169 manifest_text: manifest_text,
170 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
175 response_errors = json_response['errors']
176 assert_not_nil response_errors, 'Expected error in response'
177 assert(response_errors.first.include?('duplicate key'),
178 "Expected 'duplicate key' error in #{response_errors.first}")
181 [false, true].each do |unsigned|
182 test "create with duplicate name, ensure_unique_name, unsigned=#{unsigned}" do
183 permit_unsigned_manifests unsigned
184 authorize_with :active
185 manifest_text = ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:foo.txt\n"
187 manifest_text = Collection.sign_manifest manifest_text, api_token(:active)
191 owner_uuid: users(:active).uuid,
192 manifest_text: manifest_text,
193 name: "owned_by_active"
195 ensure_unique_name: true
197 assert_response :success
198 assert_equal 'owned_by_active (2)', json_response['name']
202 test "create with owner_uuid set to group i can_manage" do
203 permit_unsigned_manifests
204 authorize_with :active
205 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
208 owner_uuid: groups(:active_user_has_can_manage).uuid,
209 manifest_text: manifest_text,
210 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
213 assert_response :success
214 resp = JSON.parse(@response.body)
215 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
218 test "create with owner_uuid fails on group with only can_read permission" do
219 permit_unsigned_manifests
220 authorize_with :active
221 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
224 owner_uuid: groups(:all_users).uuid,
225 manifest_text: manifest_text,
226 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
232 test "create with owner_uuid fails on group with no permission" do
233 permit_unsigned_manifests
234 authorize_with :active
235 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
238 owner_uuid: groups(:public).uuid,
239 manifest_text: manifest_text,
240 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
246 test "admin create with owner_uuid set to group with no permission" do
247 permit_unsigned_manifests
248 authorize_with :admin
249 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
252 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
253 manifest_text: manifest_text,
254 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
257 assert_response :success
260 test "should create with collection passed as json" do
261 permit_unsigned_manifests
262 authorize_with :active
266 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
267 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
271 assert_response :success
274 test "should fail to create with checksum mismatch" do
275 permit_unsigned_manifests
276 authorize_with :active
280 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
281 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
288 test "collection UUID is normalized when created" do
289 permit_unsigned_manifests
290 authorize_with :active
293 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
294 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
297 assert_response :success
298 assert_not_nil assigns(:object)
299 resp = JSON.parse(@response.body)
300 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
303 test "get full provenance for baz file" do
304 authorize_with :active
305 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
306 assert_response :success
307 resp = JSON.parse(@response.body)
308 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
309 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
310 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
311 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
312 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
315 test "get no provenance for foo file" do
316 # spectator user cannot even see baz collection
317 authorize_with :spectator
318 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
322 test "get partial provenance for baz file" do
323 # spectator user can see bar->baz job, but not foo->bar job
324 authorize_with :spectator
325 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
326 assert_response :success
327 resp = JSON.parse(@response.body)
328 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
329 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
330 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
331 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
332 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
335 test "search collections with 'any' operator" do
336 authorize_with :active
338 where: { any: ['contains', '7f9102c395f4ffc5e3'] }
340 assert_response :success
341 found = assigns(:objects).collect(&:portable_data_hash)
342 assert_equal 2, found.count
343 assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
346 [false, true].each do |permit_unsigned|
347 test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
348 permit_unsigned_manifests permit_unsigned
349 authorize_with :active
351 d41d8cd98f00b204e9800998ecf8427e+0
352 acbd18db4cc2f85cedef654fccc4a4d8+3
353 ea10d51bcf88862dbcc36eb292017dfd+45)
355 unsigned_manifest = locators.map { |loc|
356 ". " + loc + " 0:0:foo.txt\n"
358 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
360 unsigned_manifest.length.to_s
362 # Build a manifest with both signed and unsigned locators.
364 key: Rails.configuration.blob_signing_key,
365 api_token: api_token(:active),
367 signed_locators = locators.collect do |x|
368 Blob.sign_locator x, signing_opts
371 # Leave a non-empty blob unsigned.
372 signed_locators[1] = locators[1]
374 # Leave the empty blob unsigned. This should still be allowed.
375 signed_locators[0] = locators[0]
378 ". " + signed_locators[0] + " 0:0:foo.txt\n" +
379 ". " + signed_locators[1] + " 0:0:foo.txt\n" +
380 ". " + signed_locators[2] + " 0:0:foo.txt\n"
384 manifest_text: signed_manifest,
385 portable_data_hash: manifest_uuid,
388 assert_response :success
389 assert_not_nil assigns(:object)
390 resp = JSON.parse(@response.body)
391 assert_equal manifest_uuid, resp['portable_data_hash']
392 # All of the locators in the output must be signed.
393 resp['manifest_text'].lines.each do |entry|
394 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
396 assert Blob.verify_signature m[0], signing_opts
402 test "create collection with signed manifest and explicit TTL" do
403 authorize_with :active
405 d41d8cd98f00b204e9800998ecf8427e+0
406 acbd18db4cc2f85cedef654fccc4a4d8+3
407 ea10d51bcf88862dbcc36eb292017dfd+45)
409 unsigned_manifest = locators.map { |loc|
410 ". " + loc + " 0:0:foo.txt\n"
412 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
414 unsigned_manifest.length.to_s
416 # build a manifest with both signed and unsigned locators.
417 # TODO(twp): in phase 4, all locators will need to be signed, so
418 # this test should break and will need to be rewritten. Issue #2755.
420 key: Rails.configuration.blob_signing_key,
421 api_token: api_token(:active),
425 ". " + locators[0] + " 0:0:foo.txt\n" +
426 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
427 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
431 manifest_text: signed_manifest,
432 portable_data_hash: manifest_uuid,
435 assert_response :success
436 assert_not_nil assigns(:object)
437 resp = JSON.parse(@response.body)
438 assert_equal manifest_uuid, resp['portable_data_hash']
439 # All of the locators in the output must be signed.
440 resp['manifest_text'].lines.each do |entry|
441 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
443 assert Blob.verify_signature m[0], signing_opts
448 test "create fails with invalid signature" do
449 authorize_with :active
451 key: Rails.configuration.blob_signing_key,
452 api_token: api_token(:active),
455 # Generate a locator with a bad signature.
456 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
457 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
458 assert !Blob.verify_signature(bad_locator, signing_opts)
460 # Creating a collection with this locator should
461 # produce 403 Permission denied.
462 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
463 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
465 unsigned_manifest.length.to_s
467 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
470 manifest_text: bad_manifest,
471 portable_data_hash: manifest_uuid
478 test "create fails with uuid of signed manifest" do
479 authorize_with :active
481 key: Rails.configuration.blob_signing_key,
482 api_token: api_token(:active),
485 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
486 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
487 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
488 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
490 signed_manifest.length.to_s
494 manifest_text: signed_manifest,
495 portable_data_hash: manifest_uuid
502 test "multiple locators per line" do
503 permit_unsigned_manifests
504 authorize_with :active
506 d41d8cd98f00b204e9800998ecf8427e+0
507 acbd18db4cc2f85cedef654fccc4a4d8+3
508 ea10d51bcf88862dbcc36eb292017dfd+45)
510 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
511 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
513 manifest_text.length.to_s
516 manifest_text: manifest_text,
517 portable_data_hash: manifest_uuid,
519 post_collection = Marshal.load(Marshal.dump(test_collection))
521 collection: post_collection
523 assert_response :success
524 assert_not_nil assigns(:object)
525 resp = JSON.parse(@response.body)
526 assert_equal manifest_uuid, resp['portable_data_hash']
528 # The manifest in the response will have had permission hints added.
529 # Remove any permission hints in the response before comparing it to the source.
530 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
531 assert_equal manifest_text, stripped_manifest
534 test "multiple signed locators per line" do
535 permit_unsigned_manifests
536 authorize_with :active
538 d41d8cd98f00b204e9800998ecf8427e+0
539 acbd18db4cc2f85cedef654fccc4a4d8+3
540 ea10d51bcf88862dbcc36eb292017dfd+45)
543 key: Rails.configuration.blob_signing_key,
544 api_token: api_token(:active),
547 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
548 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
550 unsigned_manifest.length.to_s
552 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
553 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
557 manifest_text: signed_manifest,
558 portable_data_hash: manifest_uuid,
561 assert_response :success
562 assert_not_nil assigns(:object)
563 resp = JSON.parse(@response.body)
564 assert_equal manifest_uuid, resp['portable_data_hash']
565 # All of the locators in the output must be signed.
566 # Each line is of the form "path locator locator ... 0:0:file.txt"
567 # entry.split[1..-2] will yield just the tokens in the middle of the line
568 returned_locator_count = 0
569 resp['manifest_text'].lines.each do |entry|
570 entry.split[1..-2].each do |tok|
571 returned_locator_count += 1
572 assert Blob.verify_signature tok, signing_opts
575 assert_equal locators.count, returned_locator_count
578 test 'Reject manifest with unsigned blob' do
579 permit_unsigned_manifests false
580 authorize_with :active
581 unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
582 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
585 manifest_text: unsigned_manifest,
586 portable_data_hash: manifest_uuid,
590 "Creating a collection with unsigned blobs should respond 403"
591 assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
592 "Collection should not exist in database after failed create"
595 test 'List expired collection returns empty list' do
596 authorize_with :active
598 where: {name: 'expired_collection'},
600 assert_response :success
601 found = assigns(:objects)
602 assert_equal 0, found.count
605 test 'Show expired collection returns 404' do
606 authorize_with :active
608 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
613 test 'Update expired collection returns 404' do
614 authorize_with :active
616 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
618 name: "still expired"
624 test 'List collection with future expiration time succeeds' do
625 authorize_with :active
627 where: {name: 'collection_expires_in_future'},
629 found = assigns(:objects)
630 assert_equal 1, found.count
634 test 'Show collection with future expiration time succeeds' do
635 authorize_with :active
637 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
639 assert_response :success
642 test 'Update collection with future expiration time succeeds' do
643 authorize_with :active
645 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
647 name: "still not expired"
650 assert_response :success